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

https://github.com/ajanthanm/ansible · Python · 130 lines · 78 code · 25 blank · 27 comment · 19 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. import distutils.spawn
  19. import traceback
  20. import os
  21. import shutil
  22. import subprocess
  23. from ansible import errors
  24. from ansible import utils
  25. from ansible.callbacks import vvv
  26. class Connection(object):
  27. ''' Local chroot based connections '''
  28. def __init__(self, runner, host, port, *args, **kwargs):
  29. self.chroot = host
  30. self.has_pipelining = False
  31. if os.geteuid() != 0:
  32. raise errors.AnsibleError("chroot connection requires running as root")
  33. # we're running as root on the local system so do some
  34. # trivial checks for ensuring 'host' is actually a chroot'able dir
  35. if not os.path.isdir(self.chroot):
  36. raise errors.AnsibleError("%s is not a directory" % self.chroot)
  37. chrootsh = os.path.join(self.chroot, 'bin/sh')
  38. if not utils.is_executable(chrootsh):
  39. raise errors.AnsibleError("%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot)
  40. self.chroot_cmd = distutils.spawn.find_executable('chroot')
  41. if not self.chroot_cmd:
  42. raise errors.AnsibleError("chroot command not found in PATH")
  43. self.runner = runner
  44. self.host = host
  45. # port is unused, since this is local
  46. self.port = port
  47. def connect(self, port=None):
  48. ''' connect to the chroot; nothing to do here '''
  49. vvv("THIS IS A LOCAL CHROOT DIR", host=self.chroot)
  50. return self
  51. def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', in_data=None, su=None, su_user=None):
  52. ''' run a command on the chroot '''
  53. if su or su_user:
  54. raise errors.AnsibleError("Internal Error: this module does not support running commands via su")
  55. if in_data:
  56. raise errors.AnsibleError("Internal Error: this module does not support optimized module pipelining")
  57. # We enter chroot as root so sudo stuff can be ignored
  58. if executable:
  59. local_cmd = [self.chroot_cmd, self.chroot, executable, '-c', cmd]
  60. else:
  61. local_cmd = '%s "%s" %s' % (self.chroot_cmd, self.chroot, cmd)
  62. vvv("EXEC %s" % (local_cmd), host=self.chroot)
  63. p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring),
  64. cwd=self.runner.basedir,
  65. stdin=subprocess.PIPE,
  66. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  67. stdout, stderr = p.communicate()
  68. return (p.returncode, '', stdout, stderr)
  69. def put_file(self, in_path, out_path):
  70. ''' transfer a file from local to chroot '''
  71. if not out_path.startswith(os.path.sep):
  72. out_path = os.path.join(os.path.sep, out_path)
  73. normpath = os.path.normpath(out_path)
  74. out_path = os.path.join(self.chroot, normpath[1:])
  75. vvv("PUT %s TO %s" % (in_path, out_path), host=self.chroot)
  76. if not os.path.exists(in_path):
  77. raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
  78. try:
  79. shutil.copyfile(in_path, out_path)
  80. except shutil.Error:
  81. traceback.print_exc()
  82. raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
  83. except IOError:
  84. traceback.print_exc()
  85. raise errors.AnsibleError("failed to transfer file to %s" % out_path)
  86. def fetch_file(self, in_path, out_path):
  87. ''' fetch a file from chroot to local '''
  88. if not in_path.startswith(os.path.sep):
  89. in_path = os.path.join(os.path.sep, in_path)
  90. normpath = os.path.normpath(in_path)
  91. in_path = os.path.join(self.chroot, normpath[1:])
  92. vvv("FETCH %s TO %s" % (in_path, out_path), host=self.chroot)
  93. if not os.path.exists(in_path):
  94. raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
  95. try:
  96. shutil.copyfile(in_path, out_path)
  97. except shutil.Error:
  98. traceback.print_exc()
  99. raise errors.AnsibleError("failed to copy: %s and %s are the same" % (in_path, out_path))
  100. except IOError:
  101. traceback.print_exc()
  102. raise errors.AnsibleError("failed to transfer file to %s" % out_path)
  103. def close(self):
  104. ''' terminate the connection; nothing to do here '''
  105. pass