Skip to content

Commit

Permalink
Skip deleted files when syncing local git folder to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejeda, Engelbert committed Nov 11, 2019
1 parent 00754b2 commit 64943b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ansible_taskrunner/libs/providers/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
sys.exit(1)
logger.info('Checking for locally changed files ...')
if loc_is_git:
cmd = 'git diff-index --name-only HEAD -- && git ls-files --others --exclude-standard'
cmd = '''(git diff-index HEAD --name-status | awk '$1 != "D" {print $2}') && git ls-files --others --exclude-standard'''
local_changed = os.popen(cmd).readlines()
else:
# If local path is not a git repo then
Expand All @@ -171,6 +171,7 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
sys.exit(1)
else:
to_sync = list(set(local_changed))
logger.debug('Files to sync: %s' % ' '.join(to_sync))
if len(to_sync) > 0:
logger.info("Performing sync to %s ..." % remote_dir)
for path in to_sync:
Expand Down
14 changes: 9 additions & 5 deletions ansible_taskrunner/libs/sshutil/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
try:
import paramiko
from paramiko import SSHClient, ssh_exception
from libs.sshutil.scp import SCPException
except ImportError as e:
print('Error in %s ' % os.path.basename(self_file_name))
print('Failed to import at least one required module')
Expand Down Expand Up @@ -49,12 +50,15 @@ def create_parent_dirs(self, remote_path):
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))
logger.debug("Loc Sync Target {}".format(local_path))
logger.debug("Rem 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.")
try:
self.create_parent_dirs(remote_path)
self.scp.put(local_path, remote_path=remote_path, preserve_times=True, recursive=True)
logger.debug('Sync ok for %s' % local_path)
except SCPException as e:
logger.error('Failed to sync {} to remote {} - error was {}'.format(local_path, remote_path, e))
else:
logger.warning("Skipping %s as it does not exist" % local_path.strip())
return
Expand Down

0 comments on commit 64943b1

Please sign in to comment.