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

Manager

NebulaBase class holds the configuration parameters as defined in the GUSTAVO_CONFIG_FILE

Attributes:

Name Type Description
nebulaObj nebula object

The nebula object provided by the Nebula Python API

REGISTRY_IP : string Hostname or IP address of Registry

REGISTRY_PORT : string Port number of Registry

REDIS_IP : string Hostname or IP address of Redis

REDIS_PORT : string Port number of Redis DB

REDIS_AUTH_TOKEN : string Auth token for Redis

MANAGER_IP : string Hostname or IP address of the machine where the Manager is hosted

NEBULA_USERNAME : string Username for Nebula

NEBULA_PASSWORD : string Password for Nebula

NEBULA_AUTH_TOKEN : string Auth token for the Nebula API

NEBULA_PROTOCOL : string Protocol for communicating with Nebula, http or https

DOCKER_HOST : string Docker Hostname

DOCKER_HOST_SOCKET : string Socket for Docker Client

WORKER_NMODE: string Worker network mode

TODO: Make setNebulaParams() 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/NebulaBase.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
class NebulaBase:
    """
    NebulaBase class holds the configuration parameters as defined in the GUSTAVO_CONFIG_FILE

    Attributes
    ----------

    nebulaObj : nebula object
        The nebula object provided by the Nebula Python API

    REGISTRY_IP : string
        Hostname or IP address of Registry

    REGISTRY_PORT : string
        Port number of Registry

    REDIS_IP : string
        Hostname or IP address of Redis

    REDIS_PORT : string
        Port number of Redis DB

    REDIS_AUTH_TOKEN : string
        Auth token for Redis

    MANAGER_IP : string
        Hostname or IP address of the machine where the Manager is hosted

    NEBULA_USERNAME : string
        Username for Nebula

    NEBULA_PASSWORD : string
        Password for Nebula

    NEBULA_AUTH_TOKEN : string
        Auth token for the Nebula API

    NEBULA_PROTOCOL : string
        Protocol for communicating with Nebula, http or https

    DOCKER_HOST : string
        Docker Hostname

    DOCKER_HOST_SOCKET : string
        Socket for Docker Client

    WORKER_NMODE: string
        Worker network mode

    TODO: Make setNebulaParams() 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):
        """
        Inorder to make NebulaBase rest friendly replaced sys.exit() with raising exceptions which will get excepted
        in gustavo.py and eventually return a dictionary there {"error": True, "response": reason for error}
        """
        self.base_config = None
        if "GUSTAVO_CONFIG_FILE" in os.environ:
            self.base_config = os.environ["GUSTAVO_CONFIG_FILE"]
            if not os.path.isfile(self.base_config):
                click.echo(
                    click.style(
                        "GUSTAVO_CONFIG_FILE: {} path not valid".format(
                            self.base_config
                        ),
                        fg="red",
                    )
                )
                raise PathInvalid
        else:
            click.echo(click.style("GUSTAVO_CONFIG_FILE not defined", fg="red"))
            raise FileUndefined

        self.REGISTRY_IP = None
        self.REGISTRY_PORT = None
        self.MANAGER_IP = None

        self.REDIS_IP = None
        self.REDIS_PORT = None
        self.REDIS_AUTH_TOKEN = None
        self.CACHE_PREFIX = None

        self.MANAGER_PORT = None
        self.NEBULA_USERNAME = None
        self.NEBULA_PASSWORD = None
        self.NEBULA_AUTH_TOKEN = None
        self.NEBULA_PROTOCOL = None

        self.DOCKER_HOST = None
        self.DOCKER_HOST_SOCKET = None

        self.WORKER_NMODE = None

        self.nebulaObj = None

        self.setNebulaParams()

    def setNebulaParams(self):
        """
        Sets the Nebula Params for all the class attributes
        Inorder to make NebulaBase rest friendly replaced sys.exit(); return a dictionary there
        {"error": True, "response": reason for error}
        """
        dotenv_path = Path(self.base_config)
        load_dotenv(dotenv_path=dotenv_path)

        if "CACHE_PREFIX" in os.environ.keys():
            self.CACHE_PREFIX = os.getenv("CACHE_PREFIX")
        else:
            self.CACHE_PREFIX = "nebula-reports"

        if "REDIS_HOST" in os.environ.keys():
            self.REDIS_IP = os.getenv("REDIS_HOST")
        else:
            # raise Exception("REDIS_IP undefined in base_config file")
            click.echo(click.style("REDIS_IP undefined in base_config file", fg="red"))
            # sys.exit()
            return {"error": True, "response": "REDIS_IP undefined in base_config file"}

        if "REDIS_PORT" in os.environ.keys():
            self.REDIS_PORT = int(os.getenv("REDIS_PORT"))
        else:
            # raise Exception("REDIS_PORT undefined in base_config file")
            click.echo(
                click.style("REDIS_PORT undefined in base_config file", fg="red")
            )
            # sys.exit()
            return {
                "error": True,
                "response": "REDIS_PORT undefined in base_config file",
            }

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

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

        if "REGISTRY_PORT" in os.environ.keys():
            self.REGISTRY_PORT = int(os.getenv("REGISTRY_PORT"))
        else:
            # raise Exception("REGISTRY_PORT undefined in base_config file")
            click.echo(
                click.style("REGISTRY_PORT undefined in base_config file", fg="red")
            )
            # sys.exit()
            return {
                "error": True,
                "response": "REGISTRY_PORT undefined in base_config file",
            }

        if "MANAGER_HOST" in os.environ.keys():
            self.MANAGER_IP = os.getenv("MANAGER_HOST")
        else:
            # raise Exception("MANAGER_IP undefined in base_config file")
            click.echo(
                click.style("MANAGER_IP undefined in base_config file", fg="red")
            )
            # sys.exit()
            return {
                "error": True,
                "response": "MANAGER_IP undefined in base_config file",
            }

        if "MANAGER_PORT" in os.environ.keys():
            self.MANAGER_PORT = os.getenv("MANAGER_PORT")
        else:
            # raise Exception("MANAGER_PORT undefined in base_config file")

            click.echo(
                click.style("MANAGER_PORT undefined in base_config file", fg="red")
            )
            # sys.exit()
            return {
                "error": True,
                "response": "MANAGER_PORT undefined in base_config file",
            }

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

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

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

        if "NEBULA_PASSWORD" in os.environ.keys():
            self.NEBULA_PASSWORD = os.getenv(
                "NEBULA_PASSWORD"
            )  # base64.b64decode(os.getenv("NEBULA_PASSWORD").encode('utf-8')).decode('utf-8')

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

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

        if "DOCKER_HOST" in os.environ.keys():
            self.DOCKER_HOST = str(os.environ["DOCKER_HOST"])
        else:
            self.DOCKER_HOST = "unix:/var/run/docker.sock"

        if len(self.DOCKER_HOST.split(":")) == 2:
            self.DOCKER_HOST_SOCKET = self.DOCKER_HOST.split(":")[1]
        else:
            click.echo(
                click.style(
                    "ERROR: DOCKER_HOST={} must be like unix:/var/run/docker.sock .... quitting".format(
                        self.DOCKER_HOST_SOCKET
                    ),
                    fg="red",
                )
            )

        print(self.NEBULA_USERNAME + "@" + self.MANAGER_IP + ":" + self.MANAGER_PORT)

        click.echo(click.style("DOCKER_HOST:{}".format(self.DOCKER_HOST), fg="yellow"))

__init__()

Inorder to make NebulaBase rest friendly replaced sys.exit() with raising exceptions which will get excepted in gustavo.py and eventually return a dictionary there {"error": True, "response": reason for error}

Source code in src/NebulaBase.py
 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
def __init__(self):
    """
    Inorder to make NebulaBase rest friendly replaced sys.exit() with raising exceptions which will get excepted
    in gustavo.py and eventually return a dictionary there {"error": True, "response": reason for error}
    """
    self.base_config = None
    if "GUSTAVO_CONFIG_FILE" in os.environ:
        self.base_config = os.environ["GUSTAVO_CONFIG_FILE"]
        if not os.path.isfile(self.base_config):
            click.echo(
                click.style(
                    "GUSTAVO_CONFIG_FILE: {} path not valid".format(
                        self.base_config
                    ),
                    fg="red",
                )
            )
            raise PathInvalid
    else:
        click.echo(click.style("GUSTAVO_CONFIG_FILE not defined", fg="red"))
        raise FileUndefined

    self.REGISTRY_IP = None
    self.REGISTRY_PORT = None
    self.MANAGER_IP = None

    self.REDIS_IP = None
    self.REDIS_PORT = None
    self.REDIS_AUTH_TOKEN = None
    self.CACHE_PREFIX = None

    self.MANAGER_PORT = None
    self.NEBULA_USERNAME = None
    self.NEBULA_PASSWORD = None
    self.NEBULA_AUTH_TOKEN = None
    self.NEBULA_PROTOCOL = None

    self.DOCKER_HOST = None
    self.DOCKER_HOST_SOCKET = None

    self.WORKER_NMODE = None

    self.nebulaObj = None

    self.setNebulaParams()

setNebulaParams()

Sets the Nebula Params for all the class attributes Inorder to make NebulaBase rest friendly replaced sys.exit(); return a dictionary there {"error": True, "response": reason for error}

Source code in src/NebulaBase.py
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
def setNebulaParams(self):
    """
    Sets the Nebula Params for all the class attributes
    Inorder to make NebulaBase rest friendly replaced sys.exit(); return a dictionary there
    {"error": True, "response": reason for error}
    """
    dotenv_path = Path(self.base_config)
    load_dotenv(dotenv_path=dotenv_path)

    if "CACHE_PREFIX" in os.environ.keys():
        self.CACHE_PREFIX = os.getenv("CACHE_PREFIX")
    else:
        self.CACHE_PREFIX = "nebula-reports"

    if "REDIS_HOST" in os.environ.keys():
        self.REDIS_IP = os.getenv("REDIS_HOST")
    else:
        # raise Exception("REDIS_IP undefined in base_config file")
        click.echo(click.style("REDIS_IP undefined in base_config file", fg="red"))
        # sys.exit()
        return {"error": True, "response": "REDIS_IP undefined in base_config file"}

    if "REDIS_PORT" in os.environ.keys():
        self.REDIS_PORT = int(os.getenv("REDIS_PORT"))
    else:
        # raise Exception("REDIS_PORT undefined in base_config file")
        click.echo(
            click.style("REDIS_PORT undefined in base_config file", fg="red")
        )
        # sys.exit()
        return {
            "error": True,
            "response": "REDIS_PORT undefined in base_config file",
        }

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

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

    if "REGISTRY_PORT" in os.environ.keys():
        self.REGISTRY_PORT = int(os.getenv("REGISTRY_PORT"))
    else:
        # raise Exception("REGISTRY_PORT undefined in base_config file")
        click.echo(
            click.style("REGISTRY_PORT undefined in base_config file", fg="red")
        )
        # sys.exit()
        return {
            "error": True,
            "response": "REGISTRY_PORT undefined in base_config file",
        }

    if "MANAGER_HOST" in os.environ.keys():
        self.MANAGER_IP = os.getenv("MANAGER_HOST")
    else:
        # raise Exception("MANAGER_IP undefined in base_config file")
        click.echo(
            click.style("MANAGER_IP undefined in base_config file", fg="red")
        )
        # sys.exit()
        return {
            "error": True,
            "response": "MANAGER_IP undefined in base_config file",
        }

    if "MANAGER_PORT" in os.environ.keys():
        self.MANAGER_PORT = os.getenv("MANAGER_PORT")
    else:
        # raise Exception("MANAGER_PORT undefined in base_config file")

        click.echo(
            click.style("MANAGER_PORT undefined in base_config file", fg="red")
        )
        # sys.exit()
        return {
            "error": True,
            "response": "MANAGER_PORT undefined in base_config file",
        }

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

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

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

    if "NEBULA_PASSWORD" in os.environ.keys():
        self.NEBULA_PASSWORD = os.getenv(
            "NEBULA_PASSWORD"
        )  # base64.b64decode(os.getenv("NEBULA_PASSWORD").encode('utf-8')).decode('utf-8')

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

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

    if "DOCKER_HOST" in os.environ.keys():
        self.DOCKER_HOST = str(os.environ["DOCKER_HOST"])
    else:
        self.DOCKER_HOST = "unix:/var/run/docker.sock"

    if len(self.DOCKER_HOST.split(":")) == 2:
        self.DOCKER_HOST_SOCKET = self.DOCKER_HOST.split(":")[1]
    else:
        click.echo(
            click.style(
                "ERROR: DOCKER_HOST={} must be like unix:/var/run/docker.sock .... quitting".format(
                    self.DOCKER_HOST_SOCKET
                ),
                fg="red",
            )
        )

    print(self.NEBULA_USERNAME + "@" + self.MANAGER_IP + ":" + self.MANAGER_PORT)

    click.echo(click.style("DOCKER_HOST:{}".format(self.DOCKER_HOST), fg="yellow"))