/
main.py
791 lines (669 loc) · 24.6 KB
/
main.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import click
from click_shell.core import Shell
import ipaddress as ipadd
from prettytable import PrettyTable
from jumbo.core import clusters, nodes, services, vagrant
from jumbo.utils import session as ss, exceptions as ex, checks
from jumbo.cli import printlogo
from jumbo.utils.settings import OS
def print_with_color(message, color):
if OS != 'Windows':
click.secho(message, fg=color)
else:
click.echo(message)
@click.group(invoke_without_command=True)
@click.option('--cluster', '-c')
@click.pass_context
def jumbo(ctx, cluster):
"""
Execute a Jumbo command.
If no command is passed, start the Jumbo shell interactive mode.
"""
# Create the shell
sh = Shell(prompt=click.style('jumbo > ', fg='green') if OS != 'Windows'
else 'jumbo > ',
intro=printlogo.jumbo_ascii() +
'\nJumbo Shell. Enter "help" for list of supported commands.' +
' Type "quit" to leave the Jumbo Shell.' +
click.style('\nJumbo v0.4.4',
fg='cyan'))
# Save the shell in the click context (to modify its prompt later on)
ctx.meta['jumbo_shell'] = sh.shell
# Register commands that can be used in the shell
sh.add_command(create)
sh.add_command(exit)
sh.add_command(delete)
sh.add_command(use)
sh.add_command(addnode)
sh.add_command(rmnode)
sh.add_command(editnode)
sh.add_command(listclusters)
sh.add_command(listnodes)
sh.add_command(repair)
sh.add_command(addservice)
sh.add_command(addcomponent)
sh.add_command(listcomponents)
sh.add_command(rmservice)
sh.add_command(rmcomponent)
sh.add_command(checkservice)
sh.add_command(listservices)
sh.add_command(start)
sh.add_command(stop)
sh.add_command(status)
sh.add_command(provision)
sh.add_command(restart)
# If cluster exists, call manage command (saves the shell in session
# variable svars and adapts the shell prompt)
if cluster:
if not checks.check_cluster(cluster):
click.echo('This cluster does not exist.'
' Use "create NAME" to create it.', err=True)
else:
ctx.invoke(use, name=cluster)
# Run the command, or the shell if no command is passed
sh.invoke(ctx)
@jumbo.command()
@click.pass_context
def exit(ctx):
"""Reset current context.
:param ctx: Click context
"""
if ss.svars.get('cluster'):
ss.svars['cluster'] = None
ctx.meta['jumbo_shell'].prompt = click.style('jumbo > ', fg='green')
else:
click.echo('Use "quit" to quit the shell. Exit only removes context.')
####################
# cluster commands #
####################
def set_context(ctx, name):
to_print = 'jumbo (%s) > ' % name
ctx.meta['jumbo_shell'].prompt = click.style(
to_print, fg='green') if OS != 'Windows' else to_print
def validate_cluster_name_cb(ctx, param, value):
if not value:
return value
if '-' in value:
raise click.BadParameter('"%s" is not a valid name for a cluster.\n'
'Forbidden characters: "-"' % value)
return value
@jumbo.command()
@click.argument('name', callback=validate_cluster_name_cb)
@click.option('--domain', '-d', help='Domain name of the cluster')
@click.option('--template', '-t', help='Preconfigured cluster name')
@click.pass_context
def create(ctx, name, domain, template):
"""Create a new cluster.
:param name: New cluster name
"""
click.echo('Creating "%s"...' % name)
try:
clusters.create_cluster(cluster=name,
domain=domain,
template=template)
except ex.CreationError as e:
print_with_color(e.message, 'red')
else:
click.echo('Cluster "{}" created (domain name = "{}").'.format(
name,
domain if domain else '%s.local' % name))
set_context(ctx, name)
@jumbo.command()
@click.argument('name')
@click.pass_context
def use(ctx, name):
"""Set a cluster to manage. Persist --cluster option.
:param name: Cluster name
"""
click.echo('Loading %s...' % name)
try:
ss.load_config(cluster=name)
ss.dump_config(services.get_services_components_hosts())
except ex.LoadError as e:
print_with_color(e.message, 'red')
if e.type == 'NoConfFile':
click.echo('Use "repair" to regenerate `jumbo_config`.')
else:
click.echo('Cluster "%s" loaded.' % name)
set_context(ctx, name)
@jumbo.command()
@click.argument('name')
@click.option('--force', '-f', is_flag=True, help='Force deletion')
@click.pass_context
def delete(ctx, name, force):
"""Delete a cluster.
:param name: Name of the cluster to delete
"""
if not force:
if not click.confirm(
'Are you sure you want to delete the cluster %s' % name):
return
try:
clusters.delete_cluster(cluster=name)
except ex.LoadError as e:
print_with_color(e.message, 'red')
else:
click.echo('Cluster "%s" deleted.' % name)
ss.clear()
ctx.meta['jumbo_shell'].prompt = click.style('jumbo > ', fg='green')
@jumbo.command()
@click.option('--full', is_flag=True, help='Force full display')
def listclusters(full):
"""List clusters managed by Jumbo."""
try:
limit = 40
if full:
limit = 1000
cluster_table = PrettyTable(['Name', 'Domain Name', 'VMs',
'Services'])
cluster_table.align['Name'] = 'l'
cluster_table.align['Domain Name'] = 'l'
cluster_table.align['Services'] = 'l'
for cluster in clusters.list_clusters():
cluster_table.add_row([cluster['cluster'],
cluster['domain'],
len(cluster['nodes']),
'\n'.join(cluster['services'])])
except ex.LoadError as e:
print_with_color(e.message, 'red')
if e.type == 'NoConfFile':
click.echo('Use "repair" to regenerate "jumbo_config".')
else:
cluster_table.sortby = 'Name'
click.echo(cluster_table)
@jumbo.command()
@click.argument('name')
@click.option('--domain', '-d', help='Domain name of the cluster')
def repair(name, domain):
"""Recreate "jumbo_config" if it doesn't exist.
:param name: Cluster name
"""
if clusters.repair_cluster(cluster=name,
domain=domain):
click.echo('Recreated "jumbo_config" from scratch '
'for cluster "{}" (domain name = "{}").'
.format(name, domain if domain else '%s.local' % name))
else:
click.echo('Nothing to repair in cluster "%s".' % name)
###############
# VM commands #
###############
def validate_ip_cb(ctx, param, value):
if not value:
return value
try:
ipadd.ip_address(value)
except ValueError:
raise click.BadParameter('"%s" is not a valid IP address.' % value)
return value
@jumbo.command()
@click.argument('name')
@click.option('--types', '-t', multiple=True,
type=click.Choice(services.get_available_types()),
required=True, help='VM host type(s)')
@click.option('--ip', '-i', callback=validate_ip_cb, prompt='IP',
help='VM IP address')
@click.option('--ram', '-r', type=int, prompt='RAM (MB)',
help='RAM allocated to the VM in MB')
@click.option('--cpus', '-p', default=1,
help='Number of CPUs allocated to the VM')
@click.option('--cluster', '-c')
@click.pass_context
def addnode(ctx, name, types, ip, ram, cpus, cluster):
"""
Create a new VM in the cluster being managed.
Another cluster can be specified with "--cluster".
:param name: New VM name
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
try:
nodes.add_node(name, ip, ram, types, cpus, cluster=cluster)
count = services.auto_install_node(name, cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
if e.type == 'NoConfFile':
click.echo('Use "repair" to regenerate `jumbo_config`.')
switched = False
else:
click.echo('Machine "{}" added to cluster "{}". {}'
.format(name, cluster,
'{} clients auto installed on "{}".'
.format(count, name) if count else ''))
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('name')
@click.pass_context
@click.option('--cluster', '-c')
@click.option('--force', '-f', is_flag=True, help='Force deletion')
def rmnode(ctx, name, cluster, force):
"""Removes a VM.
:param name: VM name
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
if not force:
if not click.confirm(
'Are you sure you want to remove the node "{}" '
'of cluster "{}"?'.format(name, cluster)):
return
try:
nodes.remove_node(cluster=cluster, node=name)
except ex.LoadError as e:
print_with_color(e.message, 'red')
if e.type == 'NoConfFile':
click.echo('Use "repair" to regenerate `jumbo_config`')
switched = False
else:
click.echo('Machine "{}" removed of cluster "{}".'
.format(name, cluster))
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('name', required=True)
@click.option('--ip', '-i', callback=validate_ip_cb,
help='VM new IP address')
@click.option('--ram', '-r', type=int,
help='RAM allocated to the VM in MB')
@click.option('--cpus', '-p',
help='Number of CPUs allocated to the VM')
@click.option('--cluster', '-c')
@click.pass_context
def editnode(ctx, name, ip, ram, cpus, cluster):
"""Modify an already existing node in the cluster being managed.
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
if ip:
if not click.confirm('Changing IP adress after cluster provisionning '
'will break things. Do you want to continue?'):
return
if ip or ram or cpus:
try:
changed = nodes.edit_node(name, ip, ram, cpus, cluster=cluster)
if changed:
click.echo('\n"{}" configurations changed:\n - {}'.format(
name,
'\n - '.join(
'{}: {} -> {}'
.format(c[0], c[1], c[2]) for c in changed))
)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
else:
click.echo('Nothing to do. Type "help editnode" for usage.')
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.option('--cluster', '-c')
def listnodes(cluster):
"""
List VMs in the cluster being managed.
Another cluster can be specified with "--cluster".
"""
if not cluster:
cluster = ss.svars['cluster']
try:
node_table = PrettyTable(
['Name', 'Types', 'IP', 'RAM (MB)', 'CPUs'])
for m in clusters.list_nodes(cluster=cluster):
node_table.add_row([m['name'], ', '.join(m['types']), m['ip'],
m['ram'], m['cpus']])
except ex.LoadError as e:
print_with_color(e.message, 'red')
else:
print_colorized_table(node_table)
#####################
# services commands #
#####################
@jumbo.command()
@click.argument('name')
@click.option('--cluster', '-c')
@click.option('--no-auto', is_flag=True, help='Avoid auto-install')
@click.option('--ha', '-h', is_flag=True, help='High Availability mode')
@click.option('--recursive', '-r', is_flag=True,
help='Also auto-install all other services needed')
@click.pass_context
def addservice(ctx, name, cluster, no_auto, ha, recursive):
"""
Add a service to a cluster and auto-install its components
on the best fitting hosts.
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
try:
msg = ''
auto_installed_count = 0
if recursive:
dep_serv = ''
dep_comp = ''
dependencies = services.get_service_dependencies(name=name,
ha=ha,
first=True,
cluster=cluster)
if dependencies['components'] or dependencies['services']:
click.echo('The service %s and its dependencies will be'
' installed. Dependencies:' % name)
if dependencies['components']:
dep_comp = ('Components:\n - %s\n' %
'\n - '.join(
'{} {}'
.format(v, k) for k, v in
dependencies['components'].items()))
if dependencies['services']:
dep_serv = ('Services:\n - %s\n' %
'\n - '.join(dependencies['services']))
if click.confirm('{}{}\nDo you want to continue?'
.format(dep_comp, dep_serv)):
auto_installed_count = services.install_dependencies(
dependencies=dependencies,
cluster=cluster)
msg += 'Auto-installed the dependencies:\n{}{}'.format(
dep_comp, dep_serv)
else:
click.echo('Installation canceled.')
return
services.add_service(name=name, ha=ha, cluster=cluster)
if no_auto:
msg += ('No component has been auto-installed (except clients). '
'Use "addcomp" manually.')
else:
count = services.auto_assign(service=name, ha=ha, cluster=cluster)
msg += ('{} type{} of component{} auto-installed. '
'Use "listcomponents -a" for details.'
.format(count + auto_installed_count,
's' if count > 1 else '',
's' if count > 1 else ''))
except ex.LoadError as e:
print_with_color(e.message, 'red')
switched = False
if e.type == 'NotExist':
click.echo('Available services:\n - %s'
% '\n - '.join(services.get_available_services()))
except ex.CreationError as e:
print_with_color(e.message, 'red')
else:
click.echo('Service "{}" and related clients added to cluster "{}".\n'
.format(name, cluster) + msg)
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('service')
@click.option('--cluster', '-c')
@click.option('--force', '-f', is_flag=True, help='Force deletion')
@click.pass_context
def rmservice(ctx, service, cluster, force):
"""
Removes service from cluster.
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
if not force:
if not click.confirm(
'Are you sure you want to remove the service "{}" and all its '
'components of cluster "{}"?'.format(service, cluster)):
return
try:
services.remove_service(service, cluster=cluster)
except ex.LoadError as e:
print_with_color(e.message, 'red')
except ex.CreationError as e:
print_with_color(e.message, 'red')
switched = True
else:
click.echo('Service "{}" and its components removed from cluster "{}".'
.format(service, cluster))
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('name')
@click.option('--node', '-n', required=True)
@click.option('--cluster', '-c')
@click.pass_context
def addcomponent(ctx, name, node, cluster):
"""
Add component to a node.
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
try:
services.add_component(name, node=node, cluster=cluster)
except ex.LoadError as e:
print_with_color(e.message, 'red')
switched = False
except ex.CreationError as e:
print_with_color(e.message, 'red')
else:
click.echo('Component "{}" added to node "{}/{}".'
.format(name, cluster, node))
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('name')
@click.option('--node', '-n', required=True)
@click.option('--cluster', '-c')
@click.option('--force', '-f', is_flag=True, help='Force deletion')
@click.pass_context
def rmcomponent(ctx, name, node, cluster, force):
"""
Remove component from specified node.
"""
switched = True if cluster else False
if not cluster:
cluster = ss.svars['cluster']
if not force:
if not click.confirm(
'Are you sure you want to remove the component "{}" '
'of node "{}/{}"?'.format(name, cluster, node)):
return
try:
services.remove_component(name,
node=node,
cluster=cluster)
except ex.LoadError as e:
print_with_color(e.message, 'red')
switched = False
except ex.CreationError as e:
print_with_color(e.message, 'red')
else:
click.echo('Component "{}" removed of node "{}/{}"'
.format(name, cluster, node))
finally:
if switched:
set_context(ctx, cluster)
@jumbo.command()
@click.argument('node', required=False)
@click.option('--cluster', '-c')
@click.option('--all', '-a', is_flag=True,
help='List components on all nodes')
@click.option('--abbr', is_flag=True, help='Display abbreviations')
def listcomponents(node, cluster, all, abbr):
"""
List compononents on a given node.
"""
if not cluster:
cluster = ss.svars['cluster']
if all:
for m in ss.svars['nodes']:
comp_table = PrettyTable(['Component', 'Service'])
comp_table.align['Component'] = 'l'
comp_table.align['Service'] = 'l'
click.echo('\n' + m['name'] + ':')
try:
for c in services.list_components(node=m['name'],
cluster=cluster):
service = services.check_component(c)
comp_table.add_row([
c if not abbr else services.get_abbr(c, service),
service
])
comp_table.sortby = 'Service'
except ex.LoadError as e:
print_with_color(e.message, 'red')
else:
print_colorized_table(comp_table)
else:
if node is None:
click.secho('You need to specify a node name. Use --all to list'
' all nodes', fg='red', err=True)
return
try:
comp_table = PrettyTable(['Component', 'Service'])
comp_table.align['Component'] = 'l'
comp_table.align['Service'] = 'l'
for c in services.list_components(node=node,
cluster=cluster):
service = services.check_component(c)
comp_table.add_row([
c if not abbr else services.get_abbr(c, service),
service
])
comp_table.sortby = 'Service'
except ex.LoadError as e:
print_with_color(e.message, 'red')
else:
print_colorized_table(comp_table)
@jumbo.command()
@click.argument('name')
@click.option('--cluster', '-c')
def checkservice(name, cluster):
"""Check if a service is complete (if all needed components are installed).
:param name: Service name
"""
if not cluster:
cluster = ss.svars['cluster']
try:
missing_comp = services.check_service_complete(name=name,
cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
else:
if missing_comp:
click.echo('The service "{}" misses:\n{}'
.format(name, '\n'.join(missing_comp)))
else:
click.echo('The service "%s" is complete.' % name)
@jumbo.command()
@click.option('--cluster', '-c')
def listservices(cluster):
"""Check the state of all services installed.
:param cluster: Cluster name
:type cluster: str
"""
if not cluster:
cluster = ss.svars['cluster']
try:
table_serv = PrettyTable(['Service', 'HA', 'Missing components'])
table_serv.align = 'l'
for s in ss.svars['services']:
color = 'green'
missing_comp = services.check_service_complete(name=s,
cluster=cluster)
if missing_comp:
print_missing = '\n'.join(missing_comp)
color = 'yellow' if len(missing_comp) < 5 else 'red'
else:
print_missing = '-'
table_serv.add_row([click.style(s, fg=color),
'X' if services.check_ha(s) else ' ',
print_missing])
table_serv.sortby = 'Service'
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
else:
click.echo(table_serv)
####################
# Vagrant commands #
####################
@jumbo.command()
@click.argument('cluster_name', required=False)
@click.option('--cluster', '-c')
def start(cluster_name, cluster):
"""Launches the VMs (vagrant up)
"""
cluster = cluster_name if cluster_name else cluster
if not cluster:
cluster = ss.svars['cluster']
try:
vagrant.cmd(['vagrant', 'up', '--color'], cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
@jumbo.command()
@click.argument('cluster_name', required=False)
@click.option('--cluster', '-c')
def stop(cluster_name, cluster):
cluster = cluster_name if cluster_name else cluster
if not cluster:
cluster = ss.svars['cluster']
try:
vagrant.cmd(['vagrant', 'halt', '--color'], cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
@jumbo.command()
@click.argument('cluster_name', required=False)
@click.option('--cluster', '-c')
def status(cluster_name, cluster):
cluster = cluster_name if cluster_name else cluster
if not cluster:
cluster = ss.svars['cluster']
try:
vagrant.cmd(['vagrant', 'status', '--color'], cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
@jumbo.command()
@click.argument('cluster_name', required=False)
@click.option('--cluster', '-c')
def provision(cluster_name, cluster):
cluster = cluster_name if cluster_name else cluster
if not cluster:
cluster = ss.svars['cluster']
try:
vagrant.cmd(['vagrant', 'up', '--provision', '--color'],
cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
@jumbo.command()
@click.argument('cluster_name', required=False)
@click.option('--cluster', '-c')
def restart(cluster_name, cluster):
cluster = cluster_name if cluster_name else cluster
if not cluster:
cluster = ss.svars['cluster']
try:
vagrant.cmd(['vagrant', 'halt', '--color'], cluster=cluster)
vagrant.cmd(['vagrant', 'up', '--color'], cluster=cluster)
except (ex.LoadError, ex.CreationError) as e:
print_with_color(e.message, 'red')
@jumbo.command()
def logo():
"""Print a random ASCII logo.
"""
click.echo(printlogo.jumbo_ascii())
def print_colorized_table(table):
to_print = []
if OS != 'Windows':
color = False
lines = table.get_string().split('\n')
for i, l in enumerate(lines):
if i > 2 and lines[i] != lines[-1]:
to_print.append(click.style(
l, fg='blue' if color else 'white'))
color = not color
else:
to_print.append(l)
else:
to_print = table
for l in to_print:
click.echo(l)