removal of chdir in controller (#57781)

* removal of chdir in controller

also ensure fetch/put uses relative to task cwd
This commit is contained in:
Brian Coca 2019-06-14 13:45:41 -04:00 committed by GitHub
parent de87b25a45
commit 342d77b32d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- removed chdir from action plugins when using local connection, moved into plugin itself to avoid future issues with threads.

View file

@ -1054,13 +1054,9 @@ class ActionBase(with_metaclass(ABCMeta, object)):
# Change directory to basedir of task for command execution when connection is local
if self._connection.transport == 'local':
cwd = os.getcwd()
os.chdir(to_bytes(self._loader.get_basedir()))
try:
self._connection.cwd = to_bytes(self._loader.get_basedir(), errors='surrogate_or_strict')
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
finally:
if self._connection.transport == 'local':
os.chdir(cwd)
# stdout and stderr may be either a file-like or a bytes object.
# Convert either one to a text type

View file

@ -39,6 +39,11 @@ class Connection(ConnectionBase):
transport = 'local'
has_pipelining = True
def __init__(self, *args, **kwargs):
super(Connection, self).__init__(*args, **kwargs)
self.cwd = None
def _connect(self):
''' connect to the local host; nothing to do here '''
@ -76,7 +81,8 @@ class Connection(ConnectionBase):
p = subprocess.Popen(
cmd,
shell=isinstance(cmd, (text_type, binary_type)),
executable=executable, # cwd=...
executable=executable,
cwd=self.cwd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@ -123,11 +129,19 @@ class Connection(ConnectionBase):
display.debug("done with local.exec_command()")
return (p.returncode, stdout, stderr)
def _ensure_abs(self, path):
if not os.path.isabs(path) and self.cwd is not None:
path = os.path.normpath(os.path.join(self.cwd, path))
return path
def put_file(self, in_path, out_path):
''' transfer a file from local to local '''
super(Connection, self).put_file(in_path, out_path)
in_path = self._ensure_abs(in_path)
out_path = self._ensure_abs(out_path)
display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._play_context.remote_addr)
if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')):
raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path)))