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

Composer

Bases: NebulaBase

Creates a class Composer with NebulaBase as the base class.

TODO: Make methods in this class 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/Composer.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
class Composer(NebulaBase):
    """
    Creates a class Composer with NebulaBase as the base class.

    TODO: Make methods in this class 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)

        if self.NEBULA_PROTOCOL:
            self.nebulaObj = Nebula(
                username=self.NEBULA_USERNAME,
                host=self.MANAGER_IP,
                port=self.MANAGER_PORT,
                token=self.NEBULA_AUTH_TOKEN,
                password=self.NEBULA_PASSWORD,
                protocol=self.NEBULA_PROTOCOL,
            )
        else:
            self.NEBULA_PROTOCOL = "http"
            self.nebulaObj = Nebula(
                username=self.NEBULA_USERNAME,
                host=self.MANAGER_IP,
                port=self.MANAGER_PORT,
                token=self.NEBULA_AUTH_TOKEN,
                password=self.NEBULA_PASSWORD,
            )

    def checkLocalRepoImages(self, name, tag):
        """
        Checks the existence of local images

        Parameters
        ----------

        name: string
            Name of the image

        tag: string
            The tag of the image

        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 images were not found
        If the key "error" is False it means that the images were not found


        """
        if name == "all":
            url = urlparse(
                self.NEBULA_PROTOCOL
                + "://"
                + str(self.REGISTRY_IP)
                + ":"
                + str(self.REGISTRY_PORT)
                + "/v2/_catalog"
            )
            response = requests.get(url.geturl(), auth=None, verify=False)
            response_dict = json.loads(response.text)
            print(response_dict)
            return {"error": False, "response": response_dict}
        else:
            url = urlparse(
                self.NEBULA_PROTOCOL
                + "://"
                + str(self.REGISTRY_IP)
                + ":"
                + str(self.REGISTRY_PORT)
                + "/v2/"
                + str(name)
                + "/tags/list"
            )
            try:
                response = requests.get(url.geturl(), auth=None, verify=False)
            except requests.exceptions.RequestException as e:  # This is the correct syntax
                raise SystemExit(e)
            response_dict = json.loads(response.text)
            if "errors" in response_dict.keys():
                error_msg = response_dict["errors"][0]
                if "code" in error_msg.keys() and error_msg["code"] == "NAME_UNKNOWN":
                    click.echo(
                        click.style(
                            "{} has not been found in the local repository, add this to the syncer mapping list to pull from remote".format(
                                name
                            ),
                            fg="red",
                        )
                    )
                else:
                    click.echo(click.style(str(error_msg), fg="red"))
                # sys.exit()
                return {
                    "error": True,
                    "response": "{} has not been found in the local repository, add this to the syncer mapping list to pull from remote".format(
                        name
                    ),
                }
            elif "name" in response_dict.keys():
                print(response_dict)
                if tag == "all":
                    # sys.exit()
                    return {"error": True, "response": "tag cannot be all"}
                elif tag not in response_dict["tags"]:
                    click.echo(
                        click.style(
                            "tag: "
                            + str(tag)
                            + " not found among tag list "
                            + str(response_dict["tags"]),
                            fg="red",
                        )
                    )
                    # sys.exit()
                    return {
                        "error": True,
                        "response": "tag: "
                        + str(tag)
                        + " not found among tag list "
                        + str(response_dict["tags"]),
                    }
                else:
                    click.echo(
                        click.style(
                            "{}:{} have been found in local registry".format(name, tag),
                            fg="green",
                        )
                    )
                    return {
                        "error": False,
                        "response": "{}:{} have been found in local registry".format(
                            name, tag
                        ),
                    }
            else:
                click.echo(click.style("Unknown error has occured", fg="red"))
                click.echo(click.style(str(response_dict["errors"][0]), fg="red"))
                # sys.exit()
                return {
                    "error": True,
                    "response": "unknown error has occured"
                    + str(response_dict["errors"][0]),
                }

    def checkImageExists(self, name):
        """
        Check if image exists

        Parameters
        ----------

        name: string
            Name of the image to be checked
        """
        repository_name = name.split(
            str(self.REGISTRY_IP) + ":" + str(self.REGISTRY_PORT) + "/"
        )[1]
        if len(repository_name.split(":")) == 1:
            tag = "latest"
        else:
            tag = repository_name.split(":")[1]
        # http://10.0.0.70:5000/v2/ubuntu/tags/list
        self.checkLocalRepoImages(repository_name, tag)

    def printDiagnosticResponse(
        self, reply, accept_code, moding, asset_type, asset_name
    ):
        # 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 which is printed in printDiagnosticResponse
        #              If the key "error" is False it means that there is some error which is printed in printDiagnosticResponse
        # print(reply)
        # returnval = False
        if reply["status_code"] == accept_code:
            click.echo(
                click.style(
                    moding + "ed nebula " + asset_type + " : " + asset_name, fg="green"
                )
            )
            # returnval = True
            return {
                "error": False,
                "response": moding + "ed nebula " + asset_type + " : " + asset_name,
            }
        elif reply["status_code"] == 400:
            click.echo(
                click.style(
                    "error "
                    + moding
                    + "ing "
                    + asset_type
                    + " : "
                    + asset_name
                    + ", missing or incorrect parameters",
                    fg="red",
                )
            )
            return {
                "error": True,
                "response": "error "
                + moding
                + "ing "
                + asset_type
                + " : "
                + asset_name
                + ", missing or incorrect parameters",
            }
        elif reply["status_code"] == 403:
            if asset_type + "_exists" in reply["reply"].keys():
                if reply["reply"][asset_type + "_exists"]:
                    click.echo(
                        click.style(
                            "error "
                            + moding
                            + "ing "
                            + asset_type
                            + " : "
                            + asset_name
                            + ", "
                            + str(asset_type)
                            + " already exists",
                            fg="red",
                        )
                    )
                    return {
                        "error": True,
                        "response": "error "
                        + moding
                        + "ing "
                        + asset_type
                        + " : "
                        + asset_name
                        + ", "
                        + str(asset_type)
                        + " already exists",
                    }
                else:
                    click.echo(
                        click.style(
                            "error "
                            + moding
                            + "ing "
                            + asset_type
                            + " : "
                            + asset_name
                            + ", "
                            + str(asset_type)
                            + " does not exist",
                            fg="red",
                        )
                    )
                    return {
                        "error": True,
                        "response": "error "
                        + moding
                        + "ing "
                        + asset_type
                        + " : "
                        + asset_name
                        + ", "
                        + str(asset_type)
                        + " does not exist",
                    }
            else:
                click.echo(
                    click.style(
                        "error "
                        + moding
                        + "ing "
                        + asset_type
                        + " : "
                        + asset_name
                        + ", server replied with :"
                        + str(reply["reply"]),
                        fg="red",
                    )
                )
                return {
                    "error": True,
                    "response": "error "
                    + moding
                    + "ing "
                    + asset_type
                    + " : "
                    + asset_name
                    + ", server replied with :"
                    + str(reply["reply"]),
                }
        else:
            click.echo(
                click.style(
                    "error "
                    + moding
                    + "ing "
                    + asset_type
                    + " : "
                    + asset_name
                    + ", are you logged in? did you send the right params & app name?",
                    fg="red",
                )
            )
            return {
                "error": True,
                "response": "error "
                + moding
                + "ing "
                + asset_type
                + " : "
                + asset_name
                + ", are you logged in? did you send the right params & app name?",
            }
        # return returnval

    def prune_device_group_images(self, app):
        """
        Prunes the images on device group

        Parameters
        ----------

        app: string
            Name of the app whose images are to be pruned

        """
        reply = self.nebulaObj.prune__device_group_images(app)
        if reply["status_code"] == 202:
            click.echo(
                click.style(
                    "pruning images on devices running app: " + app, fg="yellow"
                )
            )
            return {
                "error": False,
                "response": "pruning images on devices running app: " + app,
            }
        else:
            click.echo(
                click.style(
                    "error pruning images on devices running app:"
                    + app
                    + ", are you logged in? did you sent the right app name?",
                    fg="red",
                )
            )
            return {
                "error": True,
                "response": "error pruning images on devices running app: " + app,
            }

    # ("device_group","bca","create",device_group_config)
    def handleAsset(self, asset_type, asset_name, mode, config=None):

        """
        Handles a generic asset either an app or a device group and performs the functions of create, update or delete.

        Parameters
        ----------

        asset_type : string
            Type of asset, legal values could be either an "app" or "device_group"

        asset_name: string
            The name of the asset

        mode : string
            The mode of handling, allowed values are "create", "update" and "delete"

        config : dict
            The configuration of the asset to be created or updated. Not applicable in case of delete.

        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 asset did not run successfully
        If the key "error" is False it means that the handle asset did not run successfully
        """

        # retval = False
        if config == None and (mode.lower() == "create" or mode.lower() == "update"):
            click.echo(
                click.style(
                    "config is invlaid for " + asset_type + " : " + asset_name,
                    fg="green",
                )
            )
            return {
                "error": True,
                "response": "config is invlaid for " + asset_type + " : " + asset_name,
            }

        if mode.lower() == "create":
            accept_code = 200
            if asset_type == "app":
                self.checkImageExists(config["docker_image"])
                reply = self.nebulaObj.create_app(asset_name, config)

            elif asset_type == "device_group":
                reply = self.nebulaObj.create_device_group(asset_name, config)

            else:
                click.echo(click.style("unknown asset type " + asset_type, fg="red"))
                return {"error": True, "response": "unknown asset type " + asset_type}
            moding = "creat"

        elif mode.lower() == "update":
            accept_code = 202
            if asset_type == "app":
                self.checkImageExists(config["docker_image"])
                reply = self.nebulaObj.update_app(asset_name, config)

            elif asset_type == "device_group":
                reply = self.nebulaObj.update_device_group(asset_name, config)

            else:
                click.echo(click.style("unknown asset type " + asset_type, fg="red"))
                return {"error": True, "response": "unknown asset type " + asset_type}
            moding = "updat"

        elif mode.lower() == "delete":
            accept_code = 200
            if asset_type == "app":
                reply = self.nebulaObj.delete_app(asset_name)

            elif asset_type == "device_group":
                reply = self.nebulaObj.delete_device_group(asset_name)

            else:
                click.echo(click.style("unknown asset type " + asset_type, fg="red"))
                return {"error": True, "response": "unknown asset type " + asset_type}
            moding = "delet"
        else:
            click.echo(click.style("unknown command " + mode, fg="red"))
            return {"error": True, "response": "unknown command " + mode}

        retval = self.printDiagnosticResponse(
            reply, accept_code, moding, asset_type, asset_name
        )

        return retval

    def handleDeviceGroup(self, app_list, mode, device_group="bca"):
        """
        Handles aspects of the device group such as deleting, updating of apps

        Parameters
        ----------

        app_list : string
            Comma separated list of strings to be updated or deleted from the device group

        mode: string
            The mode i.e. update or delete

        device_group : string
            The device group name to be handled

        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 device group did not run successfully
        If the key "error" is False it means that the handle device group did run successfully
        """
        response = self.nebulaObj.list_device_group(device_group)
        success = self.printDiagnosticResponse(
            response, 200, "check", "app list for", device_group
        )
        if not success["error"]:
            new_app_list = app_list.split(",")
            existing_app_list = response["reply"]["apps"]
            apps_to_be_modified = existing_app_list
            if mode != "update" and mode != "delete":
                click.echo(click.style("unsupported mode " + mode, fg="red"))
                return {"error": True, "response": "unsupported mode"}
            for app in new_app_list:
                if mode == "update" and app not in existing_app_list:
                    apps_to_be_modified = apps_to_be_modified + [app]
                if mode == "delete" and app in existing_app_list:
                    apps_to_be_modified.remove(app)

            device_group_config = dict({"apps": apps_to_be_modified})
            print(device_group_config)
            self.handleAsset(
                "device_group", device_group, "update", device_group_config
            )
            response = self.nebulaObj.list_device_group(device_group)

            self.printDiagnosticResponse(
                response, 200, "check", "app list for", device_group
            )
            click.echo(response["reply"]["apps"])

            return {"error": False, "response": response["reply"]["apps"]}

checkImageExists(name)

Check if image exists

Parameters:

Name Type Description Default
name

Name of the image to be checked

required
Source code in src/Composer.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def checkImageExists(self, name):
    """
    Check if image exists

    Parameters
    ----------

    name: string
        Name of the image to be checked
    """
    repository_name = name.split(
        str(self.REGISTRY_IP) + ":" + str(self.REGISTRY_PORT) + "/"
    )[1]
    if len(repository_name.split(":")) == 1:
        tag = "latest"
    else:
        tag = repository_name.split(":")[1]
    # http://10.0.0.70:5000/v2/ubuntu/tags/list
    self.checkLocalRepoImages(repository_name, tag)

checkLocalRepoImages(name, tag)

Checks the existence of local images

Parameters:

Name Type Description Default
name

Name of the image

required

tag: string The tag of the image

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 images were not found
If the key "error" is False it means that the images were not found
Source code in src/Composer.py
 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
def checkLocalRepoImages(self, name, tag):
    """
    Checks the existence of local images

    Parameters
    ----------

    name: string
        Name of the image

    tag: string
        The tag of the image

    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 images were not found
    If the key "error" is False it means that the images were not found


    """
    if name == "all":
        url = urlparse(
            self.NEBULA_PROTOCOL
            + "://"
            + str(self.REGISTRY_IP)
            + ":"
            + str(self.REGISTRY_PORT)
            + "/v2/_catalog"
        )
        response = requests.get(url.geturl(), auth=None, verify=False)
        response_dict = json.loads(response.text)
        print(response_dict)
        return {"error": False, "response": response_dict}
    else:
        url = urlparse(
            self.NEBULA_PROTOCOL
            + "://"
            + str(self.REGISTRY_IP)
            + ":"
            + str(self.REGISTRY_PORT)
            + "/v2/"
            + str(name)
            + "/tags/list"
        )
        try:
            response = requests.get(url.geturl(), auth=None, verify=False)
        except requests.exceptions.RequestException as e:  # This is the correct syntax
            raise SystemExit(e)
        response_dict = json.loads(response.text)
        if "errors" in response_dict.keys():
            error_msg = response_dict["errors"][0]
            if "code" in error_msg.keys() and error_msg["code"] == "NAME_UNKNOWN":
                click.echo(
                    click.style(
                        "{} has not been found in the local repository, add this to the syncer mapping list to pull from remote".format(
                            name
                        ),
                        fg="red",
                    )
                )
            else:
                click.echo(click.style(str(error_msg), fg="red"))
            # sys.exit()
            return {
                "error": True,
                "response": "{} has not been found in the local repository, add this to the syncer mapping list to pull from remote".format(
                    name
                ),
            }
        elif "name" in response_dict.keys():
            print(response_dict)
            if tag == "all":
                # sys.exit()
                return {"error": True, "response": "tag cannot be all"}
            elif tag not in response_dict["tags"]:
                click.echo(
                    click.style(
                        "tag: "
                        + str(tag)
                        + " not found among tag list "
                        + str(response_dict["tags"]),
                        fg="red",
                    )
                )
                # sys.exit()
                return {
                    "error": True,
                    "response": "tag: "
                    + str(tag)
                    + " not found among tag list "
                    + str(response_dict["tags"]),
                }
            else:
                click.echo(
                    click.style(
                        "{}:{} have been found in local registry".format(name, tag),
                        fg="green",
                    )
                )
                return {
                    "error": False,
                    "response": "{}:{} have been found in local registry".format(
                        name, tag
                    ),
                }
        else:
            click.echo(click.style("Unknown error has occured", fg="red"))
            click.echo(click.style(str(response_dict["errors"][0]), fg="red"))
            # sys.exit()
            return {
                "error": True,
                "response": "unknown error has occured"
                + str(response_dict["errors"][0]),
            }

handleAsset(asset_type, asset_name, mode, config=None)

Handles a generic asset either an app or a device group and performs the functions of create, update or delete.

Parameters:

Name Type Description Default
asset_type string

Type of asset, legal values could be either an "app" or "device_group"

required

asset_name: string The name of the asset

mode : string The mode of handling, allowed values are "create", "update" and "delete"

config : dict The configuration of the asset to be created or updated. Not applicable in case of delete.

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 asset did not run successfully
If the key "error" is False it means that the handle asset did not run successfully
Source code in src/Composer.py
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
def handleAsset(self, asset_type, asset_name, mode, config=None):

    """
    Handles a generic asset either an app or a device group and performs the functions of create, update or delete.

    Parameters
    ----------

    asset_type : string
        Type of asset, legal values could be either an "app" or "device_group"

    asset_name: string
        The name of the asset

    mode : string
        The mode of handling, allowed values are "create", "update" and "delete"

    config : dict
        The configuration of the asset to be created or updated. Not applicable in case of delete.

    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 asset did not run successfully
    If the key "error" is False it means that the handle asset did not run successfully
    """

    # retval = False
    if config == None and (mode.lower() == "create" or mode.lower() == "update"):
        click.echo(
            click.style(
                "config is invlaid for " + asset_type + " : " + asset_name,
                fg="green",
            )
        )
        return {
            "error": True,
            "response": "config is invlaid for " + asset_type + " : " + asset_name,
        }

    if mode.lower() == "create":
        accept_code = 200
        if asset_type == "app":
            self.checkImageExists(config["docker_image"])
            reply = self.nebulaObj.create_app(asset_name, config)

        elif asset_type == "device_group":
            reply = self.nebulaObj.create_device_group(asset_name, config)

        else:
            click.echo(click.style("unknown asset type " + asset_type, fg="red"))
            return {"error": True, "response": "unknown asset type " + asset_type}
        moding = "creat"

    elif mode.lower() == "update":
        accept_code = 202
        if asset_type == "app":
            self.checkImageExists(config["docker_image"])
            reply = self.nebulaObj.update_app(asset_name, config)

        elif asset_type == "device_group":
            reply = self.nebulaObj.update_device_group(asset_name, config)

        else:
            click.echo(click.style("unknown asset type " + asset_type, fg="red"))
            return {"error": True, "response": "unknown asset type " + asset_type}
        moding = "updat"

    elif mode.lower() == "delete":
        accept_code = 200
        if asset_type == "app":
            reply = self.nebulaObj.delete_app(asset_name)

        elif asset_type == "device_group":
            reply = self.nebulaObj.delete_device_group(asset_name)

        else:
            click.echo(click.style("unknown asset type " + asset_type, fg="red"))
            return {"error": True, "response": "unknown asset type " + asset_type}
        moding = "delet"
    else:
        click.echo(click.style("unknown command " + mode, fg="red"))
        return {"error": True, "response": "unknown command " + mode}

    retval = self.printDiagnosticResponse(
        reply, accept_code, moding, asset_type, asset_name
    )

    return retval

handleDeviceGroup(app_list, mode, device_group='bca')

Handles aspects of the device group such as deleting, updating of apps

Parameters:

Name Type Description Default
app_list string

Comma separated list of strings to be updated or deleted from the device group

required

mode: string The mode i.e. update or delete

device_group : string The device group name to be handled

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 device group did not run successfully
If the key "error" is False it means that the handle device group did run successfully
Source code in src/Composer.py
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
def handleDeviceGroup(self, app_list, mode, device_group="bca"):
    """
    Handles aspects of the device group such as deleting, updating of apps

    Parameters
    ----------

    app_list : string
        Comma separated list of strings to be updated or deleted from the device group

    mode: string
        The mode i.e. update or delete

    device_group : string
        The device group name to be handled

    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 device group did not run successfully
    If the key "error" is False it means that the handle device group did run successfully
    """
    response = self.nebulaObj.list_device_group(device_group)
    success = self.printDiagnosticResponse(
        response, 200, "check", "app list for", device_group
    )
    if not success["error"]:
        new_app_list = app_list.split(",")
        existing_app_list = response["reply"]["apps"]
        apps_to_be_modified = existing_app_list
        if mode != "update" and mode != "delete":
            click.echo(click.style("unsupported mode " + mode, fg="red"))
            return {"error": True, "response": "unsupported mode"}
        for app in new_app_list:
            if mode == "update" and app not in existing_app_list:
                apps_to_be_modified = apps_to_be_modified + [app]
            if mode == "delete" and app in existing_app_list:
                apps_to_be_modified.remove(app)

        device_group_config = dict({"apps": apps_to_be_modified})
        print(device_group_config)
        self.handleAsset(
            "device_group", device_group, "update", device_group_config
        )
        response = self.nebulaObj.list_device_group(device_group)

        self.printDiagnosticResponse(
            response, 200, "check", "app list for", device_group
        )
        click.echo(response["reply"]["apps"])

        return {"error": False, "response": response["reply"]["apps"]}

prune_device_group_images(app)

Prunes the images on device group

Parameters:

Name Type Description Default
app

Name of the app whose images are to be pruned

required
Source code in src/Composer.py
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
def prune_device_group_images(self, app):
    """
    Prunes the images on device group

    Parameters
    ----------

    app: string
        Name of the app whose images are to be pruned

    """
    reply = self.nebulaObj.prune__device_group_images(app)
    if reply["status_code"] == 202:
        click.echo(
            click.style(
                "pruning images on devices running app: " + app, fg="yellow"
            )
        )
        return {
            "error": False,
            "response": "pruning images on devices running app: " + app,
        }
    else:
        click.echo(
            click.style(
                "error pruning images on devices running app:"
                + app
                + ", are you logged in? did you sent the right app name?",
                fg="red",
            )
        )
        return {
            "error": True,
            "response": "error pruning images on devices running app: " + app,
        }