-
Notifications
You must be signed in to change notification settings - Fork 7
/
nebulactl.py
executable file
·946 lines (821 loc) · 46.7 KB
/
nebulactl.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
#!/usr/bin/env python3.7
import click, json, ast, os, base64
from NebulaPythonSDK import Nebula
from os.path import expanduser
VERSION = "2.7.1"
# i'm separating the nebulactl.py to 2 parts, the first is the NebulaCall class below which is going to be in charge of
# the communication with the SDK\API & formatting of replies to something a bit more CLI worthy
class NebulaCall:
# the init step reads from the authfile, not keeping connection open as it's a CLI
def __init__(self, ):
try:
home = expanduser("~")
auth_file = open(home + "/.nebula.json", "r")
auth_json = json.load(auth_file)
for key, value in auth_json.items():
if value == "":
auth_json[key] = None
if auth_json["password"] is None:
nebula_pass = None
else:
nebula_pass = base64.b64decode(auth_json["password"].encode('utf-8')).decode('utf-8')
if auth_json["token"] is None:
nebula_token = None
else:
nebula_token = base64.b64decode(auth_json["token"].encode('utf-8')).decode('utf-8')
self.connection = Nebula(username=auth_json["username"], host=auth_json["host"], port=auth_json["port"],
token=nebula_token, password=nebula_pass, protocol=auth_json["protocol"],
host_uri=auth_json["host_uri"])
except Exception as e:
click.echo(click.style(e, fg="red"))
click.echo(click.style("error reading ~/nebula.json auth file, try logging in first", fg="red"))
exit(2)
def create_app(self, app, config):
reply = self.connection.create_app(app, config)
if reply["status_code"] == 200:
click.echo(click.style("creating nebula app: " + app, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error creating " + app + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error creating " + app + ", app already exist", fg="red"))
else:
click.echo(click.style("error creating " + app
+ ", are you logged in? did you sent the right params & app name?", fg="red"))
def delete_app(self, app):
reply = self.connection.delete_app(app)
if reply["status_code"] == 200:
click.echo(click.style("deleting nebula app: " + app, fg="magenta"))
elif reply["status_code"] == 403:
click.echo(click.style("error deleting " + app + ", app doesn't exist", fg="red"))
else:
click.echo(click.style("error deleting " + app
+ ", are you logged in? did you sent the right app name?", fg="red"))
def list_apps(self):
reply = self.connection.list_apps()
if reply["status_code"] == 200:
reply_json = reply["reply"]
if len(reply_json["apps"]) == 0:
click.echo("no apps in nebula cluster")
else:
click.echo("nebula cluster apps:")
for app in reply_json["apps"]:
click.echo(app)
else:
click.echo(
click.style(
"error retuning list of nebula apps, are you logged in?",
fg="red"))
def check_api(self):
reply = self.connection.check_api()
if reply["status_code"] == 200:
reply_json = reply["reply"]
if reply_json == {'api_available': True}:
click.echo("nebula responding as expected")
else:
click.echo(
click.style(
"nebula api not responding, are you logged in?",
fg="red"))
else:
click.echo(
click.style(
"nebula api not responding, are you logged in?",
fg="red"))
def list_app_info(self, app):
reply = self.connection.list_app_info(app)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing " + app
+ " info, are you logged in? did you sent the right app name?", fg="red"))
def stop_app(self, app):
reply = self.connection.stop_app(app)
if reply["status_code"] == 202:
click.echo("stopping nebula app: " + app)
else:
click.echo(click.style("error stopping " + app +
", are you logged in? did you sent the right app name?", fg="red"))
def start_app(self, app):
reply = self.connection.start_app(app)
if reply["status_code"] == 202:
click.echo("starting nebula app: " + app)
else:
click.echo(click.style("error starting " + app
+ ", are you logged in? did you sent the right app name?", fg="red"))
def restart_app(self, app):
reply = self.connection.restart_app(app)
if reply["status_code"] == 202:
click.echo(click.style("restarting nebula app: " + app, fg="yellow"))
else:
click.echo(click.style("error restarting " + app
+ ", are you logged in? did you sent the right app name?", fg="red"))
def update_app(self, app, config):
reply = self.connection.update_app(app, config)
if reply["status_code"] == 202:
click.echo("updating nebula app: " + app)
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + app + ", missing or incorrect parameters", fg="red"))
else:
click.echo(click.style("error updating " + app
+ ", are you logged in? did you sent the right params & app name?", fg="red"))
def prune_device_group_images(self, app):
reply = self.connection.prune__device_group_images(app)
if reply["status_code"] == 202:
click.echo(click.style("pruning images on devices running app: " + app, fg="yellow"))
else:
click.echo(click.style("error pruning images on devices running app:" + app
+ ", are you logged in? did you sent the right app name?", fg="red"))
def prune_images(self):
reply = self.connection.prune_images()
if reply["status_code"] == 202:
click.echo(click.style("pruning images on all devices", fg="yellow"))
else:
click.echo(click.style("error pruning images on all devices, are you logged in? did you sent the "
"right app name?", fg="red"))
def list_device_group(self, device_group):
reply = self.connection.list_device_group(device_group)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing device_group :" + device_group
+ ", are you logged in? did you sent the right device_group name?", fg="red"))
def list_device_groups(self):
reply = self.connection.list_device_groups()
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing device_groups, are you logged in?", fg="red"))
def delete_device_group(self, device_group):
reply = self.connection.delete_device_group(device_group)
if reply["status_code"] == 200:
click.echo(click.style("deleted device_group " + device_group, fg="red"))
else:
click.echo(click.style("error deleting device_group :" + device_group
+ ", are you logged in? did you sent the right device_group name?", fg="red"))
def create_device_group(self, device_group, config):
reply = self.connection.create_device_group(device_group, config)
if reply["status_code"] == 200:
click.echo(click.style("creating nebula device_group: " + device_group, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error creating " + device_group + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error creating " + device_group + ", device_group already exist", fg="red"))
else:
click.echo(click.style("error creating " + device_group
+ ", are you logged in? did you sent the right params & app name?", fg="red"))
def update_device_group(self, device_group, config):
reply = self.connection.update_device_group(device_group, config)
if reply["status_code"] == 202:
click.echo(click.style("updating nebula device_group: " + device_group, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + device_group + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error updating " + device_group + ", device_group does not exist", fg="red"))
else:
click.echo(click.style("error updating " + device_group
+ ", are you logged in? did you sent the right params & app name?", fg="red"))
def list_reports(self, page_size, hostname, device_group, report_creation_time_filter, report_creation_time,
last_id, updated):
reply = self.connection.list_reports(page_size, hostname, device_group, report_creation_time_filter,
report_creation_time, last_id, updated)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, values in list(reply_json.items()):
click.echo(str(key) + ":")
for value in values:
if value == "$oid":
click.echo(json.dumps(values["$oid"]))
else:
click.echo(json.dumps(value))
else:
click.echo(click.style("error listing reports, are you logged in?", fg="red"))
def list_users(self):
reply = self.connection.list_users()
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing users, are you logged in?", fg="red"))
def list_user(self, user):
reply = self.connection.list_user(user)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing user :" + user
+ ", are you logged in? did you sent the right user name?", fg="red"))
def delete_user(self, user):
reply = self.connection.delete_user(user)
if reply["status_code"] == 200:
click.echo(click.style("deleted user " + user, fg="red"))
else:
click.echo(click.style("error deleting user :" + user
+ ", are you logged in? did you sent the right user name?", fg="red"))
def update_user(self, user, config):
reply = self.connection.update_user(user, config)
if reply["status_code"] == 200:
click.echo(click.style("updating nebula user: " + user, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + user + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error updating " + user + ", user does not exist", fg="red"))
else:
click.echo(click.style("error updating " + user
+ ", are you logged in? did you sent the right params & user name?", fg="red"))
def refresh_user_token(self, user):
reply = self.connection.refresh_user_token(user)
reply_json = reply["reply"]
if reply["status_code"] == 200:
click.echo(click.style("updating nebula user token: " + user, fg="green"))
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + user + " token, missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error updating " + user + " token, user does not exist", fg="red"))
else:
click.echo(click.style("error updating " + user
+ " token, are you logged in? did you sent the right params & user name?", fg="red"))
def create_user(self, user, config):
reply = self.connection.create_user(user, config)
if reply["status_code"] == 200:
click.echo(click.style("creating nebula user: " + user, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error creating " + user + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error creating " + user + ", user already exist", fg="red"))
else:
click.echo(click.style("error creating " + user
+ ", are you logged in? did you sent the right params & user name?", fg="red"))
def list_user_groups(self):
reply = self.connection.list_user_groups()
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing user groups, are you logged in?", fg="red"))
def list_user_group(self, user_group):
reply = self.connection.list_user_group(user_group)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing user group: " + user_group
+ ", are you logged in? did you sent the right user name?", fg="red"))
def delete_user_group(self, user_group):
reply = self.connection.delete_user_group(user_group)
if reply["status_code"] == 200:
click.echo(click.style("deleted user group " + user_group, fg="red"))
else:
click.echo(click.style("error deleting user group: " + user_group
+ ", are you logged in? did you sent the right user name?", fg="red"))
def update_user_group(self, user_group, config):
reply = self.connection.update_user_group(user_group, config)
if reply["status_code"] == 200:
click.echo(click.style("updating nebula user group: " + user_group, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + user_group + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error updating " + user_group + ", user does not exist", fg="red"))
else:
click.echo(click.style("error updating " + user_group
+ ", are you logged in? did you sent the right params & user name?", fg="red"))
def create_user_group(self, user_group, config):
reply = self.connection.create_user_group(user_group, config)
if reply["status_code"] == 200:
click.echo(click.style("creating nebula user group: " + user_group, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error creating " + user_group + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error creating " + user_group + ", user already exist", fg="red"))
else:
click.echo(click.style("error creating " + user_group
+ ", are you logged in? did you sent the right params & user name?", fg="red"))
def create_cron_job(self, cron_job, config):
reply = self.connection.create_cron_job(cron_job, config)
if reply["status_code"] == 200:
click.echo(click.style("creating nebula cron_job: " + cron_job, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error creating " + cron_job + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error creating " + cron_job + ", cron_job already exist", fg="red"))
else:
click.echo(click.style("error creating " + cron_job
+ ", are you logged in? did you sent the right params & cron_job name?", fg="red"))
def update_cron_job(self, cron_job, config):
reply = self.connection.update_cron_job(cron_job, config)
if reply["status_code"] == 200:
click.echo(click.style("updating nebula cron_job: " + cron_job, fg="green"))
elif reply["status_code"] == 400:
click.echo(click.style("error updating " + cron_job + ", missing or incorrect parameters", fg="red"))
elif reply["status_code"] == 403:
click.echo(click.style("error updating " + cron_job + ", cron_job does not exist", fg="red"))
else:
click.echo(click.style("error updating " + cron_job
+ ", are you logged in? did you sent the right params & cron_job name?", fg="red"))
def list_cron_jobs(self):
reply = self.connection.list_cron_jobs()
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing cron_jobs, are you logged in?", fg="red"))
def list_cron_job_info(self, cron_job):
reply = self.connection.list_cron_job_info(cron_job)
reply_json = reply["reply"]
if reply["status_code"] == 200:
for key, value in list(reply_json.items()):
click.echo(str(key) + ": " + json.dumps(value))
else:
click.echo(click.style("error listing " + cron_job
+ " info, are you logged in? did you sent the right cron_job name?", fg="red"))
def delete_cron_job(self, cron_job):
reply = self.connection.delete_cron_job(cron_job)
if reply["status_code"] == 200:
click.echo(click.style("deleting nebula cron_job: " + cron_job, fg="magenta"))
elif reply["status_code"] == 403:
click.echo(click.style("error deleting " + cron_job + ", app doesn't exist", fg="red"))
else:
click.echo(click.style("error deleting " + cron_job
+ ", are you logged in? did you sent the right cron_job name?", fg="red"))
# the 2nd part of nebulactl.py, the click functions from here until the end of the file are in charge of the CLI side of
# things, meaning help text, arguments input, arguments prompts & login file interfacing
@click.version_option(version=VERSION)
@click.group(help="Connect to a Nebula orchestrator management endpoint, Create Nebula apps and Manage them all from "
"a simple CLI.")
def nebulactl():
pass
# command group for everything image pruning related
@click.version_option(version=VERSION)
@nebulactl.group(help="Prune images.")
def prune():
pass
# command group for everything app related
@click.version_option(version=VERSION)
@nebulactl.group(help="Manage nebula apps.")
def apps():
pass
# command group for everything device_group related
@click.version_option(version=VERSION)
@nebulactl.group(help="Manage nebula device_groups.")
def device_groups():
pass
# command group for everything users related
@click.version_option(version=VERSION)
@nebulactl.group(help="Manage nebula users.")
def users():
pass
# command group for everything user groups related
@click.version_option(version=VERSION)
@nebulactl.group(help="Manage nebula user groups.")
def user_groups():
pass
# command group for everything cron jobs related
@click.version_option(version=VERSION)
@nebulactl.group(help="Manage nebula cron jobs.")
def cron_jobs():
pass
# command group for everything device_group related
@nebulactl.command(help="List nebula device reports.")
@click.option('--page_size', '-p', default=20, type=click.IntRange(1, 1000), help='the number of reports per page')
@click.option('--hostname', '-h', default=None, help='the hostname to filter reports by')
@click.option('--device_group', '-d', default=None, help='the device_group to filter reports by')
@click.option('--report_creation_time_filter', '-f', default="gt", help='the logic of filtering time by')
@click.option('--report_creation_time', '-r', default=None, help='time since unix epoch to filter by')
@click.option('--last_id', '-l', default=None, help='last_id of the previous page results')
@click.option('--updated', '-u', default=None, help='if True returns only reports where the device configuration was '
'updated, if False returns only reports when then device '
'configuration was not updated and if not set returns all reports')
def reports(page_size, hostname, device_group, report_creation_time_filter, report_creation_time, last_id, updated):
connection = NebulaCall()
connection.list_reports(page_size, hostname, device_group, report_creation_time_filter, report_creation_time,
last_id, updated)
# creates a cred file at ~/.nebula.json with the auth credentials or updates it's values if it exists
@nebulactl.command(help="login to nebula")
@click.option('--username', '-u', prompt='what is nebula manager basic auth username?',
help='nebula manager basic auth username. Optional if token provided', default="")
@click.option('--password', '-p', prompt='what is nebula manager basic auth password?', hide_input=True,
confirmation_prompt=True, help='nebula manager basic auth password. Optional if token provided', default="")
@click.option('--token', '-t', prompt='what is nebula manager auth token?', hide_input=True,
confirmation_prompt=True, help='nebula manager auth token. Optional if username and password provided', default="")
@click.option('--host', '-h', prompt='what is nebula manager host?', help='nebula manager host. Optional if host_uri provided', default="")
@click.option('--port', '-c', prompt='what is nebula manager port?', help='nebula manager port, defaults to 80',
default=80, type=click.IntRange(1, 65535))
@click.option('--protocol', '-P', prompt='what is nebula manager protocol?', default="http",
help='nebula manager protocol, defaults to http')
@click.option('--host_uri', '-U', prompt='what is nebula manager URI?', default="",
help='nebula manager complete URI, including protocol, host, port and path. Optional if host provided')
def login(username, password, token, host, port, protocol, host_uri):
home = expanduser("~")
auth_file = open(home + "/.nebula.json", "w+")
json.dump({"username": username, "password": base64.b64encode(password.encode('utf-8')).decode('utf-8'),
"token": base64.b64encode(token.encode('utf-8')).decode('utf-8'), "host": host, "port": port,
"protocol": protocol, "host_uri": host_uri}, auth_file)
auth_file.write('\n')
# deletes the cred file from the user home folder
@nebulactl.command(help="logout of nebula, useful when you want to make sure to delete stored credentials")
def logout():
home = expanduser("~")
os.remove(home + "/.nebula.json", )
@nebulactl.command(help="check nebula api responds")
def ping():
connection = NebulaCall()
connection.check_api()
@apps.command(help="list nebula apps", name="list")
def list_apps():
connection = NebulaCall()
connection.list_apps()
# create requires all the params so prompting for everything that missing with sensible\empty defaults where possible
@apps.command(help="create a new nebula app", name="create")
@click.option('--app', '-a', help='nebula app name to create', prompt='what is nebula app name to create?')
@click.option('--starting_ports', '-p', prompt="what are the app starting ports?", default=[],
help='starting ports to run in the format of X:Y,A:B where X,A=host_port & Y,B=container_port')
@click.option('--containers_per', '-c', prompt="what are the app containers_per value?",
help='cpu:X or server:X where X is the number of containers per cpu or server to have')
@click.option('--env_vars', '-e', help='nebula app envvars in the format of key:value,key1:value1... defaults to none',
prompt="what are the app envvars?")
@click.option('--image', '-i', help='nebula app docker image', prompt="what is the app docker image?")
@click.option('--running/--stopped', '-r/-s', default=True, help='nebula app running/stopped state, defaults to True',
prompt="should the app start in the running state?")
@click.option('--networks', '-n', default="", prompt="what is the app networks?",
help='nebula app network mode in csv format, defaults to [] ("nebula")')
@click.option('--volumes', '-v', default=[], prompt="what is the app volume mounts?",
help='nebula app volume mounts in csv format, defaults to [] (none/empty)')
@click.option('--devices', '-d', default=[], prompt="what is the app devices mounts?",
help='nebula app devices mounts in csv format, defaults to [] (none/empty)')
@click.option('--privileged/--unprivileged', '-P/-U', default=False,
help='nebula app privileged state, defaults to False',
prompt="should the app start with privileged permissions?")
@click.option('--rolling/--restart', '-R/-S', default=False, help='nebula app rolling restart/normal restart state',
prompt="should the app roll or restart normally?")
def create_app(app, starting_ports, containers_per, env_vars, image, running, networks, volumes, devices, privileged,
rolling):
starting_ports = starting_ports.split(",")
ports_list = []
for ports in starting_ports:
host_port, container_port = ports.split(":")
ports_dict = {str(host_port): str(container_port)}
ports_list.append(ports_dict)
containers_per = str(containers_per).split(":")
containers_per_dict = {containers_per[0]: int(containers_per[1])}
if len(volumes) != 0:
volumes = volumes.split(",")
if len(volumes) != 0:
networks = networks.split(",")
env_vars = ast.literal_eval("{\"" + env_vars.replace(":", "\":\"").replace(",", "\",\"") + "\"}")
config_json = {"starting_ports": ports_list, "containers_per": containers_per_dict,
"env_vars": dict(env_vars), "docker_image": str(image), "running": bool(running),
"networks": networks, "volumes": volumes, "devices": devices,
"privileged": bool(privileged), "rolling_restart": bool(rolling)}
connection = NebulaCall()
connection.create_app(app, config_json)
@apps.command(help="delete a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to delete?', help='nebula app name to delete')
@click.confirmation_option(help='auto confirm you want to delete the app',
prompt="are you sure you want to delete? there is no restore option")
def delete(app):
connection = NebulaCall()
connection.delete_app(app)
@apps.command(help="list info of a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to get info of?', help='nebula app name to get info of')
def info(app):
connection = NebulaCall()
connection.list_app_info(app)
@apps.command(help="start a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to start?', help='nebula app name to start')
def start(app):
connection = NebulaCall()
connection.start_app(app)
@apps.command(help="stop a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to stop?', help='nebula app name to stop')
def stop(app):
connection = NebulaCall()
connection.stop_app(app)
@apps.command(help="restart a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to restart?', help='nebula app name to restart')
def restart(app):
connection = NebulaCall()
connection.restart_app(app)
# update can be any combination of params, only one that's 100% required is the --app so it's the only one i'm prompting
@apps.command(help="update a nebula app")
@click.option('--app', '-a', prompt='what is nebula app name to update?', help='nebula app name to update')
@click.option('--starting_ports', '-p',
help='starting ports to run in the format of X:Y,A:B where X,A=host_port & Y,B=container_port')
@click.option('--containers_per', '-c',
help='cpu:X or server:X where X is the number of containers per cpu or server to have')
@click.option('--env_vars', '-e', help='nebula app envvars in the format of key:value,key1:value1...')
@click.option('--image', '-i', help='nebula app docker image')
@click.option('--running/--stopped', '-r/-s', help='nebula app running/stopped state')
@click.option('--networks', '-n', help='nebula app network mode in csv format')
@click.option('--volumes', '-v', help='nebula app volume mounts in csv format')
@click.option('--devices', '-d', help='nebula app devices mounts in csv format, defaults to [] (none/empty)')
@click.option('--privileged/--unprivileged', '-P/-U', help='nebula app privileged state, defaults to False')
@click.option('--rolling/--restart', '-R/-S', help='nebula app rolling restart/normal restart state')
def update(app, starting_ports, containers_per, env_vars, image, running, networks, volumes, devices, privileged,
rolling):
config_json = {}
if starting_ports is not None:
starting_ports = starting_ports.split(",")
ports_list = []
for ports in starting_ports:
ports = ports.split(":")
ports_dict = {str(ports[0]): str(ports[1])}
ports_list.append(ports_dict)
config_json["starting_ports"] = ports_list
if containers_per is not None:
containers_per = str(containers_per).split(":")
containers_per_dict = {containers_per[0]: int(containers_per[1])}
config_json["containers_per"] = containers_per_dict
if env_vars is not None:
env_vars = ast.literal_eval("{\"" + env_vars.replace(":", "\":\"").replace(",", "\",\"") + "\"}")
config_json["env_vars"] = dict(env_vars)
if image is not None:
config_json["docker_image"] = str(image)
if running is not None:
config_json["running"] = bool(running)
if rolling is not None:
config_json["rolling_restart"] = bool(rolling)
if networks is not None:
if networks != '[]':
networks = networks.split(",")
config_json["networks"] = networks
elif networks == '[]':
config_json["networks"] = []
if devices is not None:
if devices != '[]':
devices = devices.split(",")
config_json["devices"] = devices
elif devices == '[]':
config_json["devices"] = []
if privileged is not None:
config_json["privileged"] = bool(privileged)
if volumes is not None:
if volumes != '[]':
volumes = volumes.split(",")
config_json["volumes"] = volumes
elif volumes == '[]':
config_json["volumes"] = []
connection = NebulaCall()
connection.update_app(app, config_json)
@device_groups.command(help="list a device_group", name="info")
@click.option('--device_group', '-d', help='nebula device_group to get config of',
prompt='what is the device_group name?')
def device_group_info(device_group):
connection = NebulaCall()
connection.list_device_group(device_group)
@device_groups.command(help="list all device_groups", name="list")
def list_all_device_groups():
connection = NebulaCall()
connection.list_device_groups()
@prune.command(help="prune unused images on a device_group", name="device_group")
@click.option('--device_group', '-d', help='nebula device_group to prune images on',
prompt='what is the device_group name on devices you want to prune unused images on?')
def prune_device_group(device_group):
connection = NebulaCall()
connection.prune_device_group_images(device_group)
@prune.command(help="prune unused images on all device_groups", name="all")
def prune_all():
connection = NebulaCall()
connection.prune_images()
@device_groups.command(help="delete a device_group", name="delete")
@click.option('--device_group', '-d', help='nebula device_group to delete', prompt='what is the device_group name?')
@click.confirmation_option(help='auto confirm you want to delete the device_group',
prompt="are you sure you want to delete? there is no restore option")
def device_group_delete(device_group):
connection = NebulaCall()
connection.delete_device_group(device_group)
@device_groups.command(help="create a new nebula device_group", name="create")
@click.option('--device_group', '-d', help='nebula device_group to create', prompt='what is the device_group name?')
@click.option('--apps', '-a', prompt="what are the device_group apps?",
help='a CSV list of the apps that are part of the device_group')
@click.option('--cron_jobs', '-c', prompt="what are the device_group cron_jobs?",
help='a CSV list of the cron_jobs that are part of the device_group')
def device_group_create(device_group, apps, cron_jobs):
apps_list = apps.split(",")
config_json = {"apps": apps_list, "cron_jobs": cron_jobs}
connection = NebulaCall()
connection.create_device_group(device_group, config_json)
@device_groups.command(help="update a new nebula device_group", name="update")
@click.option('--device_group', '-d', help='nebula device_group to update', prompt='what is the device_group name?')
@click.option('--apps', '-a', prompt="what are the device_group apps?",
help='a CSV list of the apps that are part of the device_group')
@click.option('--cron_jobs', '-c', help='a CSV list of the cron_jobs that are part of the device_group')
def device_group_update(device_group, apps=None, cron_jobs=None):
config_json = {}
if apps is not None:
apps_list = apps.split(",")
config_json["apps"] = apps_list
if cron_jobs is not None:
cron_jobs_list = cron_jobs.split(",")
config_json["cron_jobs"] = cron_jobs_list
connection = NebulaCall()
connection.update_device_group(device_group, config_json)
@users.command(help="list nebula users", name="list")
def list_users():
connection = NebulaCall()
connection.list_users()
@users.command(help="list a user", name="info")
@click.option('--user', '-u', help='nebula user to get config of',
prompt='what is the user name?')
def user_info(user):
connection = NebulaCall()
connection.list_user(user)
@users.command(help="delete a user", name="delete")
@click.option('--user', '-u', help='nebula user to delete', prompt='what is the user name?')
@click.confirmation_option(help='auto confirm you want to delete the user',
prompt="are you sure you want to delete? there is no restore option")
def user_delete(user):
connection = NebulaCall()
connection.delete_user(user)
@users.command(help="refresh a user token", name="refresh")
@click.option('--user', '-u', help='nebula user to refresh the token of',
prompt='what is the user name?')
def refresh_user_token(user):
connection = NebulaCall()
connection.refresh_user_token(user)
@users.command(help="update a new nebula user", name="update")
@click.option('--user', '-u', help='nebula user to update', prompt='what is the user name?')
@click.option('--password', '-p', prompt="what are the user password?", help='the user basic auth password')
@click.option('--token', '-t', prompt="what are the user token?", help='the user bearer token')
def user_update(user, password, token):
config_json = {"password": password, "token": token}
connection = NebulaCall()
connection.update_user(user, config_json)
@users.command(help="create a new nebula user", name="create")
@click.option('--user', '-u', help='nebula user to create', prompt='what is the user name?')
@click.option('--password', '-p', prompt="what are the user password?", help='the user basic auth password')
@click.option('--token', '-t', prompt="what are the user token?", help='the user bearer token')
def user_create(user, password, token):
config_json = {"password": password, "token": token}
connection = NebulaCall()
connection.create_user(user, config_json)
@user_groups.command(help="list nebula user groups", name="list")
def list_user_groups():
connection = NebulaCall()
connection.list_user_groups()
@user_groups.command(help="list a user group", name="info")
@click.option('--group', '-g', help='nebula user group to get config of', prompt='what is the user group name?')
def user_group_info(group):
connection = NebulaCall()
connection.list_user_group(group)
@user_groups.command(help="delete a user group", name="delete")
@click.option('--group', '-g', help='nebula user group to delete', prompt='what is the user group name?')
@click.confirmation_option(help='auto confirm you want to delete the user group',
prompt="are you sure you want to delete? there is no restore option")
def user_group_delete(group):
connection = NebulaCall()
connection.delete_user_group(group)
@user_groups.command(help="update a new nebula user group", name="update")
@click.option('--group', '-g', help='nebula user to create', prompt='what is the user name?')
@click.option('--members', '-m', help='nebula user group members, defaults to [] (none/empty)')
@click.option('--pruning/--no-pruning', '-P/-N', help='image prunning allowed\not allowed')
@click.option('--admin/--user', '-A/-U', help='are group members considered admins')
@click.option('--apps', '-a', help='what apps will group member have access and what access type? {} (none/empty)')
@click.option('--device_group', '-d',
help='what device_groups will group member have access and what access type? {} (none/empty)')
def user_group_update(group, members, pruning, admin, apps, device_group):
config_json = {}
if members is not None:
config_json["group_members"]: members
if pruning is not None:
config_json["pruning_allowed"]: pruning
if apps is not None:
config_json["apps"]: apps
if device_group is not None:
config_json["device_groups"]: device_group
if admin is not None:
config_json["admin"]: admin
connection = NebulaCall()
connection.create_user_group(group, config_json)
@user_groups.command(help="create a new nebula user group", name="create")
@click.option('--group', '-g', help='nebula user to create', prompt='what is the user name?')
@click.option('--members', '-m', default=[], prompt="who are the group_members?",
help='nebula user group members, defaults to [] (none/empty)')
@click.option('--pruning/--no-pruning', '-P/-N', default=False, help='image prunning allowed\not allowed',
prompt="should user group allow pruning of images? defaults to not allowed")
@click.option('--admin/--user', '-A/-U', default=False, help='are group members considered admins',
prompt="are group members considered admins or users? defaults to users")
@click.option('--apps', '-a', default={}, prompt="what apps will group member have access and what access type?",
help='what apps will group member have access and what access type? {} (none/empty)')
@click.option('--device_group', '-d', default={},
prompt="what device_groups will group member have access and what access type?",
help='what device_groups will group member have access and what access type? {} (none/empty)')
def user_group_create(group, members, pruning, admin, apps, device_group):
config_json = {
"group_members": members,
"pruning_allowed": pruning,
"apps": apps,
"device_groups": device_group,
"admin": admin
}
connection = NebulaCall()
connection.create_user_group(group, config_json)
# create requires all the params so prompting for everything that missing with sensible\empty defaults where possible
@cron_jobs.command(help="create a new nebula cron_jobs", name="create")
@click.option('--cron_job', '-c', help='nebula cron_job name to create',
prompt='what is nebula cron_job name to create?')
@click.option('--schedule', '-t', help='nebula cron_job schedule to create',
prompt='what is nebula cron_job schedule to create?')
@click.option('--env_vars', '-e', help='nebula cron_job envvars in the format of key:value,key1:value1... defaults to '
'none',
prompt="what are the app envvars?")
@click.option('--image', '-i', help='nebula cron_job docker image', prompt="what is the cron_job docker image?")
@click.option('--running/--stopped', '-r/-s', default=True, help='nebula cron_job running/stopped state, defaults to '
'True',
prompt="should the cron_job start in the running state?")
@click.option('--networks', '-n', default="", prompt="what is the cron_job networks?",
help='nebula cron_job network mode in csv format, defaults to [] ("nebula")')
@click.option('--volumes', '-v', default=[], prompt="what is the cron_job volume mounts?",
help='nebula cron_job volume mounts in csv format, defaults to [] (none/empty)')
@click.option('--devices', '-d', default=[], prompt="what is the cron_job devices mounts?",
help='nebula cron_job devices mounts in csv format, defaults to [] (none/empty)')
@click.option('--privileged/--unprivileged', '-P/-U', default=False,
help='nebula cron_job privileged state, defaults to False',
prompt="should the app start with privileged permissions?")
def create_cron_jobs(cron_job, schedule, env_vars, image, running, networks, volumes, devices, privileged, rolling):
if volumes is not []:
volumes = volumes.split(",")
if networks is not []:
networks = networks.split(",")
env_vars = ast.literal_eval("{\"" + env_vars.replace(":", "\":\"").replace(",", "\",\"") + "\"}")
config_json = {"schedule": schedule, "env_vars": dict(env_vars), "docker_image": str(image),
"running": bool(running), "networks": networks, "volumes": volumes, "devices": devices,
"privileged": bool(privileged), "rolling_restart": bool(rolling)}
connection = NebulaCall()
connection.create_cron_job(cron_job, config_json)
# update can be any combination of params, only one that's 100% required is the --cron_job
@cron_jobs.command(help="update a nebula cron_job", name="update")
@click.option('--cron_job', '-c', prompt='what is nebula cron_job name to update?',
help='nebula cron_job name to update')
@click.option('--schedule', '-t', help='cron schedule to run the cron_job at ')
@click.option('--env_vars', '-e', help='nebula cron_job envvars in the format of key:value,key1:value1...')
@click.option('--image', '-i', help='nebula cron_job docker image')
@click.option('--running/--stopped', '-r/-s', help='nebula cron_job running/stopped state')
@click.option('--networks', '-n', help='nebula cron_job network mode in csv format')
@click.option('--volumes', '-v', help='nebula cron_job volume mounts in csv format')
@click.option('--devices', '-d', help='nebula cron_job devices mounts in csv format, defaults to [] (none/empty)')
@click.option('--privileged/--unprivileged', '-P/-U', help='nebula cron_job privileged state, defaults to False')
def update_cron_job(cron_job, schedule, env_vars, image, running, networks, volumes, devices, privileged):
config_json = {}
if schedule is not None:
config_json["schedule"] = schedule
if env_vars is not None:
env_vars = ast.literal_eval("{\"" + env_vars.replace(":", "\":\"").replace(",", "\",\"") + "\"}")
config_json["env_vars"] = dict(env_vars)
if image is not None:
config_json["docker_image"] = str(image)
if running is not None:
config_json["running"] = bool(running)
if networks is not None:
if networks != '[]':
networks = networks.split(",")
config_json["networks"] = networks
elif networks == '[]':
config_json["networks"] = []
if devices is not None:
if devices != '[]':
devices = devices.split(",")
config_json["devices"] = devices
elif devices == '[]':
config_json["devices"] = []
if privileged is not None:
config_json["privileged"] = bool(privileged)
if volumes is not None:
if volumes != '[]':
volumes = volumes.split(",")
config_json["volumes"] = volumes
elif volumes == '[]':
config_json["volumes"] = []
connection = NebulaCall()
connection.update_cron_job(cron_job, config_json)
@cron_jobs.command(help="list nebula cron_jobs", name="list")
def list_cron_jobs():
connection = NebulaCall()
connection.list_cron_jobs()
@cron_jobs.command(help="delete a nebula cron_jobs", name="delete")
@click.option('--cron_job', '-c', prompt='what is nebula cron_job name to delete?',
help='nebula cron_job name to delete')
@click.confirmation_option(help='auto confirm you want to delete the cron_jobs',
prompt="are you sure you want to delete? there is no restore option")
def delete_cron_job(cron_job):
connection = NebulaCall()
connection.delete_app(cron_job)
@cron_jobs.command(help="list info of a nebula cron_job", name="info")
@click.option('--cron_job', '-c', prompt='what is nebula cron_jobs name to get info of?',
help='nebula cron_job name to get info of')
def list_cron_job(cron_job):
connection = NebulaCall()
connection.list_cron_job_info(cron_job)
if __name__ == '__main__':
nebulactl()