def transfer_file(self, file, dest_path, ip): try: connection = self.connect(self.ssh_user, self.ssh_private_key, ip) sftp = connection.open_sftp() except paramiko.ssh_exception.SSHException: raise Exception("cannot send {0} to {1}, please check" " your ssh connection".format(file, ip)) if os.path.isdir(file): parent_dir = "/".join(file.split('/')[:-1]) try: sftp.mkdir(dest_path) except IOError: pass for dirpath, dirnames, filenames in os.walk(file): remote_path = dest_path + dirpath.split(parent_dir)[1] try: sftp.mkdir(remote_path) except: LOG.info("clearstack: Directory {0} is already created" "in remote host".format(remote_path)) for filename in filenames: local_path = os.path.join(dirpath, filename) remote_filepath = os.path.join(remote_path, filename) sftp.put(local_path, remote_filepath) else: filename = file.split('/')[-1] sftp.put(file, "{0}/{1}".format(dest_path, filename)) connection.close()
def run(self): LOG.info(self.description) if self.function_args: if not self.function(*self.function_args): raise Exception("error running {0}({1})" .format(self.function.__name__, self.function_args)) else: if not self.function(): raise Exception("error running {0}" .format(self.function.__name__))