|
| 1 | +# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 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 | +from __future__ import (absolute_import, division, print_function) |
| 18 | +__metaclass__ = type |
| 19 | + |
| 20 | +import os |
| 21 | + |
| 22 | +from ansible.module_utils._text import to_bytes |
| 23 | +from ansible.module_utils.six import string_types |
| 24 | +from ansible.module_utils.parsing.convert_bool import boolean |
| 25 | +from ansible.plugins.action import ActionBase |
| 26 | +from ansible.utils.hashing import checksum, md5, secure_hash |
| 27 | +from ansible.utils.path import makedirs_safe |
| 28 | + |
| 29 | + |
| 30 | +REMOTE_CHECKSUM_ERRORS = { |
| 31 | + '0': "unable to calculate the checksum of the remote file", |
| 32 | + '1': "the remote file does not exist", |
| 33 | + '2': "no read permission on remote file", |
| 34 | + '3': "remote file is a directory, fetch cannot work on directories", |
| 35 | + '4': "python isn't present on the system. Unable to compute checksum", |
| 36 | + '5': "stdlib json was not found on the remote machine. Only the raw module can work without those installed", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +class ActionModule(ActionBase): |
| 41 | + |
| 42 | + def run(self, tmp=None, task_vars=None): |
| 43 | + ''' handler for fetch operations ''' |
| 44 | + if task_vars is None: |
| 45 | + task_vars = dict() |
| 46 | + |
| 47 | + result = super(ActionModule, self).run(tmp, task_vars) |
| 48 | + del tmp # tmp no longer has any effect |
| 49 | + |
| 50 | + try: |
| 51 | + if self._play_context.check_mode: |
| 52 | + result['skipped'] = True |
| 53 | + result['msg'] = 'check mode not (yet) supported for this module' |
| 54 | + return result |
| 55 | + |
| 56 | + source = self._task.args.get('src', None) |
| 57 | + dest = self._task.args.get('dest', None) |
| 58 | + flat = boolean(self._task.args.get('flat'), strict=False) |
| 59 | + fail_on_missing = boolean(self._task.args.get('fail_on_missing', True), strict=False) |
| 60 | + validate_checksum = boolean(self._task.args.get('validate_checksum', True), strict=False) |
| 61 | + |
| 62 | + # validate source and dest are strings FIXME: use basic.py and module specs |
| 63 | + if not isinstance(source, string_types): |
| 64 | + result['msg'] = "Invalid type supplied for source option, it must be a string" |
| 65 | + |
| 66 | + if not isinstance(dest, string_types): |
| 67 | + result['msg'] = "Invalid type supplied for dest option, it must be a string" |
| 68 | + |
| 69 | + if source is None or dest is None: |
| 70 | + result['msg'] = "src and dest are required" |
| 71 | + |
| 72 | + if result.get('msg'): |
| 73 | + result['failed'] = True |
| 74 | + return result |
| 75 | + |
| 76 | + source = self._connection._shell.join_path(source) |
| 77 | + source = self._remote_expand_user(source) |
| 78 | + |
| 79 | + # calculate checksum for the remote file, don't bother if using |
| 80 | + # become as slurp will be used Force remote_checksum to follow |
| 81 | + # symlinks because fetch always follows symlinks |
| 82 | + remote_checksum = self._remote_checksum(source, all_vars=task_vars, follow=True) |
| 83 | + |
| 84 | + # calculate the destination name |
| 85 | + if os.path.sep not in self._connection._shell.join_path('a', ''): |
| 86 | + source = self._connection._shell._unquote(source) |
| 87 | + source_local = source.replace('\\', '/') |
| 88 | + else: |
| 89 | + source_local = source |
| 90 | + |
| 91 | + dest = os.path.expanduser(dest) |
| 92 | + if flat: |
| 93 | + if os.path.isdir(to_bytes(dest, errors='surrogate_or_strict')) and not dest.endswith(os.sep): |
| 94 | + result['msg'] = "dest is an existing directory, use a trailing slash if you want to fetch src into that directory" |
| 95 | + result['file'] = dest |
| 96 | + result['failed'] = True |
| 97 | + return result |
| 98 | + if dest.endswith(os.sep): |
| 99 | + # if the path ends with "/", we'll use the source filename as the |
| 100 | + # destination filename |
| 101 | + base = os.path.basename(source_local) |
| 102 | + dest = os.path.join(dest, base) |
| 103 | + if not dest.startswith("/"): |
| 104 | + # if dest does not start with "/", we'll assume a relative path |
| 105 | + dest = self._loader.path_dwim(dest) |
| 106 | + else: |
| 107 | + # files are saved in dest dir, with a subdir for each host, then the filename |
| 108 | + if 'inventory_hostname' in task_vars: |
| 109 | + target_name = task_vars['inventory_hostname'] |
| 110 | + else: |
| 111 | + target_name = self._play_context.remote_addr |
| 112 | + dest = "%s/%s/%s" % (self._loader.path_dwim(dest), target_name, source_local) |
| 113 | + |
| 114 | + dest = dest.replace("//", "/") |
| 115 | + |
| 116 | + if remote_checksum in REMOTE_CHECKSUM_ERRORS: |
| 117 | + result['changed'] = False |
| 118 | + result['file'] = source |
| 119 | + result['msg'] = REMOTE_CHECKSUM_ERRORS[remote_checksum] |
| 120 | + # Historically, these don't fail because you may want to transfer |
| 121 | + # a log file that possibly MAY exist but keep going to fetch other |
| 122 | + # log files. Today, this is better achieved by adding |
| 123 | + # ignore_errors or failed_when to the task. Control the behaviour |
| 124 | + # via fail_when_missing |
| 125 | + if fail_on_missing: |
| 126 | + result['failed'] = True |
| 127 | + del result['changed'] |
| 128 | + else: |
| 129 | + result['msg'] += ", not transferring, ignored" |
| 130 | + return result |
| 131 | + |
| 132 | + # calculate checksum for the local file |
| 133 | + local_checksum = checksum(dest) |
| 134 | + |
| 135 | + if remote_checksum != local_checksum: |
| 136 | + # create the containing directories, if needed |
| 137 | + makedirs_safe(os.path.dirname(dest)) |
| 138 | + |
| 139 | + # fetch the file and check for changes |
| 140 | + self._connection.fetch_file(source, dest) |
| 141 | + new_checksum = secure_hash(dest) |
| 142 | + # For backwards compatibility. We'll return None on FIPS enabled systems |
| 143 | + try: |
| 144 | + new_md5 = md5(dest) |
| 145 | + except ValueError: |
| 146 | + new_md5 = None |
| 147 | + |
| 148 | + if validate_checksum and new_checksum != remote_checksum: |
| 149 | + result.update(dict(failed=True, md5sum=new_md5, |
| 150 | + msg="checksum mismatch", file=source, dest=dest, remote_md5sum=None, |
| 151 | + checksum=new_checksum, remote_checksum=remote_checksum)) |
| 152 | + else: |
| 153 | + result.update({'changed': True, 'md5sum': new_md5, 'dest': dest, |
| 154 | + 'remote_md5sum': None, 'checksum': new_checksum, |
| 155 | + 'remote_checksum': remote_checksum}) |
| 156 | + else: |
| 157 | + # For backwards compatibility. We'll return None on FIPS enabled systems |
| 158 | + try: |
| 159 | + local_md5 = md5(dest) |
| 160 | + except ValueError: |
| 161 | + local_md5 = None |
| 162 | + result.update(dict(changed=False, md5sum=local_md5, file=source, dest=dest, checksum=local_checksum)) |
| 163 | + |
| 164 | + finally: |
| 165 | + self._remove_tmp_path(self._connection._shell.tmpdir) |
| 166 | + |
| 167 | + return result |
0 commit comments