def run_commands(exec_dir, kind_bench, commands, stdout_tee=None, stderr_tee=None, target=None): returncode = -1 output = '' pwd = os.getcwd() os.chdir(exec_dir) try: # the commands is multiple lines, and was included by Quotation actual_commands = get_actual_commands(commands, target) try: logging.debug("the actual commands running in local is: %s" % actual_commands) result = utils.run(actual_commands, stdout_tee=stdout_tee, stderr_tee=stderr_tee, verbose=True) except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1]) except Exception, e: logging.debug(e)
def _run(self, command, timeout, ignore_status, stdout, stderr, connect_timeout, env, options, stdin, args): """Helper function for run""" ssh_cmd = self.ssh_command(connect_timeout, options) if not env.strip(): env = "" else: env = "export %s;" % env for arg in args: command += ' "%s"' % utils.sh_escape(arg) full_cmd = '%s "%s %s"' % (ssh_cmd, env, utils.sh_escape(command)) result = utils.run(full_cmd, timeout, True, stdout, stderr, verbose=False, stdin=stdin, stderr_is_expected=ignore_status) #have no utils.run, need to realize # the error message will show up in band(indistinguishable from stuff sent through the # SSH connection). so we hace the remote computer echo the message "Connected." before # running any command. if result.exit_status == 255: if re.search(r'^ssh: connect to the host .* port .*: ' r'Connection timed out\r$', result.stderr): raise error.ServSSHTimeour("ssh timed out", result) if "Permission Denied." in result.stderr: msg = "ssh permission denied" raise error.ServSSHPemissionDenidError(msg, result) if not ignore_status and result.exit_status > 0: raise error.ServRunError("command execution error", result) return result
def run_remote_commands(exec_dir, classify, bench_name, commands, target, stdout_tee=None, stderr_tee=None): returncode = -1 output = '' try: # the commands is multiple lines, and was included by Quotation final_commands = get_actual_commands(commands, exec_dir) if final_commands is not None and final_commands != '': logging.debug( "the actual commands running on the remote host is: %s" % final_commands) result = target.run(final_commands, stdout_tee=stdout_tee, stderr_tee=stderr_tee, verbose=True) else: return ['Not command specifited', -1] except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])
def get_BMC_host(self): try: bmc_ip = settings.get_value('BMCER', 'host', type=str) port = settings.get_value('BMCER', 'port', type=int) user = settings.get_value('BMCER', 'user', type=str) passwd = settings.get_value('BMCER', 'password', type=str) command = settings.get_value('BMCER', 'command', type=str) except Exception as e: raise error.ServRunError(e.args[0], e.args[1]) else: self.command = command try: bmc_host = host_factory.create_host(bmc_ip, user, passwd, port) except Exception as e: raise error.ServRunError(e.args[0], e.args[1]) else: self.host = bmc_host
def send_file(self, source, dest, delete_dest=False, preserve_symlinks=False): """ Copy files from a local path to the remote host. """ self.start_master_ssh() if isinstance(source, basestring): source_is_dir = os.path.isdir(source) source = [source] remote_dest = self._encode_remote_paths([dest]) # if rsync is diabled or fails, try scp try_scp = True if try_scp: # scp has no equivalent to --delete, just drop the entire dest dir if delete_dest: dest_exists = False try: self.run("test -x %s" % dest) dest_exists = True except error.ServRunError: pass dest_is_dir = False if dest_exists: try: self.run("test -d %s" % dest) dest_is_dir = True except error.ServRunError: pass # if there is a list of more than one path, destination *has* # to be a dir.It therr's a single path being transferred and # it is a dir, the destination also has to be a dir if len(source) > 1 and source_is_dir: dest_is_dir = True if dest_exists and dest_is_dir: cmd = "rm -fr %s && mkdir %s" % (dest, dest) elif not dest_exists and dest_is_dir: cmd = "mkdir %s" % dest self.run(cmd) local_sources = self._make_rsync_compatible_source(source, True) if local_sources: scp = self._make_scp_cmd(local_sources, remote_dest) try: utils.run(scp) # utils.run except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])
def run(self, command, timeout=3600, ignore_status=False, stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS, connect_timeout=30, options='', stdin=None, verbose=True, args=()): """ run a command on the remote host """ if verbose: logging.debug("Running (ssh) '%s'" % command) self.start_master_ssh() env = " ".join("=".join(pair) for pair in self.env.iteritems()) try: return self._run(command, timeout, ignore_status, stdout_tee, stderr_tee, connect_timeout, env, options, stdin, args) except error.CmdError, cmderr: raise error.ServRunError(cmderr.args[0], cmderr.args[1])
def remote_BMC_operate(self): returncode = -1 try: self.get_BMC_host() except Exception as e: return returncode if not self.host or not self.command: return returncode try: result = self.host.run(self.command) except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])
def run_commands(exec_dir, classify, bench_name, commands, stdout_tee=None, stderr_tee=None): returncode = -1 output = '' pwd = os.getcwd() os.chdir(exec_dir) caliper_dir = os.path.abspath(os.path.join(exec_dir, "..", "..")) try: # the commands is multiple lines, and was included by Quotation if commands[0] == '\'' or commands[0] == '\"': actual_commands = commands[1:-1] try: logging.debug("the actual commands running is: %s" % actual_commands) result = utils.run(actual_commands, stdout_tee=stdout_tee, stderr_tee=stderr_tee, verbose=True) except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1]) #elif re.search('\.py', commands ) or re.search('\.sh', commands): # script_file_path = os.path.join(caliper_dir, TEST_CFG_DIR, classify, bench_name, commands) # if re.search(".*?\.py", commands): # try: # result = utils.run("python %s" % script_file_path, stdout_tee=stdout_tee, # stderr_tee=stderr_tee, verbose=True) # except error.CmdError, e: # raise error.ServRunError(e.args[0], e.args[1]) # else: # if re.search(".*?\.sh", commands): # try: # result = utils.run("%s %s" % (shell_path, script_file_path), # stdout_tee= stdout_tee, stderr_tee=stderr_tee, verbose=True) # except error.CmdError, e: # raise error.ServRunError(e.args[0], e.args[1]) else:
def get_host_name(host): try: arch_result = host.run("/bin/uname -a") except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])
def get_target_exec_dir(target): try: target_arch = get_host_arch(target) except error.ServUnsupportedError, e: raise error.ServRunError(e.args[0], e.args[1])
def get_local_machine_arch(): try: arch_result = run("/bin/uname -a") except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])
import os import re from caliper.client.shared.utils import * from caliper.client.shared import error from caliper.client.shared import caliper_path from caliper.client.shared.settings import settings def get_target_exec_dir(target): try: target_arch = get_host_arch(target) except error.ServUnsupportedError, e: raise error.ServRunError(e.args[0], e.args[1]) except error.ServRunError, e: raise error.ServRunError(e.args[0], e.args[1]) target_execution_dir = os.path.abspath( os.path.join(caliper_path.GEN_DIR, target_arch)) return target_execution_dir def get_host_arch(host): try: arch_result = host.run("/bin/uname -a") except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1]) else: returncode = arch_result.exit_status if returncode == 0: output = arch_result.stdout
def nuke_pid(pid, signal_queue=(signal.SIGTERM, signal.SIGKILL)): for sig in signal_queue: if signal_pid(pid, sig): return raise error.ServRunError("Could not kill %d" % pid, None)
def run_all_tests(caliper_dir, gen_dir, target): try: server_arch = server_utils.get_local_machine_arch() except error.ServUnsupportedError, e: raise error.ServRunError(e.args[0], e.args[1])
def get_target_ip(target): try: ip_result = target.run("ifconfig") except error.CmdError, e: raise error.ServRunError(e.args[0], e.args[1])