| 1 |
# (c) 2013-2016, Michael DeHaan <[email protected]> |
| 2 |
# Stephen Fromm <[email protected]> |
| 3 |
# Brian Coca <[email protected]> |
| 4 |
# Toshio Kuratomi <[email protected]> |
| 5 |
# |
| 6 |
# This file is part of Ansible |
| 7 |
# |
| 8 |
# Ansible is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# Ansible is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import codecs |
| 22 |
import os |
| 23 |
import os.path |
| 24 |
import re |
| 25 |
import tempfile |
| 26 |
|
| 27 |
from ansible import constants as C |
| 28 |
from ansible.errors import AnsibleActionFail |
| 29 |
from ansible.module_utils.common.text.converters import to_text |
| 30 |
from ansible.module_utils.parsing.convert_bool import boolean |
| 31 |
from ansible.plugins.action import ActionBase |
| 32 |
from ansible.utils.hashing import checksum_s |
| 33 |
|
| 34 |
|
| 35 |
class ActionModule(ActionBase): |
| 36 |
|
| 37 |
TRANSFERS_FILES = True |
| 38 |
|
| 39 |
def _assemble_from_fragments(self, src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False, decrypt=True): |
| 40 |
""" assemble a file from a directory of fragments """ |
| 41 |
|
| 42 |
tmpfd, temp_path = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP) |
| 43 |
tmp = os.fdopen(tmpfd, 'wb') |
| 44 |
delimit_me = False |
| 45 |
add_newline = False |
| 46 |
|
| 47 |
for f in (to_text(p, errors='surrogate_or_strict') for p in sorted(os.listdir(src_path))): |
| 48 |
if compiled_regexp and not compiled_regexp.search(f): |
| 49 |
continue |
| 50 |
fragment = u"%s/%s" % (src_path, f) |
| 51 |
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')): |
| 52 |
continue |
| 53 |
|
| 54 |
with open(self._loader.get_real_file(fragment, decrypt=decrypt), 'rb') as fragment_fh: |
| 55 |
fragment_content = fragment_fh.read() |
| 56 |
|
| 57 |
# always put a newline between fragments if the previous fragment didn't end with a newline. |
| 58 |
if add_newline: |
| 59 |
tmp.write(b'\n') |
| 60 |
|
| 61 |
# delimiters should only appear between fragments |
| 62 |
if delimit_me: |
| 63 |
if delimiter: |
| 64 |
# un-escape anything like newlines |
| 65 |
delimiter = codecs.escape_decode(delimiter)[0] |
| 66 |
tmp.write(delimiter) |
| 67 |
# always make sure there's a newline after the |
| 68 |
# delimiter, so lines don't run together |
| 69 |
if delimiter[-1] != b'\n': |
| 70 |
tmp.write(b'\n') |
| 71 |
|
| 72 |
tmp.write(fragment_content) |
| 73 |
delimit_me = True |
| 74 |
if fragment_content.endswith(b'\n'): |
| 75 |
add_newline = False |
| 76 |
else: |
| 77 |
add_newline = True |
| 78 |
|
| 79 |
tmp.close() |
| 80 |
return temp_path |
| 81 |
|
| 82 |
def run(self, tmp=None, task_vars=None): |
| 83 |
|
| 84 |
self._supports_check_mode = True |
| 85 |
|
| 86 |
super(ActionModule, self).run(tmp, task_vars) |
| 87 |
|
| 88 |
del tmp # tmp no longer has any effect |
| 89 |
|
| 90 |
if task_vars is None: |
| 91 |
task_vars = dict() |
| 92 |
|
| 93 |
src = self._task.args.get('src', None) |
| 94 |
dest = self._task.args.get('dest', None) |
| 95 |
delimiter = self._task.args.get('delimiter', None) |
| 96 |
remote_src = self._task.args.get('remote_src', 'yes') |
| 97 |
regexp = self._task.args.get('regexp', None) |
| 98 |
follow = self._task.args.get('follow', False) |
| 99 |
ignore_hidden = self._task.args.get('ignore_hidden', False) |
| 100 |
decrypt = self._task.args.pop('decrypt', True) |
| 101 |
|
| 102 |
try: |
| 103 |
if src is None or dest is None: |
| 104 |
raise AnsibleActionFail("src and dest are required") |
| 105 |
|
| 106 |
if boolean(remote_src, strict=False): |
| 107 |
# call assemble via ansible.legacy to allow library/ overrides of the module without collection search |
| 108 |
return self._execute_module(module_name='ansible.legacy.assemble', task_vars=task_vars) |
| 109 |
|
| 110 |
src = self._find_needle('files', src) |
| 111 |
|
| 112 |
if not os.path.isdir(src): |
| 113 |
raise AnsibleActionFail(u"Source (%s) is not a directory" % src) |
| 114 |
|
| 115 |
_re = None |
| 116 |
if regexp is not None: |
| 117 |
_re = re.compile(regexp) |
| 118 |
|
| 119 |
# Does all work assembling the file |
| 120 |
path = self._assemble_from_fragments(src, delimiter, _re, ignore_hidden, decrypt) |
| 121 |
|
| 122 |
path_checksum = checksum_s(path) |
| 123 |
dest = self._remote_expand_user(dest) |
| 124 |
dest_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=follow) |
| 125 |
|
| 126 |
diff = {} |
| 127 |
|
| 128 |
# setup args for running modules |
| 129 |
new_module_args = self._task.args.copy() |
| 130 |
|
| 131 |
# clean assemble specific options |
| 132 |
for opt in ['remote_src', 'regexp', 'delimiter', 'ignore_hidden', 'decrypt']: |
| 133 |
if opt in new_module_args: |
| 134 |
del new_module_args[opt] |
| 135 |
new_module_args['dest'] = dest |
| 136 |
|
| 137 |
if path_checksum != dest_stat['checksum']: |
| 138 |
|
| 139 |
if self._task.diff: |
| 140 |
diff = self._get_diff_data(dest, path, task_vars) |
| 141 |
|
| 142 |
remote_path = self._connection._shell.join_path(self._connection._shell.tmpdir, 'src') |
| 143 |
xfered = self._transfer_file(path, remote_path) |
| 144 |
|
| 145 |
# fix file permissions when the copy is done as a different user |
| 146 |
self._fixup_perms2((self._connection._shell.tmpdir, remote_path)) |
| 147 |
|
| 148 |
new_module_args.update(dict(src=xfered,)) |
| 149 |
|
| 150 |
res = self._execute_module(module_name='ansible.legacy.copy', module_args=new_module_args, task_vars=task_vars) |
| 151 |
if diff: |
| 152 |
res['diff'] = diff |
| 153 |
return res |
| 154 |
else: |
| 155 |
return self._execute_module(module_name='ansible.legacy.file', module_args=new_module_args, task_vars=task_vars) |
| 156 |
|
| 157 |
finally: |
| 158 |
self._remove_tmp_path(self._connection._shell.tmpdir) |