PageRenderTime 37ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ansible/runner/action_plugins/copy.py

https://github.com/ajanthanm/ansible
Python | 363 lines | 251 code | 47 blank | 65 comment | 87 complexity | 2e5cc6549ec88b58d92852d3d42a2d15 MD5 | raw file
  1# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
  2#
  3# This file is part of Ansible
  4#
  5# Ansible is free software: you can redistribute it and/or modify
  6# it under the terms of the GNU General Public License as published by
  7# the Free Software Foundation, either version 3 of the License, or
  8# (at your option) any later version.
  9#
 10# Ansible is distributed in the hope that it will be useful,
 11# but WITHOUT ANY WARRANTY; without even the implied warranty of
 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13# GNU General Public License for more details.
 14#
 15# You should have received a copy of the GNU General Public License
 16# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 17
 18import os
 19
 20from ansible import utils
 21import ansible.constants as C
 22import ansible.utils.template as template
 23from ansible import errors
 24from ansible.runner.return_data import ReturnData
 25import base64
 26import json
 27import stat
 28import tempfile
 29import pipes
 30
 31## fixes https://github.com/ansible/ansible/issues/3518
 32# http://mypy.pythonblogs.com/12_mypy/archive/1253_workaround_for_python_bug_ascii_codec_cant_encode_character_uxa0_in_position_111_ordinal_not_in_range128.html
 33import sys
 34reload(sys)
 35sys.setdefaultencoding("utf8")
 36
 37
 38class ActionModule(object):
 39
 40    def __init__(self, runner):
 41        self.runner = runner
 42
 43    def run(self, conn, tmp_path, module_name, module_args, inject, complex_args=None, **kwargs):
 44        ''' handler for file transfer operations '''
 45
 46        # load up options
 47        options = {}
 48        if complex_args:
 49            options.update(complex_args)
 50        options.update(utils.parse_kv(module_args))
 51        source  = options.get('src', None)
 52        content = options.get('content', None)
 53        dest    = options.get('dest', None)
 54        raw     = utils.boolean(options.get('raw', 'no'))
 55        force   = utils.boolean(options.get('force', 'yes'))
 56
 57        # content with newlines is going to be escaped to safely load in yaml
 58        # now we need to unescape it so that the newlines are evaluated properly
 59        # when writing the file to disk
 60        if content:
 61            if isinstance(content, unicode):
 62                try:
 63                    content = content.decode('unicode-escape')
 64                except UnicodeDecodeError:
 65                    pass
 66
 67        if (source is None and content is None and not 'first_available_file' in inject) or dest is None:
 68            result=dict(failed=True, msg="src (or content) and dest are required")
 69            return ReturnData(conn=conn, result=result)
 70        elif (source is not None or 'first_available_file' in inject) and content is not None:
 71            result=dict(failed=True, msg="src and content are mutually exclusive")
 72            return ReturnData(conn=conn, result=result)
 73
 74        # Check if the source ends with a "/"
 75        source_trailing_slash = False
 76        if source:
 77            source_trailing_slash = source.endswith("/")
 78
 79        # Define content_tempfile in case we set it after finding content populated.
 80        content_tempfile = None
 81
 82        # If content is defined make a temp file and write the content into it.
 83        if content is not None:
 84            try:
 85                # If content comes to us as a dict it should be decoded json.
 86                # We need to encode it back into a string to write it out.
 87                if type(content) is dict:
 88                    content_tempfile = self._create_content_tempfile(json.dumps(content))
 89                else:
 90                    content_tempfile = self._create_content_tempfile(content)
 91                source = content_tempfile
 92            except Exception, err:
 93                result = dict(failed=True, msg="could not write content temp file: %s" % err)
 94                return ReturnData(conn=conn, result=result)
 95        # if we have first_available_file in our vars
 96        # look up the files and use the first one we find as src
 97        elif 'first_available_file' in inject:
 98            found = False
 99            for fn in inject.get('first_available_file'):
