Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active August 4, 2016 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudnull/9c798af2920ec1593b028be928c15513 to your computer and use it in GitHub Desktop.
Save cloudnull/9c798af2920ec1593b028be928c15513 to your computer and use it in GitHub Desktop.
# Copyright 2016, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# (c) 2016, Kevin Carter <kevin.carter@rackspace.com>
import imp
import os
# NOTICE(cloudnull): The connection plugin imported using the full path to the
# file because the linear strategy plugin is not importable.
import ansible.plugins.strategy as strategy
LINEAR = imp.load_source(
'ssh',
os.path.join(os.path.dirname(strategy.__file__), 'linear.py')
)
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.
"""
if 'physical_host' in task_vars:
vars = play_context._attributes['vars']
if 'physical_host' not in vars:
physical_host = self._inventory.get_host(
task_vars['physical_host']
)
vars['physical_host'] = physical_host.vars['ansible_ssh_host']
if 'container_name' in task_vars:
vars = play_context._attributes['vars']
if 'container_name' not in vars:
vars['container_name'] = task_vars['container_name']
return super(StrategyModule, self)._queue_task(
host,
task,
task_vars,
play_context
)
# Copyright 2016, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# (c) 2016, Kevin Carter <kevin.carter@rackspace.com>
import imp
import os
# NOTICE(cloudnull): The connection plugin imported using the full path to the
# file because the ssh connection plugin is not importable.
import ansible.plugins.connection as conn
SSH = imp.load_source(
'ssh',
os.path.join(os.path.dirname(conn.__file__), 'ssh.py')
)
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class Connection(SSH.Connection):
"""Transport options for LXC containers.
This transport option makes the assumption that the playbook context has
vars within it that contain "physical_host" which is the machine running a
given container and "container_name" which is the actual name of the
container. These options can be added into the playbook via vars set as
attributes or though the modification of the a given execution strategy to
set the attributes accordingly.
This plugin operates exactly the same way as the standard SSH plugin but
will pad pathing or add command syntax for lxc containers when a container
is detected at runtime.
"""
transport = 'ssh'
def __init__(self, *args, **kwargs):
super(Connection, self).__init__(*args, **kwargs)
self.args = args
self.kwargs = kwargs
self.vars = self._play_context._attributes['vars']
self.container_name = self.vars.get('container_name')
self.physical_host = self.vars.get('physical_host')
if self._container_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)
return super(Connection, self)._exec_command(cmd, in_data, sudoable)
def _container_check(self):
if self.container_name:
if self.physical_host:
if self.physical_host != self._play_context.remote_addr:
return True
return False
def _container_path_pad(self, path, fake_path=False):
args = (
'ssh',
self.host,
u"lxc-info --name %s --pid | awk '/PID:/ {print $2}'"
% self.container_name
)
returncode, stdout, _ = self._run(
self._build_command(*args),
in_data=None,
sudoable=False
)
if returncode == 0:
pad = os.path.join(
'/proc/%s/root' % stdout.strip(),
path.lstrip(os.sep)
)
display.vvv(
u'The path has been padded with the following to support a'
u' container rootfs: [ %s ]' % pad
)
return pad
else:
raise SSH.AnsibleError(
u'No valid container info was found for container "%s" Please'
u' check the state of the container.' % self.container_name
)
def fetch_file(self, in_path, out_path):
"""fetch a file from remote to local."""
if self._container_check():
in_path = self._container_path_pad(path=in_path)
return super(Connection, self).fetch_file(in_path, out_path)
def put_file(self, in_path, out_path):
"""transfer a file from local to remote."""
if self._container_check():
out_path = self._container_path_pad(path=out_path)
return super(Connection, self).put_file(in_path, out_path)
def close(self):
# If we have a persistent ssh connection (ControlPersist), we can ask it
# to stop listening. Otherwise, there's nothing to do here.
if self._connected and self._persistent:
cmd = self._build_command('ssh', '-O', 'stop', self.host)
cmd = map(SSH.to_bytes, cmd)
p = SSH.subprocess.Popen(cmd, stdin=SSH.subprocess.PIPE, stdout=SSH.subprocess.PIPE, stderr=SSH.subprocess.PIPE)
p.communicate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment