def rename(self, ref, newname, with_sync=True, with_triggers=True, logger=None): """ Allows an object "ref" to be given a newname without affecting the rest of the object tree. """ # Nothing to do when it is the same name if newname == ref.name: return True # make a copy of the object, but give it a new name. oldname = ref.name newref = ref.make_clone() newref.set_name(newname) self.add(newref, with_triggers=with_triggers, save=True) # for mgmt classes, update all objects that use it if ref.COLLECTION_TYPE == "mgmtclass": for what in ["distro", "profile", "system"]: items = self.api.find_items(what, {"mgmt_classes": oldname}) for item in items: for i in range(0, len(item.mgmt_classes)): if item.mgmt_classes[i] == oldname: item.mgmt_classes[i] = newname self.api.add_item(what, item, save=True) # for a repo, rename the mirror directory if ref.COLLECTION_TYPE == "repo": path = "/var/www/cobbler/repo_mirror/%s" % ref.name if os.path.exists(path): newpath = "/var/www/cobbler/repo_mirror/%s" % newref.name os.renames(path, newpath) # for a distro, rename the mirror and references to it if ref.COLLECTION_TYPE == 'distro': path = utils.find_distro_path(self.api.settings(), ref) # create a symlink for the new distro name utils.link_distro(self.api.settings(), newref) # test to see if the distro path is based directly # on the name of the distro. If it is, things need # to updated accordingly if os.path.exists(path) and path == "/var/www/cobbler/ks_mirror/%s" % ref.name: newpath = "/var/www/cobbler/ks_mirror/%s" % newref.name os.renames(path, newpath) # update any reference to this path ... distros = self.api.distros() for d in distros: if d.kernel.find(path) == 0: d.set_kernel(d.kernel.replace(path, newpath)) d.set_initrd(d.initrd.replace(path, newpath)) self.config.serialize_item(self, d) # now descend to any direct ancestors and point them at the new object allowing # the original object to be removed without orphanage. Direct ancestors # will either be profiles or systems. Note that we do have to care as # set_parent is only really meaningful for subprofiles. We ideally want a more # generic set_parent. kids = ref.get_children() for k in kids: if k.COLLECTION_TYPE == "distro": raise CX(_("internal error, not expected to have distro child objects")) elif k.COLLECTION_TYPE == "profile": if k.parent != "": k.set_parent(newname) else: k.set_distro(newname) self.api.profiles().add(k, save=True, with_sync=with_sync, with_triggers=with_triggers) elif k.COLLECTION_TYPE == "system": k.set_profile(newname) self.api.systems().add(k, save=True, with_sync=with_sync, with_triggers=with_triggers) elif k.COLLECTION_TYPE == "repo": raise CX(_("internal error, not expected to have repo child objects")) else: raise CX(_("internal error, unknown child type (%s), cannot finish rename" % k.COLLECTION_TYPE)) # now delete the old version self.remove(oldname, with_delete=True, with_triggers=with_triggers) return True
def link_distros(self): for distro in self.api.distros(): self.logger.debug("Linking Distro %s" % distro.name) utils.link_distro(self.settings, distro)