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() 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 _post_process_artifacts(command_name, expected_artifacts): for _, artifact in expected_artifacts.items(): if not os.path.isfile(artifact) and not os.path.isdir(artifact): raise FatalError(('''The command '{}' did not generate ''' '''the specified output artifact '{}'.'''.format(command_name, artifact))) elif os.path.isfile(artifact): chown_to_user(artifact)
def run_all(self): workdir = self.config.get_workdir() applied_playbooks = [] with tempfile.TemporaryDirectory(dir=workdir) as tempdir: chown_to_user(tempdir) inventory = self._write_inventory_file(tempdir) playbook_list = self.config.get_ordered_path_items("playbooks") for name, path, extra_vars in playbook_list: sfc = SharedFolderCoordinator(self.config) extra_vars['edi_shared_folder_mountpoints'] = sfc.get_mountpoints() logging.info(("Running playbook {} located in " "{} with extra vars:\n{}" ).format(name, path, yaml.dump(remove_passwords(extra_vars), default_flow_style=False))) extra_vars_file = os.path.join(tempdir, ("extra_vars_{}" ).format(name)) with open(extra_vars_file, encoding='utf-8', mode='w') as f: f.write(yaml.dump(extra_vars)) ansible_user = extra_vars.get("edi_config_management_user_name") self._run_playbook(path, inventory, extra_vars_file, ansible_user) applied_playbooks.append(name) return applied_playbooks
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 run_all(self): workdir = get_workdir() applied_playbooks = [] with tempfile.TemporaryDirectory(dir=workdir) as tempdir: chown_to_user(tempdir) inventory = self._write_inventory_file(tempdir) for name, path, extra_vars, in self._get_playbooks(): logging.info(("Running playbook {} located in " "{} with extra vars:\n{}" ).format(name, path, yaml.dump(remove_passwords(extra_vars), default_flow_style=False))) extra_vars_file = os.path.join(tempdir, ("extra_vars_{}" ).format(name)) with open(extra_vars_file, encoding='utf-8', mode='w') as f: f.write(yaml.dump(extra_vars)) ansible_user = extra_vars.get("edi_config_management_user_name") self._run_playbook(path, inventory, extra_vars_file, ansible_user) applied_playbooks.append(name) return applied_playbooks
def _flush_command_file(output_dir, filename, content): output_file = os.path.join(output_dir, filename) with open(output_file, encoding="UTF-8", mode="w") as result_file: result_file.write(content) st = os.stat(output_file) os.chmod(output_file, st.st_mode | stat.S_IEXEC) chown_to_user(output_file) return output_file
def workspace(): """ Provides a workspace folder within the project root and changes into it. The workspace folder can be used to perform tests. """ with tempfile.TemporaryDirectory(dir=get_project_root(), prefix="tmp-pytest-") as workspace_dir: chown_to_user(workspace_dir) current_cwd = os.getcwd() os.chdir(workspace_dir) try: yield workspace_dir finally: os.chdir(current_cwd)
def run(self, config_file, introspection_method=None): self._setup_parser(config_file) if introspection_method: print(introspection_method()) return self._result() 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 = self.config.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) shutil.move(qemu_binary, self._result()) print_success("Fetched qemu binary {}.".format(self._result())) return self._result()
def _write_inventory_file(self, tempdir): inventory_file = os.path.join(tempdir, "inventory") with open(inventory_file, encoding='utf-8', mode='w') as f: f.write("[edi]\n{}\n".format(self.target)) chown_to_user(inventory_file) return inventory_file