Exemple #1
0
def delete_object(request, del_obj, **kwargs):
    num_ref = get_related_models(del_obj)
    if num_ref:
        request.xml_response.error(
            "cannot delete {} '{}': {}".format(
                del_obj._meta.object_name, str(del_obj),
                logging_tools.get_plural("reference", num_ref)), logger)
    else:
        del_obj.delete()
        if kwargs.get("xml_log", True):
            request.xml_response.info(
                "deleted {}".format(del_obj._meta.object_name), logger)
        else:
            logger.info("deleted {}".format(del_obj._meta.object_name))
Exemple #2
0
 def post(self, request):
     _post = request.POST
     part_dev = device.objects.get(Q(pk=_post["pk"]))
     logger.info("clearing partition info from {}".format(str(part_dev)))
     _part = part_dev.act_partition_table
     if _part is None:
         request.xml_response.error(
             "no partition table defined for {}".format(str(part_dev)))
     else:
         part_dev.act_partition_table = None
         part_dev.save(update_fields=["act_partition_table"])
         if not _part.user_created and not get_related_models(_part):
             request.xml_response.warn(
                 "partition table {} removed".format(_part))
             _part.delete()
Exemple #3
0
 def post(self, request):
     _post = request.POST
     try:
         cur_p = package.objects.get(Q(pk=_post["pk"]))
     except package.DoesNotExist:
         request.xml_response.error("package not found", logger)
     else:
         num_ref = get_related_models(cur_p)
         if num_ref:
             request.xml_response.error(
                 "cannot remove: {}".format(
                     logging_tools.get_plural("reference", num_ref)),
                 logger)
         else:
             cur_p.delete()
             request.xml_response.info("removed package", logger)
