def cmd( args: str, env: Dict[str, str] = None, cwd: str = None, wait: bool = True, shell: bool = False, ) -> str: """ Execute a command on the host and return a tuple containing the exit status and result string. stderr output is folded into the stdout result string. :param args: command arguments :param env: environment to run command with :param cwd: directory to run command in :param wait: True to wait for status, False otherwise :param shell: True to use shell, False otherwise :return: combined stdout and stderr :raises CoreCommandError: when there is a non-zero exit status or the file to execute is not found """ logging.debug("command cwd(%s) wait(%s): %s", cwd, wait, args) if shell is False: args = shlex.split(args) try: p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd, shell=shell) if wait: stdout, stderr = p.communicate() status = p.wait() if status != 0: raise CoreCommandError(status, args, stdout, stderr) return stdout.decode("utf-8").strip() else: return "" except OSError: raise CoreCommandError(-1, args)
def check_cmd(args, **kwargs): """ Execute a command on the host and return a tuple containing the exit status and result string. stderr output is folded into the stdout result string. :param list[str]|str args: command arguments :param dict kwargs: keyword arguments to pass to subprocess.Popen :return: combined stdout and stderr :rtype: str :raises CoreCommandError: when there is a non-zero exit status or the file to execute is not found """ kwargs["stdout"] = subprocess.PIPE kwargs["stderr"] = subprocess.STDOUT args = split_args(args) logging.debug("command: %s", args) try: p = subprocess.Popen(args, **kwargs) stdout, _ = p.communicate() status = p.wait() if status != 0: raise CoreCommandError(status, args, stdout) return stdout.decode("utf-8").strip() except OSError: raise CoreCommandError(-1, args)
def get_info(self): args = "lxc list {name} --format json".format(name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) data = json.loads(output) if not data: raise CoreCommandError( status, args, "LXC({name}) not present".format(name=self.name)) return data[0]
def get_info(self): args = "docker inspect {name}".format(name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) data = json.loads(output) if not data: raise CoreCommandError( status, args, "docker({name}) not present".format(name=self.name)) return data[0]
def remote_cmd(self, cmd, env=None, cwd=None, wait=True): """ Run command remotely using server connection. :param str cmd: command to run :param dict env: environment for remote command, default is None :param str cwd: directory to run command in, defaults to None, which is the user's home directory :param bool wait: True to wait for status, False to background process :return: stdout when success :rtype: str :raises CoreCommandError: when a non-zero exit status occurs """ replace_env = env is not None if not wait: cmd += " &" logging.debug("remote cmd server(%s) cwd(%s) wait(%s): %s", self.host, cwd, wait, cmd) try: if cwd is None: result = self.conn.run(cmd, hide=CMD_HIDE, env=env, replace_env=replace_env) else: with self.conn.cd(cwd): result = self.conn.run(cmd, hide=CMD_HIDE, env=env, replace_env=replace_env) return result.stdout.strip() except UnexpectedExit as e: stdout, stderr = e.streams_for_display() raise CoreCommandError(e.result.exited, cmd, stdout, stderr)
def get_info(self) -> Dict: args = f"lxc list {self.name} --format json" output = self.run(args) data = json.loads(output) if not data: raise CoreCommandError(1, args, f"LXC({self.name}) not present") return data[0]
def get_info(self) -> Dict: args = f"docker inspect {self.name}" output = self.run(args) data = json.loads(output) if not data: raise CoreCommandError(1, args, f"docker({self.name}) not present") return data[0]
def copy_file(self, source, destination): if destination[0] != "/": destination = os.path.join("/root/", destination) args = "lxc file push {source} {name}/{destination}".format( source=source, name=self.name, destination=destination) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output)
def get_pid(self): args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format( name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) self.pid = output logging.debug("node(%s) pid: %s", self.name, self.pid) return output
def test_run_startup_exception(self): # given node = mock.MagicMock() node.cmd.side_effect = CoreCommandError(1, "error") service = MyService(node) # when with pytest.raises(ConfigServiceBootError): service.run_startup(wait=True)
def node_net_cmd(self, args): if not self.up: logging.debug("node down, not running network command: %s", args) return 0 status, output = self.client.ns_cmd(args) if status: raise CoreCommandError(status, args, output) return output
def test_run_validation_non_blocking_exception(self): # given node = mock.MagicMock() node.cmd.side_effect = CoreCommandError(1, "error") service = MyService(node) service.validation_mode = ConfigServiceMode.NON_BLOCKING service.validation_period = 0 service.validation_timer = 0 # when with pytest.raises(ConfigServiceBootError): service.run_validation()
def check_cmd(self, args): """ Runs shell command on node. :param list[str]|str args: command to run :return: combined stdout and stderr :rtype: str :raises CoreCommandError: when a non-zero exit status occurs """ status, output = self.cmd_output(args) if status: raise CoreCommandError(status, args, output) return output.strip()
def check_cmd(self, args): """ Run command and return exit status and combined stdout and stderr. :param list[str]|str args: command to run :return: combined stdout and stderr :rtype: str :raises core.CoreCommandError: when there is a non-zero exit status """ status, output = self.cmd_output(args) if status != 0: raise CoreCommandError(status, args, output) return output.strip()
def test_service_validate_error(self, session: Session): # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_TWO) node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid")) # when status = session.services.validate_service(node, my_service) # then assert status
def cmd(args, wait=True): """ Runs a command on and returns the exit status. :param list[str]|str args: command arguments :param bool wait: wait for command to end or not :return: command status :rtype: int """ args = split_args(args) logging.debug("command: %s", args) try: p = subprocess.Popen(args) if not wait: return 0 return p.wait() except OSError: raise CoreCommandError(-1, args)
def cmd_output(args): """ Execute a command on the host and return a tuple containing the exit status and result string. stderr output is folded into the stdout result string. :param list[str]|str args: command arguments :return: command status and stdout :rtype: tuple[int, str] :raises CoreCommandError: when the file to execute is not found """ args = split_args(args) logging.debug("command: %s", args) try: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = p.communicate() status = p.wait() return status, stdout.decode("utf-8").strip() except OSError: raise CoreCommandError(-1, args)
def copy_file(self, source, destination): args = "docker cp {source} {name}:{destination}".format( source=source, name=self.name, destination=destination) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output)