From df97e808c6f537ddc5332a0856c8a306a58291a2 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 17 Mar 2021 17:46:27 -0400 Subject: [PATCH] Wire up client retrieval of remoteconfig remoteconfig can now watch for completion and return data to client --- confluent_server/confluent/runansible.py | 33 +++++++++++++++++++++-- confluent_server/confluent/selfservice.py | 11 +++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/confluent_server/confluent/runansible.py b/confluent_server/confluent/runansible.py index bd608c49..f57b9a5b 100644 --- a/confluent_server/confluent/runansible.py +++ b/confluent_server/confluent/runansible.py @@ -21,6 +21,7 @@ except ImportError: pass import eventlet import eventlet.green.subprocess as subprocess +import json import msgpack import os import struct @@ -43,6 +44,34 @@ class PlayRunner(object): self.results = [] return avail + def dump_text(self): + retinfo = self.dump_dict() + textout = '' + for result in retinfo['results']: + textout += 'TASK [{}] *******************************\n'.format( + result['task_name']) + for warning in result['warnings']: + textout += '[WARNING]: ' + warning + '\n' + if 'errorinfo' in result: + textout += '{} => {}\n'.format(result['state'], + result['errorinfo']) + else: + if result['changed']: + textout += 'changed\n' + else: + textout += result['state'] + '\n' + textout += '\n' + return textout + + def dump_json(self): + return json.dumps(self.dump_dict()) + + def dump_dict(self): + return { + 'complete': self.complete, + 'results': self.get_available_results() + } + def _really_run_playbooks(self): with open(os.devnull, 'w+') as devnull: targnodes = ','.join(self.nodes) @@ -69,7 +98,7 @@ def run_playbooks(playfiles, nodes): runner._start_playbooks() -def print_result(result, state, collector): +def print_result(result, state, collector=None): output = { 'task_name': result.task_name, 'changed': result._result['changed'], @@ -104,7 +133,7 @@ if __name__ == '__main__': print_result(result, 'UNREACHABLE', self) def v2_runner_on_ok(self, result, *args, **kwargs): - print_result(result, 'ok', self) + print_result(result, 'ok') def v2_runner_on_failed(self, result, *args, **kwargs): print_result(result, 'FAILED', self) diff --git a/confluent_server/confluent/selfservice.py b/confluent_server/confluent/selfservice.py index 0c5dc94b..5e4cee4c 100644 --- a/confluent_server/confluent/selfservice.py +++ b/confluent_server/confluent/selfservice.py @@ -282,7 +282,16 @@ def handle_request(env, start_response): yield '' return elif env['PATH_INFO'].startswith('/self/remoteconfig/status'): - scriptcat = env['PATH_INFO'].replace('/self/remoteconfig/', '') + rst = runansible.running_status.get(nodename, None) + if not rst: + start_response('204 Not Running', ()) + yield '' + return + start_response('200 OK', ()) + if rst.complete: + del runansible.running_status[nodename] + yield rst.dump_text() + return elif env['PATH_INFO'].startswith('/self/scriptlist/'): scriptcat = env['PATH_INFO'].replace('/self/scriptlist/', '') slist, _ = get_scriptlist(