-
Notifications
You must be signed in to change notification settings - Fork 390
/
haproxy_process.py
213 lines (190 loc) · 7.03 KB
/
haproxy_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import itertools
import os
import shlex
import subprocess
import logging
import cert_mgr.barbican_cert_manager as barbican_cert_mgr
HAPROXY_DIR = "/var/lib/contrail/loadbalancer/haproxy"
HAPROXY_PROCESS = 'haproxy'
HAPROXY_PROCESS_CONF = HAPROXY_PROCESS + ".conf"
SUPERVISOR_BASE_DIR = '/etc/contrail/supervisord_vrouter_files/lbaas-haproxy-'
LOG_FILE= '/var/log/contrail/contrail-lb-haproxy-stdout.log'
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
filename=LOG_FILE)
log_levels = {
'MSG': {
'name': 'MSG',
'value': 35,
},
}
for log_level_key in log_levels.keys():
log_level = log_levels[log_level_key]
logging.addLevelName(log_level['value'], log_level['name'])
def delete_haproxy_dir(base_dir, loadbalancer_id):
dir_name = base_dir + "/" + loadbalancer_id
cmd = "rm -rf " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()
def create_haproxy_dir(base_dir, loadbalancer_id):
dir_name = base_dir + "/" + loadbalancer_id
cmd = "mkdir -p " + dir_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list)
p.communicate()
return dir_name
def get_haproxy_config_file(cfg_file, dir_name):
f = open(cfg_file)
content = f.read()
f.close()
kvps = content.split(':::::')
for kvp in kvps or []:
KeyValue = kvp.split('::::')
if (KeyValue[0] == 'haproxy_config'):
break;
haproxy_cfg_file = dir_name + "/" + HAPROXY_PROCESS_CONF
haproxy_conf = KeyValue[1]
if 'ssl crt http' in haproxy_conf:
haproxy_conf = barbican_cert_mgr.update_ssl_conf(haproxy_conf, dir_name)
if haproxy_conf is None:
return None
f = open(haproxy_cfg_file, 'w+')
f.write(haproxy_conf)
f.close()
return haproxy_cfg_file
def get_pid_file_from_conf_file(conf_file):
dir_name = os.path.dirname(conf_file)
pid_file = dir_name + "/" + "haproxy.pid"
return pid_file
def stop_haproxy(loadbalancer_id, daemon_mode=False):
conf_file = HAPROXY_DIR + "/" + loadbalancer_id + "/" + HAPROXY_PROCESS_CONF
try:
if daemon_mode:
_stop_haproxy_daemon(loadbalancer_id, conf_file)
else:
pool_id = os.path.split(os.path.dirname(conf_file))[1]
_stop_supervisor_haproxy(pool_id)
except Exception as e:
msg = "Exception in Stopping haproxy for Loadbalancer-ID %s" %loadbalancer_id
logging.exception(msg)
logging.error(e.__class__)
logging.error(e.__doc__)
logging.error(e.message)
delete_haproxy_dir(HAPROXY_DIR, loadbalancer_id)
def start_update_haproxy(loadbalancer_id, cfg_file,
netns, daemon_mode=False, keystone_auth_conf_file=None):
try:
pool_id = loadbalancer_id
dir_name = create_haproxy_dir(HAPROXY_DIR, loadbalancer_id)
haproxy_cfg_file = get_haproxy_config_file(cfg_file, dir_name)
if haproxy_cfg_file is None:
msg = "Failed to Create haproxy config for Loadbalancer-ID %s" %loadbalancer_id
logging.error(msg)
stop_haproxy(loadbalancer_id, daemon_mode)
return False
except Exception as e:
msg = "Exception in Createing haproxy config for Loadbalancer-ID %s" %loadbalancer_id
logging.exception(msg)
logging.error(e.__class__)
logging.error(e.__doc__)
logging.error(e.message)
stop_haproxy(loadbalancer_id, daemon_mode)
return False
try:
if daemon_mode:
_start_haproxy_daemon(pool_id, netns, haproxy_cfg_file)
else:
_start_supervisor_haproxy(pool_id, netns, haproxy_cfg_file)
except Exception as e:
msg = "Exception in Starting/Updating haproxy for Loadbalancer-ID %s" %loadbalancer_id
logging.exception(msg)
logging.error(e.__class__)
logging.error(e.__doc__)
logging.error(e.message)
return False
return True
def _get_lbaas_pid(conf_file):
pid_file = get_pid_file_from_conf_file(conf_file)
if not os.path.isfile(pid_file):
return None
cmd = 'cat %s' % pid_file
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
pid, err = p.communicate()
if err:
return None
return pid
def _stop_haproxy_daemon(loadbalancer_id, conf_file):
log_msg = log_levels['MSG']
last_pid = _get_lbaas_pid(conf_file)
if last_pid:
cmd_list = shlex.split('kill -9 ' + last_pid)
subprocess.Popen(cmd_list)
msg = "Stopping haproxy for Loadbalancer-ID %s" %loadbalancer_id
logging.log(log_msg['value'], msg)
def _start_haproxy_daemon(pool_id, netns, conf_file):
log_msg = log_levels['MSG']
loadbalancer_id = pool_id
last_pid = _get_lbaas_pid(conf_file)
if last_pid:
msg = "Updating haproxy for Loadbalancer-ID %s" %loadbalancer_id
sf_opt = '-sf ' + last_pid
else:
msg = "Starting haproxy for Loadbalancer-ID %s" %loadbalancer_id
sf_opt = ''
pid_file = get_pid_file_from_conf_file(conf_file)
logging.log(log_msg['value'], msg)
cmd = 'ip netns exec %s haproxy -f %s -p %s %s' % \
(netns, conf_file, pid_file, sf_opt)
cmd_list = shlex.split(cmd)
subprocess.Popen(cmd_list)
def _stop_supervisor_haproxy(pool_id):
pool_suffix = _get_pool_suffix(pool_id)
file_name = SUPERVISOR_BASE_DIR + pool_suffix + '.ini'
cmd = "rm " + file_name
cmd_list = shlex.split(cmd)
subprocess.Popen(cmd_list)
_update_supervisor()
def _start_supervisor_haproxy(pool_id, netns, conf_file):
data = []
data.extend(_set_config(pool_id, netns, conf_file))
pool_suffix = _get_pool_suffix(pool_id)
with open(SUPERVISOR_BASE_DIR + pool_suffix + '.ini', "w") as f:
f.write('\n'.join(data) + '\n')
_update_supervisor()
def _get_pool_suffix(pool_id):
return pool_id.split('-')[0]
def _update_supervisor():
cmd = "supervisorctl -s unix:///tmp/supervisord_vrouter.sock update"
cmd_list = shlex.split(cmd)
subprocess.Popen(cmd_list)
def _set_config(pool_id, netns, conf_file):
pool_suffix = _get_pool_suffix(pool_id)
program_name = 'lbaas-haproxy-%s' % pool_suffix
cmd = "supervisorctl -s unix:///tmp/supervisord_vrouter.sock pid "
cmd += program_name
cmd_list = shlex.split(cmd)
p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
last_pid, err = p.communicate()
try:
int(last_pid)
sf_opt = '-sf ' + last_pid
except ValueError:
sf_opt = ''
opts = [
'[program:%s]' % program_name,
'command=ip netns exec %s haproxy -f %s -db %s' % \
(netns, conf_file, sf_opt),
'priority=420',
'autostart=true',
'killasgroup=true',
'stdout_capture_maxbytes=1MB',
'redirect_stderr=true',
'stdout_logfile=/var/log/contrail/lbaas-haproxy-stdout.log',
'stderr_logfile=/dev/null',
'startsecs=5',
'exitcodes=0'
]
return itertools.chain(o for o in opts)