2015-01-02 13:51:15 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
# (c) 2013, Dylan Martin <dmartin@seattlecentral.edu>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-13 20:28:01 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.plugins.action import ActionBase
|
2015-07-03 22:59:49 +00:00
|
|
|
from ansible.utils.boolean import boolean
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
|
|
|
|
TRANSFERS_FILES = True
|
|
|
|
|
2015-10-22 23:07:26 +00:00
|
|
|
def run(self, tmp=None, task_vars=None):
|
2015-01-02 13:51:15 +00:00
|
|
|
''' handler for unarchive operations '''
|
2015-10-22 23:07:26 +00:00
|
|
|
if task_vars is None:
|
|
|
|
task_vars = dict()
|
|
|
|
|
|
|
|
result = super(ActionModule, self).run(tmp, task_vars)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
source = self._task.args.get('src', None)
|
|
|
|
dest = self._task.args.get('dest', None)
|
2015-07-03 22:59:49 +00:00
|
|
|
copy = boolean(self._task.args.get('copy', True))
|
2015-01-02 13:51:15 +00:00
|
|
|
creates = self._task.args.get('creates', None)
|
|
|
|
|
|
|
|
if source is None or dest is None:
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
|
|
|
result['msg'] = "src (or content) and dest are required"
|
|
|
|
return result
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2016-03-21 21:17:53 +00:00
|
|
|
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
|
2015-01-02 13:51:15 +00:00
|
|
|
if not tmp:
|
2016-03-21 21:17:53 +00:00
|
|
|
tmp = self._make_tmp_path(remote_user)
|
2016-04-20 17:39:12 +00:00
|
|
|
self._cleanup_remote_tmp = True
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
if creates:
|
|
|
|
# do not run the command if the line contains creates=filename
|
|
|
|
# and the filename already exists. This allows idempotence
|
|
|
|
# of command executions.
|
2015-06-04 19:43:07 +00:00
|
|
|
result = self._execute_module(module_name='stat', module_args=dict(path=creates), task_vars=task_vars)
|
2015-01-02 13:51:15 +00:00
|
|
|
stat = result.get('stat', None)
|
|
|
|
if stat and stat.get('exists', False):
|
2015-10-22 23:07:26 +00:00
|
|
|
result['skipped'] = True
|
|
|
|
result['msg'] = "skipped, since %s exists" % creates
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-10-22 23:07:26 +00:00
|
|
|
return result
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2015-09-24 20:29:36 +00:00
|
|
|
dest = self._remote_expand_user(dest) # CCTODO: Fix path for Windows hosts.
|
2015-01-02 13:51:15 +00:00
|
|
|
source = os.path.expanduser(source)
|
|
|
|
|
|
|
|
if copy:
|
2015-10-22 20:03:37 +00:00
|
|
|
if self._task._role is not None:
|
|
|
|
source = self._loader.path_dwim_relative(self._task._role._role_path, 'files', source)
|
2015-01-02 13:51:15 +00:00
|
|
|
else:
|
2015-10-22 20:03:37 +00:00
|
|
|
source = self._loader.path_dwim_relative(self._loader.get_basedir(), 'files', source)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
2016-05-26 21:47:11 +00:00
|
|
|
remote_checksum = self._remote_checksum(dest, all_vars=task_vars, follow=True)
|
2015-12-30 18:49:39 +00:00
|
|
|
if remote_checksum == '4':
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
2015-12-30 18:49:39 +00:00
|
|
|
result['msg'] = "python isn't present on the system. Unable to compute checksum"
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-10-22 23:07:26 +00:00
|
|
|
return result
|
2015-12-30 18:49:39 +00:00
|
|
|
elif remote_checksum != '3':
|
2015-10-22 23:07:26 +00:00
|
|
|
result['failed'] = True
|
2015-12-30 18:49:39 +00:00
|
|
|
result['msg'] = "dest '%s' must be an existing dir" % dest
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-10-22 23:07:26 +00:00
|
|
|
return result
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
if copy:
|
|
|
|
# transfer the file to a remote tmp location
|
2016-03-21 21:17:53 +00:00
|
|
|
tmp_src = self._connection._shell.join_path(tmp, 'source')
|
|
|
|
self._transfer_file(source, tmp_src)
|
2015-01-02 13:51:15 +00:00
|
|
|
|
|
|
|
# handle diff mode client side
|
|
|
|
# handle check mode client side
|
|
|
|
|
2016-03-21 21:17:53 +00:00
|
|
|
if copy:
|
|
|
|
# fix file permissions when the copy is done as a different user
|
|
|
|
self._fixup_perms(tmp, remote_user, recursive=True)
|
2015-01-02 13:51:15 +00:00
|
|
|
# Build temporary module_args.
|
|
|
|
new_module_args = self._task.args.copy()
|
|
|
|
new_module_args.update(
|
|
|
|
dict(
|
|
|
|
src=tmp_src,
|
|
|
|
original_basename=os.path.basename(source),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
new_module_args = self._task.args.copy()
|
|
|
|
new_module_args.update(
|
|
|
|
dict(
|
|
|
|
original_basename=os.path.basename(source),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# execute the unarchive module now, with the updated args
|
2015-10-22 23:07:26 +00:00
|
|
|
result.update(self._execute_module(module_args=new_module_args, task_vars=task_vars))
|
2016-04-20 17:39:12 +00:00
|
|
|
self._remove_tmp_path(tmp)
|
2015-10-22 23:07:26 +00:00
|
|
|
return result
|