From 27cce305e68b4cc4aeaae1ee1d47d09e9f9d4abe Mon Sep 17 00:00:00 2001 From: slohse Date: Fri, 23 Dec 2016 00:34:47 +0100 Subject: [PATCH] iocage connector that wraps around jail connector. (#17986) Added iocage connector that extends the jail connector. Uses iocage to translate iocage tags or UUIDs/partial UUIDs to the actual jail name and then uses the jail connector for actual functionality. --- lib/ansible/plugins/connection/iocage.py | 68 ++++++++++++++++++++++++ lib/ansible/plugins/connection/jail.py | 4 ++ 2 files changed, 72 insertions(+) create mode 100644 lib/ansible/plugins/connection/iocage.py diff --git a/lib/ansible/plugins/connection/iocage.py b/lib/ansible/plugins/connection/iocage.py new file mode 100644 index 0000000000..d6a960cb5c --- /dev/null +++ b/lib/ansible/plugins/connection/iocage.py @@ -0,0 +1,68 @@ +# based on jail.py (c) 2013, Michael Scherer +# (c) 2015, Toshio Kuratomi +# (c) 2016, Stephan Lohse +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import subprocess +from ansible.plugins.connection.jail import Connection as Jail + +from ansible.errors import AnsibleError + +try: + from __main__ import display +except ImportError: + from ansible.utils.display import Display + display = Display() + + +class Connection(Jail): + ''' Local iocage based connections ''' + + transport = 'iocage' + + def __init__(self, play_context, new_stdin, *args, **kwargs): + self.ioc_jail = play_context.remote_addr + + self.iocage_cmd = Jail._search_executable('iocage') + + jail_uuid = self.get_jail_uuid() + + kwargs[Jail.modified_jailname_key] = 'ioc-{}'.format(jail_uuid) + + display.vvv(u"Jail {iocjail} has been translated to {rawjail}".format( + iocjail=self.ioc_jail, rawjail=kwargs[Jail.modified_jailname_key]), + host=kwargs[Jail.modified_jailname_key]) + + super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) + + def get_jail_uuid(self): + p = subprocess.Popen([self.iocage_cmd, 'get', 'host_hostuuid', self.ioc_jail], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + + stdout, stderr = p.communicate() + # otherwise p.returncode would not be set + p.wait() + + if p.returncode != 0: + raise AnsibleError(u"iocage returned an error: {}".format(stdout)) + + return stdout.strip('\n') + diff --git a/lib/ansible/plugins/connection/jail.py b/lib/ansible/plugins/connection/jail.py index f6bd394ad7..5e54d95229 100644 --- a/lib/ansible/plugins/connection/jail.py +++ b/lib/ansible/plugins/connection/jail.py @@ -41,6 +41,8 @@ except ImportError: class Connection(ConnectionBase): ''' Local BSD Jail based connections ''' + modified_jailname_key = 'conn_jail_name' + transport = 'jail' # Pipelining may work. Someone needs to test by setting this to True and # having pipelining=True in their ansible.cfg @@ -54,6 +56,8 @@ class Connection(ConnectionBase): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) self.jail = self._play_context.remote_addr + if self.modified_jailname_key in kwargs : + self.jail = kwargs[self.modified_jailname_key] if os.geteuid() != 0: raise AnsibleError("jail connection requires running as root")