2017-06-06 08:26:25 +00:00
|
|
|
# (c) 2017 Red Hat Inc.
|
2016-10-26 17:38:08 +00:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-08-14 18:47:20 +00:00
|
|
|
import re
|
2016-10-26 17:38:08 +00:00
|
|
|
import os
|
|
|
|
import pty
|
|
|
|
import subprocess
|
|
|
|
|
2017-07-05 18:07:26 +00:00
|
|
|
from ansible.module_utils._text import to_bytes, to_text
|
2017-05-12 16:13:51 +00:00
|
|
|
from ansible.module_utils.six.moves import cPickle
|
2016-10-26 17:38:08 +00:00
|
|
|
from ansible.plugins.connection import ConnectionBase
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def _do_it(self, action):
|
|
|
|
|
|
|
|
master, slave = pty.openpty()
|
|
|
|
p = subprocess.Popen(["ansible-connection"], stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
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')
|
|
|
|
stdin.write(to_bytes(action))
|
|
|
|
stdin.write(b'\n\n')
|
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-08-14 18:47:20 +00:00
|
|
|
return (p.returncode, stdout, stderr)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
def exec_command(self, cmd, in_data=None, sudoable=True):
|
|
|
|
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
2017-08-14 18:47:20 +00:00
|
|
|
return self._do_it('EXEC: ' + cmd)
|
2016-10-26 17:38:08 +00:00
|
|
|
|
|
|
|
def put_file(self, in_path, out_path):
|
|
|
|
super(Connection, self).put_file(in_path, out_path)
|
|
|
|
self._do_it('PUT: %s %s' % (in_path, out_path))
|
|
|
|
|
|
|
|
def fetch_file(self, in_path, out_path):
|
|
|
|
super(Connection, self).fetch_file(in_path, out_path)
|
|
|
|
self._do_it('FETCH: %s %s' % (in_path, out_path))
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._connected = False
|
2017-06-06 08:26:25 +00:00
|
|
|
|
|
|
|
def run(self):
|
2017-07-05 18:07:26 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
2017-08-14 18:47:20 +00:00
|
|
|
socket_path = None
|
|
|
|
rc, out, err = self._do_it('RUN:')
|
2017-08-15 15:36:20 +00:00
|
|
|
match = re.search(br"#SOCKET_PATH#: (\S+)", out)
|
2017-08-14 18:47:20 +00:00
|
|
|
if match:
|
|
|
|
socket_path = to_text(match.group(1).strip(), errors='surrogate_or_strict')
|
|
|
|
|
|
|
|
return socket_path
|