From 6072a49adb33f2bce3f48b9c7b8be7d96cf8501c Mon Sep 17 00:00:00 2001 From: liyuenan Date: Thu, 22 Dec 2016 16:59:08 +0800 Subject: Update ansible version JIRA: COMPASS-511 Change-Id: Id0e83c65a08c9f9036b5d3a597b905e8a7bde6e8 Signed-off-by: liyuenan --- deploy/status_callback.py | 100 +++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 49 deletions(-) (limited to 'deploy/status_callback.py') diff --git a/deploy/status_callback.py b/deploy/status_callback.py index 30dffd34..58072de2 100755 --- a/deploy/status_callback.py +++ b/deploy/status_callback.py @@ -1,86 +1,97 @@ import httplib import json -import sys -import logging +import sys # noqa:F401 +from ansible.plugins.callback import CallbackBase -def task_error(host, data): - logging.info("task_error: host=%s,data=%s" % (host, data)) + +def task_error(display, host, data): + display.display("task_error: host=%s,data=%s" % (host, data)) # if isinstance(data, dict): # invocation = data.pop('invocation', {}) - notify_host("localhost", host, "failed") + notify_host(display, "localhost", host, "failed") -class CallbackModule(object): +class CallbackModule(CallbackBase): """ logs playbook results, per host, in /var/log/ansible/hosts """ + CALLBACK_VERSION = 2.0 + CALLBACK_TYPE = 'notification' + CALLBACK_NAME = 'status_callback' + CALLBACK_NEEDS_WHITELIST = True + + def __init__(self): + super(CallbackModule, self).__init__() - def on_any(self, *args, **kwargs): + def v2_on_any(self, *args, **kwargs): pass - def runner_on_failed(self, host, res, ignore_errors=False): - task_error(host, res) + def v2_runner_on_failed(self, host, res, ignore_errors=False): + task_error(self._display, host, res) - def runner_on_ok(self, host, res): + def v2_runner_on_ok(self, host, res): pass - def runner_on_skipped(self, host, item=None): + def v2_runner_on_skipped(self, host, item=None): pass - def runner_on_unreachable(self, host, res): + def v2_runner_on_unreachable(self, host, res): pass - def runner_on_no_hosts(self): + def v2_runner_on_no_hosts(self): pass - def runner_on_async_poll(self, host, res, jid, clock): + def v2_runner_on_async_poll(self, host, res, jid, clock): pass - def runner_on_async_ok(self, host, res, jid): + def v2_runner_on_async_ok(self, host, res, jid): pass - def runner_on_async_failed(self, host, res, jid): - task_error(host, res) + def v2_runner_on_async_failed(self, host, res, jid): + task_error(self._display, host, res) - def playbook_on_start(self): + def v2_playbook_on_start(self): pass - def playbook_on_notify(self, host, handler): + def v2_playbook_on_notify(self, host, handler): pass - def playbook_on_no_hosts_matched(self): + def v2_playbook_on_no_hosts_matched(self): pass - def playbook_on_no_hosts_remaining(self): + def v2_playbook_on_no_hosts_remaining(self): pass - def playbook_on_task_start(self, name, is_conditional): + def v2_playbook_on_task_start(self, name, is_conditional): pass - def playbook_on_vars_prompt(self, varname, private=True, prompt=None, + def v2_playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None): # noqa pass - def playbook_on_setup(self): + def v2_playbook_on_setup(self): pass - def playbook_on_import_for_host(self, host, imported_file): + def v2_playbook_on_import_for_host(self, host, imported_file): pass - def playbook_on_not_import_for_host(self, host, missing_file): + def v2_playbook_on_not_import_for_host(self, host, missing_file): pass - def playbook_on_play_start(self, name): - pass + def v2_playbook_on_play_start(self, play): + self.play = play + self.loader = self.play.get_loader() + return - def playbook_on_stats(self, stats): - logging.info("playbook_on_stats enter") + def v2_playbook_on_stats(self, stats): + self._display.display("playbook_on_stats enter") + all_vars = self.play.get_variable_manager().get_vars(self.loader) + host_vars = all_vars["hostvars"] hosts = sorted(stats.processed.keys()) - host_vars = self.playbook.inventory.get_variables(hosts[0]) - cluster_name = host_vars['cluster_name'] + cluster_name = host_vars[hosts[0]]['cluster_name'] failures = False unreachable = False @@ -94,12 +105,12 @@ class CallbackModule(object): if failures or unreachable: for host in hosts: - notify_host("localhost", host, "error") + notify_host(self._display, "localhost", host, "error") return for host in hosts: clusterhost_name = host + "." + cluster_name - notify_host("localhost", clusterhost_name, "succ") + notify_host(self._display, "localhost", clusterhost_name, "succ") def raise_for_status(resp): @@ -123,7 +134,7 @@ def auth(conn): return json.loads(resp.read())["token"] -def notify_host(compass_host, host, status): +def notify_host(display, compass_host, host, status): if status == "succ": body = {"ready": True} url = "/api/clusterhosts/%s/state_internal" % host @@ -132,7 +143,7 @@ def notify_host(compass_host, host, status): host = host.strip("host") url = "/api/clusterhosts/%s/state" % host else: - logging.error("notify_host: host %s with status %s is not supported" + display.error("notify_host: host %s with status %s is not supported" % (host, status)) return @@ -142,26 +153,17 @@ def notify_host(compass_host, host, status): conn = httplib.HTTPConnection(compass_host, 80) token = auth(conn) headers["X-Auth-Token"] = token - logging.info("host=%s,url=%s,body=%s,headers=%s" % - (compass_host, url, json.dumps(body), headers)) + display.display("host=%s,url=%s,body=%s,headers=%s" % + (compass_host, url, json.dumps(body), headers)) conn.request("POST", url, json.dumps(body), headers) resp = conn.getresponse() try: raise_for_status(resp) - logging.info( + display.display( "notify host status success!!! status=%s, body=%s" % (resp.status, resp.read())) except Exception as e: - logging.error("http request failed %s" % str(e)) + display.error("http request failed %s" % str(e)) raise finally: conn.close() - -if __name__ == "__main__": - if len(sys.argv) != 3: - logging.error("params: host, status is need") - sys.exit(1) - - host = sys.argv[1] - status = sys.argv[2] - notify_host(host, status) -- cgit 1.2.3-korg