Skip to content

Commit ef46f12

Browse files
authored
Merge pull request #108 from berttejeda/develop
Develop
2 parents 111285b + 01a156a commit ef46f12

5 files changed

Lines changed: 37 additions & 18 deletions

File tree

HISTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
History
33
=======
44

5+
## Release 2019-11-11 v1.3.6
6+
7+
* Refactor git diff command and embedded inventory file logic [330d5de](https://github.com/berttejeda/ansible-taskrunner/commit/330d5de010758e6467312afa827863ba2388e073)
8+
* Skip deleted files when syncing local git folder to remote [64943b1](https://github.com/berttejeda/ansible-taskrunner/commit/64943b1c9c62fc8192d6d35e17db5002dfb995a9)
9+
10+
## Release 2019-11-04 v1.3.5
11+
12+
* Catch potential crash due to unexpected parameter mapping (python 2.x) [3f37e3e](https://github.com/berttejeda/ansible-taskrunner/commit/3f37e3e6c3bfa6cafc66fa5db5514f81e91879a2)
13+
514
## Release 2019-11-04 v1.3.4
615

716
* Import missing logging module [56b3ca4](https://github.com/berttejeda/ansible-taskrunner/commit/56b3ca4292a189d169937efc5d8c3a7dbcfb0563)

ansible_taskrunner/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
# Private variables
8585
__author__ = 'etejeda'
86-
__version__ = '1.3.4'
86+
__version__ = '1.3.6'
8787
__program_name__ = 'tasks'
8888

8989
# Logging

ansible_taskrunner/libs/providers/ansible.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ def options(func):
5454
option = click.option('---debug', type=str, help='Start task run with ansible in debug mode',
5555
default=False, required=False)
5656
func = option(func)
57-
option = click.option('---inventory', help='Override embedded inventory specification',
57+
option = click.option('---inventory', help='Specify inventory path if not using embedded inventory',
5858
required=False)
5959
func = option(func)
60+
option = click.option('---inventory-tmp-dir', help='Override path where we create embedded temporary inventory',
61+
required=False)
62+
func = option(func)
6063
return func
6164

6265
def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwargs):
@@ -148,8 +151,12 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
148151
sys.exit(1)
149152
logger.info('Checking for locally changed files ...')
150153
if loc_is_git:
151-
cmd = 'git diff-index --name-only HEAD -- && git ls-files --others --exclude-standard'
152-
local_changed = os.popen(cmd).readlines()
154+
# List modified and untracked files
155+
changed_cmd = '''git diff-index HEAD --name-status'''
156+
changed_files = [f.strip().split('\t')[1] for f in os.popen(changed_cmd).readlines() if not f.startswith('D\t')]
157+
untracked_cmd = '''git ls-files --others --exclude-standard'''
158+
untracked_files = [f.strip() for f in os.popen(untracked_cmd).readlines()]
159+
local_changed = changed_files + untracked_files
153160
else:
154161
# If local path is not a git repo then
155162
# we'll only sync files in the current working directory
@@ -160,6 +167,7 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
160167
logger.info('Checking for remotely changed files ...')
161168
no_clobber = settings.get('at_no_clobber')
162169
if rem_is_git:
170+
remote_changed_cmd = '''{} | awk '$1 != "D" {{print $2}}' && {}'''.format(changed_cmd, untracked_cmd)
163171
remote_changed = remote_sub_process.call(remote_dir, cmd)
164172
if remote_changed:
165173
if no_clobber:
@@ -182,12 +190,6 @@ def invoke_bastion_mode(self, bastion_settings, invocation, remote_command, kwar
182190
remote_path = os.path.normpath(_remote_path).replace('\\','/')
183191
logger.debug('Syncing {} to remote {}'.format(file_path, remote_path))
184192
sftp_sync.to_remote(file_path, remote_path)
185-
# remote_command = ' '.join([a for a in sys.argv if a != '---bastion-mode'][1:])
186-
# tasks_file_override = invocation.get('tasks_file_override')
187-
# if tasks_file_override:
188-
# remote_command = 'tasks -f {} {}'.format(tasks_file_override, remote_command)
189-
# else:
190-
# remote_command = 'tasks {}'.format(remote_command)
191193
remote_command_result = remote_sub_process.call(remote_dir, remote_command, stdout_listen=True)
192194
if remote_command_result.returncode > 0:
193195
logger.error('Remote command failed with: %s' % ' '.join(remote_command_result.stderr))
@@ -217,7 +219,10 @@ def invocation(self,
217219
'ansible_playbook_command', 'ansible-playbook')
218220
# Embedded inventory logic
219221
embedded_inventory = False
222+
# Employ an exit trap if we're using bastion mode
220223
trap = ''
224+
# Where to create the temporary inventory (if applicable)
225+
inventory_dir = kwargs.get('_inventory_tmp_dir') or yaml_vars.get('inventory_dir')
221226
inventory_input = kwargs.get('_inventory')
222227
embedded_inventory_string = yaml_vars.get('inventory')
223228
if not inventory_input and not embedded_inventory_string:
@@ -233,12 +238,13 @@ def invocation(self,
233238
trap = 'trap "rm -f %s" EXIT' % ans_inv_fp
234239
else:
235240
if bastion_settings.get('enabled'):
236-
ans_inv_fp = '/tmp/ansible.inventory.%s.tmp.ini' % str(uuid.uuid4())
241+
inventory_dir = '/tmp' if not inventory_dir else inventory_dir
242+
ans_inv_fp = '{}/ansible.inventory.{}.tmp.ini'.format(inventory_dir, uuid.uuid4())
237243
ans_inv_fso_desc = None
238244
if not debug:
239245
trap = 'trap "rm -f %s" EXIT' % ans_inv_fp
240246
else:
241-
ans_inv_fso_desc, ans_inv_fp = mkstemp(prefix='ansible-inventory', suffix='.tmp.ini')
247+
ans_inv_fso_desc, ans_inv_fp = mkstemp(prefix='ansible-inventory', suffix='.tmp.ini', dir=inventory_dir)
242248
logger.info("No inventory specified")
243249
logger.info("Created temporary inventory file %s (normally deleted after run)" % ans_inv_fp)
244250
inventory_input = embedded_inventory_string

ansible_taskrunner/libs/sshutil/sync.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
try:
2121
import paramiko
2222
from paramiko import SSHClient, ssh_exception
23+
from libs.sshutil.scp import SCPException
2324
except ImportError as e:
2425
print('Error in %s ' % os.path.basename(self_file_name))
2526
print('Failed to import at least one required module')
@@ -49,12 +50,15 @@ def create_parent_dirs(self, remote_path):
4950
self.sftp_obj.stat(directory)
5051

5152
def to_remote(self, local_path, remote_path):
52-
logger.debug("Lcl Sync Target {}".format(local_path))
53-
logger.debug("Rmt Sync Target {}".format(remote_path))
53+
logger.debug("Loc Sync Target {}".format(local_path))
54+
logger.debug("Rem Sync Target {}".format(remote_path))
5455
if os.path.exists(local_path):
55-
self.create_parent_dirs(remote_path)
56-
self.scp.put(local_path, remote_path=remote_path, preserve_times=True, recursive=True)
57-
logger.debug("Successfully copied to remote.")
56+
try:
57+
self.create_parent_dirs(remote_path)
58+
self.scp.put(local_path, remote_path=remote_path, preserve_times=True, recursive=True)
59+
logger.debug('Sync ok for %s' % local_path)
60+
except SCPException as e:
61+
logger.error('Failed to sync {} to remote {} - error was {}'.format(local_path, remote_path, e))
5862
else:
5963
logger.warning("Skipping %s as it does not exist" % local_path.strip())
6064
return

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = ansible_taskrunner
33
author = Engelbert Tejeda
44
author_email = berttejeda@gmail.com
55
description = ansible-playbook wrapper with YAML-abstracted python click cli options
6-
version: 1.3.4
6+
version: 1.3.6
77
url = https://github.com/berttejeda/ansible_taskrunner
88
keywords =
99
ansible

0 commit comments

Comments
 (0)