Exemple #4
0
    def post(self, request):
        _stream_data = json.loads(request.POST["stream_data"])
        _mode = request.POST["mode"]
        import pprint
        # pprint.pprint(_stream_data)
        logger.info("handling '{}' config stream with {}".format(
            _mode,
            logging_tools.get_plural("entry", len(_stream_data)),
        ))
        # config pks changed
        config_pks = set()
        # mon check command pks changes
        mc_pks = set()
        # _stream_data = []
        for _data in _stream_data:
            # iterate over stream
            dev_pk = _data["dev_pk"]
            meta_pk = _data["meta_pk"]
            conf_pk = _data["conf_pk"]
            mc_pk = _data["element_pk"]
            # mode == mon is only valid for non-config related calls
            device_obj = device.objects.get(Q(pk=dev_pk))
            if dev_pk == meta_pk:
                meta_obj = device_obj
                is_meta = True
            else:
                meta_obj = device.objects.get(Q(pk=meta_pk))
                is_meta = False
            devs_in_group = meta_obj.device_group.device_group.all().count()
            if _mode != "mon":
                # config handling (general, services)
                config_pks.add(conf_pk)
                # checked = bool(int(_post["value"]))
                config_obj = config.objects.get(Q(pk=conf_pk))
                try:
                    local_dc = device_config.objects.get(
                        Q(config=config_obj) & Q(device=device_obj))
                except device_config.DoesNotExist:
                    local_dc = None
                    local_checked = False
                except device_config.MultipleObjectsReturned:
                    # hm, fix internal structure error
                    all_local_dcs = device_config.objects.filter(
                        Q(config=config_obj) & Q(device=device_obj))
                    local_dc = all_local_dcs.pop(0)
                    all_local_dcs.delete()
                    local_checked = True
                else:
                    local_checked = True
                if is_meta:
                    meta_checked = local_checked
                    meta_dc = local_dc
                else:
                    try:
                        meta_dc = device_config.objects.get(
                            Q(config=config_obj) & Q(device=meta_obj))
                    except device_config.DoesNotExist:
                        meta_dc = None
                        meta_checked = False
                    else:
                        meta_checked = True
                logger.info(
                    "device {}{} / config {}: {} / {}, {} in device_group".
                    format(
                        str(device_obj),
                        " [MD]" if is_meta else "",
                        str(config_obj),
                        "local set" if local_checked else "local unset",
                        "meta set" if meta_checked else "meta unset",
                        logging_tools.get_plural("device", devs_in_group),
                    ))
                if is_meta:
                    # local and meta are ident
                    if local_checked:
                        # check if we can safely remove the config
                        delete_object(request, meta_dc, xml_log=False)
                        request.xml_response.info(
                            "removed meta config {}".format(str(config_obj)),
                            logger)
                    else:
                        # device configs set for devices in device_group
                        to_remove = device_config.objects.filter(
                            Q(config=config_obj)
                            & Q(device__device_group=meta_obj.device_group))
                        # check if we can safely set the meta device_config
                        set_meta = True
                        if len(to_remove):
                            if any([
                                    True for del_obj in to_remove
                                    if get_related_models(del_obj)
                            ]):
                                # some of these are undeletable, stop
                                request.xml_response.error(
                                    "device configs are in use (hence protected)",
                                    logger)
                                set_meta = False
                            else:
                                _to_del = len(to_remove)
                                to_remove.delete()
                                request.xml_response.info(
                                    "removed config {} from {}".format(
                                        str(config_obj),
                                        logging_tools.get_plural(
                                            "device", _to_del),
                                    ), logger)
                        if set_meta:
                            # set meta config
                            _meta_dc = device_config(device=device_obj,
                                                     config=config_obj).save()
                            request.xml_response.info(
                                "added meta config {}".format(str(config_obj)),
                                logger)
                else:
                    # handling of actions for non-meta devices
                    if not local_checked and not meta_checked:
                        # simple local config set
                        local_dc = device_config(device=device_obj,
                                                 config=config_obj).save()
                        request.xml_response.info(
                            "added config {}".format(str(config_obj)), logger)
                    elif local_checked:
                        # delete local config
                        # check if we can safely remove the config
                        # if _mode == "mon":
                        #    mc_pks.add(mc_obj.idx)
                        #    toggle_exclude_mon_device(request, mc_obj, device_obj)
                        # else:
                        delete_object(request, local_dc, xml_log=False)
                        request.xml_response.info(
                            "removed config {}".format(str(config_obj)),
                            logger)
                    elif meta_checked and not local_checked:
                        # create local config, remove meta config, set all other device configs
                        # if _mode == "mon":
                        #     mc_pks.add(mc_obj.idx)
                        #     toggle_exclude_mon_device(request, mc_obj, device_obj)
                        # else:
                        # check if we can safely delete the config from the meta
                        if get_related_models(meta_dc):
                            # no, stop
                            request.xml_response.error(
                                "meta config {} is in use".format(
                                    str(config_obj)), logger)
                        else:
                            delete_object(request, meta_dc, xml_log=False)
                            for set_dev in meta_obj.device_group.device_group.all(
                            ).exclude(Q(pk__in=[meta_obj.pk, device_obj.pk])):
                                device_config(
                                    device=set_dev,
                                    config=config_obj,
                                ).save()
                            request.xml_response.warn(
                                "removed meta config {} and added {}".format(
                                    str(config_obj),
                                    logging_tools.get_plural(
                                        "device", devs_in_group - 1)), logger)
                    else:
                        # meta and local checked, should never happen ...
                        request.xml_response.warn(
                            "both local and meta set, removing local config",
                            logger)
                        local_dc.remove()
            else:
                # mon mode, only modify device relationships from mon_check_command
                mc_obj = mon_check_command.objects.get(Q(pk=mc_pk))
                # moncheck_command handling
                mc_sel = mc_obj.devices.all().values_list("idx", flat=True)
                mc_pks.add(mc_obj.idx)
                local_checked = device_obj.idx in mc_sel
                meta_checked = meta_obj.idx in mc_sel
                logger.info(
                    "device {}{} / MonCheck {}: {} / {}, {} in device_group".
                    format(
                        str(device_obj),
                        " [MD]" if is_meta else "",
                        str(mc_obj),
                        "local set" if local_checked else "local unset",
                        "meta set" if meta_checked else "meta unset",
                        logging_tools.get_plural("device", devs_in_group),
                    ))
                if is_meta:
                    if local_checked:
                        # check if we can safely remove the config
                        mc_obj.devices.remove(meta_obj)
                        request.xml_response.info(
                            "removed meta MonCheck {}".format(str(mc_obj)),
                            logger)
                    else:
                        # set moncheck to meta
                        mc_obj.devices.remove(*device.objects.filter(
                            Q(device_group=meta_obj.device_group)))
                        mc_obj.devices.add(meta_obj)
                        request.xml_response.info(
                            "added meta MonCheck {}".format(str(mc_obj)),
                            logger)
                else:
                    if not local_checked and not meta_checked:
                        # add m2m
                        mc_obj.devices.add(device_obj)
                        request.xml_response.info(
                            "added MonCheck {}".format(str(mc_obj)), logger)
                    elif local_checked:
                        # delete m2m
                        mc_obj.devices.remove(device_obj)
                        request.xml_response.info(
                            "removed MonCheck {}".format(str(mc_obj)), logger)
                    elif meta_checked and not local_checked:
                        mc_obj.devices.remove(meta_obj)
                        for set_dev in meta_obj.device_group.device_group.all(
                        ).exclude(Q(pk__in=[meta_obj.pk, device_obj.pk])):
                            mc_obj.devices.add(set_dev)
                        request.xml_response.warn(
                            "removed meta MonCheck {} and added {}".format(
                                str(mc_obj),
                                logging_tools.get_plural(
                                    "device", devs_in_group - 1)), logger)
                    else:
                        # meta and local checked, should never happen ...
                        request.xml_response.warn(
                            "both local and meta MonCheck set, removing local MonCheck",
                            logger)
                        mc_obj.devices.remove(device_obj)

        # check exclusion lists
        changeset = E.changeset()

        # excluded monchecks

        # for mc in mon_check_command.objects.filter(
        #    Q(config_rel__in=config_pks) | Q(idx__in=mc_pks)
        # ).prefetch_related("exclude_devices"):
        #    changeset.append(
        #        E.mon_check_command_excl(
        #            *[
        #                E.exclude(
        #                    dev_idx="{:d}".format(dev_idx)
        #                ) for dev_idx in mc.exclude_devices.all().values_list("idx", flat=True)
        #            ],
        #            pk="{:d}".format(mc.idx)
        #        )
        #    )

        # moncheck devices

        for mc in mon_check_command.objects.filter(
                Q(idx__in=mc_pks)).prefetch_related("devices"):
            changeset.append(
                E.mon_check_command(*[
                    E.device(dev_idx="{:d}".format(dev_idx))
                    for dev_idx in mc.devices.all().values_list("idx",
                                                                flat=True)
                ],
                                    pk="{:d}".format(mc.idx)))

        # selected configs

        for pk in config_pks:
            changeset.append(
                E.config(*[
                    E.entry(**{k: str(v)
                               for k, v in result.items()})
                    for result in device_config.objects.filter(Q(
                        config=pk)).values("device", "config", "pk", "date")
                ],
                         pk="{:d}".format(pk)))
        # print(etree.tostring(changeset, pretty_print=True, encoding="unicode"))
        request.xml_response["response"] = changeset
