2016-10-26 17:38:08 +00:00
|
|
|
#!/usr/bin/env python
|
2017-11-09 20:04:40 +00:00
|
|
|
# Copyright: (c) 2017, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2016-10-26 17:38:08 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
2017-01-13 00:20:25 +00:00
|
|
|
__metaclass__ = type
|
2016-10-26 17:38:08 +00:00
|
|
|
__requires__ = ['ansible']
|
2017-01-13 00:20:25 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
try:
|
|
|
|
import pkg_resources
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
import fcntl
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import traceback
|
2017-06-06 08:26:25 +00:00
|
|
|
import errno
|
2017-11-09 20:04:40 +00:00
|
|
|
import json
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
from ansible import constants as C
|
2017-07-05 18:07:26 +00:00
|
|
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
2017-05-12 16:13:51 +00:00
|
|
|
from ansible.module_utils.six import PY3
|
2018-01-24 21:18:45 +00:00
|
|
|
from ansible.module_utils.six.moves import cPickle, StringIO
|
2017-11-14 20:51:14 +00:00
|
|
|
from ansible.module_utils.connection import Connection, ConnectionError, send_data, recv_data
|
2017-11-09 20:04:40 +00:00
|
|
|
from ansible.module_utils.service import fork_process
|
2016-10-26 17:38:08 +00:00
|
|
|
from ansible.playbook.play_context import PlayContext
|
2017-08-15 20:38:59 +00:00
|
|
|
from ansible.plugins.loader import connection_loader
|
2016-10-26 17:38:08 +00:00
|
|
|
from ansible.utils.path import unfrackpath, makedirs_safe
|
2017-03-21 03:08:02 +00:00
|
|
|
from ansible.utils.display import Display
|
2017-11-09 20:04:40 +00:00
|
|
|
from ansible.utils.jsonrpc import JsonRpcServer
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2016-11-30 21:26:49 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
class ConnectionProcess(object):
|
2016-10-26 17:38:08 +00:00
|
|
|
'''
|
2017-11-09 20:04:40 +00:00
|
|
|
The connection process wraps around a Connection object that manages
|
|
|
|
the connection to a remote device that persists over the playbook
|
2016-10-26 17:38:08 +00:00
|
|
|
'''
|
2017-12-15 04:51:56 +00:00
|
|
|
def __init__(self, fd, play_context, socket_path, original_path, ansible_playbook_pid=None):
|
2016-10-26 17:38:08 +00:00
|
|
|
self.play_context = play_context
|
2017-11-09 20:04:40 +00:00
|
|
|
self.socket_path = socket_path
|
|
|
|
self.original_path = original_path
|
2017-03-21 02:26:18 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
self.fd = fd
|
|
|
|
self.exception = None
|
2016-11-30 21:26:49 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
self.srv = JsonRpcServer()
|
|
|
|
self.sock = None
|
2017-03-14 14:31:02 +00:00
|
|
|
|
2017-11-22 15:30:06 +00:00
|
|
|
self.connection = None
|
2017-12-15 04:51:56 +00:00
|
|
|
self._ansible_playbook_pid = ansible_playbook_pid
|
2017-11-22 15:30:06 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
def start(self):
|
|
|
|
try:
|
|
|
|
messages = list()
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
messages.append('control socket path is %s' % self.socket_path)
|
|
|
|
|
|
|
|
# If this is a relative path (~ gets expanded later) then plug the
|
|
|
|
# key's path on to the directory we originally came from, so we can
|
|
|
|
# find it now that our cwd is /
|
|
|
|
if self.play_context.private_key_file and self.play_context.private_key_file[0] not in '~/':
|
|
|
|
self.play_context.private_key_file = os.path.join(self.original_path, self.play_context.private_key_file)
|
2017-12-15 04:51:56 +00:00
|
|
|
self.connection = connection_loader.get(self.play_context.connection, self.play_context, '/dev/null',
|
|
|
|
ansible_playbook_pid=self._ansible_playbook_pid)
|
2017-11-16 18:49:57 +00:00
|
|
|
self.connection.set_options()
|
2017-11-09 20:04:40 +00:00
|
|
|
self.connection._connect()
|
2017-11-22 15:30:06 +00:00
|
|
|
self.connection._socket_path = self.socket_path
|
2017-11-09 20:04:40 +00:00
|
|
|
self.srv.register(self.connection)
|
2018-02-13 15:59:37 +00:00
|
|
|
messages.extend(sys.stdout.getvalue().splitlines())
|
2017-11-09 20:04:40 +00:00
|
|
|
messages.append('connection to remote device started successfully')
|
|
|
|
|
|
|
|
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
self.sock.bind(self.socket_path)
|
|
|
|
self.sock.listen(1)
|
|
|
|
messages.append('local domain socket listeners started successfully')
|
|
|
|
except Exception as exc:
|
|
|
|
result['error'] = to_text(exc)
|
|
|
|
result['exception'] = traceback.format_exc()
|
|
|
|
finally:
|
|
|
|
result['messages'] = messages
|
|
|
|
self.fd.write(json.dumps(result))
|
|
|
|
self.fd.close()
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
2017-11-22 15:30:06 +00:00
|
|
|
while self.connection.connected:
|
2017-06-06 08:26:25 +00:00
|
|
|
signal.signal(signal.SIGALRM, self.connect_timeout)
|
|
|
|
signal.signal(signal.SIGTERM, self.handler)
|
2016-10-26 17:38:08 +00:00
|
|
|
signal.alarm(C.PERSISTENT_CONNECT_TIMEOUT)
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
self.exception = None
|
|
|
|
(s, addr) = self.sock.accept()
|
2017-06-06 08:26:25 +00:00
|
|
|
signal.alarm(0)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
signal.signal(signal.SIGALRM, self.command_timeout)
|
2016-10-26 17:38:08 +00:00
|
|
|
while True:
|
|
|
|
data = recv_data(s)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
signal.alarm(self.connection._play_context.timeout)
|
|
|
|
resp = self.srv.handle_request(data)
|
2016-11-30 21:26:49 +00:00
|
|
|
signal.alarm(0)
|
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
send_data(s, to_bytes(resp))
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
s.close()
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
except Exception as e:
|
2017-06-06 08:26:25 +00:00
|
|
|
# socket.accept() will raise EINTR if the socket.close() is called
|
2017-11-09 20:04:40 +00:00
|
|
|
if hasattr(e, 'errno'):
|
|
|
|
if e.errno != errno.EINTR:
|
|
|
|
self.exception = traceback.format_exc()
|
|
|
|
else:
|
|
|
|
self.exception = traceback.format_exc()
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
finally:
|
|
|
|
# when done, close the connection properly and cleanup
|
|
|
|
# the socket file so it can be recreated
|
2017-06-06 08:26:25 +00:00
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def connect_timeout(self, signum, frame):
|
2017-08-01 17:45:45 +00:00
|
|
|
display.display('persistent connection idle timeout triggered, timeout value is %s secs' % C.PERSISTENT_CONNECT_TIMEOUT, log_only=True)
|
2017-06-06 08:26:25 +00:00
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def command_timeout(self, signum, frame):
|
2017-08-01 17:45:45 +00:00
|
|
|
display.display('command timeout triggered, timeout value is %s secs' % self.play_context.timeout, log_only=True)
|
2017-06-06 08:26:25 +00:00
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def handler(self, signum, frame):
|
|
|
|
display.display('signal handler called with signal %s' % signum, log_only=True)
|
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def shutdown(self):
|
2017-11-09 20:04:40 +00:00
|
|
|
""" Shuts down the local domain socket
|
|
|
|
"""
|
2017-11-22 15:30:06 +00:00
|
|
|
if os.path.exists(self.socket_path):
|
|
|
|
try:
|
|
|
|
if self.sock:
|
|
|
|
self.sock.close()
|
|
|
|
if self.connection:
|
|
|
|
self.connection.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
if os.path.exists(self.socket_path):
|
|
|
|
os.remove(self.socket_path)
|
|
|
|
setattr(self.connection, '_socket_path', None)
|
|
|
|
setattr(self.connection, '_connected', False)
|
2017-06-06 08:26:25 +00:00
|
|
|
display.display('shutdown complete', log_only=True)
|
|
|
|
|
2017-08-17 19:04:43 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
def main():
|
2017-11-09 20:04:40 +00:00
|
|
|
""" Called to initiate the connect to the remote device
|
|
|
|
"""
|
|
|
|
rc = 0
|
|
|
|
result = {}
|
|
|
|
messages = list()
|
|
|
|
socket_path = None
|
|
|
|
|
2017-05-12 16:13:51 +00:00
|
|
|
# Need stdin as a byte stream
|
|
|
|
if PY3:
|
|
|
|
stdin = sys.stdin.buffer
|
|
|
|
else:
|
|
|
|
stdin = sys.stdin
|
2017-01-13 00:20:25 +00:00
|
|
|
|
2018-01-24 21:18:45 +00:00
|
|
|
# Note: update the below log capture code after Display.display() is refactored.
|
|
|
|
saved_stdout = sys.stdout
|
|
|
|
sys.stdout = StringIO()
|
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
try:
|
|
|
|
# read the play context data via stdin, which means depickling it
|
2017-05-12 16:13:51 +00:00
|
|
|
cur_line = stdin.readline()
|
|
|
|
init_data = b''
|
2017-11-09 20:04:40 +00:00
|
|
|
|
2017-05-12 16:13:51 +00:00
|
|
|
while cur_line.strip() != b'#END_INIT#':
|
2017-05-13 01:04:48 +00:00
|
|
|
if cur_line == b'':
|
2017-05-12 16:13:51 +00:00
|
|
|
raise Exception("EOF found before init data was complete")
|
2016-10-26 17:38:08 +00:00
|
|
|
init_data += cur_line
|
2017-05-12 16:13:51 +00:00
|
|
|
cur_line = stdin.readline()
|
2017-11-09 20:04:40 +00:00
|
|
|
|
2017-05-13 01:04:48 +00:00
|
|
|
if PY3:
|
|
|
|
pc_data = cPickle.loads(init_data, encoding='bytes')
|
|
|
|
else:
|
|
|
|
pc_data = cPickle.loads(init_data)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
play_context = PlayContext()
|
|
|
|
play_context.deserialize(pc_data)
|
2018-02-13 15:59:37 +00:00
|
|
|
display.verbosity = play_context.verbosity
|
2017-06-06 08:26:25 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
except Exception as e:
|
2017-11-09 20:04:40 +00:00
|
|
|
rc = 1
|
|
|
|
result.update({
|
|
|
|
'error': to_text(e),
|
|
|
|
'exception': traceback.format_exc()
|
|
|
|
})
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
ssh = connection_loader.get('ssh', class_only=True)
|
2017-12-15 04:51:56 +00:00
|
|
|
ansible_playbook_pid = sys.argv[1]
|
|
|
|
cp = ssh._create_control_path(play_context.remote_addr, play_context.port, play_context.remote_user, play_context.connection, ansible_playbook_pid)
|
2017-11-09 20:04:40 +00:00
|
|
|
|
|
|
|
# create the persistent connection dir if need be and create the paths
|
|
|
|
# which we will be using later
|
|
|
|
tmp_path = unfrackpath(C.PERSISTENT_CONTROL_PATH_DIR)
|
|
|
|
makedirs_safe(tmp_path)
|
|
|
|
|
|
|
|
lock_path = unfrackpath("%s/.ansible_pc_lock" % tmp_path)
|
|
|
|
socket_path = unfrackpath(cp % dict(directory=tmp_path))
|
|
|
|
|
|
|
|
# if the socket file doesn't exist, spin up the daemon process
|
|
|
|
lock_fd = os.open(lock_path, os.O_RDWR | os.O_CREAT, 0o600)
|
|
|
|
fcntl.lockf(lock_fd, fcntl.LOCK_EX)
|
|
|
|
|
|
|
|
if not os.path.exists(socket_path):
|
|
|
|
messages.append('local domain socket does not exist, starting it')
|
|
|
|
original_path = os.getcwd()
|
|
|
|
r, w = os.pipe()
|
|
|
|
pid = fork_process()
|
|
|
|
|
|
|
|
if pid == 0:
|
|
|
|
try:
|
|
|
|
os.close(r)
|
|
|
|
wfd = os.fdopen(w, 'w')
|
2017-12-15 04:51:56 +00:00
|
|
|
process = ConnectionProcess(wfd, play_context, socket_path, original_path, ansible_playbook_pid)
|
2017-11-09 20:04:40 +00:00
|
|
|
process.start()
|
2017-11-16 18:49:57 +00:00
|
|
|
except Exception:
|
2017-11-09 20:04:40 +00:00
|
|
|
messages.append(traceback.format_exc())
|
|
|
|
rc = 1
|
|
|
|
|
|
|
|
fcntl.lockf(lock_fd, fcntl.LOCK_UN)
|
|
|
|
os.close(lock_fd)
|
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
process.run()
|
|
|
|
|
|
|
|
sys.exit(rc)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
else:
|
|
|
|
os.close(w)
|
|
|
|
rfd = os.fdopen(r, 'r')
|
|
|
|
data = json.loads(rfd.read())
|
|
|
|
messages.extend(data.pop('messages'))
|
|
|
|
result.update(data)
|
2016-11-30 21:26:49 +00:00
|
|
|
|
2017-11-09 20:04:40 +00:00
|
|
|
else:
|
|
|
|
messages.append('found existing local domain socket, using it!')
|
2017-11-14 20:51:14 +00:00
|
|
|
conn = Connection(socket_path)
|
|
|
|
pc_data = to_text(init_data)
|
|
|
|
try:
|
|
|
|
messages.extend(conn.update_play_context(pc_data))
|
|
|
|
except Exception as exc:
|
|
|
|
# Only network_cli has update_play context, so missing this is
|
|
|
|
# not fatal e.g. netconf
|
2017-11-15 20:40:32 +00:00
|
|
|
if isinstance(exc, ConnectionError) and getattr(exc, 'code', None) == -32601:
|
2017-11-14 20:51:14 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
result.update({
|
|
|
|
'error': to_text(exc),
|
|
|
|
'exception': traceback.format_exc()
|
|
|
|
})
|
2016-11-30 21:26:49 +00:00
|
|
|
|
2018-01-24 21:18:45 +00:00
|
|
|
messages.append(sys.stdout.getvalue())
|
2017-11-09 20:04:40 +00:00
|
|
|
result.update({
|
|
|
|
'messages': messages,
|
|
|
|
'socket_path': socket_path
|
|
|
|
})
|
2016-10-26 17:38:08 +00:00
|
|
|
|
2018-01-24 21:18:45 +00:00
|
|
|
sys.stdout = saved_stdout
|
2017-11-09 20:04:40 +00:00
|
|
|
if 'exception' in result:
|
|
|
|
rc = 1
|
|
|
|
sys.stderr.write(json.dumps(result))
|
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
sys.stdout.write(json.dumps(result))
|
2016-11-30 21:26:49 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
sys.exit(rc)
|
|
|
|
|
2018-02-13 15:59:37 +00:00
|
|
|
|
2016-10-26 17:38:08 +00:00
|
|
|
if __name__ == '__main__':
|
2017-03-21 03:08:02 +00:00
|
|
|
display = Display()
|
2016-10-26 17:38:08 +00:00
|
|
|
main()
|