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

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