Esempio n. 1
0
    def remove(self, name, with_delete=True, with_sync=True, with_triggers=True, recursive=False, logger=None):
        """
        Remove element named 'name' from the collection
        """
        name = name.lower()
        obj = self.find(name=name)

        if obj is not None:

            if with_delete:
                if with_triggers:
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/delete/system/pre/*", [], logger)
                if with_sync:
                    lite_sync = litesync.CobblerLiteSync(self.collection_mgr, logger=logger)
                    lite_sync.remove_single_system(name)
            self.lock.acquire()
            try:
                del self.listing[name]
            finally:
                self.lock.release()
            self.collection_mgr.serialize_delete(self, obj)
            if with_delete:
                if with_triggers:
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/delete/system/post/*", [], logger)
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/change/*", [], logger)

            return

        raise CX(_("cannot delete an object that does not exist: %s") % name)
Esempio n. 2
0
    def remove(self,
               name,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection
        """
        name = name.lower()
        if not recursive:
            for v in self.collection_mgr.systems():
                if v.profile is not None and v.profile.lower() == name:
                    raise CX("removal would orphan system: %s" % v.name)

        obj = self.find(name=name)
        if obj is not None:
            if recursive:
                kids = obj.get_children()
                for k in kids:
                    if k.COLLECTION_TYPE == "profile":
                        self.collection_mgr.api.remove_profile(
                            k.name,
                            recursive=recursive,
                            delete=with_delete,
                            with_triggers=with_triggers)
                    else:
                        self.collection_mgr.api.remove_system(
                            k.name,
                            recursive=recursive,
                            delete=with_delete,
                            with_triggers=with_triggers)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/profile/pre/*", [])
            self.lock.acquire()
            try:
                del self.listing[name]
            finally:
                self.lock.release()
            self.collection_mgr.serialize_delete(self, obj)
            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/profile/post/*", [])
                    utils.run_triggers(self.collection_mgr.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [])
                if with_sync:
                    lite_sync = litesync.CobblerLiteSync(self.collection_mgr)
                    lite_sync.remove_single_profile(name)
            return

        raise CX("cannot delete an object that does not exist: %s" % name)
Esempio n. 3
0
    def remove(self,
               name,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = True):
        """
        Remove element named 'name' from the collection
        """

        # NOTE: with_delete isn't currently meaningful for repos
        # but is left in for consistancy in the API.  Unused.

        name = name.lower()

        # first see if any Groups use this distro
        if not recursive:
            for v in self.collection_mgr.systems():
                if v.image is not None and v.image.lower() == name:
                    raise CX("removal would orphan system: %s" % v.name)

        obj = self.find(name=name)

        if obj is not None:

            if recursive:
                kids = obj.get_children()
                for k in kids:
                    self.collection_mgr.api.remove_system(k, recursive=True)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/image/pre/*", [])
                if with_sync:
                    lite_sync = litesync.CobblerLiteSync(self.collection_mgr)
                    lite_sync.remove_single_image(name)

            self.lock.acquire()
            try:
                del self.listing[name]
            finally:
                self.lock.release()
            self.collection_mgr.serialize_delete(self, obj)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/image/post/*", [])
                    utils.run_triggers(self.collection_mgr.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [])

            return

        raise CX("cannot delete an object that does not exist: %s" % name)
Esempio n. 4
0
    def remove(self,
               name,
               with_delete=True,
               with_sync=True,
               with_triggers=True,
               recursive=False,
               logger=None):
        """
        Remove element named 'name' from the collection
        """
        name = name.lower()
        if not recursive:
            for system in self.collection_mgr.systems():
                if system.profile is not None and system.profile.lower(
                ) == name:
                    raise CX("removal would orphan system: %s" % system.name)

        obj = self.find(name=name)
        if obj is not None:
            if recursive:
                kids = obj.get_children()
                for k in kids:
                    k.parent_menus.pop(name)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/menu/pre/*", [],
                        logger)
            self.lock.acquire()
            try:
                del self.listing[name]
            finally:
                self.lock.release()
            self.collection_mgr.serialize_delete(self, obj)
            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/menu/post/*", [],
                        logger)
                    utils.run_triggers(self.collection_mgr.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [], logger)
                if with_sync:
                    lite_sync = litesync.CobblerLiteSync(self.collection_mgr,
                                                         logger=logger)
                    lite_sync.remove_single_menu()
            return

        raise CX("cannot delete an object that does not exist: %s" % name)
Esempio n. 5
0
    def remove(self,
               name,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection
        """
        name = name.lower()

        # first see if any Groups use this distro
        if not recursive:
            for v in self.collection_mgr.profiles():
                if v.distro and v.distro.lower() == name:
                    raise CX("removal would orphan profile: %s" % v.name)

        obj = self.find(name=name)

        if obj is not None:
            kernel = obj.kernel
            if recursive:
                kids = obj.get_children()
                for k in kids:
                    self.collection_mgr.api.remove_profile(
                        k.name,
                        recursive=recursive,
                        delete=with_delete,
                        with_triggers=with_triggers)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/distro/pre/*", [])
                if with_sync:
                    lite_sync = litesync.CobblerLiteSync(self.collection_mgr)
                    lite_sync.remove_single_distro(name)
            self.lock.acquire()
            try:
                del self.listing[name]
            finally:
                self.lock.release()

            self.collection_mgr.serialize_delete(self, obj)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.collection_mgr.api, obj,
                        "/var/lib/cobbler/triggers/delete/distro/post/*", [])
                    utils.run_triggers(self.collection_mgr.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [])

            # look through all mirrored directories and find if any directory is holding
            # this particular distribution's kernel and initrd
            settings = self.collection_mgr.settings()
            possible_storage = glob.glob(settings.webdir + "/distro_mirror/*")
            path = None
            for storage in possible_storage:
                if os.path.dirname(obj.kernel).find(storage) != -1:
                    path = storage
                    continue

            # if we found a mirrored path above, we can delete the mirrored storage /if/
            # no other object is using the same mirrored storage.
            if with_delete and path is not None and os.path.exists(
                    path) and kernel.find(settings.webdir) != -1:
                # this distro was originally imported so we know we can clean up the associated
                # storage as long as nothing else is also using this storage.
                found = False
                distros = self.api.distros()
                for d in distros:
                    if d.kernel.find(path) != -1:
                        found = True
                if not found:
                    utils.rmtree(path)
Esempio n. 6
0
    def add(self, ref, save: bool = False, with_copy: bool = False, with_triggers: bool = True, with_sync: bool = True,
            quick_pxe_update: bool = False, check_for_duplicate_names: bool = False,
            check_for_duplicate_netinfo: bool = False, logger=None):
        """
        Add an object to the collection

        :param ref: The reference to the object.
        :param save: If this is true then the objet is persited on the disk.
        :param with_copy: Is a bit of a misnomer, but lots of internal add operations can run with "with_copy" as False.
                          True means a real final commit, as if entered from the command line (or basically, by a user).
                          With with_copy as False, the particular add call might just be being run during
                          deserialization, in which case extra semantics around the add don't really apply. So, in that
                          case, don't run any triggers and don't deal with any actual files.
        :param with_sync: If a sync should be triggered when the object is renamed.
        :param with_triggers: If triggers should be run when the object is renamed.
        :param quick_pxe_update: This decides if there should be run a quick or full update after the add was done.
        :param check_for_duplicate_names: If the name of an object should be unique or not.
        :type check_for_duplicate_names: bool
        :param check_for_duplicate_netinfo: This checks for duplicate network information. This only has an effect on
                                            systems.
        :type check_for_duplicate_netinfo: bool
        :param logger: The logger to audit the action with.
        """
        item_base.Item.remove_from_cache(ref)
        if ref is None:
            raise CX("Unable to add a None object")
        if ref.name is None:
            raise CX("Unable to add an object without a name")

        ref.check_if_valid()

        if ref.uid == '':
            ref.uid = self.collection_mgr.generate_uid()

        if save is True:
            now = time.time()
            if ref.ctime == 0:
                ref.ctime = now
            ref.mtime = now

        if self.lite_sync is None:
            self.lite_sync = litesync.CobblerLiteSync(self.collection_mgr, logger=logger)

        # migration path for old API parameter that I've renamed.
        if with_copy and not save:
            save = with_copy

        if not save:
            # For people that aren't quite aware of the API if not saving the object, you can't run these features.
            with_triggers = False
            with_sync = False

        # Avoid adding objects to the collection if an object of the same/ip/mac already exists.
        self.__duplication_checks(ref, check_for_duplicate_names, check_for_duplicate_netinfo)

        if ref.COLLECTION_TYPE != self.collection_type():
            raise CX("API error: storing wrong data type in collection")

        # failure of a pre trigger will prevent the object from being added
        if save and with_triggers:
            utils.run_triggers(self.api, ref, "/var/lib/cobbler/triggers/add/%s/pre/*" % self.collection_type())

        self.lock.acquire()
        try:
            self.listing[ref.name.lower()] = ref
        finally:
            self.lock.release()

        # perform filesystem operations
        if save:
            # Save just this item if possible, if not, save the whole collection
            self.collection_mgr.serialize_item(self, ref)

            if with_sync:
                if isinstance(ref, system.System):
                    # we don't need openvz containers to be network bootable
                    if ref.virt_type == "openvz":
                        ref.netboot_enabled = False
                    self.lite_sync.add_single_system(ref.name)
                elif isinstance(ref, profile.Profile):
                    # we don't need openvz containers to be network bootable
                    if ref.virt_type == "openvz":
                        ref.enable_menu = False
                    self.lite_sync.add_single_profile(ref.name)
                elif isinstance(ref, distro.Distro):
                    self.lite_sync.add_single_distro(ref.name)
                elif isinstance(ref, image.Image):
                    self.lite_sync.add_single_image(ref.name)
                elif isinstance(ref, repo.Repo):
                    pass
                elif isinstance(ref, mgmtclass.Mgmtclass):
                    pass
                elif isinstance(ref, package.Package):
                    pass
                elif isinstance(ref, file.File):
                    pass
                else:
                    print("Internal error. Object type not recognized: %s" % type(ref))
            if not with_sync and quick_pxe_update:
                if isinstance(ref, system.System):
                    self.lite_sync.update_system_netboot_status(ref.name)

            # save the tree, so if neccessary, scripts can examine it.
            if with_triggers:
                utils.run_triggers(self.api, ref, "/var/lib/cobbler/triggers/change/*", [], logger)
                utils.run_triggers(self.api, ref, "/var/lib/cobbler/triggers/add/%s/post/*" % self.collection_type(), [], logger)

        # update children cache in parent object
        parent = ref.get_parent()
        if parent is not None:
            parent.children[ref.name] = ref