1# Based on local.py (c) 2012, Michael DeHaan <michael.dehaan@gmail.com> 2# Based on chroot.py (c) 2013, Maykel Moya <mmoya@speedyrails.com> 3# Copyright (c) 2013, Michael Scherer <misc@zarb.org> 4# Copyright (c) 2017 Ansible Project 5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 6 7from __future__ import (absolute_import, division, print_function) 8__metaclass__ = type 9 10DOCUMENTATION = """ 11 author: Michael Scherer (@msherer) <misc@zarb.org> 12 connection: funcd 13 short_description: Use funcd to connect to target 14 description: 15 - This transport permits you to use Ansible over Func. 16 - For people who have already setup func and that wish to play with ansible, 17 this permit to move gradually to ansible without having to redo completely the setup of the network. 18 version_added: "1.1" 19 options: 20 remote_addr: 21 description: 22 - The path of the chroot you want to access. 23 default: inventory_hostname 24 vars: 25 - name: ansible_host 26 - name: ansible_func_host 27""" 28 29HAVE_FUNC = False 30try: 31 import func.overlord.client as fc 32 HAVE_FUNC = True 33except ImportError: 34 pass 35 36import os 37import tempfile 38import shutil 39 40from ansible.errors import AnsibleError 41from ansible.utils.display import Display 42 43display = Display() 44 45 46class Connection(object): 47 ''' Func-based connections ''' 48 49 has_pipelining = False 50 51 def __init__(self, runner, host, port, *args, **kwargs): 52 self.runner = runner 53 self.host = host 54 # port is unused, this go on func 55 self.port = port 56 57 def connect(self, port=None): 58 if not HAVE_FUNC: 59 raise AnsibleError("func is not installed") 60 61 self.client = fc.Client(self.host) 62 return self 63 64 def exec_command(self, cmd, become_user=None, sudoable=False, executable='/bin/sh', in_data=None): 65 ''' run a command on the remote minion ''' 66 67 if in_data: 68 raise AnsibleError("Internal Error: this module does not support optimized module pipelining") 69 70 # totally ignores privlege escalation 71 display.vvv("EXEC %s" % (cmd), host=self.host) 72 p = self.client.command.run(cmd)[self.host] 73 return (p[0], p[1], p[2]) 74 75 def _normalize_path(self, path, prefix): 76 if not path.startswith(os.path.sep): 77 path = os.path.join(os.path.sep, path) 78 normpath = os.path.normpath(path) 79 return os.path.join(prefix, normpath[1:]) 80 81 def put_file(self, in_path, out_path): 82 ''' transfer a file from local to remote ''' 83 84 out_path = self._normalize_path(out_path, '/') 85 display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.host) 86 self.client.local.copyfile.send(in_path, out_path) 87 88 def fetch_file(self, in_path, out_path): 89 ''' fetch a file from remote to local ''' 90 91 in_path = self._normalize_path(in_path, '/') 92 display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host) 93 # need to use a tmp dir due to difference of semantic for getfile 94 # ( who take a # directory as destination) and fetch_file, who 95 # take a file directly 96 tmpdir = tempfile.mkdtemp(prefix="func_ansible") 97 self.client.local.getfile.get(in_path, tmpdir) 98 shutil.move(os.path.join(tmpdir, self.host, os.path.basename(in_path)), out_path) 99 shutil.rmtree(tmpdir) 100 101 def close(self): 102 ''' terminate the connection; nothing to do here ''' 103 pass 104