From 0e6971671d732833c5a87c9149baf60f24f51b52 Mon Sep 17 00:00:00 2001 From: Alberto Donato Date: Wed, 19 Aug 2015 09:22:21 +0300 Subject: [PATCH] Move actions to action.py --- .coveragerc | 1 + actions/actions.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++ actions/pause | 20 +---------------- actions/resume | 20 +---------------- 4 files changed, 57 insertions(+), 38 deletions(-) create mode 100755 actions/actions.py mode change 100755 => 120000 actions/pause mode change 100755 => 120000 actions/resume diff --git a/.coveragerc b/.coveragerc index 972ccd41..3e345e68 100644 --- a/.coveragerc +++ b/.coveragerc @@ -4,3 +4,4 @@ exclude_lines = if __name__ == .__main__.: include= hooks/keystone_* + actions/actions.py diff --git a/actions/actions.py b/actions/actions.py new file mode 100755 index 00000000..b3031fce --- /dev/null +++ b/actions/actions.py @@ -0,0 +1,54 @@ +import sys +import os + +from charmhelpers.core.host import service_pause, service_resume +from charmhelpers.core.hookenv import action_fail, status_set + +from hooks.keystone_utils import services + + +def pause(args): + """Pause all the swift services. + + @raises Exception if any services fail to stop + """ + for service in services(): + stopped = service_pause(service) + if not stopped: + raise Exception("{} didn't stop cleanly.".format(service)) + status_set( + "maintenance", "Paused. Use 'resume' action to resume normal service.") + + +def resume(args): + """Resume all the swift services. + + @raises Exception if any services fail to start + """ + for service in services(): + started = service_resume(service) + if not started: + raise Exception("{} didn't start cleanly.".format(service)) + status_set("active", "") + + +# A dictionary of all the defined actions to callables (which take +# parsed arguments). +ACTIONS = {"pause": pause, "resume": resume} + + +def main(args): + action_name = os.path.basename(args[0]) + try: + action = ACTIONS[action_name] + except KeyError: + return "Action %s undefined" % action_name + else: + try: + action(args) + except Exception as e: + action_fail(str(e)) + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/actions/pause b/actions/pause deleted file mode 100755 index 8a43572d..00000000 --- a/actions/pause +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/python - -import sys - -from charmhelpers.core import hookenv, host - -from hooks.keystone_utils import services - - -def pause_service(service): - result = host.service_pause(service) - if not result: - hookenv.action_fail("Failed to pause service {}.".format(service)) - return result - -if __name__ == "__main__": - for service in services(): - if not pause_service(service): - sys.exit(-1) diff --git a/actions/pause b/actions/pause new file mode 120000 index 00000000..405a394e --- /dev/null +++ b/actions/pause @@ -0,0 +1 @@ +actions.py \ No newline at end of file diff --git a/actions/resume b/actions/resume deleted file mode 100755 index 1a7a844a..00000000 --- a/actions/resume +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/python - -import sys - -from charmhelpers.core import hookenv, host - -from hooks.keystone_utils import services - - -def resume_service(service): - result = host.service_resume(service) - if not result: - hookenv.action_fail("Failed to resume service {}.".format(service)) - return result - -if __name__ == "__main__": - for service in services(): - if not resume_service(service): - sys.exit(-1) diff --git a/actions/resume b/actions/resume new file mode 120000 index 00000000..405a394e --- /dev/null +++ b/actions/resume @@ -0,0 +1 @@ +actions.py \ No newline at end of file