Skip to content

Commit

Permalink
SFTP Sync now creates intermediate directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejeda, Engelbert committed Oct 10, 2019
1 parent 5c800fa commit 320d93b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions ansible_taskrunner/libs/sshutil/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Import third-party and custom modules
try:
import paramiko
from paramiko import SSHClient, ssh_exception
from paramiko import SSHClient, SFTPClient, ssh_exception
from socket import gaierror
import libs.sshutil.scp
from libs.sshutil.sync import SSHSync
Expand Down Expand Up @@ -100,7 +100,8 @@ def sync(self):
"""pointer to scp module's sync function
"""
scp = SCPClient(self.ssh.get_transport(), progress = self.progress)
sync = SSHSync(scp)
sftp = self.ssh.open_sftp()
sync = SSHSync(scp, sftp)
logger.info("Successfully initialized SCP client.")
return sync

Expand Down
18 changes: 17 additions & 1 deletion ansible_taskrunner/libs/sshutil/sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import pathlib
import sys

# Setup Logging
Expand Down Expand Up @@ -29,14 +30,29 @@

class SSHSync:

def __init__(self, scp_obj):
def __init__(self, scp_obj, sftp_obj):
self.scp = scp_obj
self.sftp_obj = sftp_obj
pass

def create_parent_dirs(self, remote_path):
remote_path = pathlib.Path(remote_path)
parent_list = list(remote_path.parents)
# reverse the parent directories so it would create the ancestor frist
parent_list.reverse()
for parent in parent_list:
directory = parent.as_posix() # covert to posix path
try:
self.sftp_obj.stat(directory) # test if remote_dir exists
except IOError:
self.sftp_obj.mkdir(directory)
self.sftp_obj.stat(directory)

def to_remote(self, local_path, remote_path):
logger.debug("Lcl Sync Target {}".format(local_path))
logger.debug("Rmt Sync Target {}".format(remote_path))
if os.path.exists(local_path):
self.create_parent_dirs(remote_path)
self.scp.put(local_path, remote_path=remote_path, preserve_times=True, recursive=True)
logger.debug("Successfully copied to remote.")
else:
Expand Down

0 comments on commit 320d93b

Please sign in to comment.