Skip to content

Commit

Permalink
synchronize: add minimal support for kubectl connection
Browse files Browse the repository at this point in the history
This change enable using the synchronize module with kubectl connection
through the oc rsync command.
  • Loading branch information
TristanCacqueray committed Sep 10, 2019
1 parent 0f52b18 commit 60ba246
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
23 changes: 14 additions & 9 deletions lib/ansible/modules/files/synchronize.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,14 @@ def main():
verify_host = module.params['verify_host']
link_dest = module.params['link_dest']

is_oc = rsync == 'oc'
if '/' not in rsync:
rsync = module.get_bin_path(rsync, required=True)

cmd = [rsync, '--delay-updates', '-F']
if is_oc:
cmd = [rsync, 'rsync', '-q', '--progress=false']
else:
cmd = [rsync, '--delay-updates', '-F']
_sshpass_pipe = None
if rsync_password:
try:
Expand Down Expand Up @@ -508,7 +512,7 @@ def main():
if source.startswith('rsync://') and dest.startswith('rsync://'):
module.fail_json(msg='either src or dest must be a localhost', rc=1)

if is_rsh_needed(source, dest):
if not is_oc and is_rsh_needed(source, dest):

# https://github.com/ansible/ansible/issues/15907
has_rsh = False
Expand Down Expand Up @@ -561,13 +565,14 @@ def main():
cmd.append('--link-dest=%s' % link_path)

changed_marker = '<<CHANGED>>'
cmd.append('--out-format=' + changed_marker + '%i %n%L')

# expand the paths
if '@' not in source:
source = os.path.expanduser(source)
if '@' not in dest:
dest = os.path.expanduser(dest)
if not is_oc:
cmd.append('--out-format=' + changed_marker + '%i %n%L')

# expand the paths
if '@' not in source:
source = os.path.expanduser(source)
if '@' not in dest:
dest = os.path.expanduser(dest)

cmd.append(source)
cmd.append(dest)
Expand Down
17 changes: 15 additions & 2 deletions lib/ansible/plugins/action/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def run(self, tmp=None, task_vars=None):
# ssh paramiko docker buildah and local are fully supported transports. Anything
# else only works with delegate_to
if delegate_to is None and self._connection.transport not in \
('ssh', 'paramiko', 'local', 'docker', 'buildah'):
('ssh', 'paramiko', 'local', 'docker', 'buildah', 'kubectl'):
result['failed'] = True
result['msg'] = (
"synchronize uses rsync to function. rsync needs to connect to the remote "
Expand Down Expand Up @@ -391,7 +391,7 @@ def run(self, tmp=None, task_vars=None):

# If launching synchronize against docker container
# use rsync_opts to support container to override rsh options
if self._remote_transport in ['docker', 'buildah']:
if self._remote_transport in ['docker', 'buildah', 'kubectl']:
# Replicate what we do in the module argumentspec handling for lists
if not isinstance(_tmp_args.get('rsync_opts'), MutableSequence):
tmp_rsync_opts = _tmp_args.get('rsync_opts', [])
Expand All @@ -413,6 +413,19 @@ def run(self, tmp=None, task_vars=None):
_tmp_args['rsync_opts'].append("--rsh=%s exec -i" % self._docker_cmd)
elif self._remote_transport in ['buildah']:
_tmp_args['rsync_opts'].append("--rsh=buildah run --")
elif self._remote_transport in ['kubectl']:
for k in ('src', 'dest'):
# Remove non existing local basename from path
if _tmp_args[k].startswith('localhost:') and not os.path.exists(_tmp_args[k].lstrip('localhost:')):
_tmp_args[k] = os.path.dirname(_tmp_args[k])
_tmp_args[k] = _tmp_args[k].lstrip('localhost:').split('@', 1)[-1]
_tmp_args = dict(
_local_rsync_path='oc',
src=_tmp_args['src'],
dest=_tmp_args['dest'],
archive=False,
compress=False,
)

# run the module and store the result
result.update(self._execute_module('synchronize', module_args=_tmp_args, task_vars=task_vars))
Expand Down

0 comments on commit 60ba246

Please sign in to comment.