def clean_trees(self): """ Delete any previously built pxelinux.cfg tree and virt tree info and then create directories. Note: for SELinux reasons, some information goes in ``/tftpboot``, some in ``/var/www/cobbler`` and some must be duplicated in both. This is because PXE needs tftp, and automatic installation and Virt operations need http. Only the kernel and initrd images are duplicated, which is unfortunate, though SELinux won't let me give them two contexts, so symlinks are not a solution. *Otherwise* duplication is minimal. """ # clean out parts of webdir and all of /tftpboot/images and /tftpboot/pxelinux.cfg for x in os.listdir(self.settings.webdir): path = os.path.join(self.settings.webdir, x) if os.path.isfile(path): if not x.endswith(".py"): utils.rmfile(path, logger=self.logger) if os.path.isdir(path): if x not in self.settings.webdir_whitelist: # delete directories that shouldn't exist utils.rmtree(path, logger=self.logger) if x in [ "autoinstall_templates", "autoinstall_templates_sys", "images", "systems", "distros", "profiles", "repo_profile", "repo_system", "rendered" ]: # clean out directory contents utils.rmtree_contents(path, logger=self.logger) # self.make_tftpboot() utils.rmtree_contents(self.pxelinux_dir, logger=self.logger) utils.rmtree_contents(self.grub_dir, logger=self.logger) utils.rmtree_contents(self.images_dir, logger=self.logger) utils.rmtree_contents(self.yaboot_bin_dir, logger=self.logger) utils.rmtree_contents(self.yaboot_cfg_dir, logger=self.logger) utils.rmtree_contents(self.rendered_dir, logger=self.logger)
def remove_single_profile(self, name, rebuild_menu=True): # delete profiles/$name file in webdir utils.rmfile(os.path.join(self.settings.webdir, "profiles", name)) # delete contents on autoinstalls/$name directory in webdir utils.rmtree(os.path.join(self.settings.webdir, "autoinstalls", name)) if rebuild_menu: self.sync.tftpgen.make_pxe_menu()
def remove_single_image(self, name): """ Sync removing a single image. :param name: The name of the image. """ bootloc = self.settings.tftpboot_location utils.rmfile(os.path.join(bootloc, "images2", name))
def remove_single_distro(self, name): bootloc = self.settings.tftpboot_location # delete contents of images/$name directory in webdir utils.rmtree(os.path.join(self.settings.webdir, "images", name)) # delete contents of images/$name in tftpboot utils.rmtree(os.path.join(bootloc, "images", name)) # delete potential symlink to tree in webdir/links utils.rmfile(os.path.join(self.settings.webdir, "links", name))
def remove_single_system(self, name): bootloc = self.settings.tftpboot_location # delete contents of autoinsts_sys/$name in webdir system_record = self.systems.find(name=name) for (name, interface) in list(system_record.interfaces.items()): filename = system_record.get_config_filename(interface=name) utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", filename)) utils.rmfile(os.path.join(bootloc, "grub", filename.upper()))
def test_rmfile(tmpdir: Path): # Arrange tfile = tmpdir.join("testfile") # Act utils.rmfile(tfile) # Assert assert not os.path.exists(tfile)
def remove_single_profile(self, name: str, rebuild_menu: bool = True): """ Sync removing a single profile. :param name: The name of the profile. :param rebuild_menu: Whether to rebuild the grub/... menu or not. """ # delete profiles/$name file in webdir utils.rmfile(os.path.join(self.settings.webdir, "profiles", name)) # delete contents on autoinstalls/$name directory in webdir utils.rmtree(os.path.join(self.settings.webdir, "autoinstalls", name)) if rebuild_menu: self.tftpgen.make_pxe_menu()
def remove_single_distro(self, name): """ Sync removing a single distro. :param name: The name of the distribution. """ bootloc = self.settings.tftpboot_location # delete contents of images/$name directory in webdir utils.rmtree(os.path.join(self.settings.webdir, "images", name)) # delete contents of images/$name in tftpboot utils.rmtree(os.path.join(bootloc, "images", name)) # delete potential symlink to tree in webdir/links utils.rmfile(os.path.join(self.settings.webdir, "links", name)) # delete potential distro config files utils.rmglob_files( os.path.join(self.settings.webdir, "distro_mirror", "config"), name + "*.repo")
def test_rmfile(): # Arrange filepath = "/dev/shm/testfile" Path(filepath).touch() assert os.path.exists(filepath) # Act result = utils.rmfile(filepath) # Assert assert result assert not os.path.exists(filepath)
def remove_single_system(self, name: str): """ Sync removing a single system. :param name: The name of the system. """ bootloc = self.settings.tftpboot_location # delete contents of autoinsts_sys/$name in webdir system_record = self.systems.find(name=name) for (name, interface) in list(system_record.interfaces.items()): pxe_filename = system_record.get_config_filename(interface=name, loader="pxe") grub_filename = system_record.get_config_filename(interface=name, loader="grub") utils.rmfile(os.path.join(bootloc, "pxelinux.cfg", pxe_filename)) utils.rmfile(os.path.join(bootloc, "grub", "system", grub_filename)) utils.rmfile(os.path.join(bootloc, "grub", "system_link", system_record.name))
def write_pxe_file(self, filename, system, profile, distro, arch, image=None, include_header=True, metadata=None, format="pxe"): """ Write a configuration file for the boot loader(s). More system-specific configuration may come in later, if so that would appear inside the system object in api.py Can be used for different formats, "pxe" (default) and "grub". """ if arch is None: raise CX("missing arch") if image and not os.path.exists(image.file): return None # nfs:// URLs or something, can't use for TFTP if metadata is None: metadata = {} (rval, settings) = utils.input_string_or_dict(self.settings.to_dict()) if rval: for key in list(settings.keys()): metadata[key] = settings[key] # --- # just some random variables template = None buffer = "" # --- autoinstall_path = None kernel_path = None initrd_path = None img_path = None if image is None: # not image based, it's something normalish img_path = os.path.join("/images", distro.name) if 'http' in distro.kernel and 'http' in distro.initrd: kernel_path = distro.kernel initrd_path = distro.initrd else: kernel_path = os.path.join("/images", distro.name, os.path.basename(distro.kernel)) initrd_path = os.path.join("/images", distro.name, os.path.basename(distro.initrd)) # Find the automatic installation file if we inherit from another profile if system: blended = utils.blender(self.api, True, system) else: blended = utils.blender(self.api, True, profile) autoinstall_path = blended.get("autoinstall", "") # update metadata with all known information # this allows for more powerful templating metadata.update(blended) else: # this is an image we are making available, not kernel+initrd if image.image_type == "direct": kernel_path = os.path.join("/images2", image.name) elif image.image_type == "memdisk": kernel_path = "/memdisk" initrd_path = os.path.join("/images2", image.name) else: # CD-ROM ISO or virt-clone image? We can't PXE boot it. kernel_path = None initrd_path = None if img_path is not None and "img_path" not in metadata: metadata["img_path"] = img_path if kernel_path is not None and "kernel_path" not in metadata: metadata["kernel_path"] = kernel_path if initrd_path is not None and "initrd_path" not in metadata: metadata["initrd_path"] = initrd_path # --- # choose a template if system: if format == "grub": if system.netboot_enabled: template = os.path.join(self.settings.boot_loader_conf_template_dir, "grubsystem.template") buffer += 'set system="{system}"\n'.format(system=system.name) else: local = os.path.join(self.settings.boot_loader_conf_template_dir, "grublocal.template") if os.path.exists(local): template = local else: # pxe if system.netboot_enabled: template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxesystem.template") if arch == "ppc" or arch == "ppc64": # to inherit the distro and system's boot_loader values correctly blended_system = utils.blender(self.api, False, system) if blended_system["boot_loader"] == "pxelinux": template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxesystem_ppc.template") else: template = os.path.join(self.settings.boot_loader_conf_template_dir, "yaboot_ppc.template") elif arch.startswith("arm"): template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxesystem_arm.template") elif distro and distro.os_version.startswith("esxi"): # ESXi uses a very different pxe method, using more files than # a standard automatic installation file and different options - # so giving it a dedicated PXE template makes more sense than # shoe-horning it into the existing templates template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxesystem_esxi.template") else: # local booting on ppc requires removing the system-specific dhcpd.conf filename if arch is not None and (arch == "ppc" or arch == "ppc64"): # Disable yaboot network booting for all interfaces on the system for (name, interface) in list(system.interfaces.items()): filename = "%s" % system.get_config_filename(interface=name).lower() # Remove symlink to the yaboot binary f3 = os.path.join(self.bootloc, "ppc", filename) if os.path.lexists(f3): utils.rmfile(f3) f3 = os.path.join(self.bootloc, "etc", filename) if os.path.lexists(f3): utils.rmfile(f3) # Yaboot/OF doesn't support booting locally once you've # booted off the network, so nothing left to do return None else: template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxelocal.template") else: # not a system record, so this is a profile record or an image if arch.startswith("arm"): template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxeprofile_arm.template") elif format == "grub": template = os.path.join(self.settings.boot_loader_conf_template_dir, "grubprofile.template") elif distro and distro.os_version.startswith("esxi"): # ESXi uses a very different pxe method, see comment above in the system section template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxeprofile_esxi.template") else: template = os.path.join(self.settings.boot_loader_conf_template_dir, "pxeprofile.template") if kernel_path is not None: metadata["kernel_path"] = kernel_path if initrd_path is not None: metadata["initrd_path"] = initrd_path # generate the kernel options and append line: kernel_options = self.build_kernel_options(system, profile, distro, image, arch, autoinstall_path) metadata["kernel_options"] = kernel_options if distro and distro.os_version.startswith("esxi") and filename is not None: append_line = "BOOTIF=%s" % (os.path.basename(filename)) elif "initrd_path" in metadata and (not arch or arch not in ["ppc", "ppc64", "arm"]): append_line = "append initrd=%s" % (metadata["initrd_path"]) else: append_line = "append " append_line = "%s%s" % (append_line, kernel_options) if arch == "ppc" or arch == "ppc64": # remove the prefix "append" # TODO: this looks like it's removing more than append, really # not sure what's up here... append_line = append_line[7:] if distro and distro.os_version.startswith("xenserver620"): append_line = "%s" % (kernel_options) metadata["append_line"] = append_line # store variables for templating metadata["menu_label"] = "" if profile: if arch not in ["ppc", "ppc64"]: metadata["menu_label"] = "MENU LABEL %s" % profile.name metadata["profile_name"] = profile.name elif image: metadata["menu_label"] = "MENU LABEL %s" % image.name metadata["profile_name"] = image.name if system: if (system.serial_device is not None) or (system.serial_baud_rate is not None): if system.serial_device: serial_device = system.serial_device else: serial_device = 0 if system.serial_baud_rate: serial_baud_rate = system.serial_baud_rate else: serial_baud_rate = 115200 if format == "pxe": buffer += "serial %d %d\n" % (serial_device, serial_baud_rate) elif format == "grub": buffer += "set serial_console=true\nset serial_baud={baud}\nset serial_line={device}\n".format(baud=serial_baud_rate, device=serial_device) # get the template if kernel_path is not None: template_fh = open(template) template_data = template_fh.read() template_fh.close() else: # this is something we can't PXE boot template_data = "\n" # save file and/or return results, depending on how called. buffer += self.templar.render(template_data, metadata, None) if filename is not None: self.logger.info("generating: %s" % filename) fd = open(filename, "w") fd.write(buffer) fd.close() return buffer
def write_all_system_files(self, system, menu_items): profile = system.get_conceptual_parent() if profile is None: raise CX("system %(system)s references a missing profile %(profile)s" % {"system": system.name, "profile": system.profile}) distro = profile.get_conceptual_parent() image_based = False image = None if distro is None: if profile.COLLECTION_TYPE == "profile": raise CX("profile %(profile)s references a missing distro %(distro)s" % {"profile": system.profile, "distro": profile.distro}) else: image_based = True image = profile pxe_metadata = {'pxe_menu_items': menu_items} # hack: s390 generates files per system not per interface if not image_based and distro.arch.startswith("s390"): short_name = system.name.split('.')[0] s390_name = 'linux' + short_name[7:10] self.logger.info("Writing s390x pxe config for %s" % short_name) # Always write a system specific _conf and _parm file pxe_f = os.path.join(self.bootloc, "s390x", "s_%s" % s390_name) conf_f = "%s_conf" % pxe_f parm_f = "%s_parm" % pxe_f c_templ = os.path.join(self.settings.boot_loader_conf_template_dir, "s390x_conf.template") p_templ = os.path.join(self.settings.boot_loader_conf_template_dir, "s390x_parm.template") template_conf_f = open(c_templ) template_parm_f = open(p_templ) self.logger.info("Files: (conf,param) - (%s,%s)" % (conf_f, parm_f)) self.logger.info("Template Files: (conf,param) - (%s,%s)" % (c_templ, p_templ)) blended = utils.blender(self.api, True, system) self.templar.render(template_conf_f, blended, conf_f) # FIXME: profiles also need this data! # FIXME: the _conf and _parm files are limited to 80 characters in length # gather default kernel_options and default kernel_options_s390x kopts = blended.get("kernel_options", "") hkopts = shlex.split(utils.dict_to_string(kopts)) blended["kernel_options"] = hkopts self.templar.render(template_parm_f, blended, parm_f) # Write system specific zPXE file if system.is_management_supported(): kernel_path = os.path.join("/images", distro.name, os.path.basename(distro.kernel)) initrd_path = os.path.join("/images", distro.name, os.path.basename(distro.initrd)) with open(pxe_f, 'w') as out: out.write(kernel_path + '\n' + initrd_path + '\n') else: # ensure the file doesn't exist utils.rmfile(pxe_f) return # generate one record for each described NIC .. for (name, interface) in list(system.interfaces.items()): pxe_name = system.get_config_filename(interface=name) grub_name = system.get_config_filename(interface=name, loader="grub") if pxe_name is not None: pxe_path = os.path.join(self.bootloc, "pxelinux.cfg", pxe_name) if grub_name is not None: grub_path = os.path.join(self.bootloc, "grub", "system", grub_name) if grub_path is None and pxe_path is None: self.logger.warning("invalid interface recorded for system (%s,%s)" % (system.name, name)) continue if image_based: working_arch = image.arch else: working_arch = distro.arch if working_arch is None: raise CX("internal error, invalid arch supplied") # for tftp only ... if working_arch in ["i386", "x86", "x86_64", "arm", "aarch64", "ppc64le", "ppc64el", "standard"]: # ToDo: This is old, move this logic into item_system.get_config_filename() pass elif working_arch == "ppc" or working_arch == "ppc64": # Determine filename for system-specific bootloader config filename = "%s" % system.get_config_filename(interface=name).lower() # to inherit the distro and system's boot_loader values correctly blended_system = utils.blender(self.api, False, system) if blended_system["boot_loader"] == "pxelinux": pass else: pxe_path = os.path.join(self.bootloc, "etc", filename) # Link to the yaboot binary f3 = os.path.join(self.bootloc, "ppc", filename) if os.path.lexists(f3): utils.rmfile(f3) os.symlink("../yaboot", f3) else: continue if system.is_management_supported(): if not image_based: if pxe_path: self.write_pxe_file(pxe_path, system, profile, distro, working_arch, metadata=pxe_metadata) if grub_path: self.write_pxe_file(grub_path, system, profile, distro, working_arch, format="grub") # Generate a link named after system to the mac file for easier lookup link_path = os.path.join(self.bootloc, "grub", "system_link", system.name) if os.path.exists(link_path): utils.rmfile(link_path) os.symlink(os.path.join("..", "system", grub_name), link_path) else: self.write_pxe_file(pxe_path, system, None, None, working_arch, image=profile, metadata=pxe_metadata) else: # ensure the file doesn't exist utils.rmfile(pxe_path) if grub_path: utils.rmfile(grub_path)
def remove_single_image(self, name): bootloc = self.settings.tftpboot_location utils.rmfile(os.path.join(bootloc, "images2", name))
def write_all_system_files(self, system, menu_items): profile = system.get_conceptual_parent() if profile is None: raise CX("system %(system)s references a missing profile %(profile)s" % {"system": system.name, "profile": system.profile}) distro = profile.get_conceptual_parent() image_based = False image = None if distro is None: if profile.COLLECTION_TYPE == "profile": raise CX("profile %(profile)s references a missing distro %(distro)s" % {"profile": system.profile, "distro": profile.distro}) else: image_based = True image = profile pxe_metadata = {'pxe_menu_items': menu_items} # hack: s390 generates files per system not per interface if not image_based and distro.arch.startswith("s390"): short_name = system.name.split('.')[0] s390_name = 'linux' + short_name[7:10] self.logger.info("Writing s390x pxe config for %s" % short_name) # Always write a system specific _conf and _parm file pxe_f = os.path.join(self.bootloc, "s390x", "s_%s" % s390_name) conf_f = "%s_conf" % pxe_f parm_f = "%s_parm" % pxe_f self.logger.info("Files: (conf,param) - (%s,%s)" % (conf_f, parm_f)) blended = utils.blender(self.api, True, system) # FIXME: profiles also need this data! # gather default kernel_options and default kernel_options_s390x kernel_options = self.build_kernel_options(system, profile, distro, image, "s390x", blended.get("autoinstall", "")) kopts_aligned = "" column = 0 for option in kernel_options.split(): opt_len = len(option) if opt_len > 78: kopts_aligned += '\n' + option + ' ' column = opt_len + 1 self.logger.error("Kernel paramer [%s] too long %s" % (option, opt_len)) continue if column + opt_len > 78: kopts_aligned += '\n' + option + ' ' column = opt_len + 1 else: kopts_aligned += option + ' ' column += opt_len + 1 # Write system specific zPXE file if system.is_management_supported(): if system.netboot_enabled: self.logger.info("S390x: netboot_enabled") kernel_path = os.path.join("/images", distro.name, os.path.basename(distro.kernel)) initrd_path = os.path.join("/images", distro.name, os.path.basename(distro.initrd)) with open(pxe_f, 'w') as out: out.write(kernel_path + '\n' + initrd_path + '\n') with open(parm_f, 'w') as out: out.write(kopts_aligned) # Write conf file with one newline in it if netboot is enabled with open(conf_f, 'w') as out: out.write('\n') else: self.logger.info("S390x: netboot_disabled") # Write empty conf file if netboot is disabled open(conf_f, 'w').close() else: # ensure the files do exist self.logger.info("S390x: management not supported") utils.rmfile(pxe_f) utils.rmfile(conf_f) utils.rmfile(parm_f) self.logger.info("S390x: pxe: [%s], conf: [%s], parm: [%s]" % (pxe_f, conf_f, parm_f)) return # generate one record for each described NIC .. for (name, interface) in list(system.interfaces.items()): pxe_name = system.get_config_filename(interface=name) grub_name = system.get_config_filename(interface=name, loader="grub") if pxe_name is not None: pxe_path = os.path.join(self.bootloc, "pxelinux.cfg", pxe_name) if grub_name is not None: grub_path = os.path.join(self.bootloc, "grub", "system", grub_name) if grub_path is None and pxe_path is None: self.logger.warning("invalid interface recorded for system (%s,%s)" % (system.name, name)) continue if image_based: working_arch = image.arch else: working_arch = distro.arch if working_arch is None: raise CX("internal error, invalid arch supplied") # for tftp only ... if working_arch in ["i386", "x86", "x86_64", "arm", "aarch64", "ppc64le", "ppc64el", "standard"]: # ToDo: This is old, move this logic into item_system.get_config_filename() pass elif working_arch == "ppc" or working_arch == "ppc64": # Determine filename for system-specific bootloader config filename = "%s" % system.get_config_filename(interface=name).lower() # to inherit the distro and system's boot_loader values correctly blended_system = utils.blender(self.api, False, system) if blended_system["boot_loader"] == "pxelinux": pass else: pxe_path = os.path.join(self.bootloc, "etc", filename) # Link to the yaboot binary f3 = os.path.join(self.bootloc, "ppc", filename) if os.path.lexists(f3): utils.rmfile(f3) os.symlink("../yaboot", f3) else: continue if system.is_management_supported(): if not image_based: if pxe_path: self.write_pxe_file(pxe_path, system, profile, distro, working_arch, metadata=pxe_metadata) if grub_path: self.write_pxe_file(grub_path, system, profile, distro, working_arch, format="grub") # Generate a link named after system to the mac file for easier lookup link_path = os.path.join(self.bootloc, "grub", "system_link", system.name) if os.path.exists(link_path): utils.rmfile(link_path) os.symlink(os.path.join("..", "system", grub_name), link_path) else: self.write_pxe_file(pxe_path, system, None, None, working_arch, image=profile, metadata=pxe_metadata) else: # ensure the file doesn't exist utils.rmfile(pxe_path) if grub_path: utils.rmfile(grub_path)
def write_all_system_files(self, system, menu_items): profile = system.get_conceptual_parent() if profile is None: raise CX("system %(system)s references a missing profile %(profile)s" % {"system": system.name, "profile": system.profile}) distro = profile.get_conceptual_parent() image_based = False image = None if distro is None: if profile.COLLECTION_TYPE == "profile": raise CX("profile %(profile)s references a missing distro %(distro)s" % {"profile": system.profile, "distro": profile.distro}) else: image_based = True image = profile pxe_metadata = {'pxe_menu_items': menu_items} # generate one record for each described NIC .. for (name, interface) in list(system.interfaces.items()): pxe_name = system.get_config_filename(interface=name) grub_name = system.get_config_filename(interface=name, loader="grub") if pxe_name is not None: pxe_path = os.path.join(self.bootloc, "pxelinux.cfg", pxe_name) if grub_name is not None: grub_path = os.path.join(self.bootloc, "grub", "system", grub_name) if grub_path is None and pxe_path is None: self.logger.warning("invalid interface recorded for system (%s,%s)" % (system.name, name)) continue if image_based: working_arch = image.arch else: working_arch = distro.arch if working_arch is None: raise CX("internal error, invalid arch supplied") # for tftp only ... if working_arch in ["i386", "x86", "x86_64", "arm", "armv7", "ppc64le", "ppc64el", "standard"]: # ToDo: This is old, move this logic into item_system.get_config_filename() pass elif working_arch.startswith("ppc"): # Determine filename for system-specific bootloader config filename = "%s" % system.get_config_filename(interface=name).lower() # to inherit the distro and system's boot_loader values correctly blended_system = utils.blender(self.api, False, system) if blended_system["boot_loader"] == "pxelinux": pass else: pxe_path = os.path.join(self.bootloc, "etc", filename) # Link to the yaboot binary f3 = os.path.join(self.bootloc, "ppc", filename) if os.path.lexists(f3): utils.rmfile(f3) os.symlink("../yaboot", f3) else: continue if system.is_management_supported(): if not image_based: if pxe_path: self.write_pxe_file(pxe_path, system, profile, distro, working_arch, metadata=pxe_metadata) if grub_path: self.write_pxe_file(grub_path, system, profile, distro, working_arch, format="grub") # Generate a link named after system to the mac file for easier lookup link_path = os.path.join(self.bootloc, "grub", "system_link", system.name) if os.path.exists(link_path): utils.rmfile(link_path) os.symlink(os.path.join("..", "system", grub_name), link_path) else: self.write_pxe_file(pxe_path, system, None, None, working_arch, image=profile, metadata=pxe_metadata) else: # ensure the file doesn't exist utils.rmfile(pxe_path) if grub_path: utils.rmfile(grub_path)
def unlink_files(globex): for f in glob.glob(globex): if os.path.isfile(f): utils.rmfile(f)