Add explicit substitution of relative paths on the remote host just in case the connection programs do something unexpected.

This commit is contained in:
Toshio Kuratomi 2015-10-03 17:24:57 -07:00
commit 567deb5ac2
4 changed files with 78 additions and 27 deletions

View file

@ -22,6 +22,7 @@ __metaclass__ = type
import distutils.spawn
import os
import os.path
import subprocess
import traceback
@ -67,8 +68,6 @@ class Connection(ConnectionBase):
return cmd
def list_jails(self):
# FIXME: cwd= needs to be set to the basedir of the playbook, which
# should come from loader, but is not in the connection plugins
p = subprocess.Popen([self.jls_cmd, '-q', 'name'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -78,8 +77,6 @@ class Connection(ConnectionBase):
return stdout.split()
def get_jail_path(self):
# FIXME: cwd= needs to be set to the basedir of the playbook, which
# should come from loader, but is not in the connection plugins
p = subprocess.Popen([self.jls_cmd, '-j', self.jail, '-q', 'path'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -107,8 +104,6 @@ class Connection(ConnectionBase):
local_cmd = [self.jexec_cmd, self.jail, executable, '-c', cmd]
self._display.vvv("EXEC %s" % (local_cmd), host=self.jail)
# FIXME: cwd= needs to be set to the basedir of the playbook, which
# should come from loader, but is not in the connection plugins
p = subprocess.Popen(local_cmd, shell=False, stdin=stdin,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -130,11 +125,26 @@ class Connection(ConnectionBase):
stdout, stderr = p.communicate(in_data)
return (p.returncode, stdout, stderr)
def _prefix_login_path(self, remote_path):
''' Make sure that we put files into a standard path
If a path is relative, then we need to choose where to put it.
ssh chooses $HOME but we aren't guaranteed that a home dir will
exist in any given chroot. So for now we're choosing "/" instead.
This also happens to be the former default.
Can revisit using $HOME instead if it's a problem
'''
if not remote_path.startswith(os.path.sep):
remote_path = os.path.join(os.path.sep, remote_path)
return os.path.normpath(remote_path)
def put_file(self, in_path, out_path):
''' transfer a file from local to jail '''
super(Connection, self).put_file(in_path, out_path)
self._display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.jail)
out_path = self._prefix_login_path(out_path)
try:
with open(in_path, 'rb') as in_file:
try:
@ -156,6 +166,7 @@ class Connection(ConnectionBase):
super(Connection, self).fetch_file(in_path, out_path)
self._display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.jail)
in_path = self._prefix_login_path(in_path)
try:
p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE))
except OSError: