From 5af635977ef7f1164a0aa5784ff1bf0992a9d7b2 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Mon, 21 Nov 2016 16:51:42 +0000 Subject: [PATCH] Add ability to execute against a remote chroot In some situations it may be useful to execute a role or set of tasks against a remote chroot. This patch aims to add that capability. Change-Id: Icebec389807aa8782f97a95f63687eb5606f7b22 --- connection/ssh.py | 20 +++++++++++++++++++- strategy/linear.py | 12 +++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/connection/ssh.py b/connection/ssh.py index c7419d8..0ba54e4 100644 --- a/connection/ssh.py +++ b/connection/ssh.py @@ -47,20 +47,38 @@ class Connection(SSH.Connection): self.args = args self.kwargs = kwargs self.vars = self._play_context._attributes['vars'] + self.chroot_path = self.vars.get('chroot_path') self.container_name = self.vars.get('container_name') self.physical_host = self.vars.get('physical_host') self.physical_hostname = self.vars.get('physical_hostname') - if self._container_check(): + if self._container_check() or self._chroot_check(): self.host = self._play_context.remote_addr = self.physical_host def _exec_command(self, cmd, in_data=None, sudoable=True): """run a command on the remote host.""" + if self._container_check(): lxc_command = 'lxc-attach --name %s' % self.container_name cmd = '%s -- %s' % (lxc_command, cmd) + if self._chroot_check(): + chroot_command = 'chroot %s' % self.chroot_path + cmd = '%s %s' % (chroot_command, cmd) + return super(Connection, self)._exec_command(cmd, in_data, sudoable) + def _chroot_check(self): + if self.chroot_path: + SSH.display.vvv(u'chroot_path: "%s"' % self.chroot_path) + if self.physical_hostname: + SSH.display.vvv( + u'physical_hostname: "%s"' % self.physical_hostname + ) + SSH.display.vvv(u'chroot confirmed') + return True + + return False + def _container_check(self): if self.container_name: SSH.display.vvv(u'container_name: "%s"' % self.container_name) diff --git a/strategy/linear.py b/strategy/linear.py index ca4bc38..18d89af 100644 --- a/strategy/linear.py +++ b/strategy/linear.py @@ -94,8 +94,8 @@ class StrategyModule(LINEAR.StrategyModule): def _queue_task(self, host, task, task_vars, play_context): """Queue a task to be sent to the worker. - Modify the playbook_context to support adding attributes for LXC - containers. + Modify the playbook_context to support adding attributes for remote + LXC containers or remote chroots. """ templar = LINEAR.Templar(loader=self._loader, variables=task_vars) if not self._check_when(host, task, templar, task_vars): @@ -104,7 +104,7 @@ class StrategyModule(LINEAR.StrategyModule): _play_context = copy.deepcopy(play_context) _vars = _play_context._attributes['vars'] if task.delegate_to: - # If a task uses delegation change teh play_context + # If a task uses delegation change the play_context # to use paramiko with pipelining disabled for this # one task on its collection of hosts. if _play_context.pipelining: @@ -148,6 +148,12 @@ class StrategyModule(LINEAR.StrategyModule): if container_name: _vars['container_name'] = container_name + chroot_path = _vars.get('chroot_path') + if not chroot_path: + chroot_path = task_vars.get('chroot_path') + if chroot_path: + _vars['chroot_path'] = chroot_path + return super(StrategyModule, self)._queue_task( host, task,