From 7fa96de883bda319c684acc91ecf5b3e33646e0a Mon Sep 17 00:00:00 2001 From: Liam Young Date: Thu, 20 Sep 2018 10:01:45 +0000 Subject: [PATCH] Add generic action code. Add generic action code that utilises charms_openstack.bus.discover() so that the top level charm does not need to make any changes to inherit generic actions. Change-Id: I8b3422b915e2477d936e5cdd2d883c6815487577 --- actions.yaml | 13 +++++ actions/os_actions.py | 72 ++++++++++++++++++++++++++-- actions/os_generic_actions.py | 89 ----------------------------------- actions/pause | 1 + actions/resume | 1 + tox.ini | 2 +- 6 files changed, 83 insertions(+), 95 deletions(-) create mode 100644 actions.yaml delete mode 100755 actions/os_generic_actions.py create mode 120000 actions/pause create mode 120000 actions/resume diff --git a/actions.yaml b/actions.yaml new file mode 100644 index 0000000..06aa0fd --- /dev/null +++ b/actions.yaml @@ -0,0 +1,13 @@ +"pause": + "description": | + Pause services. + If the deployment is clustered using the hacluster charm, the + corresponding hacluster unit on the node must first be paused as well. + Not doing so may lead to an interruption of service. +"resume": + "description": | + Resume services. + If the deployment is clustered using the hacluster charm, the + corresponding hacluster unit on the node must be resumed as well. +"debug": + "description": "Collect debug data" diff --git a/actions/os_actions.py b/actions/os_actions.py index 93c3af9..eebab2b 100755 --- a/actions/os_actions.py +++ b/actions/os_actions.py @@ -1,10 +1,72 @@ #!/usr/bin/env python3 +# Copyright 2016 Canonical Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -print("Please specialise this file in the top level charm: ") -print(""" +import os import sys -import os_generic_actions -import charm.openstack.CHARM_NAME # noqa +# Load modules from $CHARM_DIR/lib +sys.path.append('lib') -sys.exit(os_generic_actions.run_action(sys.argv))""") +from charms.layer import basic +basic.bootstrap_charm_deps() + +import charmhelpers.core.hookenv as hookenv +import charms_openstack.bus +import charms_openstack.charm + +charms_openstack.bus.discover() + + +def pause_action(*args): + """Run the pause action.""" + with charms_openstack.charm.provide_charm_instance() as charm_instance: + charm_instance.pause() + # Run _assess_status rather than assess_status so the call is not + # deferred. + charm_instance._assess_status() + + +def resume_action(*args): + """Run the resume action.""" + with charms_openstack.charm.provide_charm_instance() as charm_instance: + charm_instance.resume() + # Run _assess_status rather than assess_status so the call is not + # deferred. + charm_instance._assess_status() + + +# Actions to function mapping, to allow for illegal python action names that +# can map to a python function. +ACTIONS = { + "pause": pause_action, + "resume": resume_action, +} + + +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: + hookenv.action_fail(str(e)) + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/actions/os_generic_actions.py b/actions/os_generic_actions.py deleted file mode 100755 index 8eb8ac5..0000000 --- a/actions/os_generic_actions.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2016 Canonical Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os -import sys - -# Load modules from $CHARM_DIR/lib -sys.path.append('lib') - -from charms.layer import basic -basic.bootstrap_charm_deps() -basic.init_config_states() - -import charms.reactive as reactive -import charmhelpers.core.hookenv as hookenv -import charms_openstack.charm - -"""This module defines the generic OpenStack charm actions. To use these the -charm needs to call run_action having imported the charms charm class e.g. - -``` -#!/usr/bin/env python3 -import sys -import os_generic_actions - -import charm.openstack.aodh # noqa - -sys.exit(os_generic_actions.run_action(sys.argv)) -``` - -The above code can be placed in a file called *os_actions.py* symbolic links -with the action names can then be created. - -``` -lrwxrwxrwx 1 root root 13 Sep 18 06:45 pause -> os_actions.py -lrwxrwxrwx 1 root root 13 Sep 18 06:45 resume -> os_actions.py -``` - -""" - -def pause_action(*args): - """Run the pause action.""" - with charms_openstack.charm.provide_charm_instance() as charm_instance: - charm_instance.pause() - # Run _assess_status rather than assess_status so the call is not - # deferred. - charm_instance._assess_status() - - -def resume_action(*args): - """Run the resume action.""" - with charms_openstack.charm.provide_charm_instance() as charm_instance: - charm_instance.resume() - # Run _assess_status rather than assess_status so the call is not - # deferred. - charm_instance._assess_status() - - -# Actions to function mapping, to allow for illegal python action names that -# can map to a python function. -ACTIONS = { - "pause": pause_action, - "resume": resume_action, -} - - -def run_action(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: - hookenv.action_fail(str(e)) diff --git a/actions/pause b/actions/pause new file mode 120000 index 0000000..93db417 --- /dev/null +++ b/actions/pause @@ -0,0 +1 @@ +os_actions.py \ No newline at end of file diff --git a/actions/resume b/actions/resume new file mode 120000 index 0000000..93db417 --- /dev/null +++ b/actions/resume @@ -0,0 +1 @@ +os_actions.py \ No newline at end of file diff --git a/tox.ini b/tox.ini index ce0c797..04dcd0c 100644 --- a/tox.ini +++ b/tox.ini @@ -48,4 +48,4 @@ deps = -r{toxinidir}/test-requirements.txt commands = /bin/true [testenv:pep8] -commands = /bin/true +commands = flake8 --ignore=E402 actions/ reactive/