Browse code

Finishing off porting of chroot connection plugin

James Cammarata authored on 2015/08/27 05:41:05
Showing 1 changed files
... ...
@@ -20,14 +20,16 @@ from __future__ import (absolute_import, division, print_function)
20 20
 __metaclass__ = type
21 21
 
22 22
 import distutils.spawn
23
-import traceback
24 23
 import os
25 24
 import shlex
26 25
 import subprocess
26
+import traceback
27
+
28
+from ansible import constants as C
27 29
 from ansible.errors import AnsibleError
28
-from ansible import utils
30
+from ansible.plugins.connections import ConnectionBase
31
+from ansible.utils.path import is_executable
29 32
 from ansible.utils.unicode import to_bytes
30
-import ansible.constants as C
31 33
 
32 34
 
33 35
 class Connection(ConnectionBase):
... ...
@@ -51,13 +53,17 @@ class Connection(ConnectionBase):
51 51
             raise AnsibleError("%s is not a directory" % self.chroot)
52 52
 
53 53
         chrootsh = os.path.join(self.chroot, 'bin/sh')
54
-        if not utils.is_executable(chrootsh):
54
+        if not is_executable(chrootsh):
55 55
             raise AnsibleError("%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot)
56 56
 
57 57
         self.chroot_cmd = distutils.spawn.find_executable('chroot')
58 58
         if not self.chroot_cmd:
59 59
             raise AnsibleError("chroot command not found in PATH")
60 60
 
61
+    @property
62
+    def transport(self):
63
+        ''' used to identify this connection object '''
64
+        return 'chroot'
61 65
 
62 66
     def _connect(self, port=None):
63 67
         ''' connect to the chroot; nothing to do here '''
... ...
@@ -86,8 +92,8 @@ class Connection(ConnectionBase):
86 86
         return the process's exit code immediately.
87 87
         '''
88 88
 
89
-        if sudoable and self.runner.become and self.runner.become_method not in self.become_methods_supported:
90
-            raise AnsibleError("Internal Error: this module does not support running commands via %s" % self.runner.become_method)
89
+        if sudoable and self._play_context.become and self._play_context.become_method not in self.become_methods_supported:
90
+            raise AnsibleError("Internal Error: this module does not support running commands via %s" % self._play_context.become_method)
91 91
 
92 92
         if in_data:
93 93
             raise AnsibleError("Internal Error: this module does not support optimized module pipelining")
... ...
@@ -96,8 +102,9 @@ class Connection(ConnectionBase):
96 96
         local_cmd = self._generate_cmd(executable, cmd)
97 97
 
98 98
         self._display.vvv("EXEC %s" % (local_cmd), host=self.chroot)
99
+        # FIXME: cwd= needs to be set to the basedir of the playbook, which
100
+        #        should come from loader, but is not in the connection plugins
99 101
         p = subprocess.Popen(local_cmd, shell=False,
100
-                             cwd=self.runner.basedir,
101 102
                              stdin=stdin,
102 103
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
103 104
 
... ...
@@ -119,7 +126,7 @@ class Connection(ConnectionBase):
119 119
         try:
120 120
             with open(in_path, 'rb') as in_file:
121 121
                 try:
122
-                    p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), None, stdin=in_file)
122
+                    p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, self.BUFSIZE), None, stdin=in_file)
123 123
                 except OSError:
124 124
                     raise AnsibleError("chroot connection requires dd command in the chroot")
125 125
                 try:
... ...
@@ -138,16 +145,16 @@ class Connection(ConnectionBase):
138 138
         self._display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.chroot)
139 139
 
140 140
         try:
141
-            p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE), None)
141
+            p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, self.BUFSIZE), None)
142 142
         except OSError:
143 143
             raise AnsibleError("chroot connection requires dd command in the chroot")
144 144
 
145 145
         with open(out_path, 'wb+') as out_file:
146 146
             try:
147
-                chunk = p.stdout.read(BUFSIZE)
147
+                chunk = p.stdout.read(self.BUFSIZE)
148 148
                 while chunk:
149 149
                     out_file.write(chunk)
150
-                    chunk = p.stdout.read(BUFSIZE)
150
+                    chunk = p.stdout.read(self.BUFSIZE)
151 151
             except:
152 152
                 traceback.print_exc()
153 153
                 raise AnsibleError("failed to transfer file %s to %s" % (in_path, out_path))