def find_files(path: sh.ShellCommandType, name: NameType = None, command: sh.ShellCommandType = 'find', ssh_client: ssh.SSHClientFixture = None, modified_since: tobiko.Seconds = None, **execute_params) -> typing.List[str]: if not path: raise ValueError("Path can't be empty") command_line = sh.shell_command(command) + path if name is not None: command_line += f"-name '{name}'" if modified_since is not None: # round seconds to the next minute minutes = math.ceil(modified_since / 60.) command_line += f"-mmin {minutes}" result = sh.execute(command_line, ssh_client=ssh_client, expect_exit_status=None, **execute_params) if result.exit_status == 0: output_lines: typing.List[str] = [ line.strip() for line in result.stdout.splitlines() if line.strip()] if output_lines: return output_lines raise FilesNotFound(path=path, name=name, login=ssh_client and ssh_client.login or None, exit_status=result.exit_status, stderr=result.stderr.strip())
def command(self) -> sh.ShellCommand: try: return self._command except AttributeError: pass command = sh.shell_command('curl') + self.get_options() + self.url self._command = command self.addCleanup(self._del_command) return command
def get_ping_command(self, parameters): host = parameters.host if not host: raise ValueError("Ping host destination hasn't been specified") command = sh.shell_command([self.get_ping_executable(parameters)] + self.get_ping_options(parameters) + [host]) LOG.debug('Got ping command from interface %r for host %r: %r', self, host, command) return command
def get_ping_command(self, parameters): host = parameters.host if not host: raise ValueError(f"Invalid destination host: '{host}'") command = sh.shell_command([self.get_ping_executable(parameters)] + self.get_ping_options(parameters) + [host]) LOG.debug('Got ping command from interface %r for host %r: %r', self, host, command) return command
def execute_ip(ifconfig_args, ip_command=None, ignore_errors=False, **execute_params) -> str: if ip_command: ip_command = sh.shell_command(ip_command) else: ip_command = IP_COMMAND command = ip_command + ifconfig_args result = sh.execute(command, stdin=False, stdout=True, stderr=True, expect_exit_status=None, **execute_params) if not ignore_errors and result.exit_status: raise IpError(error=result.stderr, exit_status=result.exit_status) return result.stdout
def grep(pattern: str, command: typing.Optional[sh.ShellCommandType] = None, grep_command: sh.ShellCommandType = 'zgrep -Eh', files: typing.Optional[typing.List[str]] = None, ssh_client: ssh.SSHClientFixture = None, blank_lines: bool = True, **execute_params) -> typing.List[str]: if not pattern: raise ValueError("Pattern string can't be empty") if command: if files: raise ValueError("File list must be empty when command is given") command_line = sh.shell_command(command) + ['|'] + grep_command + [ '-e', pattern ] elif files: command_line = sh.shell_command(grep_command) + ['-e', pattern] + files else: raise ValueError("command and files can't be both empty or None") try: stdout = sh.execute(command_line, ssh_client=ssh_client, **execute_params).stdout except sh.ShellCommandFailed as ex: if ex.exit_status > 1: # Some unknown problem occurred raise stdout = ex.stdout output_lines: typing.List[str] = [ line for line in stdout.splitlines() if blank_lines or line.strip() ] if output_lines: return output_lines login = ssh_client.login if ssh_client else None raise NoMatchingLinesFound(pattern=pattern, files=files, login=login)
def __init__(self, command: sh.ShellCommandType = 'ansible-playbook', inventory_filename: str = None, playbook: str = 'main', playbook_dirname: str = None, ssh_client: ssh.SSHClientType = None, work_dir: str = None): super(AnsiblePlaybook, self).__init__() self._command = sh.shell_command(command) self._inventory_filename = inventory_filename self._playbook = playbook self._playbook_dirname = playbook_dirname self._ssh_client = ssh_client self._work_dir = work_dir self._work_files: typing.Dict[str, str] = {}
def execute_curl(hostname: typing.Union[str, netaddr.IPAddress] = None, port: int = None, path: str = None, scheme: str = None, ssh_client: ssh.SSHClientType = None, connect_timeout: tobiko.Seconds = None, fail_silently: bool = True, retry_count: int = None, retry_timeout: tobiko.Seconds = None, retry_interval: tobiko.Seconds = None, **execute_params) -> str: """Execute curl command Returns the command output in case of success, raises ShellCommandFailed otherwise. """ netloc = make_netloc(hostname=hostname, port=port) url = make_url(scheme=scheme, netloc=netloc, path=path) command = sh.shell_command('curl -g') if fail_silently: command += '-f' if connect_timeout is not None: command += f'--connect-timeout {int(connect_timeout)}' command += url for attempt in tobiko.retry(count=retry_count, timeout=retry_timeout, interval=retry_interval, default_count=1, default_timeout=60., default_interval=1.): try: return sh.execute(command, ssh_client=ssh_client, **execute_params).stdout except sh.ShellCommandFailed as ex: if ex.exit_status in CURL_CONNECTION_ERRORS: # Retry on connection errors attempt.check_limits() else: raise raise RuntimeError('Unexpected failure')
def combine_cloud_configs(objs): packages = [] runcmd = [] extra_params = {} for obj in objs: if obj: if not isinstance(obj, abc.Mapping): obj = dict(obj) for package in obj.pop('packages', []): if package and package not in packages: packages.append(package) for cmdline in obj.pop('runcmd', []): if cmdline: cmdline = list(sh.shell_command(cmdline)) if cmdline: runcmd.append(cmdline) if obj: extra_params.update(obj) return CloudConfig.create(packages=packages or None, runcmd=runcmd or None, **extra_params)
def test_list_nodes_processes(self): node = random.choice(topology.list_openstack_nodes()) filename = sh.execute( 'mktemp', ssh_client=node.ssh_client).stdout.strip() self.addCleanup(sh.execute, f"rm -f '{filename}'", ssh_client=node.ssh_client) command_line = sh.shell_command(f"tail -F '{filename}'") process = sh.process(command_line, ssh_client=node.ssh_client) # Process isn't listed before creation processes = topology.list_nodes_processes( command_line=command_line, hostnames=[node.hostname]) self.assertEqual([], processes, 'Process listed before executing it') # Process is listed after creation process.execute() self.addCleanup(process.kill) processes = topology.list_nodes_processes( command_line=command_line, hostnames=[node.hostname]) self.assertEqual(command_line, processes.unique.command_line) self.assertIs(node.ssh_client, processes.unique.ssh_client) # Process isn't listed after kill processes.unique.kill() for attempt in tobiko.retry(timeout=30., interval=5.): processes = topology.list_nodes_processes( command_line=command_line, hostnames=[node.hostname]) if not processes: break if attempt.is_last: self.fail("Process listed after killing it")
def customize_image_file(self, base_file: str) -> str: customized_file = base_file + '.1' if os.path.isfile(customized_file): if (os.stat(base_file).st_mtime_ns < os.stat(customized_file).st_mtime_ns): LOG.debug(f"Image file is up to date '{customized_file}'") return customized_file else: LOG.debug(f"Remove obsolete image file '{customized_file}'") os.remove(customized_file) work_file = sh.execute('mktemp').stdout.strip() try: LOG.debug(f"Copy base image file: '{base_file}' to '{work_file}'") sh.put_file(base_file, work_file) options = self.get_virt_customize_options() if options: command = sh.shell_command(['virt-customize', '-a', work_file]) sh.execute(command + options) sh.get_file(work_file, customized_file) return customized_file finally: sh.execute(['rm', '-f', work_file])
def expected_command(self, command): command = sh.shell_command(command) if self.shell: command = sh.shell_command(self.shell) + [str(command)] return str(command)
def add_runcmd(self, *command_lines: sh.ShellCommandType): runcmd = self.runcmd for command_line in command_lines: command_line = sh.shell_command(command_line) runcmd.append(list(command_line))
def get_iperf3_client_command( self, parameters: _parameters.Iperf3ClientParameters) \ -> sh.ShellCommand: options = self.get_iperf3_client_options(parameters=parameters) return sh.shell_command('iperf3') + options
fields = line.strip().split() interface = fields[0] interfaces.append(interface) return interfaces def get_network_main_route_device(dest_ip, **execute_params): output = execute_ip(['route', 'get', dest_ip], **execute_params) if output: for line in output.splitlines(): fields = line.strip().split() device_index = fields.index('dev') + 1 return fields[device_index] IP_COMMAND = sh.shell_command(['/sbin/ip']) def execute_ip(ifconfig_args, ip_command=None, ignore_errors=False, **execute_params) -> str: if ip_command: ip_command = sh.shell_command(ip_command) else: ip_command = IP_COMMAND command = ip_command + ifconfig_args result = sh.execute(command, stdin=False, stdout=True, stderr=True, expect_exit_status=None, **execute_params) if not ignore_errors and result.exit_status: raise IpError(error=result.stderr, exit_status=result.exit_status) return result.stdout
def expected_command(self, command): command = sh.shell_command(command) if self.expected_shell is not None: command = sh.shell_command(self.expected_shell) + [str(command)] return str(command)