You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
9.2 KiB
Python
264 lines
9.2 KiB
Python
7 years ago
|
# Copyright 2017, David Wilson
|
||
|
#
|
||
|
# Redistribution and use in source and binary forms, with or without
|
||
|
# modification, are permitted provided that the following conditions are met:
|
||
|
#
|
||
7 years ago
|
# 1. Redistributions of source code must retain the above copyright notice,
|
||
|
# this list of conditions and the following disclaimer.
|
||
7 years ago
|
#
|
||
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||
|
# this list of conditions and the following disclaimer in the documentation
|
||
|
# and/or other materials provided with the distribution.
|
||
|
#
|
||
|
# 3. Neither the name of the copyright holder nor the names of its contributors
|
||
|
# may be used to endorse or promote products derived from this software without
|
||
|
# specific prior written permission.
|
||
|
#
|
||
7 years ago
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||
7 years ago
|
|
||
7 years ago
|
from __future__ import absolute_import
|
||
7 years ago
|
import logging
|
||
7 years ago
|
import os
|
||
7 years ago
|
import sys
|
||
7 years ago
|
import time
|
||
7 years ago
|
|
||
7 years ago
|
import ansible.errors
|
||
7 years ago
|
import ansible.plugins.connection
|
||
7 years ago
|
|
||
7 years ago
|
import mitogen.unix
|
||
7 years ago
|
from mitogen.utils import cast
|
||
7 years ago
|
|
||
7 years ago
|
import ansible_mitogen.helpers
|
||
7 years ago
|
from ansible_mitogen.services import ContextService
|
||
7 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
LOG = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
7 years ago
|
class Connection(ansible.plugins.connection.ConnectionBase):
|
||
7 years ago
|
#: mitogen.master.Router for this worker.
|
||
7 years ago
|
router = None
|
||
7 years ago
|
|
||
|
#: mitogen.master.Context representing the parent Context, which is
|
||
|
#: presently always the master process.
|
||
|
parent = None
|
||
|
|
||
|
#: mitogen.master.Context used to communicate with the target user account.
|
||
7 years ago
|
context = None
|
||
|
|
||
7 years ago
|
#: Only sudo is supported for now.
|
||
7 years ago
|
become_methods = ['sudo']
|
||
7 years ago
|
|
||
|
#: Set by the constructor according to whichever connection type this
|
||
|
#: connection should emulate. We emulate the original connection type to
|
||
|
#: work around artificial limitations in e.g. the synchronize action, which
|
||
|
#: hard-codes 'local' and 'ssh' as the only allowable connection types.
|
||
|
transport = None
|
||
7 years ago
|
|
||
7 years ago
|
#: Set to 'ansible_python_interpreter' by on_action_run().
|
||
|
python_path = None
|
||
|
|
||
|
#: Set to 'ansible_sudo_exe' by on_action_run().
|
||
|
sudo_path = None
|
||
|
|
||
|
#: Set to 'ansible_ssh_timeout' by on_action_run().
|
||
|
connect_timeout = None
|
||
|
|
||
7 years ago
|
def __init__(self, play_context, new_stdin, original_transport):
|
||
7 years ago
|
assert 'MITOGEN_LISTENER_PATH' in os.environ, (
|
||
|
'The "mitogen" connection plug-in may only be instantiated '
|
||
7 years ago
|
'by the "mitogen" strategy plug-in.'
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
self.original_transport = original_transport
|
||
|
self.transport = original_transport
|
||
|
super(Connection, self).__init__(play_context, new_stdin)
|
||
|
|
||
7 years ago
|
def on_action_run(self, task_vars):
|
||
|
"""
|
||
|
Invoked by ActionModuleMixin to indicate a new task is about to start
|
||
|
executing. We use the opportunity to grab relevant bits from the
|
||
|
task-specific data.
|
||
|
"""
|
||
|
self.connect_timeout = task_vars.get(
|
||
|
'ansible_ssh_timeout',
|
||
|
None
|
||
|
)
|
||
|
self.python_path = task_vars.get(
|
||
|
'ansible_python_interpreter',
|
||
|
'/usr/bin/python'
|
||
|
)
|
||
|
self.sudo_path = task_vars.get(
|
||
|
'ansible_sudo_exe',
|
||
|
'sudo'
|
||
|
)
|
||
|
|
||
7 years ago
|
@property
|
||
|
def connected(self):
|
||
7 years ago
|
return self.router is not None
|
||
7 years ago
|
|
||
7 years ago
|
def _connect_local(self):
|
||
7 years ago
|
"""
|
||
|
Fetch a reference to the local() Context from ContextService in the
|
||
|
master process.
|
||
|
"""
|
||
|
return mitogen.service.call(self.parent, ContextService.handle, cast({
|
||
7 years ago
|
'method': 'local',
|
||
7 years ago
|
}))
|
||
7 years ago
|
|
||
7 years ago
|
def _connect_ssh(self):
|
||
7 years ago
|
"""
|
||
|
Fetch a reference to an SSH Context matching the play context from
|
||
|
ContextService in the master process.
|
||
|
"""
|
||
|
return mitogen.service.call(
|
||
|
self.parent,
|
||
|
ContextService.handle,
|
||
|
cast({
|
||
|
'method': 'ssh',
|
||
|
'hostname': self._play_context.remote_addr,
|
||
|
'username': self._play_context.remote_user,
|
||
|
'password': self._play_context.password,
|
||
|
'port': self._play_context.port,
|
||
7 years ago
|
'python_path': self.python_path,
|
||
7 years ago
|
'ssh_path': self._play_context.ssh_executable,
|
||
7 years ago
|
'connect_timeout': self.connect_timeout,
|
||
7 years ago
|
})
|
||
|
)
|
||
7 years ago
|
|
||
7 years ago
|
def _connect_sudo(self, via=None, python_path=None):
|
||
7 years ago
|
"""
|
||
|
Fetch a reference to a sudo Context matching the play context from
|
||
|
ContextService in the master process.
|
||
|
|
||
|
:param via:
|
||
|
Parent Context of the sudo Context. For Ansible, this should always
|
||
|
be a Context returned by _connect_ssh().
|
||
|
"""
|
||
|
return mitogen.service.call(self.parent, ContextService.handle, cast({
|
||
7 years ago
|
'method': 'sudo',
|
||
|
'username': self._play_context.become_user,
|
||
|
'password': self._play_context.password,
|
||
7 years ago
|
'python_path': python_path or self.python_path,
|
||
|
'sudo_path': self.sudo_path,
|
||
7 years ago
|
'via': via,
|
||
|
}))
|
||
|
|
||
|
def _connect(self):
|
||
7 years ago
|
"""
|
||
|
Establish a connection to the master process's UNIX listener socket,
|
||
|
constructing a mitogen.master.Router to communicate with the master,
|
||
|
and a mitogen.master.Context to represent it.
|
||
|
|
||
|
Depending on the original transport we should emulate, trigger one of
|
||
|
the _connect_*() service calls defined above to cause the master
|
||
|
process to establish the real connection on our behalf, or return a
|
||
|
reference to the existing one.
|
||
|
"""
|
||
7 years ago
|
if self.connected:
|
||
|
return
|
||
|
|
||
7 years ago
|
path = os.environ['MITOGEN_LISTENER_PATH']
|
||
7 years ago
|
self.router, self.parent = mitogen.unix.connect(path)
|
||
|
|
||
7 years ago
|
if self.original_transport == 'local':
|
||
7 years ago
|
if not self._play_context.become:
|
||
|
self.context = self._connect_local()
|
||
|
else:
|
||
|
self.context = self._connect_sudo(python_path=sys.executable)
|
||
7 years ago
|
else:
|
||
7 years ago
|
self.host = self._connect_ssh()
|
||
|
if not self._play_context.become:
|
||
|
self.context = self.host
|
||
|
else:
|
||
|
self.context = self._connect_sudo(via=self.host)
|
||
7 years ago
|
|
||
7 years ago
|
def close(self):
|
||
|
"""
|
||
|
Arrange for the mitogen.master.Router running in the worker to
|
||
|
gracefully shut down, and wait for shutdown to complete. Safe to call
|
||
|
multiple times.
|
||
|
"""
|
||
|
if self.router:
|
||
|
self.router.broker.shutdown()
|
||
|
self.router.broker.join()
|
||
|
self.router = None
|
||
|
|
||
7 years ago
|
def call_async(self, func, *args, **kwargs):
|
||
7 years ago
|
"""
|
||
|
Start a function call to the target.
|
||
|
|
||
|
:returns:
|
||
|
mitogen.core.Receiver that receives the function call result.
|
||
|
"""
|
||
7 years ago
|
self._connect()
|
||
7 years ago
|
return self.context.call_async(func, *args, **kwargs)
|
||
|
|
||
|
def call(self, func, *args, **kwargs):
|
||
7 years ago
|
"""
|
||
|
Start and wait for completion of a function call in the target.
|
||
|
|
||
|
:raises mitogen.core.CallError:
|
||
|
The function call failed.
|
||
|
:returns:
|
||
|
Function return value.
|
||
|
"""
|
||
7 years ago
|
t0 = time.time()
|
||
|
try:
|
||
|
return self.call_async(func, *args, **kwargs).get().unpickle()
|
||
|
finally:
|
||
|
LOG.debug('Call %s%r took %d ms', func.func_name, args,
|
||
|
1000 * (time.time() - t0))
|
||
7 years ago
|
|
||
7 years ago
|
def exec_command(self, cmd, in_data='', sudoable=True):
|
||
|
"""
|
||
|
Implement exec_command() by calling the corresponding
|
||
|
ansible_mitogen.helpers function in the target.
|
||
|
|
||
|
:param str cmd:
|
||
|
Shell command to execute.
|
||
|
:param bytes in_data:
|
||
|
Data to supply on ``stdin`` of the process.
|
||
|
:returns:
|
||
|
(return code, stdout bytes, stderr bytes)
|
||
|
"""
|
||
7 years ago
|
return self.call(ansible_mitogen.helpers.exec_command,
|
||
|
cast(cmd), cast(in_data))
|
||
7 years ago
|
|
||
|
def fetch_file(self, in_path, out_path):
|
||
7 years ago
|
"""
|
||
|
Implement fetch_file() by calling the corresponding
|
||
|
ansible_mitogen.helpers function in the target.
|
||
|
|
||
|
:param str in_path:
|
||
|
Remote filesystem path to read.
|
||
|
:param str out_path:
|
||
|
Local filesystem path to write.
|
||
|
"""
|
||
7 years ago
|
output = self.call(ansible_mitogen.helpers.read_path,
|
||
|
cast(in_path))
|
||
7 years ago
|
ansible_mitogen.helpers.write_path(out_path, output)
|
||
7 years ago
|
|
||
|
def put_file(self, in_path, out_path):
|
||
7 years ago
|
"""
|
||
|
Implement put_file() by caling the corresponding
|
||
|
ansible_mitogen.helpers function in the target.
|
||
|
|
||
|
:param str in_path:
|
||
|
Local filesystem path to read.
|
||
|
:param str out_path:
|
||
|
Remote filesystem path to write.
|
||
|
"""
|
||
7 years ago
|
self.call(ansible_mitogen.helpers.write_path, cast(out_path),
|
||
|
ansible_mitogen.helpers.read_path(in_path))
|