def execute_together(cls, remotes, command, expected=None, raise_on_err=True, **kwargs): """Execute command on multiple remotes in async mode :type remotes: list :type command: str :type expected: list :type raise_on_err: bool :raises: DevopsCalledProcessError """ if expected is None: expected = [0] futures = {} errors = {} for remote in set(remotes): # Use distinct remotes chan, _, _, _ = remote.execute_async(command, **kwargs) futures[remote] = chan for remote, chan in futures.items(): ret = chan.recv_exit_status() chan.close() if ret not in expected: errors[remote.hostname] = ret if errors and raise_on_err: raise error.DevopsCalledProcessError(command, errors)
def check_stderr( self, command, verbose=False, timeout=None, error_info=None, raise_on_err=True, **kwargs): """Execute command expecting return code 0 and empty STDERR :type command: str :type verbose: bool :type timeout: int :type error_info: str :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ ret = self.check_call( command, verbose, timeout=timeout, error_info=error_info, raise_on_err=raise_on_err, **kwargs) if ret['stderr']: message = ( log_templates.CMD_UNEXPECTED_STDERR.format( append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], )) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError( command, ret['exit_code'], stdout=ret['stdout_brief'], stderr=ret['stdout_brief']) return ret
def check_call( cls, command, verbose=False, timeout=None, error_info=None, expected=None, raise_on_err=True, **kwargs): """Execute command and check for return code Timeout limitation: read tick is 100 ms. :type command: str :type verbose: bool :type timeout: int :type error_info: str :type expected: list :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ if expected is None: expected = [proc_enums.ExitCodes.EX_OK] else: expected = [ proc_enums.ExitCodes(code) if ( isinstance(code, int) and code in proc_enums.ExitCodes.__members__.values()) else code for code in expected ] ret = cls.execute(command, verbose, timeout, **kwargs) if ret['exit_code'] not in expected: message = ( "{append}Command '{cmd!r}' returned exit code {code!s} while " "expected {expected!s}\n" "\tSTDOUT:\n" "{stdout}" "\n\tSTDERR:\n" "{stderr}".format( append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], expected=expected, stdout=ret['stdout_str'], stderr=ret['stderr_str'] )) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError( command, ret['exit_code'], expected=expected, stdout=ret['stdout_str'], stderr=ret['stderr_str']) return ret
def check_call(cls, command, verbose=False, timeout=None, error_info=None, expected=None, raise_on_err=True, **kwargs): """Execute command and check for return code Timeout limitation: read tick is 100 ms. :type command: str :type verbose: bool :type timeout: int :type error_info: str :type expected: list :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ if expected is None: expected = [proc_enums.ExitCodes.EX_OK] else: expected = [ proc_enums.ExitCodes(code) if (isinstance(code, int) and code in proc_enums.ExitCodes.__members__.values()) else code for code in expected ] ret = cls.execute(command, verbose, timeout, **kwargs) if ret['exit_code'] not in expected: message = (log_templates.CMD_UNEXPECTED_EXIT_CODE.format( append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], expected=expected)) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError( command, ret['exit_code'], expected=expected, stdout=ret['stdout_brief'], stderr=ret['stderr_brief']) return ret
def check_stderr(self, command, verbose=False, timeout=None, error_info=None, raise_on_err=True, **kwargs): """Execute command expecting return code 0 and empty STDERR :type command: str :type verbose: bool :type timeout: int :type error_info: str :type raise_on_err: bool :rtype: ExecResult :raises: DevopsCalledProcessError """ ret = self.check_call(command, verbose, timeout=timeout, error_info=error_info, raise_on_err=raise_on_err, **kwargs) if ret['stderr']: message = ("{append}Command '{cmd!r}' STDERR while not expected\n" "\texit code: {code!s}\n" "\tSTDOUT:\n" "{stdout}" "\n\tSTDERR:\n" "{stderr}".format(append=error_info + '\n' if error_info else '', cmd=command, code=ret['exit_code'], stdout=ret['stdout_str'], stderr=ret['stderr_str'])) logger.error(message) if raise_on_err: raise error.DevopsCalledProcessError(command, ret['exit_code'], stdout=ret['stdout_str'], stderr=ret['stderr_str']) return ret