38
38
39
39
j2_env = jinja2 .Environment (autoescape = True , loader = jinja2 .FileSystemLoader (
40
40
os .path .dirname (os .path .realpath (__file__ )) + consts .AGENT_API_TEMPLATES ))
41
- UPSTART_TEMPLATE = j2_env .get_template (consts .KEEPALIVED_JINJA2_UPSTART )
42
- SYSVINIT_TEMPLATE = j2_env .get_template (consts .KEEPALIVED_JINJA2_SYSVINIT )
43
41
SYSTEMD_TEMPLATE = j2_env .get_template (consts .KEEPALIVED_JINJA2_SYSTEMD )
44
42
check_script_file_template = j2_env .get_template (
45
43
consts .KEEPALIVED_CHECK_SCRIPT )
@@ -92,32 +90,18 @@ def upload_lvs_listener_config(self, listener_id):
92
90
f .write (b )
93
91
b = stream .read (BUFFER )
94
92
95
- init_system = util .get_os_init_system ( )
93
+ file_path = util .keepalived_lvs_init_path ( listener_id )
96
94
97
- file_path = util . keepalived_lvs_init_path ( init_system , listener_id )
95
+ template = SYSTEMD_TEMPLATE
98
96
99
- if init_system == consts .INIT_SYSTEMD :
100
- template = SYSTEMD_TEMPLATE
101
-
102
- # Render and install the network namespace systemd service
103
- util .install_netns_systemd_service ()
104
- util .run_systemctl_command (
105
- consts .ENABLE , consts .AMP_NETNS_SVC_PREFIX )
106
- elif init_system == consts .INIT_UPSTART :
107
- template = UPSTART_TEMPLATE
108
- elif init_system == consts .INIT_SYSVINIT :
109
- template = SYSVINIT_TEMPLATE
110
- else :
111
- raise util .UnknownInitError ()
97
+ # Render and install the network namespace systemd service
98
+ util .install_netns_systemd_service ()
99
+ util .run_systemctl_command (
100
+ consts .ENABLE , consts .AMP_NETNS_SVC_PREFIX , False )
112
101
113
102
# Render and install the keepalivedlvs init script
114
- if init_system == consts .INIT_SYSTEMD :
115
- # mode 00644
116
- mode = stat .S_IRUSR | stat .S_IWUSR | stat .S_IRGRP | stat .S_IROTH
117
- else :
118
- # mode 00755
119
- mode = (stat .S_IRWXU | stat .S_IRGRP | stat .S_IXGRP |
120
- stat .S_IROTH | stat .S_IXOTH )
103
+ # mode 00644
104
+ mode = stat .S_IRUSR | stat .S_IWUSR | stat .S_IRGRP | stat .S_IROTH
121
105
keepalived_pid , vrrp_pid , check_pid = util .keepalived_lvs_pids_path (
122
106
listener_id )
123
107
if not os .path .exists (file_path ):
@@ -136,23 +120,15 @@ def upload_lvs_listener_config(self, listener_id):
136
120
text_file .write (text )
137
121
138
122
# Make sure the keepalivedlvs service is enabled on boot
139
- if init_system == consts . INIT_SYSTEMD :
123
+ try :
140
124
util .run_systemctl_command (
141
- consts .ENABLE , f"octavia-keepalivedlvs-{ str (listener_id )} " )
142
- elif init_system == consts .INIT_SYSVINIT :
143
- init_enable_cmd = f"insserv { file_path } "
144
- try :
145
- subprocess .check_output (init_enable_cmd .split (),
146
- stderr = subprocess .STDOUT ,
147
- encoding = 'utf-8' )
148
- except subprocess .CalledProcessError as e :
149
- LOG .debug ('Failed to enable '
150
- 'octavia-keepalivedlvs service: '
151
- '%(err)s' , {'err' : str (e )})
152
- return webob .Response (json = {
153
- 'message' : ("Error enabling "
154
- "octavia-keepalivedlvs service" ),
155
- 'details' : e .output }, status = 500 )
125
+ consts .ENABLE ,
126
+ consts .KEEPALIVEDLVS_SYSTEMD % listener_id )
127
+ except subprocess .CalledProcessError as e :
128
+ return webob .Response (json = {
129
+ 'message' : ("Error enabling "
130
+ "octavia-keepalivedlvs service" ),
131
+ 'details' : e .output }, status = 500 )
156
132
157
133
if NEED_CHECK :
158
134
# inject the check script for keepalived process
@@ -166,7 +142,6 @@ def upload_lvs_listener_config(self, listener_id):
166
142
'w' ) as script_file :
167
143
text = check_script_file_template .render (
168
144
consts = consts ,
169
- init_system = init_system ,
170
145
keepalived_lvs_pid_dir = util .keepalived_lvs_dir ()
171
146
)
172
147
script_file .write (text )
@@ -217,15 +192,10 @@ def manage_lvs_listener(self, listener_id, action):
217
192
if consts .OFFLINE == self ._check_lvs_listener_status (listener_id ):
218
193
action = consts .AMP_ACTION_START
219
194
220
- cmd = ("/usr/sbin/service "
221
- f"octavia-keepalivedlvs-{ listener_id } { action } " )
222
-
223
195
try :
224
- subprocess . check_output ( cmd . split (), stderr = subprocess . STDOUT ,
225
- encoding = 'utf-8' )
196
+ util . run_systemctl_command (
197
+ action , consts . KEEPALIVEDLVS_SYSTEMD % listener_id )
226
198
except subprocess .CalledProcessError as e :
227
- LOG .debug ('Failed to %s keepalivedlvs listener %s' ,
228
- listener_id + ' : ' + action , e )
229
199
return webob .Response (json = {
230
200
'message' : (f"Failed to { action } keepalivedlvs listener "
231
201
f"{ listener_id } " ),
@@ -280,11 +250,10 @@ def delete_lvs_listener(self, listener_id):
280
250
if os .path .exists (keepalived_pid ) and os .path .exists (
281
251
os .path .join ('/proc' ,
282
252
util .get_keepalivedlvs_pid (listener_id ))):
283
- cmd = (f"/usr/sbin/service "
284
- f"octavia-keepalivedlvs-{ listener_id } stop" )
285
253
try :
286
- subprocess .check_output (cmd .split (), stderr = subprocess .STDOUT ,
287
- encoding = 'utf-8' )
254
+ util .run_systemctl_command (
255
+ consts .STOP ,
256
+ consts .KEEPALIVEDLVS_SYSTEMD % listener_id )
288
257
except subprocess .CalledProcessError as e :
289
258
LOG .error ("Failed to stop keepalivedlvs service: %s" , e )
290
259
return webob .Response (json = {
@@ -301,31 +270,21 @@ def delete_lvs_listener(self, listener_id):
301
270
os .remove (pid )
302
271
303
272
# disable the service
304
- init_system = util .get_os_init_system ()
305
- init_path = util .keepalived_lvs_init_path (init_system , listener_id )
273
+ init_path = util .keepalived_lvs_init_path (listener_id )
306
274
307
- if init_system == consts . INIT_SYSTEMD :
275
+ try :
308
276
util .run_systemctl_command (
309
- consts .DISABLE , f"octavia-keepalivedlvs-{ listener_id } " )
310
-
311
- elif init_system == consts .INIT_SYSVINIT :
312
- init_disable_cmd = f"insserv -r { init_path } "
313
- try :
314
- subprocess .check_output (init_disable_cmd .split (),
315
- stderr = subprocess .STDOUT ,
316
- encoding = 'utf-8' )
317
- except subprocess .CalledProcessError as e :
318
- LOG .error ("Failed to disable "
319
- "octavia-keepalivedlvs-%(list)s service: "
320
- "%(err)s" , {'list' : listener_id , 'err' : str (e )})
321
- return webob .Response (json = {
322
- 'message' : (
323
- f"Error disabling octavia-keepalivedlvs-{ listener_id } "
324
- f"service" ),
325
- 'details' : e .output }, status = 500 )
326
-
327
- elif init_system != consts .INIT_UPSTART :
328
- raise util .UnknownInitError ()
277
+ consts .DISABLE ,
278
+ consts .KEEPALIVEDLVS_SYSTEMD % listener_id )
279
+ except subprocess .CalledProcessError as e :
280
+ LOG .error ("Failed to disable "
281
+ "octavia-keepalivedlvs-%(list)s service: "
282
+ "%(err)s" , {'list' : listener_id , 'err' : str (e )})
283
+ return webob .Response (json = {
284
+ 'message' : (
285
+ f"Error disabling octavia-keepalivedlvs-{ listener_id } "
286
+ "service" ),
287
+ 'details' : e .output }, status = 500 )
329
288
330
289
# delete init script ,config file and log file for that listener
331
290
if os .path .exists (init_path ):
0 commit comments