Beispiel #1
0
def test_run_triggers():
    # Arrange
    api = CobblerAPI()
    globber = ""

    # Act
    utils.run_triggers(api, None, globber)

    # Assert
    # TODO: How the heck do we check that this actually did what it is supposed to do?
    assert False
Beispiel #2
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
        """
        # NOTE: with_delete isn't currently meaningful for repos
        # but is left in for consistancy in the API.  Unused.
        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/repo/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/repo/post/*", [], logger)
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/change/*", [], logger)

                # FIXME: better use config.settings() webdir?
                path = "/var/www/cobbler/repo_mirror/%s" % obj.name
                if os.path.exists("/srv/www/"):
                    path = "/srv/www/cobbler/repo_mirror/%s" % obj.name
                if os.path.exists(path):
                    utils.rmtree(path)

            return

        raise CX(_("cannot delete an object that does not exist: %s") % name)
Beispiel #3
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/file/*", [], 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/file/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)
Beispiel #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()
        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/package/*", [], 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/package/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)
Beispiel #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

        :raises CX: In case the object does not exist.
        """
        # NOTE: with_delete isn't currently meaningful for repos
        # but is left in for consistancy in the API.  Unused.
        name = name.lower()
        obj = self.find(name=name)
        if obj is None:
            raise CX("cannot delete an object that does not exist: %s" % name)

        if with_delete:
            if with_triggers:
                utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/delete/repo/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.api, obj, "/var/lib/cobbler/triggers/delete/repo/post/*", [])
                utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/change/*", [])

            path = os.path.join(self.api.settings().webdir, "repo_mirror", obj.name)
            if os.path.exists(path):
                utils.rmtree(path)
Beispiel #6
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
        """
        # NOTE: with_delete isn't currently meaningful for repos
        # but is left in for consistancy in the API.  Unused.
        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/repo/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/repo/post/*", [], logger)
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/change/*", [], logger)

                # FIXME: better use config.settings() webdir?
                path = "/var/www/cobbler/repo_mirror/%s" % obj.name
                if os.path.exists("/srv/www/"):
                    path = "/srv/www/cobbler/repo_mirror/%s" % obj.name
                if os.path.exists(path):
                    utils.rmtree(path)

            return

        raise CX(_("cannot delete an object that does not exist: %s") % name)
Beispiel #7
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

        :raises CX
        """

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

        name = name.lower()

        # first see if any Groups use this distro
        if not recursive:
            for v in self.api.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.api.remove_system(k, recursive=True)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.api, obj,
                        "/var/lib/cobbler/triggers/delete/image/pre/*", [])
                if with_sync:
                    lite_sync = self.api.get_sync()
                    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.api, obj,
                        "/var/lib/cobbler/triggers/delete/image/post/*", [])
                    utils.run_triggers(self.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [])

            return

        raise CX("cannot delete an object that does not exist: %s" % name)
Beispiel #8
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)
Beispiel #9
0
    def remove(self,
               name: str,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection

        :param name: The name of the menu
        :param with_delete: In case the deletion triggers are executed for this menu.
        :param with_sync: In case a Cobbler Sync should be executed after the action.
        :param with_triggers: In case the Cobbler Trigger mechanism should be executed.
        :param recursive: In case you want to delete all objects this menu references.
        :raises CX: Raised in case you want to delete a none existing menu.
        """
        name = name.lower()
        obj = self.find(name=name)
        if obj is None:
            raise CX("cannot delete an object that does not exist: %s" % name)

        for profile in self.api.profiles():
            if profile.menu and profile.menu.lower() == name:
                profile.menu = ""
        for image in self.api.images():
            if image.menu and image.menu.lower() == name:
                image.menu = ""

        if recursive:
            kids = obj.get_children()
            for kid in kids:
                self.remove(kid,
                            with_delete=with_delete,
                            with_sync=False,
                            recursive=recursive)

        if with_delete:
            if with_triggers:
                utils.run_triggers(
                    self.api, obj,
                    "/var/lib/cobbler/triggers/delete/menu/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.api, obj,
                    "/var/lib/cobbler/triggers/delete/menu/post/*", [])
                utils.run_triggers(self.api, obj,
                                   "/var/lib/cobbler/triggers/change/*", [])
            if with_sync:
                lite_sync = self.api.get_sync()
                lite_sync.remove_single_menu()
Beispiel #10
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)
Beispiel #11
0
    def remove(self, name, with_delete=True, with_sync=True, with_triggers=True, recursive=True, logger=None):
        """
        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.config.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.config.api.remove_system(k, recursive=True, logger=logger)

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

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

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

            return True

        raise CX(_("cannot delete an object that does not exist: %s") % name)
Beispiel #12
0
    def remove(self, name: str, with_delete: bool = True, with_sync: bool = True, with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection

        :raises CX: In case the name of the object was not given or any other descendant would be orphaned.
        """
        name = name.lower()
        if not recursive:
            for v in self.api.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 None:
            raise CX("cannot delete an object that does not exist: %s" % name)

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

        if with_delete:
            if with_triggers:
                utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/delete/profile/pre/*", [])

        if obj.parent is not None and obj.name in obj.parent.children:
            obj.parent.children.remove(obj.name)
            # ToDo: Only serialize parent object, use:
            #       Use self.collection_mgr.serialize_one_item(obj.parent)
            self.api.serialize()

        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.api, obj, "/var/lib/cobbler/triggers/delete/profile/post/*", [])
                utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/change/*", [])
            if with_sync:
                lite_sync = self.api.get_sync()
                lite_sync.remove_single_profile(name)
Beispiel #13
0
    def remove(self, name: str, with_delete: bool = True, with_sync: bool = True, with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection

        :param name: The name of the menu
        :param with_delete: TODO
        :param with_sync: TODO
        :param with_triggers: TODO
        :param recursive: TODO
        :raises CX
        """
        name = name.lower()
        for profile in self.api.profiles():
            if profile.menu and profile.menu.lower() == name:
                profile.menu = ""
        for image in self.api.images():
            if image.menu and image.menu.lower() == name:
                image.menu = ""

        obj = self.find(name=name)
        if obj is not None:
            if recursive:
                kids = obj.get_children()
                for kid in kids:
                    self.remove(kid, with_delete=with_delete, with_sync=False, recursive=recursive)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/delete/menu/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.api, obj, "/var/lib/cobbler/triggers/delete/menu/post/*", [])
                    utils.run_triggers(self.api, obj, "/var/lib/cobbler/triggers/change/*", [])
                if with_sync:
                    lite_sync = self.api.get_sync()
                    lite_sync.remove_single_menu()
            return
        raise CX("cannot delete an object that does not exist: %s" % name)
Beispiel #14
0
    def remove(self,
               name,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = 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)
Beispiel #15
0
    def remove(self,
               name: str,
               with_delete: bool = True,
               with_sync: bool = True,
               with_triggers: bool = True,
               recursive: bool = False):
        """
        Remove element named 'name' from the collection

        :raises CX: In case the name of the object was not given.
        """
        name = name.lower()
        obj = self.find(name=name)

        if obj is None:
            raise CX("cannot delete an object that does not exist: %s" % name)

        if with_delete:
            if with_triggers:
                utils.run_triggers(
                    self.api, obj,
                    "/var/lib/cobbler/triggers/delete/system/pre/*", [])
            if with_sync:
                lite_sync = self.api.get_sync()
                lite_sync.remove_single_system(name)

        if obj.parent is not None and obj.name in obj.parent.children:
            obj.parent.children.remove(obj.name)
            self.api.serialize()

        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.api, obj,
                    "/var/lib/cobbler/triggers/delete/system/post/*", [])
                utils.run_triggers(self.api, obj,
                                   "/var/lib/cobbler/triggers/change/*", [])
Beispiel #16
0
    def run_sync_systems(self, systems: List[str]):
        """
        Syncs the specific systems with the config tree.
        """
        if not os.path.exists(self.bootloc):
            utils.die("cannot find directory: %s" % self.bootloc)

        self.logger.info("running pre-sync triggers")

        # run pre-triggers...
        utils.run_triggers(self.api, None,
                           "/var/lib/cobbler/triggers/sync/pre/*")

        self.distros = self.collection_mgr.distros()
        self.profiles = self.collection_mgr.profiles()
        self.systems = self.collection_mgr.systems()
        self.settings = self.collection_mgr.settings()
        self.repos = self.collection_mgr.repos()

        # Have the tftpd module handle copying bootloaders, distros, images, and all_system_files
        self.tftpd.sync_systems(systems)

        if self.settings.manage_dhcp:
            self.write_dhcp()
        if self.settings.manage_dns:
            self.logger.info("rendering DNS files")
            self.dns.regen_hosts()
            self.dns.write_dns_files()

        self.logger.info("cleaning link caches")
        self.clean_link_cache()

        if self.settings.manage_rsync:
            self.logger.info("rendering rsync files")
            self.rsync_gen()

        # run post-triggers
        self.logger.info("running post-sync triggers")
        utils.run_triggers(self.api, None,
                           "/var/lib/cobbler/triggers/sync/post/*")
        utils.run_triggers(self.api, None,
                           "/var/lib/cobbler/triggers/change/*")
Beispiel #17
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

        :raises CX
        """

        name = name.lower()
        obj = self.find(name=name)
        if obj is not None:
            if with_delete:
                if with_triggers:
                    utils.run_triggers(
                        self.api, obj,
                        "/var/lib/cobbler/triggers/delete/mgmtclass/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.api, obj,
                        "/var/lib/cobbler/triggers/delete/mgmtclass/post/*",
                        [])
                    utils.run_triggers(self.api, obj,
                                       "/var/lib/cobbler/triggers/change/*",
                                       [])

            return

        raise CX("cannot delete an object that does not exist: %s" % name)
    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 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, logger=logger)
                    else:
                        self.collection_mgr.api.remove_system(k.name, recursive=recursive, delete=with_delete, with_triggers=with_triggers, logger=logger)

            if with_delete:
                if with_triggers:
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/delete/profile/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/profile/post/*", [], logger)
                    utils.run_triggers(self.collection_mgr.api, obj, "/var/lib/cobbler/triggers/change/*", [], logger)
                if with_sync:
                    lite_sync = action_litesync.CobblerLiteSync(self.collection_mgr, logger=logger)
                    lite_sync.remove_single_profile(name)
            return

        raise CX(_("cannot delete an object that does not exist: %s") % name)
Beispiel #19
0
    def run(self):
        """
        Syncs the current configuration file with the config tree.
        Using the ``Check().run_`` functions previously is recommended
        """
        if not os.path.exists(self.bootloc):
            utils.die(self.logger, "cannot find directory: %s" % self.bootloc)

        self.logger.info("running pre-sync triggers")

        # run pre-triggers...
        utils.run_triggers(self.api, None,
                           "/var/lib/cobbler/triggers/sync/pre/*")

        self.distros = self.collection_mgr.distros()
        self.profiles = self.collection_mgr.profiles()
        self.systems = self.collection_mgr.systems()
        self.settings = self.collection_mgr.settings()
        self.repos = self.collection_mgr.repos()

        # execute the core of the sync operation
        self.logger.info("cleaning trees")
        self.clean_trees()

        # Have the tftpd module handle copying bootloaders, distros, images, and all_system_files
        self.tftpd.sync(self.verbose)
        # Copy distros to the webdir
        # Adding in the exception handling to not blow up if files have been moved (or the path references an NFS
        # directory that's no longer mounted)
        for d in self.distros:
            try:
                self.logger.info("copying files for distro: %s" % d.name)
                self.tftpgen.copy_single_distro_files(d, self.settings.webdir,
                                                      True)
                self.tftpgen.write_templates(d, write_file=True)
            except CX as e:
                self.logger.error(e.value)

        # make the default pxe menu anyway...
        self.tftpgen.make_pxe_menu()

        if self.settings.manage_dhcp:
            self.write_dhcp()
        if self.settings.manage_dns:
            self.logger.info("rendering DNS files")
            self.dns.regen_hosts()
            self.dns.write_dns_files()

        if self.settings.manage_tftpd:
            # copy in boot_files
            self.tftpd.write_boot_files()

        self.logger.info("cleaning link caches")
        self.clean_link_cache()

        if self.settings.manage_rsync:
            self.logger.info("rendering Rsync files")
            self.rsync_gen()

        # run post-triggers
        self.logger.info("running post-sync triggers")
        utils.run_triggers(self.api,
                           None,
                           "/var/lib/cobbler/triggers/sync/post/*",
                           logger=self.logger)
        utils.run_triggers(self.api,
                           None,
                           "/var/lib/cobbler/triggers/change/*",
                           logger=self.logger)
Beispiel #20
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)
Beispiel #21
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):
        """
        Add an object to the collection

        :param ref: The reference to the object.
        :param save: If this is true then the objet is persisted 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.
        :param check_for_duplicate_netinfo: This checks for duplicate network information. This only has an effect on
                                            systems.
        :raises TypError or ValueError
        """
        if ref is None:
            raise TypeError("Unable to add a None object")
        if ref.name is None:
            raise ValueError("Unable to add an object without a name")

        ref.check_if_valid()

        if ref.uid == '':
            ref.uid = uuid.uuid4().hex

        if save:
            now = float(time.time())
            if ref.ctime == 0.0:
                ref.ctime = now
            ref.mtime = now

        if self.lite_sync is None:
            self.lite_sync = self.api.get_sync()

        # 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 TypeError("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()

        # update children cache in parent object in case it is not in there already
        if ref.parent and ref.name not in ref.parent.children:
            ref.parent.children.append(ref.name)
            self.logger.debug("Added child \"%s\" to parent \"%s\"", ref.name,
                              ref.parent.name)

        # 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
                elif isinstance(ref, menu.Menu):
                    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/*", [])
                utils.run_triggers(
                    self.api, ref, "/var/lib/cobbler/triggers/add/%s/post/*" %
                    self.collection_type(), [])
    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()

        # first see if any Groups use this distro
        if not recursive:
            for v in self.config.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.config.api.remove_profile(k.name, recursive=recursive, delete=with_delete, with_triggers=with_triggers, logger=logger)

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

            self.config.serialize_delete(self, obj)

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


            # look through all mirrored directories and find if any directory is holding
            # this particular distribution's kernel and initrd
            settings = self.config.settings()
            possible_storage = glob.glob(settings.webdir + "/ks_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)

        return True
Beispiel #23
0
    def add(self,
            ref,
            save=False,
            with_copy=False,
            with_triggers=True,
            with_sync=True,
            quick_pxe_update=False,
            check_for_duplicate_names=False,
            check_for_duplicate_netinfo=False,
            logger=None):
        """
        Add an object to the collection

        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.
        """
        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 = 0
                    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