/
services.py
463 lines (398 loc) · 15.1 KB
/
services.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import os
from fabfile.config import *
from misc import zoolink
from fabfile.utils.fabos import detect_ostype
from fabfile.utils.cluster import get_orchestrator,get_tsn_nodes,get_toragent_nodes,\
get_esxi_contrail_vms
from fabric.contrib.files import exists
from fabfile.utils.host import hstr_to_ip
from fabfile.utils.host import get_control_host_string
@task
@roles('cfgm')
def stop_rabbitmq():
with settings(warn_only=True):
sudo('service supervisor-support-service stop')
@task
@roles('cfgm')
def restart_rabbitmq():
sudo('service supervisor-support-service restart')
@task
def stop_and_disable_qpidd():
"""stops the qpidd and disables it."""
execute(stop_and_disable_qpidd_node, env.host_string)
@task
def stop_and_disable_qpidd_node(*args):
"""stops the qpidd and disables it in one node."""
for host_string in args:
with settings(host_string=host_string, warn_only=True):
if not sudo('service qpidd status').succeeded:
print "qpidd not running, skipping stop."
return
with settings(host_string=host_string):
sudo('service qpidd stop')
sudo('chkconfig qpidd off')
@task
@roles('database')
def stop_database():
"""stops the contrail database services."""
#sudo('service supervisord-contrail-database stop')
sudo('service supervisor-database stop')
@task
@roles('cfgm')
def stop_cfgm():
"""stops the contrail config services."""
execute('stop_cfgm_node', env.host_string)
@task
def stop_cfgm_node(*args):
for host_string in args:
with settings(host_string=host_string, warn_only=True):
sudo('service supervisor-config stop')
sudo('service neutron-server stop')
sudo('service supervisor-support-service stop')
@task
@roles('cfgm')
def start_cfgm():
"""starts the contrail config services."""
with settings(warn_only=True):
sudo('service supervisor-support-service start')
sudo('service supervisor-config start')
sudo('service neutron-server start')
@task
@roles('database')
def start_database():
"""Starts the contrail database services."""
#sudo('service supervisord-contrail-database start')
sudo('service supervisor-database start')
@task
@roles('control')
def start_control():
"""Starts the contrail control services."""
sudo('service supervisor-control start')
@task
@roles('webui')
def start_webui():
"""starts the contrail webui services."""
sudo('service supervisor-webui start')
@task
@roles('collector')
def start_collector():
"""starts the contrail collector services."""
sudo('service supervisor-analytics start')
@task
@roles('control')
def stop_control():
"""Stops the contrail control services."""
execute('stop_control_node', env.host_string)
@task
def stop_control_node(*args):
"""Stops the contrail control services in once control node. USAGE:fab stop_control_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-control stop')
@task
@roles('collector')
def stop_collector():
"""stops the contrail collector services."""
with settings(warn_only=True):
sudo('service supervisor-analytics stop')
@task
@roles('compute')
def stop_vrouter():
"""stops the contrail vrouter services."""
sudo('service supervisor-vrouter stop')
@task
@roles('webui')
def stop_webui():
"""stops the contrail webui services."""
sudo('service supervisor-webui stop')
@task
@roles('database')
def restart_database():
"""Restarts the contrail database services."""
execute('restart_database_node', env.host_string)
@task
def restart_database_node(*args):
"""Restarts the contrail database services in once database node. USAGE:fab restart_database_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
execute('zoolink_node', host_string)
zoo_svc = 'zookeeper'
sudo('service %s restart' % zoo_svc)
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-database restart')
@task
@roles('openstack')
def restart_openstack():
"""Restarts the contrail openstack services."""
execute('restart_openstack_node', env.host_string)
@task
def restart_openstack_node(*args):
"""Restarts the contrail openstack services in once openstack node. USAGE:fab restart_openstack_node:user@1.1.1.1,user@2.2.2.2"""
openstack_services = [ 'httpd', 'memcached', 'supervisor-openstack']
if detect_ostype() in ['ubuntu']:
openstack_services = ['memcached', 'supervisor-openstack']
for host_string in args:
with settings(host_string=host_string):
for svc in openstack_services:
sudo('service %s restart' % svc)
@task
@roles('compute')
def restart_openstack_compute():
"""Restarts the contrail openstack compute service."""
if 'tsn' in env.roledefs.keys() and env.host_string in env.roledefs['tsn']:
return
if get_orchestrator() == 'vcenter':
return
if detect_ostype() in ['ubuntu']:
sudo('service nova-compute restart')
return
sudo('service openstack-nova-compute restart')
@task
@parallel
@roles('cfgm')
def restart_cfgm():
"""Restarts the contrail config services."""
execute("restart_cfgm_node", env.host_string)
@task
def restart_cfgm_node(*args):
"""Restarts the contrail config services in once cfgm node. USAGE:fab restart_cfgm_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-support-service restart')
sudo('service supervisor-config restart')
if get_orchestrator() == 'openstack':
sudo('service neutron-server restart')
@task
@roles('control')
def restart_control():
"""Restarts the contrail control services."""
execute("restart_control_node", env.host_string)
@task
def restart_control_node(*args):
"""Restarts the contrail control services in once control node. USAGE:fab restart_control_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-control restart')
@task
@roles('collector')
def restart_collector():
"""Restarts the contrail collector services."""
execute('restart_collector_node', env.host_string)
@task
def restart_collector_node(*args):
"""Restarts the contrail collector services in once collector node. USAGE:fab restart_collector_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-analytics restart')
@task
@roles('compute')
def restart_vrouter():
"""Restarts the contrail compute services."""
execute('restart_vrouter_node', env.host_string)
@task
def restart_vrouter_node(*args):
"""Restarts the contrail vrouter services in once vrouter node. USAGE:fab restart_vrouter_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-vrouter restart')
@task
@roles('webui')
def restart_webui():
"""Restarts the contrail webui services."""
execute('restart_webui_node', env.host_string)
@task
def restart_webui_node(*args):
"""Restarts the contrail webui services in once webui node. USAGE:fab restart_webui_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
sudo('service supervisor-webui restart')
@task
@roles('webui')
def start_redis_webui():
"""Starts redis service in webui node."""
execute('start_redis_webui_node', env.host_string)
@task
def start_redis_webui_node(*args):
"""Starts redis service in webui node. USAGE:fab start_redis_webui_node:user@1.1.1.1,user@2.2.2.2"""
for host_string in args:
with settings(host_string=host_string):
run('chkconfig redis on')
run('service redis start')
@task
@roles('build')
def stop_contrail_control_services():
"""stops the Contrail config,control,analytics,database,webui services."""
execute('stop_cfgm')
execute('stop_database')
execute('stop_collector')
execute('stop_control')
execute('stop_webui')
@task
@roles('build')
def start_contrail_control_services():
"""Starts the Contrail config,control,analytics,database,webui services."""
execute('start_cfgm')
execute('start_database')
execute('start_collector')
execute('start_control')
execute('start_webui')
@task
@roles('database')
def backup_cassandra_zk():
"""take backup of cassandra db"""
data_dir = None
out = ''
cmd = "sed -n '/data_file_directories/{n;p;}'"
# If Ubuntu
if exists('/etc/cassandra/cassandra.yaml', use_sudo=True):
yaml_file = '/etc/cassandra/cassandra.yaml'
out = sudo("%s %s" % (cmd, yaml_file))
# If redhat distros
elif exists('/etc/cassandra/conf/cassandra.yaml', use_sudo=True):
yaml_file = '/etc/cassandra/conf/cassandra.yaml'
out = sudo("%s %s" % (cmd, yaml_file))
data_dir = out[2:]
if data_dir is None:
cassandra_dir = '/var/lib/cassandra'
else:
cassandra_dir = os.path.abspath(os.path.join(data_dir,os.pardir))
if exists(cassandra_dir+'.old'):
sudo('rm -rf '+cassandra_dir+'.old')
if exists(cassandra_dir):
sudo('mv '+ cassandra_dir+' '+ cassandra_dir+'.old')
if exists('/var/lib/zookeeper/version-2.old'):
sudo('rm -rf /var/lib/zookeeper/version-2.old')
if exists('/var/lib/zookeeper/version-2'):
sudo('mv /var/lib/zookeeper/version-2 /var/lib/zookeeper/version-2.old')
@task
@roles('build')
def restart_contrail_control_services():
"""Restarts the Contrail config,control,analytics,database,webui services."""
execute('restart_cfgm')
execute('restart_database')
execute('restart_collector')
execute('restart_control')
execute('restart_webui')
@roles('openstack')
def stop_nova():
"""Stop nova services :fab stop_nova"""
host = env.host_string
openstack_services = ['nova-api', 'nova-scheduler',
'nova-conductor']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s stop' % svc)
@roles('openstack')
def start_nova():
"""Start nova services :fab stop_nova"""
host = env.host_string
openstack_services = ['nova-api', 'nova-scheduler',
'nova-conductor']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s start' % svc)
@roles('openstack')
def stop_keystone():
"""Stop keystone services :fab stop_keystone"""
host = env.host_string
openstack_services = ['keystone']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s stop' % svc)
@roles('openstack')
def start_keystone():
"""Start keystone services :fab start_keystone"""
host = env.host_string
openstack_services = ['keystone']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s start' % svc)
@roles('openstack')
def stop_glance():
"""Stop keystone services :fab stop_keystone"""
host = env.host_string
openstack_services = ['glance-api','glance-registry']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s stop' % svc)
@roles('openstack')
def start_glance():
"""Start keystone services :fab start_keystone"""
host = env.host_string
openstack_services = ['glance-api','glance-registry']
with settings(host_string=host):
for svc in openstack_services:
sudo('service %s start' % svc)
@roles('compute')
def stop_nova_openstack_compute():
"""Stop the contrail openstack compute service."""
tsn_nodes = []
tor_nodes = []
contrail_vms = []
host = env.host_string
orchestrator = get_orchestrator()
if orchestrator != 'openstack':
return
tsn_nodes = get_tsn_nodes()
tor_nodes = get_toragent_nodes()
contrail_vms = get_esxi_contrail_vms()
if (host not in tsn_nodes) and (host not in tor_nodes) and (host not in contrail_vms) :
if detect_ostype() in ['ubuntu']:
sudo('service nova-compute stop')
return
sudo('service openstack-nova-compute stop')
@roles('compute')
def start_nova_openstack_compute():
"""Start the contrail openstack compute service."""
tsn_nodes = []
tor_nodes = []
contrail_vms = []
host = env.host_string
orchestrator = get_orchestrator()
if orchestrator != 'openstack':
return
tsn_nodes = get_tsn_nodes()
tor_nodes = get_toragent_nodes()
contrail_vms = get_esxi_contrail_vms()
if (host not in tsn_nodes) and (host not in tor_nodes) and (host not in contrail_vms) :
if detect_ostype() in ['ubuntu']:
sudo('service nova-compute start')
return
sudo('service openstack-nova-compute start')
@roles('openstack')
def reboot_nova_instance():
host = env.host_string
with settings(host_string=host,warn_only=True):
sudo(
"source /etc/contrail/openstackrc;nova list --all_tenants | awk '{print $2}' | grep -v ID | xargs -L1 nova reboot --hard $2")
@task
def drop_analytics_keyspace_node(*args):
for host_string in args:
with settings(host_string=host_string, warn_only=True):
CASSANDRA_CMD = 'cqlsh %s -f ' % hstr_to_ip(get_control_host_string(host_string))
print "Dropping analytics keyspace.. this may take a while.."
sudo("echo 'describe keyspace \"ContrailAnalytics\";' > /tmp/cassandra_commands_file")
if sudo(CASSANDRA_CMD + '/tmp/cassandra_commands_file').succeeded:
sudo("echo 'drop keyspace \"ContrailAnalytics\";' > /tmp/cassandra_commands_file")
if not sudo(CASSANDRA_CMD + '/tmp/cassandra_commands_file').succeeded:
print "WARN: Drop keyspace ContrailAnalytics failed.."
else:
print "INFO: keyspace ContrailAnalytics is dropped.."
print "INFO: if snapshots are created, manual deletion may be required to free up disk.."
sudo("echo 'drop keyspace \"ContrailAnalyticsCql\";' > /tmp/cassandra_commands_file")
if not sudo(CASSANDRA_CMD + '/tmp/cassandra_commands_file').succeeded:
print "WARN: Drop keyspace ContrailAnalyticsCql failed.."
else:
print "INFO: keyspace ContrailAnalyticsCql is dropped.."
print "INFO: if snapshots are created, manual deletion may be required to free up disk.."
@task
@roles('build')
def drop_analytics_keyspace(confirm=False):
if not confirm:
print "WARN: Dropping analytics keyspace will cause analytics data to be lost permanently..."
if raw_input("Continue? Yes/No: ") != "Yes":
print "INFO: Not confirmed. Exiting..."
return
execute('stop_collector')
execute("drop_analytics_keyspace_node", env.roledefs['database'][0])