diff options
author | Ganesh Nalawade <ganesh634@gmail.com> | 2018-01-25 02:48:45 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-25 02:48:45 +0530 |
commit | 90cd87f950378c4350e925d6b930d64bd4860618 (patch) | |
tree | 9c51a67cea744cb975f29a2e802cde6286d0a6f1 /bin | |
parent | bbedb9aac458915989a5f0e930dc3cbb4decdbda (diff) | |
download | ansible-90cd87f950378c4350e925d6b930d64bd4860618.zip |
Fix debug logs failing with persistent connection (#33049)
* Fix debug logs failing with persistent connection
Fixes #33047
* As debug logs are written on stdout, it interrupts
the communication between ansible-connection(background)
process and main process. To avoid this add a string similar
to exactly identify the response string.
* Remove unwanted code in ansible-connection
* Fix review comments
* Fix spurious log emitted on ansible-connection stdout issue
* ansible-connection which runs as a background process sends a
json string (contains response received from remote device)
to foreground ansible-playbook process over stdout.
* If in case debug flag is enabled the connection_loader api
invoked from ansible-connection `ssh = connection_loader.get('ssh', class_only=True)`
results in emitting debug logs on stdout. This spurious log
interfere with the actual response and results in failure while
reading json string in ansible-playbook process
* To avoid this save stdout of ansible-connection and redirect it string
buffer to accumulate all the logs emitted by core API's
* Add these logs in `result['messages']` which is send a json string after reinstating saved stdout
* Remove unwanted code in ansible-connection
* Fix review comment
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/ansible-connection | 58 |
1 files changed, 7 insertions, 51 deletions
diff --git a/bin/ansible-connection b/bin/ansible-connection index 703352e49f..68425ff6ba 100755 --- a/bin/ansible-connection +++ b/bin/ansible-connection @@ -13,11 +13,9 @@ except Exception: import fcntl import os -import shlex import signal import socket import sys -import time import traceback import errno import json @@ -25,7 +23,7 @@ import json from ansible import constants as C from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.six import PY3 -from ansible.module_utils.six.moves import cPickle +from ansible.module_utils.six.moves import cPickle, StringIO from ansible.module_utils.connection import Connection, ConnectionError, send_data, recv_data from ansible.module_utils.service import fork_process from ansible.playbook.play_context import PlayContext @@ -154,54 +152,6 @@ class ConnectionProcess(object): setattr(self.connection, '_connected', False) display.display('shutdown complete', log_only=True) - def do_EXEC(self, data): - cmd = data.split(b'EXEC: ')[1] - return self.connection.exec_command(cmd) - - def do_PUT(self, data): - (op, src, dst) = shlex.split(to_native(data)) - return self.connection.fetch_file(src, dst) - - def do_FETCH(self, data): - (op, src, dst) = shlex.split(to_native(data)) - return self.connection.put_file(src, dst) - - def do_CONTEXT(self, data): - pc_data = data.split(b'CONTEXT: ', 1)[1] - - if PY3: - pc_data = cPickle.loads(pc_data, encoding='bytes') - else: - pc_data = cPickle.loads(pc_data) - - pc = PlayContext() - pc.deserialize(pc_data) - - try: - self.connection.update_play_context(pc) - except AttributeError: - pass - - return (0, 'ok', '') - - def do_RUN(self, data): - timeout = self.play_context.timeout - while bool(timeout): - if os.path.exists(self.socket_path): - break - time.sleep(1) - timeout -= 1 - socket_bytes = to_bytes(self.socket_path, errors='surrogate_or_strict') - return 0, b'\n#SOCKET_PATH#: %s\n' % socket_bytes, '' - - -def communicate(sock, data): - send_data(sock, data) - rc = int(recv_data(sock), 10) - stdout = recv_data(sock) - stderr = recv_data(sock) - return (rc, stdout, stderr) - def main(): """ Called to initiate the connect to the remote device @@ -217,6 +167,10 @@ def main(): else: stdin = sys.stdin + # Note: update the below log capture code after Display.display() is refactored. + saved_stdout = sys.stdout + sys.stdout = StringIO() + try: # read the play context data via stdin, which means depickling it cur_line = stdin.readline() @@ -308,11 +262,13 @@ def main(): 'exception': traceback.format_exc() }) + messages.append(sys.stdout.getvalue()) result.update({ 'messages': messages, 'socket_path': socket_path }) + sys.stdout = saved_stdout if 'exception' in result: rc = 1 sys.stderr.write(json.dumps(result)) |