def _singularity_start(self, step, cid): env = self._prepare_environment(step) # set the environment variables for k, v in env.items(): os.environ[k] = str(v) args = list(step.args) runs = list(step.runs) ecode = None if runs: info = f"[{step.id}] singularity exec {cid} {runs}" commands = runs start_fn = self._s.execute else: info = f"[{step.id}] singularity run {cid} {args}" commands = args start_fn = self._s.run log.info(info) if self._config.dry_run: return 0 options = self._get_container_options() output = start_fn(self._container, commands, stream=True, options=options) try: for line in output: log.step_info(line.strip("\n")) ecode = 0 except CalledProcessError as ex: ecode = ex.returncode return ecode
def run(self, step): """Execute the given step in docker.""" cid = pu.sanitized_name(step.id, self._config.wid) container = self._find_container(cid) if container and not self._config.reuse and not self._config.dry_run: container.remove(force=True) container = self._create_container(cid, step) log.info(f"[{step.id}] docker start") if self._config.dry_run: return 0 self._spawned_containers.add(container) try: container.start() cout = container.logs(stream=True) for line in cout: log.step_info(line.decode().rstrip()) e = container.wait()["StatusCode"] except Exception as exc: log.fail(exc) return e
def _create_container(self, cid, step): build, _, img, tag, build_ctx_path = self._get_build_info(step) if build: log.info( f"docker build {img}:{tag} {build_ctx_path}", extra={"pretag": f"[{step.id}]"}, ) if not self._config.dry_run: streamer = self._d.api.build( decode=True, path=build_ctx_path, tag=f"{img}:{tag}", rm=True, ) for chunk in streamer: if self._config.quiet: continue if "stream" in chunk: lines = [ line for line in chunk["stream"].splitlines() if line ] for line in lines: log.step_info(line.strip()) elif not self._config.skip_pull and not step.skip_pull: log.info(f"docker pull {img}:{tag}", extra={"pretag": f"[{step.id}]"}) if not self._config.dry_run: self._d.images.pull(repository=f"{img}:{tag}") if self._config.dry_run: return container_args = self._get_container_kwargs(step, f"{img}:{tag}", cid) if "volumes" not in container_args: container_args["volumes"] = [] else: container_args["volumes"] = list(container_args["volumes"]) container_args["volumes"].append( "/var/run/docker.sock:/var/run/docker.sock") log.debug(f"Container args: {container_args}") msg = f"docker create name={cid}" msg += f' image={container_args["image"]}' if container_args["entrypoint"]: msg += f' entrypoint={container_args["entrypoint"]}' if container_args["command"]: msg += f' command={container_args["command"]}' log.info(msg, extra={"pretag": f"[{step.id}]"}) container = self._d.containers.create(**container_args) return container
def _pod_read_log(self): """Read logs from the Pod after it moves into `Running` state. """ log.debug(f"reading logs from {self._pod_name}") response = self._kclient.read_namespaced_pod_log( name=self._pod_name, namespace=self._namespace, follow=True, tail_lines=10, _preload_content=False, ) for line in response: log.step_info(line.decode().rstrip())
def run(self, step): """Execute the given step in docker.""" cid = pu.sanitized_name(step.id, self._config.wid) container = self._find_container(cid) if not container and self._config.reuse: log.fail( f"Cannot find an existing container for step '{step.id}' to be reused" ) if container and not self._config.reuse and not self._config.dry_run: container.remove(force=True) container = None if not container and not self._config.reuse: container = self._create_container(cid, step) log.info("docker start", extra={"pretag": f"[{step.id}]"}) if self._config.dry_run: return 0 self._spawned_containers.add(container) try: container.start() if self._config.pty: dockerpty.start(self._d.api, container.id) else: cout = container.logs(stream=True) for line in cout: log.step_info(line.decode().rstrip()) e = container.wait()["StatusCode"] except Exception as exc: log.fail(exc) return e
def run(self, step): """Execute the given step in docker.""" cid = pu.sanitized_name(step['name'], self._config.wid) container = self._find_container(cid) if container and not self._config.reuse and not self._config.dry_run: container.remove(force=True) container = self._create_container(cid, step) log.info(f'[{step["name"]}] docker start') if self._config.dry_run: return 0 self._spawned_containers.add(container) container.start() cout = container.logs(stream=True) for line in cout: log.step_info(line.decode().rstrip()) e = container.wait()['StatusCode'] return e
def _exec_cmd(cmd, env=None, cwd=os.getcwd(), pids=set(), logging=True): pid = 0 ecode = None try: with Popen( cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True, preexec_fn=os.setsid, env=env, cwd=cwd, ) as p: pid = p.pid pids.add(p.pid) log.debug("Reading process output") output = [] for line in iter(p.stdout.readline, ""): if logging: log.step_info(line.rstrip()) else: output.append(line.rstrip()) p.wait() ecode = p.poll() log.debug(f"Code returned by process: {ecode}") except SubprocessError as ex: output = "" if not ecode: ecode = 1 log.step_info(f"Command '{cmd[0]}' failed with: {ex}") except Exception as ex: output = "" ecode = 1 log.step_info(f"Command raised non-SubprocessError error: {ex}") return pid, ecode, "\n".join(output)