def _run(self): if os.path.isfile(self._result()): logging.info(("{0} is already there. " "Delete it to regenerate it." ).format(self._result())) return self._result() self._require_sudo() bootstrap_cmd = Bootstrap() # This command is based upon the output of the bootstrap command bootstrap_result = bootstrap_cmd.run(self.config.get_base_config_file()) workdir = get_workdir() print("Going to upgrade the bootstrap image to a lxc image.") with tempfile.TemporaryDirectory(dir=workdir) as tempdir: chown_to_user(tempdir) lxcimagedir = os.path.join(tempdir, "lxcimage") self._unpack_image(bootstrap_result, lxcimagedir) self._write_container_metadata(lxcimagedir) archive = self._pack_image(tempdir, lxcimagedir) chown_to_user(archive) create_artifact_dir() shutil.move(archive, self._result()) print_success("Created lxc image {}.".format(self._result())) return self._result()
def run(self): workdir = get_workdir() create_artifact_dir() commands = self._get_commands() for filename, content, name, path, dictionary, raw_node, artifacts in commands: if self._are_all_artifacts_available(artifacts): logging.info(('''Artifacts for command '{}' are already there. ''' '''Delete them to regenerate them.''' ).format(name)) else: with tempfile.TemporaryDirectory(dir=workdir) as tmpdir: chown_to_user(tmpdir) require_root = raw_node.get('require_root', False) logging.info(("Running command {} located in " "{} with dictionary:\n{}" ).format(name, path, yaml.dump(remove_passwords(dictionary), default_flow_style=False))) command_file = self._flush_command_file(tmpdir, filename, content) self._run_command(command_file, require_root) self._post_process_artifacts(name, artifacts) return self._result(commands)
def _run(self): if os.path.isfile(self._result()): logging.info( ("{0} is already there. " "Delete it to regenerate it.").format(self._result())) return self._result() self._require_sudo() qemu_executable = Fetch().run(self.config.get_base_config_file()) print("Going to bootstrap initial image - be patient.") if self.config.get_bootstrap_tool() != "debootstrap": raise FatalError(("At the moment only debootstrap " "is supported for bootstrapping!")) workdir = get_workdir() with tempfile.TemporaryDirectory(dir=workdir) as tempdir: chown_to_user(tempdir) key_data = fetch_repository_key( self.config.get_bootstrap_repository_key()) keyring_file = build_keyring(tempdir, "temp_keyring.gpg", key_data) rootfs = self._run_debootstrap(tempdir, keyring_file, qemu_executable) self._postprocess_rootfs(rootfs, key_data) archive = self._pack_image(tempdir, rootfs) chown_to_user(archive) create_artifact_dir() shutil.move(archive, self._result()) print_success("Bootstrapped initial image {}.".format(self._result())) return self._result()
def test_artifacts_folder_removal(monkeypatch): suppress_chown_during_debuild(monkeypatch) with workspace() as workdir: create_artifact_dir() artifacts_dir = get_artifact_dir() assert str(workdir) in str(artifacts_dir) random_dir_name = get_random_string(20) abs_dir_name = os.path.join(artifacts_dir, random_dir_name) run(['mkdir', '-p', abs_dir_name]) assert os.path.isdir(abs_dir_name) safely_remove_artifacts_folder(abs_dir_name) assert not os.path.isdir(abs_dir_name)
def _run(self): if not self._needs_qemu(): return None if os.path.isfile(self._result()): logging.info(("{0} is already there. " "Delete it to re-fetch it." ).format(self._result())) return self._result() qemu_package = self.config.get_qemu_package_name() print("Going to fetch qemu Debian package ({}).".format(qemu_package)) workdir = get_workdir() with tempfile.TemporaryDirectory(dir=workdir) as tempdir: chown_to_user(tempdir) qemu_repository = self.config.get_qemu_repository() if qemu_repository: key_url = self.config.get_qemu_repository_key() else: qemu_repository = self.config.get_bootstrap_repository() key_url = self.config.get_bootstrap_repository_key() d = PackageDownloader(repository=qemu_repository, repository_key=key_url, architectures=[get_debian_architecture()]) package_file = d.download(package_name=qemu_package, dest=tempdir) apt_inst.DebFile(package_file).data.extractall(tempdir) qemu_binary = os.path.join(tempdir, 'usr', 'bin', self._get_qemu_binary_name()) chown_to_user(qemu_binary) create_artifact_dir() if not os.path.isdir(self._result_folder()): os.mkdir(self._result_folder()) chown_to_user(self._result_folder()) shutil.move(qemu_binary, self._result()) print_success("Fetched qemu binary {}.".format(self._result())) return self._result()
def _run(self): if os.path.isfile(self._result()): logging.info( ("{0} is already there. " "Delete it to regenerate it.").format(self._result())) return self._result() image_name = Publish().run(self.config.get_base_config_file()) print("Going to export lxc image from image store.") create_artifact_dir() export_image(image_name, self._image_without_extension()) if (os.path.isfile(self._image_without_extension()) and not os.path.isfile(self._result())): # Workaround for https://github.com/lxc/lxd/issues/3869 logging.info("Fixing file extension of exported image.") os.rename(self._image_without_extension(), self._result()) print_success("Exported lxc image as {}.".format(self._result())) return self._result()
def test_artifacts_folder_removal_as_sudo(): with workspace() as workdir: create_artifact_dir() artifacts_dir = get_artifact_dir() assert str(workdir) in str(artifacts_dir) random_dir_name = get_random_string(20) abs_dir_name = os.path.join(artifacts_dir, random_dir_name) mount_folder = os.path.join(workdir, get_random_string(20)) mount_point = os.path.join(abs_dir_name, 'mnt') for folder in [abs_dir_name, mount_folder, mount_point]: run(['mkdir', '-p', folder]) assert os.path.isdir(folder) run(['mount', '--bind', mount_folder, mount_point], sudo=True) with pytest.raises(FatalError) as error: safely_remove_artifacts_folder(abs_dir_name, sudo=True) assert abs_dir_name in error.value.message run(['umount', mount_point], sudo=True) safely_remove_artifacts_folder(abs_dir_name, sudo=True) assert not os.path.isdir(abs_dir_name)