PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/ansible/runner/connection_plugins/chroot.py

https://github.com/ajanthanm/ansible
Python | 130 lines | 88 code | 18 blank | 24 comment | 11 complexity | a5c61c323649b51bff9056d1b4f88565 MD5 | raw file
  1# Based on local.py (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
  2# (c) 2013, Maykel Moya <mmoya@speedyrails.com>
  3#
  4# This file is part of Ansible
  5#
  6# Ansible is free software: you can redistribute it and/or modify
  7# it under the terms of the GNU General Public License as published by
  8# the Free Software Foundation, either version 3 of the License, or
  9# (at your option) any later version.
 10#
 11# Ansible is distributed in the hope that it will be useful,
 12# but WITHOUT ANY WARRANTY; without even the implied warranty of
 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14# GNU General Public License for more details.
 15#
 16# You should have received a copy of the GNU General Public License
 17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 18
 19import distutils.spawn
 20import traceback
 21import os
 22import shutil
 23import subprocess
 24from ansible import errors
 25from ansible import utils
 26from ansible.callbacks import vvv
 27
 28class Connection(object):
 29    ''' Local chroot based connections '''
 30
 31    def __init__(self, runner, host, port, *args, **kwargs):
 32        self.chroot = host
 33        self.has_pipelining = False
 34
 35        if os.geteuid() != 0:
 36            raise errors.AnsibleError("chroot connection requires running as root")
 37
 38        # we're running as root on the local system so do some
 39        # trivial checks for ensuring 'host' is actually a chroot'able dir
 40        if not os.path.isdir(self.chroot):
 41            raise errors.AnsibleError("%s is not a directory" % self.chroot)
 42
 43        chrootsh = os.path.join(self.chroot, 'bin/sh')
 44        if not utils.is_executable(chrootsh):
 45            raise errors.AnsibleError("%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot)
 46
 47        self.chroot_cmd = distutils.spawn.find_executable('chroot')
 48        if not self.chroot_cmd:
 49            raise errors.AnsibleError("chroot command not found in PATH")
 50
 51        self.runner = runner
 52        self.host = host
 53        # port is unused, since this is local
 54        self.port = port
 55
 56    def connect(self, port=None):
 57        ''' connect to the chroot; nothing to do here '''
 58
 59        vvv("THIS IS A LOCAL CHROOT DIR", host=self.chroot)
 60
 61        return self
 62
 63    def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', in_data=None, su=None, su_user=None):
 64        ''' run a command on the chroot '''
 65
 66        if su or su_user:
 67            raise errors.AnsibleError("Internal Error: this module does not support running commands via su")
 68
 69        if in_data:
 70            raise errors.AnsibleError("Internal Error: this module does not support optimized module pipelining")
 71
 72        # We enter chroot as root so sudo stuff can be ignored
 73
 74        if executable:
 75            local_cmd = [self.chroot_cmd, self.chroot, executable, '-c', cmd]
 76        else:
 77            local_cmd = '%s "%s" %s' % (self.chroot_cmd, self.chroot, cmd)
 78
 79        vvv("EXEC %s" % (local_cmd), host=self.chroot)
 80        p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring),
 81                             cwd=self.runner.basedir,
 82                             stdin=subprocess.PIPE,
 83                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 84
 85        stdout, stderr = p.communicate()
 86        return (p.returncode, '', stdout, stderr)
 87
 88    def put_file(self, in_path, out_path):
 89        ''' transfer a file from local to chroot '''
 90
 91        if not out_path.startswith(os.path.sep):
 92            out_path = os.path.join(os.path.sep, out_path)
 93        normpath = os.path.normpath(out_path)
 94        out_path = os.path.join(self.chroot, normpath[1:])
 95
 96        vvv("PUT %s TO %s" % (in_path, out_path), host=self.chroot)
 97        if not os.path.exists(in_path):
 98            raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
 99        try:
100            shutil.copyfile(in_path, out_path)
101        except shutil.Error:
102            traceback.print_exc()
103            raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
104        except IOError:
105            traceback.print_exc()
106            raise errors.AnsibleError("failed to transfer file to %s" % out_path)
107
108    def fetch_file(self, in_path, out_path):
109        ''' fetch a file from chroot to local '''
110
111        if not in_path.startswith(os.path.sep):
112            in_path = os.path.join(os.path.sep, in_path)
113        normpath = os.path.normpath(in_path)
114        in_path = os.path.join(self.chroot, normpath[1:])
115
116        vvv("FETCH %s TO %s" % (in_path, out_path), host=self.chroot)
117        if not os.path.exists(in_path):
118            raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
119        try:
120            shutil.copyfile(in_path, out_path)
121        except shutil.Error:
122            traceback.print_exc()
123            raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
124        except IOError:
125            traceback.print_exc()
126            raise errors.AnsibleError("failed to transfer file to %s" % out_path)
127
128    def close(self):
129        ''' terminate the connection; nothing to do here '''
130        pass