def run_command(self, command: List[str], return_output: bool = True, env: Optional[Dict] = None): """ Executes command in a sandbox provided by sandcastle. :param command: the command :param return_output: return output from this method if True :param env: dict with env vars to set for the command """ # we import here so that packit does not depend on sandcastle (and thus python-kube) from sandcastle.api import Sandcastle, MappedDir md = MappedDir( local_dir=self.local_project.working_dir, path=self.config.command_handler_work_dir, with_interim_pvc=True, ) sandcastle = Sandcastle( image_reference=self.config.command_handler_image_reference, k8s_namespace_name=self.config.command_handler_k8s_namespace, mapped_dir=md, env_vars=env, ) sandcastle.run() try: logger.info("running command: %s", command) out = sandcastle.exec(command=command) if return_output: return out return None finally: sandcastle.delete_pod()
class SandcastleCommandHandler(CommandHandler): name = RunCommandType.sandcastle def __init__(self, local_project: LocalProject, config: Config): """ :param local_project: :param config: """ # we import here so that packit does not depend on sandcastle (and thus python-kube) from sandcastle.api import Sandcastle, MappedDir super().__init__(local_project, config) md = MappedDir( local_dir=local_project.working_dir, path=config.command_handler_work_dir, with_interim_pvc=True, ) self.sandcastle = Sandcastle( image_reference=config.command_handler_image_reference, k8s_namespace_name=config.command_handler_k8s_namespace, mapped_dir=md, ) def run_command(self, command: List[str], return_output=True): """ Executes command in a sandbox provided by sandcastle. :param command: the command :param return_output: return output from this method if True """ if not self.sandcastle.is_pod_already_deployed(): self.sandcastle.run() logger.info("running command: %s", command) out = self.sandcastle.exec(command=command) if return_output: return out return None def clean(self): logger.info("removing sandbox pod") self.sandcastle.delete_pod()