From c2caa65570286b247e495ba621816359e84e195a Mon Sep 17 00:00:00 2001 From: Drew Freiberger Date: Tue, 31 Jan 2017 09:52:21 -0700 Subject: [PATCH] Added actions status and cleanup Closes-Bug: 1688019 Change-Id: I9cddaa6b18991080ef91be68af761caa046a47c6 --- actions.yaml | 9 +++++++ actions/actions.py | 50 +++++++++++++++++++++++++++++++++++++-- actions/cleanup | 1 + actions/status | 1 + tests/basic_deployment.py | 18 ++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 120000 actions/cleanup create mode 120000 actions/status diff --git a/actions.yaml b/actions.yaml index 92bbce7..dfd5ce1 100644 --- a/actions.yaml +++ b/actions.yaml @@ -3,3 +3,12 @@ pause: from this unit to another unit in the hacluster resume: descrpition: Take hacluster unit out of standby mode +status: + description: Return cluster and resource status +cleanup: + description: Trigger cluster resource cleanup + params: + resource: + default: "all" + type: string + description: Resource name to cleanup diff --git a/actions/actions.py b/actions/actions.py index a624446..b128f2d 100755 --- a/actions/actions.py +++ b/actions/actions.py @@ -16,8 +16,15 @@ import sys import os +import subprocess +import traceback sys.path.append('hooks/') -from charmhelpers.core.hookenv import action_fail +from charmhelpers.core.hookenv import ( + action_fail, + action_get, + action_set, + log, +) from utils import ( pause_unit, resume_unit, @@ -37,7 +44,46 @@ def resume(args): resume_unit() -ACTIONS = {"pause": pause, "resume": resume} +def status(args): + """Display status of cluster resources. + Includes inactive resources in results.""" + cmd = ['crm', 'status', '--inactive'] + + try: + result = subprocess.check_output(cmd) + action_set({'result': result}) + except subprocess.CalledProcessError as e: + log("ERROR: Failed call to crm resource status. " + "output: {}. return-code: {}".format(e.output, e.returncode)) + log(traceback.format_exc()) + action_set({'result': ''}) + action_fail("failed to get cluster status") + + +def cleanup(args): + """Cleanup an/all hacluster resource(s). + Optional arg "resource=res_xyz_abc" """ + resource_name = (action_get("resource")).lower() + if resource_name == 'all': + cmd = ['crm_resource', '-C'] + else: + cmd = ['crm', 'resource', 'cleanup', resource_name] + + try: + subprocess.check_call(cmd) + action_set({'result': 'success'}) + except subprocess.CalledProcessError as e: + log("ERROR: Failed call to crm resource cleanup for {}. " + "output: {}. return-code: {}".format(resource_name, e.output, + e.returncode)) + log(traceback.format_exc()) + action_set({'result': 'failure'}) + action_fail("failed to cleanup crm resource " + "'{}'".format(resource_name)) + + +ACTIONS = {"pause": pause, "resume": resume, + "status": status, "cleanup": cleanup} def main(args): diff --git a/actions/cleanup b/actions/cleanup new file mode 120000 index 0000000..405a394 --- /dev/null +++ b/actions/cleanup @@ -0,0 +1 @@ +actions.py \ No newline at end of file diff --git a/actions/status b/actions/status new file mode 120000 index 0000000..405a394 --- /dev/null +++ b/actions/status @@ -0,0 +1 @@ +actions.py \ No newline at end of file diff --git a/tests/basic_deployment.py b/tests/basic_deployment.py index 3e7606e..d56561d 100644 --- a/tests/basic_deployment.py +++ b/tests/basic_deployment.py @@ -243,6 +243,24 @@ class HAClusterBasicDeployment(OpenStackAmuletDeployment): assert output == expected, 'maintenance-mode is: %s, expected: %s' \ % (output, expected) + def test_900_action_cleanup(self): + """The services can be cleaned up. """ + u.log.debug('Checking cleanup action...') + unit = self.hacluster_sentry + + assert u.status_get(unit)[0] == "active" + + action_id = u.run_action(unit, "cleanup") + assert u.wait_on_action(action_id), "Cleanup (all) action failed." + assert u.status_get(unit)[0] == "active" + + params = {'resource': 'res_ks_haproxy'} + action_id = u.run_action(unit, "cleanup", params) + assert u.wait_on_action(action_id), "Cleanup action w/resource failed." + assert u.status_get(unit)[0] == "active" + + u.log.debug('OK') + def test_910_pause_and_resume(self): """The services can be paused and resumed. """ u.log.debug('Checking pause and resume actions...')