def test_copyfile(): # Arrange test_source = "" test_destination = "" # Act utils.copyfile(test_source, test_destination) # Assert assert False
def __copy_files(self, iso_distro): """ This method copies the required and optional files from syslinux into the directories we use for building the ISO. :param iso_distro: The distro (and thus architecture) to build the ISO for. """ self.logger.info("copying syslinux files") files_to_copy = [ "isolinux.bin", "menu.c32", "chain.c32", "ldlinux.c32", "libcom32.c32", "libutil.c32", ] optional_files = ["ldlinux.c32", "libcom32.c32", "libutil.c32"] syslinux_folders = [ pathlib.Path(self.api.settings().syslinux_dir), pathlib.Path( self.api.settings().syslinux_dir).joinpath("modules/bios/"), pathlib.Path("/usr/lib/syslinux/"), pathlib.Path("/usr/lib/ISOLINUX/"), ] # file_copy_success will be used to check for missing files file_copy_success: Dict[str, bool] = { f: False for f in files_to_copy if f not in optional_files } for syslinux_folder in syslinux_folders: if syslinux_folder.exists(): for file_to_copy in files_to_copy: source_file = syslinux_folder.joinpath(file_to_copy) if source_file.exists(): utils.copyfile( str(source_file), os.path.join(self.isolinuxdir, file_to_copy), ) file_copy_success[file_to_copy] = True unsuccessful_copied_files = [ k for k, v in file_copy_success.items() if not v ] if len(unsuccessful_copied_files) > 0: self.logger.error( 'The following files were not found: "%s"', '", "'.join(unsuccessful_copied_files), ) raise FileNotFoundError( "Required file(s) not found. Please check your syslinux installation" ) self.logger.info("copying GRUB2 files") bootloader_directory = pathlib.Path( self.api.settings().bootloaders_dir) grub_efi = bootloader_directory.joinpath( "grub", self.calculate_grub_name(iso_distro)) buildiso_directory = pathlib.Path(self.api.settings().buildisodir) grub_target = buildiso_directory.joinpath("grub", "grub.efi") if grub_efi.exists(): utils.copyfile(str(grub_efi), str(grub_target), symlink=True) else: self.logger.error('The following files were not found: "%s"', grub_efi) raise FileNotFoundError( "Required file(s) not found. Please check your GRUB 2 installation" )
def run(self, iso=None, buildisodir=None, profiles=None, systems=None, distro=None, standalone: Optional[bool] = None, airgapped: Optional[bool] = None, source=None, exclude_dns: Optional[bool] = None, xorrisofs_opts: Optional[str] = None): """ A :param iso: The name of the iso. Defaults to "autoinst.iso". :param buildisodir: This overwrites the directory from the settings in which the iso is built in. :param profiles: :param systems: Don't use that when building standalone isos. :param distro: (For standalone only) :param standalone: This means that no network connection is needed to install the generated iso. :param airgapped: This option implies standalone=True. :param source: If the iso should be offline available this is the path to the sources of the image. :param exclude_dns: Whether the repositories have to be locally available or the internet is reachable. :param xorrisofs_opts: xorrisofs options to include additionally. """ if airgapped is True: standalone = True # the distro option is for stand-alone builds only if not standalone and distro is not None: utils.die("The --distro option should only be used when creating a standalone or airgapped ISO") # if building standalone, we only want --distro and --profiles (optional), systems are disallowed if standalone: if systems is not None: utils.die("When building a standalone ISO, use --distro and --profiles only, not --systems") elif distro is None: utils.die("When building a standalone ISO, you must specify a --distro") if source is not None and not os.path.exists(source): utils.die("The source specified (%s) does not exist" % source) # insure all profiles specified are children of the distro if profiles: which_profiles = self.filter_systems_or_profiles(profiles, 'profile') for profile in which_profiles: if profile.distro != distro: utils.die("When building a standalone ISO, all --profiles must be under --distro") # if iso is none, create it in . as "autoinst.iso" if iso is None: iso = "autoinst.iso" if buildisodir is None: buildisodir = self.settings.buildisodir else: if not os.path.isdir(buildisodir): utils.die("The --tempdir specified is not a directory") (buildisodir_head, buildisodir_tail) = os.path.split(os.path.normpath(buildisodir)) if buildisodir_tail != "buildiso": buildisodir = os.path.join(buildisodir, "buildiso") self.logger.info("using/creating buildisodir: %s" % buildisodir) if not os.path.exists(buildisodir): os.makedirs(buildisodir) else: shutil.rmtree(buildisodir) os.makedirs(buildisodir) # if base of buildisodir does not exist, fail create all profiles unless filtered by "profiles" imagesdir = os.path.join(buildisodir, "images") isolinuxdir = os.path.join(buildisodir, "isolinux") self.logger.info("building tree for isolinux") if not os.path.exists(imagesdir): os.makedirs(imagesdir) if not os.path.exists(isolinuxdir): os.makedirs(isolinuxdir) self.logger.info("copying miscellaneous files") files_to_copy = ["isolinux.bin", "menu.c32", "chain.c32", "ldlinux.c32", "libcom32.c32", "libutil.c32"] optional_files = ["ldlinux.c32", "libcom32.c32", "libutil.c32"] syslinux_folders = ["/usr/share/syslinux/", "/usr/lib/syslinux/modules/bios/", "/usr/lib/syslinux/", "/usr/lib/ISOLINUX/"] # file_copy_success will be used to check for missing files file_copy_success = {f: False for f in files_to_copy if f not in optional_files} for syslinux_folder in syslinux_folders: if os.path.isdir(os.path.join(syslinux_folder)): for file_to_copy in files_to_copy: source_file = os.path.join(syslinux_folder, file_to_copy) if os.path.exists(source_file): utils.copyfile(source_file, os.path.join(isolinuxdir, file_to_copy)) file_copy_success[file_to_copy] = True if False in file_copy_success.values(): for k, v in file_copy_success: if not v: self.logger.error("File not found: %s" % k) utils.die("Required file(s) not found. Please check your syslinux installation") if standalone or airgapped: self.generate_standalone_iso(imagesdir, isolinuxdir, distro, source, airgapped, profiles) else: self.generate_netboot_iso(imagesdir, isolinuxdir, profiles, systems, exclude_dns) if xorrisofs_opts is None: xorrisofs_opts = "" else: xorrisofs_opts = xorrisofs_opts.strip() # using xorrisofs instead of mkisofs nowadays, it is available everywhere... cmd = "xorrisofs -o %s %s -r -b isolinux/isolinux.bin -c isolinux/boot.cat" % (iso, xorrisofs_opts) cmd = cmd + " -no-emul-boot -boot-load-size 4" cmd = cmd + r" -boot-info-table -V Cobbler\ Install -R -J %s" % buildisodir rc = utils.subprocess_call(cmd, shell=True) if rc != 0: utils.die("xorrisofs failed") self.logger.info("ISO build complete") self.logger.info("You may wish to delete: %s" % buildisodir) self.logger.info("The output file is: %s" % iso)
def run(self, iso=None, buildisodir=None, profiles=None, systems=None, distro=None, standalone=None, airgapped=None, source=None, exclude_dns=None, mkisofs_opts=None): # the airgapped option implies standalone if airgapped is True: standalone = True # the distro option is for stand-alone builds only if not standalone and distro is not None: utils.die( self.logger, "The --distro option should only be used when creating a standalone or airgapped ISO" ) # if building standalone, we only want --distro and --profiles (optional), # systems are disallowed if standalone: if systems is not None: utils.die( self.logger, "When building a standalone ISO, use --distro and --profiles only, not --systems" ) elif distro is None: utils.die( self.logger, "When building a standalone ISO, you must specify a --distro" ) if source is not None and not os.path.exists(source): utils.die(self.logger, "The source specified (%s) does not exist" % source) # insure all profiles specified are children of the distro if profiles: which_profiles = self.filter_systems_or_profiles( profiles, 'profile') for profile in which_profiles: if profile.distro != distro: utils.die( self.logger, "When building a standalone ISO, all --profiles must be under --distro" ) # if iso is none, create it in . as "autoinst.iso" if iso is None: iso = "autoinst.iso" if buildisodir is None: buildisodir = self.settings.buildisodir else: if not os.path.isdir(buildisodir): utils.die(self.logger, "The --tempdir specified is not a directory") (buildisodir_head, buildisodir_tail) = os.path.split(os.path.normpath(buildisodir)) if buildisodir_tail != "buildiso": buildisodir = os.path.join(buildisodir, "buildiso") self.logger.info("using/creating buildisodir: %s" % buildisodir) if not os.path.exists(buildisodir): os.makedirs(buildisodir) else: shutil.rmtree(buildisodir) os.makedirs(buildisodir) # if base of buildisodir does not exist, fail # create all profiles unless filtered by "profiles" imagesdir = os.path.join(buildisodir, "images") isolinuxdir = os.path.join(buildisodir, "isolinux") self.logger.info("building tree for isolinux") if not os.path.exists(imagesdir): os.makedirs(imagesdir) if not os.path.exists(isolinuxdir): os.makedirs(isolinuxdir) self.logger.info("copying miscellaneous files") files_to_copy = [ "isolinux.bin", "menu.c32", "chain.c32", "ldlinux.c32", "libcom32.c32", "libutil.c32" ] optional_files = ["ldlinux.c32", "libcom32.c32", "libutil.c32"] syslinux_folders = [ "/usr/share/syslinux/", "/usr/lib/syslinux/modules/bios/", "/usr/lib/syslinux/", "/usr/lib/ISOLINUX/" ] # file_copy_success will be used to check for missing files file_copy_success = { f: False for f in files_to_copy if f not in optional_files } for syslinux_folder in syslinux_folders: if os.path.isdir(os.path.join(syslinux_folder)): for file_to_copy in files_to_copy: source_file = os.path.join(syslinux_folder, file_to_copy) if os.path.exists(source_file): utils.copyfile(source_file, os.path.join(isolinuxdir, file_to_copy), self.api) file_copy_success[file_to_copy] = True if False in file_copy_success.values(): for k, v in file_copy_success: if not v: self.logger.error("File not found: %s" % k) utils.die( self.logger, "Required file(s) not found. Please check your syslinux installation" ) if standalone or airgapped: self.generate_standalone_iso(imagesdir, isolinuxdir, distro, source, airgapped, profiles) else: self.generate_netboot_iso(imagesdir, isolinuxdir, profiles, systems, exclude_dns) if mkisofs_opts is None: mkisofs_opts = "" else: mkisofs_opts = mkisofs_opts.strip() # removed --quiet cmd = "mkisofs -o %s %s -r -b isolinux/isolinux.bin -c isolinux/boot.cat" % ( iso, mkisofs_opts) cmd = cmd + " -no-emul-boot -boot-load-size 4" cmd = cmd + r" -boot-info-table -V Cobbler\ Install -R -J -T %s" % buildisodir rc = utils.subprocess_call(self.logger, cmd, shell=True) if rc != 0: utils.die(self.logger, "mkisofs failed") self.logger.info("ISO build complete") self.logger.info("You may wish to delete: %s" % buildisodir) self.logger.info("The output file is: %s" % iso)