2016-10-03 05:12:51 +00:00
|
|
|
# (c) 2013-2016, Michael DeHaan <michael.dehaan@gmail.com>
|
2014-11-14 22:14:08 +00:00
|
|
|
# Stephen Fromm <sfromm@gmail.com>
|
|
|
|
# Brian Coca <briancoca+dev@gmail.com>
|
2016-10-03 05:12:51 +00:00
|
|
|
# Toshio Kuratomi <tkuratomi@ansible.com>
|
2014-11-14 22:14:08 +00:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-10-03 05:12:51 +00:00
|
|
|
import codecs
|
2014-11-14 22:14:08 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import re
|
2016-10-03 05:12:51 +00:00
|
|
|
import tempfile
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-11-22 20:50:24 +00:00
|
|
|
from ansible.constants import mk_boolean as boolean
|
2016-06-28 21:23:30 +00:00
|
|
|
from ansible.errors import AnsibleError
|
2016-09-07 05:54:17 +00:00
|
|
|
from ansible.module_utils._text import to_native, to_text
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
from ansible.utils.hashing import checksum_s
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
class ActionModule(ActionBase):
|
|
|
|
|
|
|
|
TRANSFERS_FILES = True
|
|
|
|
|
2017-03-24 19:39:25 +00:00
|
|
|
def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False, decrypt=True):
|
2014-11-14 22:14:08 +00:00
|
|
|
''' assemble a file from a directory of fragments '''
|
|
|
|
|
|
|
|
tmpfd, temp_path = tempfile.mkstemp()
|
2016-10-03 05:12:51 +00:00
|
|
|
tmp = os.fdopen(tmpfd, 'wb')
|
2014-11-14 22:14:08 +00:00
|
|
|
delimit_me = False
|
|
|
|
add_newline = False
|
|
|
|
|
2016-09-07 05:54:17 +00:00
|
|
|
for f in (to_text(p, errors='surrogate_or_strict') for p in sorted(os.listdir(src_path))):
|
2014-11-14 22:14:08 +00:00
|
|
|
if compiled_regexp and not compiled_regexp.search(f):
|
|
|
|
continue
|
2016-08-23 04:55:30 +00:00
|
|
|
fragment = u"%s/%s" % (src_path, f)
|
2015-07-09 04:27:29 +00:00
|
|
|
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
|
2014-11-14 22:14:08 +00:00
|
|
|
continue
|
2016-08-04 22:35:30 +00:00
|
|
|
|
2017-03-24 19:39:25 +00:00
|
|
|
fragment_content = open(self._loader.get_real_file(fragment, decrypt=decrypt), 'rb').read()
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# always put a newline between fragments if the previous fragment didn't end with a newline.
|
|
|
|
if add_newline:
|
2016-10-03 05:12:51 +00:00
|
|
|
tmp.write(b'\n')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# delimiters should only appear between fragments
|
|
|
|
if delimit_me:
|
|
|
|
if delimiter:
|
|
|
|
# un-escape anything like newlines
|
2016-10-03 05:12:51 +00:00
|
|
|
delimiter = codecs.escape_decode(delimiter)[0]
|
2014-11-14 22:14:08 +00:00
|
|
|
tmp.write(delimiter)
|
|
|
|
# always make sure there's a newline after the
|
|
|
|
# delimiter, so lines don't run together
|
2016-10-03 05:12:51 +00:00
|
|
|
if delimiter[-1] != b'\n':
|
|
|
|
tmp.write(b'\n')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
tmp.write(fragment_content)
|
|
|
|
delimit_me = True
|
2016-10-03 05:12:51 +00:00
|
|
|
if fragment_content.endswith(b'\n'):
|
2014-11-14 22:14:08 +00:00
|
|
|
add_newline = False
|
|
|
|
else:
|
|
|
|
add_newline = True
|
|
|
|
|
|
|
|
tmp.close()
|
|
|
|
return temp_path
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
2016-12-14 17:52:18 +00:00
|
|
|
|
|
|
|
self._supports_check_mode = False
|
2015-10-22 23:07:26 +00:00
|
|
|
|
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-12-14 17:52:18 +00:00
|
|
|
if task_vars is None:
|
|
|
|
task_vars = dict()
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
src = self._task.args.get('src', None)
|
|
|
|
dest = self._task.args.get('dest', None)
|
|
|
|
delimiter = self._task.args.get('delimiter', None)
|
|
|
|
remote_src = self._task.args.get('remote_src', 'yes')
|
|
|
|
regexp = self._task.args.get('regexp', None)
|
2016-02-15 22:11:49 +00:00
|
|
|
follow = self._task.args.get('follow', False)
|
2015-07-09 04:27:29 +00:00
|
|
|
ignore_hidden = self._task.args.get('ignore_hidden', False)
|
2017-03-24 19:39:25 +00:00
|
|
|
decrypt = self._task.args.get('decrypt', True)
|
2015-07-09 04:27:29 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
if src is None or dest is None:
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
|
|
|
result['msg'] = "src and dest are required"
|
|
|
|
return result
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-01-12 22:04:56 +00:00
|
|
|
if boolean(remote_src):
|
2017-01-12 01:32:44 +00:00
|
|
|
result.update(self._execute_module(tmp=tmp, task_vars=task_vars))
|
2015-10-22 23:07:26 +00:00
|
|
|
return result
|
2014-11-14 22:14:08 +00:00
|
|
|
else:
|
2016-06-28 21:23:30 +00:00
|
|
|
try:
|
|
|
|
src = self._find_needle('files', src)
|
|
|
|
except AnsibleError as e:
|
|
|
|
result['failed'] = True
|
2016-09-07 05:54:17 +00:00
|
|
|
result['msg'] = to_native(e)
|
2016-06-28 21:23:30 +00:00
|
|
|
return result
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2017-01-12 01:32:44 +00:00
|
|
|
if not tmp:
|
2016-12-14 17:52:18 +00:00
|
|
|
tmp = self._make_tmp_path()
|
2017-01-12 01:32:44 +00:00
|
|
|
|
2016-02-01 18:42:55 +00:00
|
|
|
if not os.path.isdir(src):
|
|
|
|
result['failed'] = True
|
2016-08-23 04:55:30 +00:00
|
|
|
result['msg'] = u"Source (%s) is not a directory" % src
|
2016-02-01 18:42:55 +00:00
|
|
|
return result
|
|
|
|
|
2016-06-28 21:23:30 +00:00
|
|
|
_re = None
|
|
|
|
if regexp is not None:
|
|
|
|
_re = re.compile(regexp)
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
# Does all work assembling the file
|
2017-03-24 19:39:25 +00:00
|
|
|
path = self._assemble_from_fragments(src, delimiter, _re, ignore_hidden, decrypt)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
path_checksum = checksum_s(path)
|
2015-09-24 20:29:36 +00:00
|
|
|
dest = self._remote_expand_user(dest)
|
2016-03-10 19:06:41 +00:00
|
|
|
dest_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=follow, tmp=tmp)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2015-08-16 05:08:31 +00:00
|
|
|
diff = {}
|
2016-03-06 15:00:36 +00:00
|
|
|
|
|
|
|
# setup args for running modules
|
|
|
|
new_module_args = self._task.args.copy()
|
|
|
|
|
|
|
|
# clean assemble specific options
|
2017-03-24 19:39:25 +00:00
|
|
|
for opt in ['remote_src', 'regexp', 'delimiter', 'ignore_hidden', 'decrypt']:
|
2016-03-06 15:00:36 +00:00
|
|
|
if opt in new_module_args:
|
|
|
|
del new_module_args[opt]
|
|
|
|
|
|
|
|
new_module_args.update(
|
|
|
|
dict(
|
|
|
|
dest=dest,
|
|
|
|
original_basename=os.path.basename(src),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-02-15 22:11:49 +00:00
|
|
|
if path_checksum != dest_stat['checksum']:
|
2015-08-16 05:08:31 +00:00
|
|
|
|
|
|
|
if self._play_context.diff:
|
2015-09-24 20:29:36 +00:00
|
|
|
diff = self._get_diff_data(dest, path, task_vars)
|
2015-08-16 05:08:31 +00:00
|
|
|
|
2016-03-21 21:17:53 +00:00
|
|
|
remote_path = self._connection._shell.join_path(tmp, 'src')
|
|
|
|
xfered = self._transfer_file(path, remote_path)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
# fix file permissions when the copy is done as a different user
|
2016-12-14 17:52:18 +00:00
|
|
|
self._fixup_perms2((tmp, remote_path))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-03-06 15:00:36 +00:00
|
|
|
new_module_args.update( dict( src=xfered,))
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2016-03-10 19:06:41 +00:00
|
|
|
res = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False)
|
2015-08-16 05:08:31 +00:00
|
|
|
if diff:
|
|
|
|
res['diff'] = diff
|
2015-10-22 23:07:26 +00:00
|
|
|
result.update(res)
|
2014-11-14 22:14:08 +00:00
|
|
|
else:
|
2016-03-10 19:06:41 +00:00
|
|
|
result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False))
|
|
|
|
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2016-03-06 15:00:36 +00:00
|
|
|
|
|
|
|
return result
|