Reference for the technical implementation of the src.Manager project code.

Manager

Bases: NebulaBase

Manager class inherits from NebulaBase and is responsible for handling all aspects of the manager, including the setup, configuration and tear down of the variety of manager services such as: * registry * redis * mongo * manager * syncer

Attributes:

Name Type Description
DREGSY_CONFIG_FILE_PATH string

Config file for DREGSY which is the Syncer service being run on the manager

DREGSY_MAPPING_FILE_PATH : string Mapping file that maps remote container images to the registry

MONGO_IP : string Hostname or IP address of Mongo

MONGO_PORT : string Port number of Mongo DB

MONGO_USERNAME : string Username for accessing Mongo DB

MONGO_PASSWORD : string Password for Mongo DB

REGISTRY_IMAGE : string Docker image name to spin up

SYNCER_IMAGE : string Docker image name to spin up

REDIS_IMAGE : string Docker image name to spin up

MONGO_IMAGE : string Docker image name to spin up

MANAGER_IMAGE : string Docker image name to spin up

MANAGER_NMODE: string Docker image network to spin up

SYNCER_NMODE: string Docker image network to spin up

TODO: Make setManagerParams() REST API-friendly, which means that instead of a sys.exit(), it needs to either throw an appropriate exception or return a status value or both. Good way to do it would be to throw an exception here and then catch it on gustavo.py

Source code in src/Manager.py
 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
