2017-11-09 20:04:40 +00:00
|
|
|
# 2017 Red Hat Inc.
|
2017-08-20 15:20:30 +00:00
|
|
|
# (c) 2017 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
2017-09-08 18:08:31 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
2017-08-20 15:20:30 +00:00
|
|
|
author: Ansible Core Team
|
|
|
|
connection: persistent
|
|
|
|
short_description: Use a persistent unix socket for connection
|
|
|
|
description:
|
|
|
|
- This is a helper plugin to allow making other connections persistent.
|
|
|
|
version_added: "2.3"
|
|
|
|
"""
|
2016-10-26 17:38:08 +00:00
|
|
|
import os
|
|
|
|
import pty
|
2017-11-09 20:04:40 +00:00
|
|
|
import json
|
2016-10-26 17:38:08 +00:00
|
|
|
import subprocess
|
2018-03-05 14:12:01 +00:00
|
|
|
import sys
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
from ansible import constants as C
|
2016-10-26 17:38:08 +00:00
|
|
|
from ansible.plugins.connection import ConnectionBase
|
2017-11-09 20:04:40 +00:00
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
from ansible.module_utils.six.moves import cPickle
|
|
|
|
from ansible.module_utils.connection import Connection as SocketConnection
|
|
|
|
from ansible.errors import AnsibleError
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
class Connection(ConnectionBase):
|
|
|
|
''' Local based connections '''
|
|
|
|
|
|
|
|
transport = 'persistent'
|
|
|
|
has_pipelining = False
|
|
|
|
|
|
|
|
def _connect(self):
|
|
|
|
self._connected = True
|
|
|
|
return self
|
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
def exec_command(self, cmd, in_data=None, sudoable=True):
|
|
|
|
display.vvvv('exec_command(), socket_path=%s' % self.socket_path, host=self._play_context.remote_addr)
|
|
|
|
connection = SocketConnection(self.socket_path)
|
|
|
|
out = connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
|
|
|
return 0, out, ''
|
|
|
|
|
|
|
|
def put_file(self, in_path, out_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def fetch_file(self, in_path, out_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._connected = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Returns the path of the persistent connection socket.
|
|
|
|
|
|
|
|
Attempts to ensure (within playcontext.timeout seconds) that the
|
|
|
|
socket path exists. If the path exists (or the timeout has expired),
|
|
|
|
returns the socket path.
|
|
|
|
"""
|
|
|
|
display.vvvv('starting connection from persistent connection plugin', host=self._play_context.remote_addr)
|
|
|
|
socket_path = self._start_connection()
|
|
|
|
display.vvvv('local domain socket path is %s' % socket_path, host=self._play_context.remote_addr)
|
|
|
|
setattr(self, '_socket_path', socket_path)
|
|
|
|
return socket_path
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
def _start_connection(self):
|
|
|
|
'''
|
|
|
|
Starts the persistent connection
|
|
|
|
'''
|
2016-10-26 17:38:08 +00:00
|
|
|
master, slave = pty.openpty()
|
2018-03-05 14:12:01 +00:00
|
|
|
|
|
|
|
python = sys.executable
|
2018-03-26 16:49:30 +00:00
|
|
|
|
|
|
|
def find_file_in_path(filename):
|
|
|
|
# Check $PATH first, followed by same directory as sys.argv[0]
|
|
|
|
paths = os.environ['PATH'].split(os.pathsep) + [os.path.dirname(sys.argv[0])]
|
|
|
|
for dirname in paths:
|
|
|
|
fullpath = os.path.join(dirname, filename)
|
|
|
|
if os.path.isfile(fullpath):
|
|
|
|
return fullpath
|
|
|
|
|
|
|
|
raise AnsibleError("Unable to find location of '%s'" % filename)
|
|
|
|
|
|
|
|
p = subprocess.Popen(
|
|
|
|
[python, find_file_in_path('ansible-connection'), to_text(os.getppid())],
|
|
|
|
stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
|
|
|
)
|
2016-10-26 17:38:08 +00:00
|
|
|
stdin = os.fdopen(master, 'wb', 0)
|
|
|
|
os.close(slave)
|
|
|
|
|
2017-05-12 16:13:51 +00:00
|
|
|
# Need to force a protocol that is compatible with both py2 and py3.
|
|
|
|
# That would be protocol=2 or less.
|
|
|
|
# Also need to force a protocol that excludes certain control chars as
|
|
|
|
# stdin in this case is a pty and control chars will cause problems.
|
|
|
|
# that means only protocol=0 will work.
|
|
|
|
src = cPickle.dumps(self._play_context.serialize(), protocol=0)
|
|
|
|
stdin.write(src)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
stdin.write(b'\n#END_INIT#\n')
|
2018-01-24 21:18:45 +00:00
|
|
|
stdin.flush()
|
2017-05-12 16:13:51 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
(stdout, stderr) = p.communicate()
|
2017-05-12 16:13:51 +00:00
|
|
|
stdin.close()
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
if p.returncode == 0:
|
|
|
|
result = json.loads(to_text(stdout, errors='surrogate_then_replace'))
|
|
|
|
else:
|
2018-03-05 14:12:01 +00:00
|
|
|
try:
|
|
|
|
result = json.loads(to_text(stderr, errors='surrogate_then_replace'))
|
2018-04-06 20:28:39 +00:00
|
|
|
except getattr(json.decoder, 'JSONDecodeError', ValueError):
|
|
|
|
# JSONDecodeError only available on Python 3.5+
|
2018-03-05 14:12:01 +00:00
|
|
|
result = {'error': to_text(stderr, errors='surrogate_then_replace')}
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
if 'messages' in result:
|
|
|
|
for msg in result.get('messages'):
|
|
|
|
display.vvvv('%s' % msg, host=self._play_context.remote_addr)
|
2017-07-05 18:07:26 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
if 'error' in result:
|
|
|
|
if self._play_context.verbosity > 2:
|
2018-03-05 14:12:01 +00:00
|
|
|
if result.get('exception'):
|
|
|
|
msg = "The full traceback is:\n" + result['exception']
|
|
|
|
display.display(msg, color=C.COLOR_ERROR)
|
2017-11-09 20:04:40 +00:00
|
|
|
raise AnsibleError(result['error'])
|
2017-08-14 18:47:20 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
return result['socket_path']
|