100                fn_orig = fn
101                fnt = template.template(self.runner.basedir, fn, inject)
102                fnd = utils.path_dwim(self.runner.basedir, fnt)
103                if not os.path.exists(fnd) and '_original_file' in inject:
104                    fnd = utils.path_dwim_relative(inject['_original_file'], 'files', fnt, self.runner.basedir, check=False)
105                if os.path.exists(fnd):
106                    source = fnd
107                    found = True
108                    break
109            if not found:
110                results = dict(failed=True, msg="could not find src in first_available_file list")
111                return ReturnData(conn=conn, result=results)
112        else:
113            source = template.template(self.runner.basedir, source, inject)
114            if '_original_file' in inject:
115                source = utils.path_dwim_relative(inject['_original_file'], 'files', source, self.runner.basedir)
116            else:
117                source = utils.path_dwim(self.runner.basedir, source)
118
119        # A list of source file tuples (full_path, relative_path) which will try to copy to the destination
120        source_files = []
121
122        # If source is a directory populate our list else source is a file and translate it to a tuple.
123        if os.path.isdir(source):
124            # Get the amount of spaces to remove to get the relative path.
125            if source_trailing_slash:
126                sz = len(source) + 1
127            else:
128                sz = len(source.rsplit('/', 1)[0]) + 1
129
130            # Walk the directory and append the file tuples to source_files.
131            for base_path, sub_folders, files in os.walk(source):
132                for file in files:
133                    full_path = os.path.join(base_path, file)
134                    rel_path = full_path[sz:]
135                    source_files.append((full_path, rel_path))
136
137            # If it's recursive copy, destination is always a dir,
138            # explicitly mark it so (note - copy module relies on this).
139            if not conn.shell.path_has_trailing_slash(dest):
140                dest = conn.shell.join_path(dest, '')
141        else:
142            source_files.append((source, os.path.basename(source)))
143
144        changed = False
145        diffs = []
146        module_result = {"changed": False}
147
148        # A register for if we executed a module.
149        # Used to cut down on command calls when not recursive.
150        module_executed = False
151
152        # Tell _execute_module to delete the file if there is one file.
153        delete_remote_tmp = (len(source_files) == 1)
154
155        # If this is a recursive action create a tmp_path that we can share as the _exec_module create is too late.
156        if not delete_remote_tmp:
157            if "-tmp-" not in tmp_path:
158                tmp_path = self.runner._make_tmp_path(conn)
159
160        for source_full, source_rel in source_files:
161            # Generate the MD5 hash of the local file.
162            local_md5 = utils.md5(source_full)
163
164            # If local_md5 is not defined we can't find the file so we should fail out.
165            if local_md5 is None:
166                result = dict(failed=True, msg="could not find src=%s" % source_full)
167                return ReturnData(conn=conn, result=result)
168
169            # This is kind of optimization - if user told us destination is
170            # dir, do path manipulation right away, otherwise we still check
171            # for dest being a dir via remote call below.
172            if conn.shell.path_has_trailing_slash(dest):
173                dest_file = conn.shell.join_path(dest, source_rel)
174            else:
175                dest_file = conn.shell.join_path(dest)
176
177            # Attempt to get the remote MD5 Hash.
178            remote_md5 = self.runner._remote_md5(conn, tmp_path, dest_file)
179
180            if remote_md5 == '3':
181                # The remote_md5 was executed on a directory.
182                if content is not None:
183                    # If source was defined as content remove the temporary file and fail out.
184                    self._remove_tempfile_if_content_defined(content, content_tempfile)
185                    result = dict(failed=True, msg="can not use content with a dir as dest")
186                    return ReturnData(conn=conn, result=result)
187                else:
188                    # Append the relative source location to the destination and retry remote_md5.
189                    dest_file = conn.shell.join_path(dest, source_rel)
190                    remote_md5 = self.runner._remote_md5(conn, tmp_path, dest_file)
191
192            if remote_md5 != '1' and not force:
193                # remote_file does not exist so continue to next iteration.
194                continue
195
196            if local_md5 != remote_md5:
197                # The MD5 hashes don't match and we will change or error out.
198                changed = True
199
200                # Create a tmp_path if missing only if this is not recursive.
201                # If this is recursive we already have a tmp_path.
202                if delete_remote_tmp:
203                    if "-tmp-" not in tmp_path:
204                        tmp_path = self.runner._make_tmp_path(conn)
205
206                if self.runner.diff and not raw:
207                    diff = self._get_diff_data(conn, tmp_path, inject, dest_file, source_full)
208                else:
209                    diff = {}
210
211                if self.runner.noop_on_check(inject):
212                    self._remove_tempfile_if_content_defined(content, content_tempfile)
213                    diffs.append(diff)
214                    changed = True
215                    module_result = dict(changed=True)
216                    continue
217
218                # Define a remote directory that we will copy the file to.
219                tmp_src = tmp_path + 'source'
220
221                if not raw:
222                    conn.put_file(source_full, tmp_src)
223                else:
224                    conn.put_file(source_full, dest_file)
225
226                # We have copied the file remotely and no longer require our content_tempfile
227                self._remove_tempfile_if_content_defined(content, content_tempfile)
228
229                # fix file permissions when the copy is done as a different user
230                if self.runner.sudo and self.runner.sudo_user != 'root' and not raw:
231                    self.runner._remote_chmod(conn, 'a+r', tmp_src, tmp_path)
232
233                if raw:
234                    # Continue to next iteration if raw is defined.
235                    continue
236
237                # Run the copy module
238
239                # src and dest here come after original and override them
240                # we pass dest only to make sure it includes trailing slash in case of recursive copy
241                module_args_tmp = "%s src=%s dest=%s original_basename=%s" % (module_args,
242                                  pipes.quote(tmp_src), pipes.quote(dest), pipes.quote(source_rel))
243
244                if self.runner.no_log:
245                    module_args_tmp = "%s NO_LOG=True" % module_args_tmp
246
247                module_return = self.runner._execute_module(conn, tmp_path, 'copy', module_args_tmp, inject=inject, complex_args=complex_args, delete_remote_tmp=delete_remote_tmp)
248                module_executed = True
249
250            else:
251                # no need to transfer the file, already correct md5, but still need to call
252                # the file module in case we want to change attributes
253                self._remove_tempfile_if_content_defined(content, content_tempfile)
254
255                if raw:
256                    # Continue to next iteration if raw is defined.
257                    # self.runner._remove_tmp_path(conn, tmp_path)
258                    continue
259
260                tmp_src = tmp_path + source_rel
261
262                # Build temporary module_args.
263                module_args_tmp = "%s src=%s original_basename=%s" % (module_args,
264                                  pipes.quote(tmp_src), pipes.quote(source_rel))
265                if self.runner.noop_on_check(inject):
266                    module_args_tmp = "%s CHECKMODE=True" % module_args_tmp
267                if self.runner.no_log:
268                    module_args_tmp = "%s NO_LOG=True" % module_args_tmp
269
270                # Execute the file module.
271                module_return = self.runner._execute_module(conn, tmp_path, 'file', module_args_tmp, inject=inject, complex_args=complex_args, delete_remote_tmp=delete_remote_tmp)
272                module_executed = True
273
274            module_result = module_return.result
275            if not module_result.get('md5sum'):
276                module_result['md5sum'] = local_md5
277            if module_result.get('failed') == True:
278                return module_return
279            if module_result.get('changed') == True:
280                changed = True
281
282        # Delete tmp_path if we were recursive or if we did not execute a module.
283        if (not C.DEFAULT_KEEP_REMOTE_FILES and not delete_remote_tmp) \
284            or (not C.DEFAULT_KEEP_REMOTE_FILES and delete_remote_tmp and not module_executed):
285            self.runner._remove_tmp_path(conn, tmp_path)
286
287        # the file module returns the file path as 'path', but 
288        # the copy module uses 'dest', so add it if it's not there
289        if 'path' in module_result and 'dest' not in module_result:
290            module_result['dest'] = module_result['path']
291
292        # TODO: Support detailed status/diff for multiple files
293        if len(source_files) == 1:
294            result = module_result
295        else:
296            result = dict(dest=dest, src=source, changed=changed)
297        if len(diffs) == 1:
298            return ReturnData(conn=conn, result=result, diff=diffs[0])
299        else:
300            return ReturnData(conn=conn, result=result)
301
302    def _create_content_tempfile(self, content):
303        ''' Create a tempfile containing defined content '''
304        fd, content_tempfile = tempfile.mkstemp()
305        f = os.fdopen(fd, 'w')
306        try:
307            f.write(content)
308        except Exception, err:
309            os.remove(content_tempfile)
310            raise Exception(err)
311        finally:
312            f.close()
313        return content_tempfile
314
315    def _get_diff_data(self, conn, tmp, inject, destination, source):
316        peek_result = self.runner._execute_module(conn, tmp, 'file', "path=%s diff_peek=1" % destination, inject=inject, persist_files=True)
317
318        if not peek_result.is_successful():
319            return {}
320
321        diff = {}
322        if peek_result.result['state'] == 'absent':
323            diff['before'] = ''
324        elif peek_result.result['appears_binary']:
325            diff['dst_binary'] = 1
326        elif peek_result.result['size'] > utils.MAX_FILE_SIZE_FOR_DIFF:
327            diff['dst_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
328        else:
329            dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % destination, inject=inject, persist_files=True)
330            if 'content' in dest_result.result:
331                dest_contents = dest_result.result['content']
332                if dest_result.result['encoding'] == 'base64':
333                    dest_contents = base64.b64decode(dest_contents)
334                else:
335                    raise Exception("unknown encoding, failed: %s" % dest_result.result)
336                diff['before_header'] = destination
337                diff['before'] = dest_contents
338
339        src = open(source)
340        src_contents = src.read(8192)
341        st = os.stat(source)
342        if "\x00" in src_contents:
343            diff['src_binary'] = 1
344        elif st[stat.ST_SIZE] > utils.MAX_FILE_SIZE_FOR_DIFF:
345            diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
346        else:
347            src.seek(0)
348            diff['after_header'] = source
349            diff['after'] = src.read()
350
351        return diff
352
353    def _remove_tempfile_if_content_defined(self, content, content_tempfile):
354        if content is not None:
355            os.remove(content_tempfile)
356
357    
358    def _result_key_merge(self, options, results):
359        # add keys to file module results to mimic copy
360        if 'path' in results.result and 'dest' not in results.result:
361            results.result['dest'] = results.result['path']
362            del results.result['path']
363        return results