class Manager(NebulaBase):
    """
    Manager class inherits from NebulaBase and is responsible for handling all aspects of the manager, including
    the setup, configuration and tear down of the variety of manager services such as:
    * `registry`
    * `redis`
    * `mongo`
    * `manager`
    * `syncer`

    Attributes
    ----------
    DREGSY_CONFIG_FILE_PATH : string
        Config file for DREGSY which is the Syncer service being run on the manager

    DREGSY_MAPPING_FILE_PATH : string
        Mapping file that maps remote container images to the registry

    MONGO_IP : string
        Hostname or IP address of Mongo

    MONGO_PORT : string
        Port number of Mongo DB

    MONGO_USERNAME : string
        Username for accessing Mongo DB

    MONGO_PASSWORD : string
        Password for Mongo DB

    REGISTRY_IMAGE : string
        Docker image name to spin up

    SYNCER_IMAGE : string
        Docker image name to spin up

    REDIS_IMAGE : string
        Docker image name to spin up

    MONGO_IMAGE : string
        Docker image name to spin up

    MANAGER_IMAGE : string
        Docker image name to spin up

    MANAGER_NMODE: string
        Docker image network to spin up

    SYNCER_NMODE: string
        Docker image network to spin up

    TODO: Make setManagerParams() REST API-friendly, which means that instead of a sys.exit(), it needs to either
          throw an appropriate exception or return a status value or both.
          Good way to do it would be to throw an exception here and then catch it on gustavo.py

    """

    def __init__(self):

        NebulaBase.__init__(self)

        self.DREGSY_CONFIG_FILE_PATH = None
        self.DREGSY_MAPPING_FILE_PATH = None

        self.MONGO_IP = None
        self.MONGO_PORT = None
        self.MONGO_USERNAME = None
        self.MONGO_PASSWORD = None

        self.REGISTRY_IMAGE = None
        self.SYNCER_IMAGE = None
        self.REDIS_IMAGE = None
        self.MONGO_IMAGE = None
        self.MANAGER_IMAGE = None

        self.MANAGER_NMODE = None
        self.SYNCER_NMODE = None

        self.service_list = "registry,redis,mongo,manager,syncer"

        self.setManagerParams()

    def setManagerParams(self):
        """
        sets the class attributes from environment vars
        """
        if "DREGSY_CONFIG_FILE_PATH" in os.environ.keys():
            if os.path.isfile(os.environ["DREGSY_CONFIG_FILE_PATH"]):
                self.DREGSY_CONFIG_FILE_PATH = os.getenv("DREGSY_CONFIG_FILE_PATH")
            else:
                # raise Exception("DREGSY_CONFIG_FILE_PATH invalid")
                click.echo(click.style("DREGSY_CONFIG_FILE_PATH invalid", fg="red"))

                return {"error": True, "response": "DREGSY_CONFIG_FILE_PATH invalid"}
        else:
            # raise Exception("DREGSY_CONFIG_FILE_PATH undefined in base_config file")
            click.echo(
                click.style(
                    "DREGSY_CONFIG_FILE_PATH undefined in base_config file", fg="red"
                )
            )

            return {
                "error": True,
                "response": "DREGSY_CONFIG_FILE_PATH undefined in base_config file",
            }

        if "DREGSY_MAPPING_FILE_PATH" in os.environ.keys():
            if os.path.isfile(os.environ["DREGSY_MAPPING_FILE_PATH"]):
                self.DREGSY_MAPPING_FILE_PATH = os.getenv("DREGSY_MAPPING_FILE_PATH")
            else:
                # raise Exception("DREGSY_MAPPING_FILE_PATH invalid")
                click.echo(click.style("DREGSY_MAPPING_FILE_PATH invalid", fg="red"))

                return {"error": True, "response": "DREGSY_MAPPING_FILE_PATH invalid"}

        else:
            # raise Exception("DREGSY_MAPPING_FILE_PATH undefined in base_config file")
            click.echo(
                click.style(
                    "DREGSY_MAPPING_FILE_PATH undefined in base_config file", fg="red"
                )
            )

            return {
                "error": True,
                "response": "DREGSY_MAPPING_FILE_PATH undefined in base_config file",
            }

        if "MONGO_USERNAME" in os.environ.keys():
            self.MONGO_USERNAME = os.getenv("MONGO_USERNAME")
        else:
            # raise Exception("MONGO_USERNAME undefined in base_config file")
            click.echo(
                click.style("MONGO_USERNAME undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "MONGO_USERNAME undefined in base_config file",
            }

        if "MONGO_PASSWORD" in os.environ.keys():
            self.MONGO_PASSWORD = os.getenv("MONGO_PASSWORD")
        else:
            # raise Exception("MONGO_PASSWORD undefined in base_config file")
            click.echo(
                click.style("MONGO_PASSWORD undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "MONGO_PASSWORD undefined in base_config file",
            }

        if "MONGO_HOST" in os.environ.keys():
            self.MONGO_IP = os.getenv("MONGO_HOST")
        else:
            # raise Exception("MONGO_IP undefined in base_config file")
            click.echo(click.style("MONGO_IP undefined in base_config file", fg="red"))

            return {"error": True, "response": "MONGO_IP undefined in base_config file"}

        if "MONGO_PORT" in os.environ.keys():
            self.MONGO_PORT = int(os.getenv("MONGO_PORT"))
        else:
            # raise Exception("MONGO_PORT undefined in base_config file")
            click.echo(
                click.style("MONGO_PORT undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "MONGO_PORT undefined in base_config file",
            }

        if "REGISTRY_IMAGE" in os.environ.keys():
            self.REGISTRY_IMAGE = os.getenv("REGISTRY_IMAGE")
        else:
            click.echo(
                click.style("REGISTRY_IMAGE undefined in base_config file", fg="red")
            )
            return {
                "error": True,
                "response": "REGISTRY_IMAGE undefined in base_config file",
            }

        if "SYNCER_IMAGE" in os.environ.keys():
            self.SYNCER_IMAGE = os.getenv("SYNCER_IMAGE")
        else:

            click.echo(
                click.style("SYNCER_IMAGE undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "SYNCER_IMAGE undefined in base_config file",
            }
        if "REDIS_IMAGE" in os.environ.keys():
            self.REDIS_IMAGE = os.getenv("REDIS_IMAGE")
        else:

            click.echo(
                click.style("REDIS_IMAGE undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "REDIS_IMAGE undefined in base_config file",
            }
        if "MONGO_IMAGE" in os.environ.keys():
            self.MONGO_IMAGE = os.getenv("MONGO_IMAGE")
        else:

            click.echo(
                click.style("MONGO_IMAGE undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "MONGO_IMAGE undefined in base_config file",
            }
        if "MANAGER_IMAGE" in os.environ.keys():
            self.MANAGER_IMAGE = os.getenv("MANAGER_IMAGE")
        else:

            click.echo(
                click.style("MANAGER_IMAGE undefined in base_config file", fg="red")
            )

            return {
                "error": True,
                "response": "MANAGER_IMAGE undefined in base_config file",
            }

        if "MANAGER_NMODE" in os.environ.keys():
            self.MANAGER_NMODE = os.getenv("MANAGER_NMODE")
        else:
            self.MANAGER_NMODE = "bridge"
            click.echo(
                click.style("MANAGER_NMODE undefined in base_config file", fg="red")
            )

        if "SYNCER_NMODE" in os.environ.keys():
            self.SYNCER_NMODE = os.getenv("SYNCER_NMODE")
        else:
            self.SYNCER_NMODE = "bridge"

            click.echo(
                click.style("SYNCER_NMODE undefined in base_config file", fg="red")
            )

        return {"error": False, "response": "Manager Params set successfully"}

    def runRegistry(self, client):
        """
        Brings up the registry service

        Parameters
        ----------
        client : docker object
            The docker client object

        Raises
        ------
        docker.errors.ImageNotFound
            If the registry image is not found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the registry did not run
        If the key "error" is False it means that the registry ran successfully
            Based on success of the run registry command
        """

        if self.REGISTRY_IMAGE:

            # success = True
            dockerow.pull(self.REGISTRY_IMAGE)
            try:
                client.containers.run(
                    image=self.REGISTRY_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    ports={"5000": self.REGISTRY_PORT},
                    name="registry",
                    restart_policy={"Name": "always"},
                    volumes=[str(self.DOCKER_HOST_SOCKET) + ":/var/run/docker.sock:rw"],
                )
                # return {"error": False, "response": {"ipfs_bootnodes": redisRet}}
            except docker.errors.ImageNotFound as e:
                click.echo(click.style(e, fg="red"))
                click.echo(click.style("Registry image not found", fg="red"))
                # return False
                return {"error": True, "response": "Registry image not found"}
            except docker.errors.APIError as e:
                click.echo(click.style(e, fg="red"))
                click.echo(
                    click.style("Registry:Trouble reaching the docker API", fg="red")
                )
                # return False
                return {
                    "error": True,
                    "response": "Registry:Trouble reaching the docker API",
                }

            # return success
            return {"error": False, "response": "Registry ran successfully"}
        else:
            print("Registry Image Not defined in config files")
            return {
                "error": True,
                "response": "Registry Image Not defined in config files",
            }

    def runSyncer(self, client):
        """
        Brings up the syncer service

        Parameters
        ----------
        client : docker object
            The docker client object

        Raises
        ------
        docker.errors.ImageNotFound
            If the registry image is not found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the syncer did not run
        If the key "error" is False it means that the syncer ran successfully
            Based on success of the run syncer command
        """

        if self.SYNCER_IMAGE:
            # success = True
            dockerow.pull(self.SYNCER_IMAGE)
            try:

                if self.SYNCER_NMODE == "host":
                    client.containers.run(
                        image=self.SYNCER_IMAGE,
                        detach=True,
                        security_opt=["label=disable"],
                        name="syncer",
                        network_mode=self.SYNCER_NMODE,
                        restart_policy={"Name": "always"},
                        volumes=[
                            self.DREGSY_CONFIG_FILE_PATH + ":/config.yaml",
                            self.DREGSY_MAPPING_FILE_PATH + ":/mappings_list.yaml",
                        ],
                    )
                else:
                    client.containers.run(
                        image=self.SYNCER_IMAGE,
                        detach=True,
                        security_opt=["label=disable"],
                        name="syncer",
                        restart_policy={"Name": "always"},
                        volumes=[
                            self.DREGSY_CONFIG_FILE_PATH + ":/config.yaml",
                            self.DREGSY_MAPPING_FILE_PATH + ":/mappings_list.yaml",
                        ],
                    )
            except docker.errors.ImageNotFound as e:
                click.echo(click.style(e, fg="red"))
                click.echo(click.style("Syncer (Dregsy) image not found", fg="red"))
                # return False
                return {"error": True, "response": "Syncer (Dregsy) image not found"}
            except docker.errors.APIError as e:
                click.echo(click.style(e, fg="red"))
                click.echo(
                    click.style("Syncer:Trouble reaching the docker API", fg="red")
                )
                # return False
                return {
                    "error": True,
                    "response": "Syncer:Trouble reaching the docker API",
                }

            # return success
            return {"error": False, "response": "Syncer run successfully"}
        else:
            print("Syncer Image Not defined in config files")
            return {
                "error": True,
                "response": "Syncer Image Not defined in config files",
            }

    def runRedis(self, client):
        """
        Brings up the Redis service

        Parameters
        ----------
        client : docker object
            The docker client object

        Raises
        ------
        docker.errors.ImageNotFound
            If the registry image is not found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the redis did not run
        If the key "error" is False it means that the redis ran successfully
            Based on success of the run redis command
        """

        if self.REDIS_IMAGE:
            # success = True
            dockerow.pull(self.REDIS_IMAGE)
            try:
                client.containers.run(
                    image=self.REDIS_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="redis",
                    ports={"6379": str(self.REDIS_PORT)},
                    restart_policy={"Name": "always"},
                    environment=["AUTH_TOKEN=" + str(self.REDIS_AUTH_TOKEN)],
                )
            except docker.errors.ImageNotFound as e:
                click.echo(click.style(e, fg="red"))
                click.echo(click.style("Redis image not found", fg="red"))
                # return False
                return {"error": True, "response": "Redis image not found"}
            except docker.errors.APIError as e:
                click.echo(click.style(e, fg="red"))
                click.echo(
                    click.style("Redis:Trouble reaching the docker API", fg="red")
                )
                # return False
                return {
                    "error": True,
                    "response": "Redis:Trouble reaching the docker API",
                }

            # return success
            return {"error": False, "response": "Redis run successfully"}
        else:
            print("Redis Image Not defined in config files")
            return {
                "error": True,
                "response": "Redis Image Not defined in config files",
            }

    def runMongo(self, client):
        """
        Brings up the Mongo service

        Parameters
        ----------
        client : docker object
            The docker client object

        Raises
        ------
        docker.errors.ImageNotFound
            If the registry image is not found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the mongo did not run
        If the key "error" is False it means that the mongo ran successfully
            Based on success of the run mongo command
        """

        if self.MONGO_IMAGE:
            # success = True
            dockerow.pull(self.MONGO_IMAGE)
            try:
                client.containers.run(
                    image=self.MONGO_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="mongo",
                    hostname="mongo",
                    ports={"27017": self.MONGO_PORT},
                    restart_policy={"Name": "always"},
                    environment=[
                        "MONGO_INITDB_ROOT_USERNAME=" + str(self.MONGO_USERNAME),
                        "MONGO_INITDB_ROOT_PASSWORD=" + str(self.MONGO_PASSWORD),
                    ],
                )
            except docker.errors.ImageNotFound as e:
                click.echo(click.style(e, fg="red"))
                click.echo(click.style("Mongo image not found", fg="red"))
                # return False
                return {"error": True, "response": "Mongo image not found"}
            except docker.errors.APIError as e:
                click.echo(click.style(e, fg="red"))
                click.echo(
                    click.style("Mongo:Trouble reaching the docker API", fg="red")
                )
                # return False
                return {
                    "error": True,
                    "response": "Mongo:Trouble reaching the docker API",
                }

            # return success
            return {"error": False, "response": "Mongo run successfully"}
        else:
            print("Mongo Image Not defined in config files")
            return {
                "error": True,
                "response": "Mongo Image Not defined in config files",
            }

    def runManager(self, client):
        """
        Brings up the Nebula Manager service

        Parameters
        ----------
        client : docker object
            The docker client object

        Raises
        ------
        docker.errors.ImageNotFound
            If the registry image is not found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the manager did not run
        If the key "error" is False it means that the manager ran successfully
            Based on success of the run manager command
        """

        if self.MANAGER_IMAGE:
            # success = True
            dockerow.pull(self.MANAGER_IMAGE)
            try:
                print("Spinning up Manager in " + self.MANAGER_NMODE + " network mode")
                if self.MANAGER_NMODE == "host":
                    client.containers.run(
                        image=self.MANAGER_IMAGE,
                        detach=True,
                        security_opt=["label=disable"],
                        name="manager",
                        network_mode=self.MANAGER_NMODE,
                        hostname="manager",
                        restart_policy={"Name": "always"},
                        environment=[
                            "MONGO_URL=mongodb://"
                            + str(self.MONGO_USERNAME)
                            + ":"
                            + str(self.MONGO_PASSWORD)
                            + "@"
                            + str(self.MONGO_IP)
                            + ":"
                            + str(self.MONGO_PORT)
                            + "/nebula?authSource=admin",
                            # "MONGO_URL=mongodb://nebula:nebula@10.0.0.70:27017/nebula?authSource=admin",
                            "SCHEMA_NAME=nebula",
                            "BASIC_AUTH_USER=" + str(self.NEBULA_USERNAME),
                            "BASIC_AUTH_PASSWORD=" + str(self.NEBULA_PASSWORD),
                            "AUTH_TOKEN=" + str(self.NEBULA_AUTH_TOKEN),
                        ],
                    )
                else:
                    client.containers.run(
                        image=self.MANAGER_IMAGE,
                        detach=True,
                        security_opt=["label=disable"],
                        name="manager",
                        hostname="manager",
                        ports={"80": self.MANAGER_PORT},
                        restart_policy={"Name": "always"},
                        environment=[
                            "MONGO_URL=mongodb://"
                            + str(self.MONGO_USERNAME)
                            + ":"
                            + str(self.MONGO_PASSWORD)
                            + "@"
                            + str(self.MONGO_IP)
                            + ":"
                            + str(self.MONGO_PORT)
                            + "/nebula?authSource=admin",
                            # "MONGO_URL=mongodb://nebula:nebula@10.0.0.70:27017/nebula?authSource=admin",
                            "SCHEMA_NAME=nebula",
                            "BASIC_AUTH_USER=" + str(self.NEBULA_USERNAME),
                            "BASIC_AUTH_PASSWORD=" + str(self.NEBULA_PASSWORD),
                            "AUTH_TOKEN=" + str(self.NEBULA_AUTH_TOKEN),
                        ],
                    )

            except docker.errors.ImageNotFound as e:
                click.echo(click.style(e, fg="red"))
                click.echo(click.style("Manager image not found", fg="red"))
                # return False
                return {"error": True, "response": "Manager image not found"}
            except docker.errors.APIError as e:
                click.echo(click.style(e, fg="red"))
                click.echo(
                    click.style("Manager:Trouble reaching the docker API", fg="red")
                )
                # return False
                return {
                    "error": True,
                    "response": "Manager:Trouble reaching the docker API",
                }

            # return success
            return {"error": False, "response": "Manager run successfully"}
        else:
            print("Manager Image Not defined in config files")
            return {
                "error": True,
                "response": "Manager Image Not defined in config files",
            }

    def checkManager(self):
        """
        Checks whether manager API is available

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the check manager did not run
        If the key "error" is False it means that the check manager ran successfully
            Based on success of the check manager command
        """

        # nebulaObj = Nebula(username=self.NEBULA_USERNAME, host=self.MANAGER_IP, port=self.MANAGER_PORT,
        #                         token=self.NEBULA_AUTH_TOKEN, password=self.NEBULA_PASSWORD)
        # response = nebulaObj.check_api()
        if not self.NEBULA_PROTOCOL:
            self.NEBULA_PROTOCOL = "http"
        url = urlparse(
            self.NEBULA_PROTOCOL
            + "://"
            + str(self.MANAGER_IP)
            + ":"
            + str(self.MANAGER_PORT)
            + "/api/v2/status"
        )
        try:
            response = requests.get(
                url.geturl(),
                headers={"Authorization": "Basic " + self.NEBULA_AUTH_TOKEN},
            )
            if response.status_code == 200:
                click.echo(click.style("Manager Up", fg="green"))
                # return True
                return {"error": False, "response": "Manager up successfully"}
        except Exception as e:
            print("Unexpected error:", e)
            return {"error": True, "response": e}

        # return False

    def waitManager(self):
        """
        Keeps waiting until Nebula Manager API responds

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the wait manager did not run
        If the key "error" is False it means that the wait manager ran successfully
            Based on success of the wait manager command
        """
        managerUp = False
        response = None
        while not managerUp:
            time.sleep(3)
            click.echo(click.style("Waiting for manager to come alive..", fg="yellow"))
            response = self.checkManager()
            if not response["error"]:
                managerUp = True
            # managerUp = self.checkManager()
        # return True
        return {"error": False, "response": "Manager alive"}

    def run(self, service_name):
        """
        Wrapper function to invoke the corresponding run function based on a given service name. Options are :
        * `registry`
        * `redis`
        * `mongo`
        * `manager`
        * `syncer`
        * `all`

        Parameters
        ----------
        service_name : string
            Name of service to run

        TODO: return success status
        """

        fg = "green"

        client = docker.from_env()
        success = None
        if service_name == "registry":
            success = self.runRegistry(client)
            if not success["error"]:
                click.echo(click.style("Registry Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Registry Image Not defined in config files",
                }

        elif service_name == "redis":
            success = self.runRedis(client)
            if not success["error"]:
                click.echo(click.style("Redis Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Redis Image Not defined in config files",
                }
        elif service_name == "syncer":
            success = self.runSyncer(client)
            if not success["error"]:
                click.echo(click.style("Syncer Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Syncer Image Not defined in config files",
                }
        elif service_name == "mongo":
            success = self.runMongo(client)
            if not success["error"]:
                click.echo(click.style("Mongo Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Mongo Image Not defined in config files",
                }
        elif service_name == "manager":
            success = self.runManager(client)
            if not success["error"]:
                self.waitManager()
            else:
                return {
                    "error": True,
                    "response": "Manager Image Not defined in config files",
                }
        elif service_name == "all":
            success = self.runRegistry(client)
            if not success["error"]:
                click.echo(click.style("Registry Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Registry Image Not defined in config files",
                }

            success = self.runRedis(client)
            if not success["error"]:
                click.echo(click.style("Redis Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Redis Image Not defined in config files",
                }
            success = self.runSyncer(client)
            if not success["error"]:
                click.echo(click.style("Syncer Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Syncer Image Not defined in config files",
                }
            success = self.runMongo(client)
            if not success["error"]:
                click.echo(click.style("Mongo Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Mongo Image Not defined in config files",
                }
            success = self.runManager(client)
            if not success["error"]:
                click.echo(click.style("Manager Up", fg="green"))
            else:
                return {
                    "error": True,
                    "response": "Manager Image Not defined in config files",
                }
            self.waitManager()

        else:
            click.echo(click.style("service_name is not valid", fg="red"))

        return fg

    def handleService(self, service_name, action):
        """
        Performs a given action on a given service name. The option for actions are:
        * `stop` : stop given service
        * `start` : start given service
        * `kill` : kill given service
        * `remove` : remove given service
        * `restart` : restart given service

        Parameters
        ----------
        service_name : string
            Name of service to run

        action : string
            Action to be taken

        Raises
        ------
        docker.errors.NotFound
            No container with the given service name has been found

        docker.errors.APIError
            If the docker API is unreachable

        Returns
        -------
        dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                     success message if everything is running"}
        If the key "error" is True it means that there is some error and the handle service did not run
        If the key "error" is False it means that the handle service ran successfully
            Based on success of the handle service command
        """

        if not isinstance(service_name, str):
            click.echo(click.style("service_name is not valid", fg="red"))

        client = docker.from_env()
        try:
            container_obj = client.containers.get(service_name)
        except docker.errors.NotFound:
            click.echo(click.style("No container called " + service_name, fg="red"))
            # return False
            return {"error": True, "response": "No container called"}

        except docker.errors.APIError:
            click.echo(click.style("Trouble reaching the docker API", fg="red"))
            # return False
            return {"error": True, "response": "Trouble reaching the docker API"}
        try:
            if action == "stop":
                click.echo(click.style("Stopping " + str(service_name), fg="yellow"))
                container_obj.stop()
                click.echo(
                    click.style("{} has been stopped".format(service_name), fg="green")
                )

            elif action == "start":
                click.echo(click.style("Starting " + str(service_name), fg="yellow"))
                container_obj.start()
                click.echo(
                    click.style("{} has been started".format(service_name), fg="green")
                )

            elif action == "kill":
                click.echo(click.style("Killing " + str(service_name), fg="yellow"))
                container_obj.kill()
                click.echo(
                    click.style("{} has been killed".format(service_name), fg="green")
                )

            elif action == "remove":
                click.echo(click.style("Removing " + str(service_name), fg="yellow"))
                container_obj.remove(force=True)
                click.echo(
                    click.style("{} has been removed".format(service_name), fg="green")
                )

            elif action == "restart":
                click.echo(click.style("Restarting " + str(service_name), fg="yellow"))
                container_obj.restart()
                click.echo(
                    click.style(
                        "{} has been restarted".format(service_name), fg="green"
                    )
                )

            else:
                click.echo(click.style("action is not valid", fg="red"))
                # return False
                return {"error": True, "response": "action is not valid"}

        except docker.errors.APIError:
            click.echo(click.style("Trouble reaching the docker API", fg="red"))
            # return False
            return {"error": True, "response": "Trouble reaching the docker API"}

        # return True
        return {"error": False, "response": "Service handled successfully"}

checkManager()

Checks whether manager API is available

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the check manager did not run
If the key "error" is False it means that the check manager ran successfully

Based on success of the check manager command

Source code in src/Manager.py
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
def checkManager(self):
    """
    Checks whether manager API is available

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the check manager did not run
    If the key "error" is False it means that the check manager ran successfully
        Based on success of the check manager command
    """

    # nebulaObj = Nebula(username=self.NEBULA_USERNAME, host=self.MANAGER_IP, port=self.MANAGER_PORT,
    #                         token=self.NEBULA_AUTH_TOKEN, password=self.NEBULA_PASSWORD)
    # response = nebulaObj.check_api()
    if not self.NEBULA_PROTOCOL:
        self.NEBULA_PROTOCOL = "http"
    url = urlparse(
        self.NEBULA_PROTOCOL
        + "://"
        + str(self.MANAGER_IP)
        + ":"
        + str(self.MANAGER_PORT)
        + "/api/v2/status"
    )
    try:
        response = requests.get(
            url.geturl(),
            headers={"Authorization": "Basic " + self.NEBULA_AUTH_TOKEN},
        )
        if response.status_code == 200:
            click.echo(click.style("Manager Up", fg="green"))
            # return True
            return {"error": False, "response": "Manager up successfully"}
    except Exception as e:
        print("Unexpected error:", e)
        return {"error": True, "response": e}

handleService(service_name, action)

Performs a given action on a given service name. The option for actions are: * stop : stop given service * start : start given service * kill : kill given service * remove : remove given service * restart : restart given service

Parameters:

Name Type Description Default
service_name string

Name of service to run

required

action : string Action to be taken

Raises:

Type Description
docker.errors.NotFound

No container with the given service name has been found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the handle service did not run
If the key "error" is False it means that the handle service ran successfully

Based on success of the handle service command

Source code in src/Manager.py
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
def handleService(self, service_name, action):
    """
    Performs a given action on a given service name. The option for actions are:
    * `stop` : stop given service
    * `start` : start given service
    * `kill` : kill given service
    * `remove` : remove given service
    * `restart` : restart given service

    Parameters
    ----------
    service_name : string
        Name of service to run

    action : string
        Action to be taken

    Raises
    ------
    docker.errors.NotFound
        No container with the given service name has been found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the handle service did not run
    If the key "error" is False it means that the handle service ran successfully
        Based on success of the handle service command
    """

    if not isinstance(service_name, str):
        click.echo(click.style("service_name is not valid", fg="red"))

    client = docker.from_env()
    try:
        container_obj = client.containers.get(service_name)
    except docker.errors.NotFound:
        click.echo(click.style("No container called " + service_name, fg="red"))
        # return False
        return {"error": True, "response": "No container called"}

    except docker.errors.APIError:
        click.echo(click.style("Trouble reaching the docker API", fg="red"))
        # return False
        return {"error": True, "response": "Trouble reaching the docker API"}
    try:
        if action == "stop":
            click.echo(click.style("Stopping " + str(service_name), fg="yellow"))
            container_obj.stop()
            click.echo(
                click.style("{} has been stopped".format(service_name), fg="green")
            )

        elif action == "start":
            click.echo(click.style("Starting " + str(service_name), fg="yellow"))
            container_obj.start()
            click.echo(
                click.style("{} has been started".format(service_name), fg="green")
            )

        elif action == "kill":
            click.echo(click.style("Killing " + str(service_name), fg="yellow"))
            container_obj.kill()
            click.echo(
                click.style("{} has been killed".format(service_name), fg="green")
            )

        elif action == "remove":
            click.echo(click.style("Removing " + str(service_name), fg="yellow"))
            container_obj.remove(force=True)
            click.echo(
                click.style("{} has been removed".format(service_name), fg="green")
            )

        elif action == "restart":
            click.echo(click.style("Restarting " + str(service_name), fg="yellow"))
            container_obj.restart()
            click.echo(
                click.style(
                    "{} has been restarted".format(service_name), fg="green"
                )
            )

        else:
            click.echo(click.style("action is not valid", fg="red"))
            # return False
            return {"error": True, "response": "action is not valid"}

    except docker.errors.APIError:
        click.echo(click.style("Trouble reaching the docker API", fg="red"))
        # return False
        return {"error": True, "response": "Trouble reaching the docker API"}

    # return True
    return {"error": False, "response": "Service handled successfully"}

run(service_name)

Wrapper function to invoke the corresponding run function based on a given service name. Options are : * registry * redis * mongo * manager * syncer * all

Parameters:

Name Type Description Default
service_name string

Name of service to run

required

TODO: return success status

Source code in src/Manager.py
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
def run(self, service_name):
    """
    Wrapper function to invoke the corresponding run function based on a given service name. Options are :
    * `registry`
    * `redis`
    * `mongo`
    * `manager`
    * `syncer`
    * `all`

    Parameters
    ----------
    service_name : string
        Name of service to run

    TODO: return success status
    """

    fg = "green"

    client = docker.from_env()
    success = None
    if service_name == "registry":
        success = self.runRegistry(client)
        if not success["error"]:
            click.echo(click.style("Registry Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Registry Image Not defined in config files",
            }

    elif service_name == "redis":
        success = self.runRedis(client)
        if not success["error"]:
            click.echo(click.style("Redis Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Redis Image Not defined in config files",
            }
    elif service_name == "syncer":
        success = self.runSyncer(client)
        if not success["error"]:
            click.echo(click.style("Syncer Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Syncer Image Not defined in config files",
            }
    elif service_name == "mongo":
        success = self.runMongo(client)
        if not success["error"]:
            click.echo(click.style("Mongo Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Mongo Image Not defined in config files",
            }
    elif service_name == "manager":
        success = self.runManager(client)
        if not success["error"]:
            self.waitManager()
        else:
            return {
                "error": True,
                "response": "Manager Image Not defined in config files",
            }
    elif service_name == "all":
        success = self.runRegistry(client)
        if not success["error"]:
            click.echo(click.style("Registry Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Registry Image Not defined in config files",
            }

        success = self.runRedis(client)
        if not success["error"]:
            click.echo(click.style("Redis Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Redis Image Not defined in config files",
            }
        success = self.runSyncer(client)
        if not success["error"]:
            click.echo(click.style("Syncer Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Syncer Image Not defined in config files",
            }
        success = self.runMongo(client)
        if not success["error"]:
            click.echo(click.style("Mongo Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Mongo Image Not defined in config files",
            }
        success = self.runManager(client)
        if not success["error"]:
            click.echo(click.style("Manager Up", fg="green"))
        else:
            return {
                "error": True,
                "response": "Manager Image Not defined in config files",
            }
        self.waitManager()

    else:
        click.echo(click.style("service_name is not valid", fg="red"))

    return fg

runManager(client)

Brings up the Nebula Manager service

Parameters:

Name Type Description Default
client docker object

The docker client object

required

Raises:

Type Description
docker.errors.ImageNotFound

If the registry image is not found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the manager did not run
If the key "error" is False it means that the manager ran successfully

Based on success of the run manager command

Source code in src/Manager.py
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
def runManager(self, client):
    """
    Brings up the Nebula Manager service

    Parameters
    ----------
    client : docker object
        The docker client object

    Raises
    ------
    docker.errors.ImageNotFound
        If the registry image is not found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the manager did not run
    If the key "error" is False it means that the manager ran successfully
        Based on success of the run manager command
    """

    if self.MANAGER_IMAGE:
        # success = True
        dockerow.pull(self.MANAGER_IMAGE)
        try:
            print("Spinning up Manager in " + self.MANAGER_NMODE + " network mode")
            if self.MANAGER_NMODE == "host":
                client.containers.run(
                    image=self.MANAGER_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="manager",
                    network_mode=self.MANAGER_NMODE,
                    hostname="manager",
                    restart_policy={"Name": "always"},
                    environment=[
                        "MONGO_URL=mongodb://"
                        + str(self.MONGO_USERNAME)
                        + ":"
                        + str(self.MONGO_PASSWORD)
                        + "@"
                        + str(self.MONGO_IP)
                        + ":"
                        + str(self.MONGO_PORT)
                        + "/nebula?authSource=admin",
                        # "MONGO_URL=mongodb://nebula:nebula@10.0.0.70:27017/nebula?authSource=admin",
                        "SCHEMA_NAME=nebula",
                        "BASIC_AUTH_USER=" + str(self.NEBULA_USERNAME),
                        "BASIC_AUTH_PASSWORD=" + str(self.NEBULA_PASSWORD),
                        "AUTH_TOKEN=" + str(self.NEBULA_AUTH_TOKEN),
                    ],
                )
            else:
                client.containers.run(
                    image=self.MANAGER_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="manager",
                    hostname="manager",
                    ports={"80": self.MANAGER_PORT},
                    restart_policy={"Name": "always"},
                    environment=[
                        "MONGO_URL=mongodb://"
                        + str(self.MONGO_USERNAME)
                        + ":"
                        + str(self.MONGO_PASSWORD)
                        + "@"
                        + str(self.MONGO_IP)
                        + ":"
                        + str(self.MONGO_PORT)
                        + "/nebula?authSource=admin",
                        # "MONGO_URL=mongodb://nebula:nebula@10.0.0.70:27017/nebula?authSource=admin",
                        "SCHEMA_NAME=nebula",
                        "BASIC_AUTH_USER=" + str(self.NEBULA_USERNAME),
                        "BASIC_AUTH_PASSWORD=" + str(self.NEBULA_PASSWORD),
                        "AUTH_TOKEN=" + str(self.NEBULA_AUTH_TOKEN),
                    ],
                )

        except docker.errors.ImageNotFound as e:
            click.echo(click.style(e, fg="red"))
            click.echo(click.style("Manager image not found", fg="red"))
            # return False
            return {"error": True, "response": "Manager image not found"}
        except docker.errors.APIError as e:
            click.echo(click.style(e, fg="red"))
            click.echo(
                click.style("Manager:Trouble reaching the docker API", fg="red")
            )
            # return False
            return {
                "error": True,
                "response": "Manager:Trouble reaching the docker API",
            }

        # return success
        return {"error": False, "response": "Manager run successfully"}
    else:
        print("Manager Image Not defined in config files")
        return {
            "error": True,
            "response": "Manager Image Not defined in config files",
        }

runMongo(client)

Brings up the Mongo service

Parameters:

Name Type Description Default
client docker object

The docker client object

required

Raises:

Type Description
docker.errors.ImageNotFound

If the registry image is not found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the mongo did not run
If the key "error" is False it means that the mongo ran successfully

Based on success of the run mongo command

Source code in src/Manager.py
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
def runMongo(self, client):
    """
    Brings up the Mongo service

    Parameters
    ----------
    client : docker object
        The docker client object

    Raises
    ------
    docker.errors.ImageNotFound
        If the registry image is not found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the mongo did not run
    If the key "error" is False it means that the mongo ran successfully
        Based on success of the run mongo command
    """

    if self.MONGO_IMAGE:
        # success = True
        dockerow.pull(self.MONGO_IMAGE)
        try:
            client.containers.run(
                image=self.MONGO_IMAGE,
                detach=True,
                security_opt=["label=disable"],
                name="mongo",
                hostname="mongo",
                ports={"27017": self.MONGO_PORT},
                restart_policy={"Name": "always"},
                environment=[
                    "MONGO_INITDB_ROOT_USERNAME=" + str(self.MONGO_USERNAME),
                    "MONGO_INITDB_ROOT_PASSWORD=" + str(self.MONGO_PASSWORD),
                ],
            )
        except docker.errors.ImageNotFound as e:
            click.echo(click.style(e, fg="red"))
            click.echo(click.style("Mongo image not found", fg="red"))
            # return False
            return {"error": True, "response": "Mongo image not found"}
        except docker.errors.APIError as e:
            click.echo(click.style(e, fg="red"))
            click.echo(
                click.style("Mongo:Trouble reaching the docker API", fg="red")
            )
            # return False
            return {
                "error": True,
                "response": "Mongo:Trouble reaching the docker API",
            }

        # return success
        return {"error": False, "response": "Mongo run successfully"}
    else:
        print("Mongo Image Not defined in config files")
        return {
            "error": True,
            "response": "Mongo Image Not defined in config files",
        }

runRedis(client)

Brings up the Redis service

Parameters:

Name Type Description Default
client docker object

The docker client object

required

Raises:

Type Description
docker.errors.ImageNotFound

If the registry image is not found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the redis did not run
If the key "error" is False it means that the redis ran successfully

Based on success of the run redis command

Source code in src/Manager.py
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
def runRedis(self, client):
    """
    Brings up the Redis service

    Parameters
    ----------
    client : docker object
        The docker client object

    Raises
    ------
    docker.errors.ImageNotFound
        If the registry image is not found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the redis did not run
    If the key "error" is False it means that the redis ran successfully
        Based on success of the run redis command
    """

    if self.REDIS_IMAGE:
        # success = True
        dockerow.pull(self.REDIS_IMAGE)
        try:
            client.containers.run(
                image=self.REDIS_IMAGE,
                detach=True,
                security_opt=["label=disable"],
                name="redis",
                ports={"6379": str(self.REDIS_PORT)},
                restart_policy={"Name": "always"},
                environment=["AUTH_TOKEN=" + str(self.REDIS_AUTH_TOKEN)],
            )
        except docker.errors.ImageNotFound as e:
            click.echo(click.style(e, fg="red"))
            click.echo(click.style("Redis image not found", fg="red"))
            # return False
            return {"error": True, "response": "Redis image not found"}
        except docker.errors.APIError as e:
            click.echo(click.style(e, fg="red"))
            click.echo(
                click.style("Redis:Trouble reaching the docker API", fg="red")
            )
            # return False
            return {
                "error": True,
                "response": "Redis:Trouble reaching the docker API",
            }

        # return success
        return {"error": False, "response": "Redis run successfully"}
    else:
        print("Redis Image Not defined in config files")
        return {
            "error": True,
            "response": "Redis Image Not defined in config files",
        }

runRegistry(client)

Brings up the registry service

Parameters:

Name Type Description Default
client docker object

The docker client object

required

Raises:

Type Description
docker.errors.ImageNotFound

If the registry image is not found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the registry did not run
If the key "error" is False it means that the registry ran successfully

Based on success of the run registry command

Source code in src/Manager.py
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
def runRegistry(self, client):
    """
    Brings up the registry service

    Parameters
    ----------
    client : docker object
        The docker client object

    Raises
    ------
    docker.errors.ImageNotFound
        If the registry image is not found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the registry did not run
    If the key "error" is False it means that the registry ran successfully
        Based on success of the run registry command
    """

    if self.REGISTRY_IMAGE:

        # success = True
        dockerow.pull(self.REGISTRY_IMAGE)
        try:
            client.containers.run(
                image=self.REGISTRY_IMAGE,
                detach=True,
                security_opt=["label=disable"],
                ports={"5000": self.REGISTRY_PORT},
                name="registry",
                restart_policy={"Name": "always"},
                volumes=[str(self.DOCKER_HOST_SOCKET) + ":/var/run/docker.sock:rw"],
            )
            # return {"error": False, "response": {"ipfs_bootnodes": redisRet}}
        except docker.errors.ImageNotFound as e:
            click.echo(click.style(e, fg="red"))
            click.echo(click.style("Registry image not found", fg="red"))
            # return False
            return {"error": True, "response": "Registry image not found"}
        except docker.errors.APIError as e:
            click.echo(click.style(e, fg="red"))
            click.echo(
                click.style("Registry:Trouble reaching the docker API", fg="red")
            )
            # return False
            return {
                "error": True,
                "response": "Registry:Trouble reaching the docker API",
            }

        # return success
        return {"error": False, "response": "Registry ran successfully"}
    else:
        print("Registry Image Not defined in config files")
        return {
            "error": True,
            "response": "Registry Image Not defined in config files",
        }

runSyncer(client)

Brings up the syncer service

Parameters:

Name Type Description Default
client docker object

The docker client object

required

Raises:

Type Description
docker.errors.ImageNotFound

If the registry image is not found

docker.errors.APIError If the docker API is unreachable

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the syncer did not run
If the key "error" is False it means that the syncer ran successfully

Based on success of the run syncer command

Source code in src/Manager.py
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
def runSyncer(self, client):
    """
    Brings up the syncer service

    Parameters
    ----------
    client : docker object
        The docker client object

    Raises
    ------
    docker.errors.ImageNotFound
        If the registry image is not found

    docker.errors.APIError
        If the docker API is unreachable

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the syncer did not run
    If the key "error" is False it means that the syncer ran successfully
        Based on success of the run syncer command
    """

    if self.SYNCER_IMAGE:
        # success = True
        dockerow.pull(self.SYNCER_IMAGE)
        try:

            if self.SYNCER_NMODE == "host":
                client.containers.run(
                    image=self.SYNCER_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="syncer",
                    network_mode=self.SYNCER_NMODE,
                    restart_policy={"Name": "always"},
                    volumes=[
                        self.DREGSY_CONFIG_FILE_PATH + ":/config.yaml",
                        self.DREGSY_MAPPING_FILE_PATH + ":/mappings_list.yaml",
                    ],
                )
            else:
                client.containers.run(
                    image=self.SYNCER_IMAGE,
                    detach=True,
                    security_opt=["label=disable"],
                    name="syncer",
                    restart_policy={"Name": "always"},
                    volumes=[
                        self.DREGSY_CONFIG_FILE_PATH + ":/config.yaml",
                        self.DREGSY_MAPPING_FILE_PATH + ":/mappings_list.yaml",
                    ],
                )
        except docker.errors.ImageNotFound as e:
            click.echo(click.style(e, fg="red"))
            click.echo(click.style("Syncer (Dregsy) image not found", fg="red"))
            # return False
            return {"error": True, "response": "Syncer (Dregsy) image not found"}
        except docker.errors.APIError as e:
            click.echo(click.style(e, fg="red"))
            click.echo(
                click.style("Syncer:Trouble reaching the docker API", fg="red")
            )
            # return False
            return {
                "error": True,
                "response": "Syncer:Trouble reaching the docker API",
            }

        # return success
        return {"error": False, "response": "Syncer run successfully"}
    else:
        print("Syncer Image Not defined in config files")
        return {
            "error": True,
            "response": "Syncer Image Not defined in config files",
        }

setManagerParams()

sets the class attributes from environment vars

Source code in src/Manager.py
 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
def setManagerParams(self):
    """
    sets the class attributes from environment vars
    """
    if "DREGSY_CONFIG_FILE_PATH" in os.environ.keys():
        if os.path.isfile(os.environ["DREGSY_CONFIG_FILE_PATH"]):
            self.DREGSY_CONFIG_FILE_PATH = os.getenv("DREGSY_CONFIG_FILE_PATH")
        else:
            # raise Exception("DREGSY_CONFIG_FILE_PATH invalid")
            click.echo(click.style("DREGSY_CONFIG_FILE_PATH invalid", fg="red"))

            return {"error": True, "response": "DREGSY_CONFIG_FILE_PATH invalid"}
    else:
        # raise Exception("DREGSY_CONFIG_FILE_PATH undefined in base_config file")
        click.echo(
            click.style(
                "DREGSY_CONFIG_FILE_PATH undefined in base_config file", fg="red"
            )
        )

        return {
            "error": True,
            "response": "DREGSY_CONFIG_FILE_PATH undefined in base_config file",
        }

    if "DREGSY_MAPPING_FILE_PATH" in os.environ.keys():
        if os.path.isfile(os.environ["DREGSY_MAPPING_FILE_PATH"]):
            self.DREGSY_MAPPING_FILE_PATH = os.getenv("DREGSY_MAPPING_FILE_PATH")
        else:
            # raise Exception("DREGSY_MAPPING_FILE_PATH invalid")
            click.echo(click.style("DREGSY_MAPPING_FILE_PATH invalid", fg="red"))

            return {"error": True, "response": "DREGSY_MAPPING_FILE_PATH invalid"}

    else:
        # raise Exception("DREGSY_MAPPING_FILE_PATH undefined in base_config file")
        click.echo(
            click.style(
                "DREGSY_MAPPING_FILE_PATH undefined in base_config file", fg="red"
            )
        )

        return {
            "error": True,
            "response": "DREGSY_MAPPING_FILE_PATH undefined in base_config file",
        }

    if "MONGO_USERNAME" in os.environ.keys():
        self.MONGO_USERNAME = os.getenv("MONGO_USERNAME")
    else:
        # raise Exception("MONGO_USERNAME undefined in base_config file")
        click.echo(
            click.style("MONGO_USERNAME undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "MONGO_USERNAME undefined in base_config file",
        }

    if "MONGO_PASSWORD" in os.environ.keys():
        self.MONGO_PASSWORD = os.getenv("MONGO_PASSWORD")
    else:
        # raise Exception("MONGO_PASSWORD undefined in base_config file")
        click.echo(
            click.style("MONGO_PASSWORD undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "MONGO_PASSWORD undefined in base_config file",
        }

    if "MONGO_HOST" in os.environ.keys():
        self.MONGO_IP = os.getenv("MONGO_HOST")
    else:
        # raise Exception("MONGO_IP undefined in base_config file")
        click.echo(click.style("MONGO_IP undefined in base_config file", fg="red"))

        return {"error": True, "response": "MONGO_IP undefined in base_config file"}

    if "MONGO_PORT" in os.environ.keys():
        self.MONGO_PORT = int(os.getenv("MONGO_PORT"))
    else:
        # raise Exception("MONGO_PORT undefined in base_config file")
        click.echo(
            click.style("MONGO_PORT undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "MONGO_PORT undefined in base_config file",
        }

    if "REGISTRY_IMAGE" in os.environ.keys():
        self.REGISTRY_IMAGE = os.getenv("REGISTRY_IMAGE")
    else:
        click.echo(
            click.style("REGISTRY_IMAGE undefined in base_config file", fg="red")
        )
        return {
            "error": True,
            "response": "REGISTRY_IMAGE undefined in base_config file",
        }

    if "SYNCER_IMAGE" in os.environ.keys():
        self.SYNCER_IMAGE = os.getenv("SYNCER_IMAGE")
    else:

        click.echo(
            click.style("SYNCER_IMAGE undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "SYNCER_IMAGE undefined in base_config file",
        }
    if "REDIS_IMAGE" in os.environ.keys():
        self.REDIS_IMAGE = os.getenv("REDIS_IMAGE")
    else:

        click.echo(
            click.style("REDIS_IMAGE undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "REDIS_IMAGE undefined in base_config file",
        }
    if "MONGO_IMAGE" in os.environ.keys():
        self.MONGO_IMAGE = os.getenv("MONGO_IMAGE")
    else:

        click.echo(
            click.style("MONGO_IMAGE undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "MONGO_IMAGE undefined in base_config file",
        }
    if "MANAGER_IMAGE" in os.environ.keys():
        self.MANAGER_IMAGE = os.getenv("MANAGER_IMAGE")
    else:

        click.echo(
            click.style("MANAGER_IMAGE undefined in base_config file", fg="red")
        )

        return {
            "error": True,
            "response": "MANAGER_IMAGE undefined in base_config file",
        }

    if "MANAGER_NMODE" in os.environ.keys():
        self.MANAGER_NMODE = os.getenv("MANAGER_NMODE")
    else:
        self.MANAGER_NMODE = "bridge"
        click.echo(
            click.style("MANAGER_NMODE undefined in base_config file", fg="red")
        )

    if "SYNCER_NMODE" in os.environ.keys():
        self.SYNCER_NMODE = os.getenv("SYNCER_NMODE")
    else:
        self.SYNCER_NMODE = "bridge"

        click.echo(
            click.style("SYNCER_NMODE undefined in base_config file", fg="red")
        )

    return {"error": False, "response": "Manager Params set successfully"}

waitManager()

Keeps waiting until Nebula Manager API responds

Returns:

Name Type Description
dictionary

success message if everything is running"}

If the key "error" is True it means that there is some error and the wait manager did not run
If the key "error" is False it means that the wait manager ran successfully

Based on success of the wait manager command

Source code in src/Manager.py
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
def waitManager(self):
    """
    Keeps waiting until Nebula Manager API responds

    Returns
    -------
    dictionary : {"error": True/False, "response": "Gives appropriate message depending on the kind of failure or a
                 success message if everything is running"}
    If the key "error" is True it means that there is some error and the wait manager did not run
    If the key "error" is False it means that the wait manager ran successfully
        Based on success of the wait manager command
    """
    managerUp = False
    response = None
    while not managerUp:
        time.sleep(3)
        click.echo(click.style("Waiting for manager to come alive..", fg="yellow"))
        response = self.checkManager()
        if not response["error"]:
            managerUp = True
        # managerUp = self.checkManager()
    # return True
    return {"error": False, "response": "Manager alive"}