Exemple #5
0
 def post(self, request):
     _post = request.POST
     # checked = bool(int(_post["value"]))
     dev_pk = int(_post["dev_pk"])
     meta_pk = int(_post["meta_pk"])
     conf_pk = int(_post["conf_pk"])
     config_obj = config.objects.get(Q(pk=conf_pk))
     device_obj = device.objects.get(Q(pk=dev_pk))
     if dev_pk == meta_pk:
         meta_obj = device_obj
         is_meta = True
     else:
         meta_obj = device.objects.get(Q(pk=meta_pk))
         is_meta = False
     devs_in_group = meta_obj.device_group.device_group.all().count()
     try:
         local_dc = device_config.objects.get(
             Q(config=config_obj) & Q(device=device_obj))
     except device_config.DoesNotExist:
         local_dc = None
         local_checked = False
     except device_config.MultipleObjectsReturned:
         # hm, fix internal structure error
         all_local_dcs = device_config.objects.filter(
             Q(config=config_obj) & Q(device=device_obj))
         local_dc = all_local_dcs.pop(0)
         all_local_dcs.delete()
         local_checked = True
     else:
         local_checked = True
     if is_meta:
         meta_checked = local_checked
         meta_dc = local_dc
     else:
         try:
             meta_dc = device_config.objects.get(
                 Q(config=config_obj) & Q(device=meta_obj))
         except device_config.DoesNotExist:
             meta_dc = None
             meta_checked = False
         else:
             meta_checked = True
     logger.info(
         u"device {}{} / config {}: {} / {}, {} in device_group".format(
             unicode(device_obj),
             " [MD]" if is_meta else "",
             unicode(config_obj),
             "local set" if local_checked else "local unset",
             "meta set" if local_checked else "meta unset",
             logging_tools.get_plural("device", devs_in_group),
         ))
     if is_meta:
         # local and meta are ident
         if local_checked:
             # check if we can safely remove the config
             delete_object(request, meta_dc, xml_log=False)
             request.xml_response.info(
                 "removed meta config {}".format(unicode(config_obj)),
                 logger)
         else:
             to_remove = device_config.objects.filter(
                 Q(config=config_obj)
                 & Q(device__device_group=meta_obj.device_group))
             # check if we can safely set the meta device_config
             set_meta = True
             if len(to_remove):
                 if any([
                         True for del_obj in to_remove
                         if get_related_models(del_obj)
                 ]):
                     request.xml_response.error(
                         "device configs are in use (hence protected)",
                         logger)
                     set_meta = False
                 else:
                     _to_del = len(to_remove)
                     to_remove.delete()
                     request.xml_response.info(
                         "removed config {} from {}".format(
                             unicode(config_obj),
                             logging_tools.get_plural("device", _to_del),
                         ), logger)
             if set_meta:
                 meta_dc = device_config(device=device_obj,
                                         config=config_obj).save()
                 request.xml_response.info(
                     "added meta config {}".format(unicode(config_obj)),
                     logger)
     else:
         # handling of actions for non-meta devices
         if not local_checked and not meta_checked:
             local_dc = device_config(device=device_obj,
                                      config=config_obj).save()
             request.xml_response.info(
                 "added config {}".format(unicode(config_obj)), logger)
         elif local_checked:
             # delete local config
             # check if we can safely remove the config
             delete_object(request, local_dc, xml_log=False)
             request.xml_response.info(
                 "removed config {}".format(unicode(config_obj)), logger)
         elif meta_checked and not local_checked:
             # create local config, remove meta config, set all other device configs
             if get_related_models(meta_dc):
                 request.xml_response.error(
                     "meta config {} is in use".format(unicode(config_obj)),
                     logger)
             else:
                 delete_object(request, meta_dc, xml_log=False)
                 for set_dev in meta_obj.device_group.device_group.all(
                 ).exclude(Q(pk__in=[meta_obj.pk, device_obj.pk])):
                     print "***", set_dev
                     device_config(
                         device=set_dev,
                         config=config_obj,
                     ).save()
                 request.xml_response.warn(
                     "removed meta config {} and added {}".format(
                         unicode(config_obj),
                         logging_tools.get_plural("device",
                                                  devs_in_group - 1)),
                     logger)
         else:
             # meta and local checked, should never happen ...
             request.xml_response.warn(
                 "both local and meta set, removing local config", logger)
             local_dc.remove()
     request.xml_response["response"] = E.config(*[
         E.device(**{k: str(v)
                     for k, v in result.iteritems()})
         for result in device_config.objects.filter(Q(
             config=config_obj.pk)).values("device", "config", "pk", "date")
     ])