def run_once(self): sig = getattr(signal, self.config['listen_signal']) dkrcmd = AsyncDockerCmd(self, 'run', self.sub_stuff['subargs'], timeout=self.config['docker_timeout']) self.logdebug("Starting background docker command, " "timeout %s seconds: " "%s", self.config['docker_timeout'], dkrcmd.command) dkrcmd.verbose = True # Runs in background self.sub_stuff['dkrcmd'] = dkrcmd dkrcmd.execute() pid = dkrcmd.process_id ss = self.config['secret_sauce'] while True: stdout = dkrcmd.stdout if stdout.count(ss) >= 1: break time.sleep(0.1) wait_start = self.config['wait_start'] self.loginfo("Container running, waiting %d seconds to send signal" % wait_start) # Allow noticable time difference for date command, # and eat into dkrcmd timeout after receiving signal. time.sleep(wait_start) self.failif(not utils.pid_is_alive(pid), "Pid %s not running after wait: %s" % (pid, dkrcmd.exit_status)) self.loginfo("Signaling pid %d with signal %s", pid, self.config['listen_signal']) utils.signal_pid(pid, sig) self.loginfo("Waiting up to %d seconds for exit", dkrcmd.timeout) # Throw exception if takes > docker_timeout to exit dkrcmd.wait()
def kill_container_by_long_id(self, long_id): """ Use docker CLI 'kill' command on container's long_id :return: pid of container's process """ # Raise KeyError if not found try: _json = self.json_by_long_id(long_id) except TypeError: # NoneType object blah blah blah raise KeyError("Container %s not found" % long_id) pid = _json[0]["State"]["Pid"] if not _json[0]["State"]["Running"] or not utils.pid_is_alive(pid): raise ValueError("Cannot kill container %s, it is not running," " or is a defunct or zombie process. Status: " " %s." % (long_id, _json[0]["State"])) _signal = self.kill_signal cmd = 'kill ' if _signal is not None: if _signal.upper().startswith('SIG'): _signal = _signal[3:] cmd += "--signal=%s " % str(_signal) cmd += str(long_id) if self.verify_output: dkrcmd = self.docker_cmd_check else: dkrcmd = self.docker_cmd self.subtest.logdebug("Killing %s with command: %s", long_id[:12], cmd) # Raise exception if not exit zero dkrcmd(cmd) return pid
def kill_container_by_long_id(self, long_id): """ Use docker CLI 'kill' command on container's long_id :param long_id: String of long-id for container :raises KeyError: if container not found :raises ValueError: if container not running, defunct, or zombie :return: pid of container's process """ # Raise KeyError if not found try: _json = self.json_by_long_id(long_id) except TypeError: # NoneType object blah blah blah raise KeyError("Container %s not found" % long_id) pid = _json[0]["State"]["Pid"] if not _json[0]["State"]["Running"] or not utils.pid_is_alive(pid): raise ValueError("Cannot kill container %s, it is not running," " or is a defunct or zombie process. Status: " " %s." % (long_id, _json[0]["State"])) _signal = self.kill_signal cmd = 'kill ' if _signal is not None: if _signal.upper().startswith('SIG'): _signal = _signal[3:] # pylint: disable=E0012,E1136 cmd += "--signal=%s " % str(_signal) cmd += str(long_id) if self.verify_output: dkrcmd = self.docker_cmd_check else: dkrcmd = self.docker_cmd self.subtest.logdebug("Killing %s with command: %s", long_id[:12], cmd) # Raise exception if not exit zero dkrcmd(cmd) return pid
def move_tasks_into_container(name, tasks): task_file = tasks_path(name) for task in tasks: try: logging.debug('moving task %s into container "%s"', task, name) utils.write_one_line(task_file, task) except Exception: if utils.pid_is_alive(task): raise # task exists but couldn't move it
def program_is_alive(program_name, pid_files_dir=None): """ Checks if the process is alive and not in Zombie state. @param program_name the name of the program @return True if still alive, False otherwise """ pid = get_pid_from_file(program_name, pid_files_dir) if pid is None: return False return utils.pid_is_alive(pid)