def setup_command(self): command = _command.shell_command(self.parameters.command) network_namespace = self.parameters.network_namespace sudo = self.parameters.sudo shell = self.parameters.shell if shell: if shell is True: shell = default_shell_command() else: shell = _command.shell_command(shell) command = shell + [str(command)] else: command = _command.shell_command(command) if network_namespace: if sudo is None: sudo = True command = network_namespace_command(network_namespace, command) if sudo: if sudo is True: sudo = default_sudo_command() else: sudo = _command.shell_command(sudo) command = sudo + command self.command = command
def setup_command(self): command = _command.shell_command(self.parameters.command) network_namespace = self.parameters.network_namespace sudo = self.parameters.sudo shell = self.parameters.shell if shell is None: shell = self.default_shell if shell is not None: tobiko.check_valid_type(shell, (bool, str)) if isinstance(shell, str): command = _command.shell_command(shell) + [str(command)] elif shell is True: command = default_shell_command() + [str(command)] if network_namespace: if sudo is None: sudo = True command = network_namespace_command(network_namespace, command) if sudo: if sudo is True: sudo = default_sudo_command() else: sudo = _command.shell_command(sudo) command = sudo + command self.command = command
def kill(self, signal: int = None, **execute_params): execute_params.update(ssh_client=self.ssh_client) command_line = _command.shell_command("kill") if signal is not None: command_line += f"-s {signal}" command_line += str(self.pid) _execute.execute(command_line, **execute_params)
def select_processes( processes: typing.Iterable[PsProcessBase], command: str = None, pid: int = None, is_kernel: typing.Optional[bool] = False, command_line: _command.ShellCommandType = None) \ -> tobiko.Selection[P]: selection = tobiko.Selection[P](processes) if selection and pid is not None: # filter files by PID selection = selection.with_attributes(pid=pid) if selection and command_line is not None: if command is None: command = _command.shell_command(command_line)[0] if selection and command is not None: # filter processes by command pattern = re.compile(command) selection = selection.select( lambda process: bool(pattern.match(str(process.command)))) if selection and is_kernel is not None: # filter kernel processes selection = selection.with_attributes(is_kernel=bool(is_kernel)) if selection and command_line is not None: pattern = re.compile(str(command_line)) selection = selection.select( lambda process: bool(pattern.match(str(process.command_line)))) return selection
def systemctl_command(command: str, *units: str, all: bool = None, no_pager: bool = None, plain: bool = None, state: str = None, type: str = None) \ -> _command.ShellCommand: command_line = _command.shell_command('systemctl') + command if all: command_line += '-a' if no_pager: command_line += '--no-pager' if plain: command_line += '--plain' if state is not None: command_line += f'"--state={state}"' if type is not None: command_line += f'-t "{type}"' if units: command_line += units return command_line
def list_processes( pid: int = None, command: str = None, is_kernel: typing.Optional[bool] = False, ssh_client: ssh.SSHClientType = None, command_line: _command.ShellCommandType = None, is_cirros: bool = None, **execute_params) -> tobiko.Selection[PsProcess]: """Returns list of running process """ ps_command = _command.shell_command('ps') if pid is None or is_cirros in [True, None]: ps_command += '-A' else: ps_command += f"-p {pid}" result = _execute.execute(ps_command, expect_exit_status=None, ssh_client=ssh_client, **execute_params) output = result.stdout and result.stdout.strip() if not output: raise PsError(error=result.stderr) # Extract a list of PsProcess instances from table body processes = tobiko.Selection[PsProcess]() for process_data in parse_table(lines=output.splitlines(), schema=PS_TABLE_SCHEMA): processes.append(PsProcess(ssh_client=ssh_client, is_cirros=is_cirros, **process_data)) return select_processes(processes, pid=pid, command=command, is_kernel=is_kernel, command_line=command_line)
def make_dirs(self, name: str, exist_ok=True): command = _command.shell_command('mkdir') if exist_ok: command += '-p' command += name self.execute(command)
def remove_files(self, filename: str, *filenames: str): filenames = (filename,) + filenames LOG.debug(f"Remove remote files as {self.login}: {filenames}") command = _command.shell_command('rm -fR') + filenames self.execute(command)
def command(self) -> _command.ShellCommand: return _command.shell_command( ['sudo', '/bin/sh', '-c', self.method.command])
def network_namespace_command(network_namespace, command): return _command.shell_command(['/sbin/ip', 'netns', 'exec', network_namespace]) + command
def default_sudo_command(): from tobiko import config CONF = config.CONF return _command.shell_command(CONF.tobiko.shell.sudo)