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, 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(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) # 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 get_file(self, source, dest, delete_dest=False, preserve_perm=True, preserve_symlinks=False): """ Copy files from the remote host to a local path Directories will be copied recursively. Args: delete_dest: if it is true, the command will also clear out any old files at dest that are not in the source preserve_perm: tells get_file() to try to preserve the sources permissions on files and dirs preserve_symlinks: try to preserver symlinks instead of transforming them into files/dirs on copy Raiseds: the scp command failed """ self.start_master_ssh() if isinstance(source, basestring): source = [source] dest = os.path.abspath(dest) try_scp = True if try_scp: if delete_dest and os.path.isdir(dest): shutil.rmtree(dest) os.mkdir(dest) remote_source = self._make_rsync_compatible_source(source, False) if remote_source: remote_source = self._encode_remote_paths(remote_source, escape=False) local_dest = utils.sh_escape(dest) scp = self._make_scp_cmd([remote_source], local_dest) try: utils.run(scp) except error.CmdError, e: raise error.ServeRunError(e.args[0], e.args[1])