diff options
Diffstat (limited to 'heartbeat/dummypy.in')
-rwxr-xr-x | heartbeat/dummypy.in | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/heartbeat/dummypy.in b/heartbeat/dummypy.in new file mode 100755 index 0000000..3e2ff09 --- /dev/null +++ b/heartbeat/dummypy.in @@ -0,0 +1,164 @@ +#!@PYTHON@ -tt +# - *- coding: utf- 8 - *- +# +# +# Dummy Python OCF RA. Does nothing except track its own state. +# Use it only as a testing tool or example for how to write +# a resource agent in python. +# +# Copyright (c) 2020 SUSE LLC, Pablo Bravo (pablo.bravo@suse.com) +# All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of version 2 of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# +# Further, this software is distributed without any warranty that it is +# free of the rightful claim of any third person regarding infringement +# or the like. Any license provided herein, whether implied or +# otherwise, applies only to this software file. Patent licenses, if +# any, provided herein do not apply to combinations of this program with +# other software, or any other product whatsoever. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. +# + +####################################################################### + +import os +import sys + +OCF_FUNCTIONS_DIR = os.environ.get( + "OCF_FUNCTIONS_DIR", + "%s/lib/heartbeat" % os.environ.get("OCF_ROOT")) +sys.path.append(OCF_FUNCTIONS_DIR) + +import ocf # noqa: E402 + +PARAM_HA_RSCTMP = ocf.get_parameter("HA_RSCTMP") +PARAM_INSTANCE = ocf.get_parameter("OCF_RESOURCE_INSTANCE") +OCF_RESKEY_state_default = '{}/dummypy-{}.state'.format(PARAM_HA_RSCTMP, PARAM_INSTANCE) +OCF_RESKEY_fake_default = "dummy" + +LONG_DESC = '''This is a Dummy Resource Agent. It does absolutely nothing +except keep track of whether its running or not. +Its purpose in life is for testing and to serve as a template for RA writers. + +NB: Please pay attention to the timeouts specified in the actions +section below. They should be meaningful for the kind of resource +the agent manages. They should be the minimum advised timeouts, +but they shouldn't/cannot cover _all_ possible resource +instances. So, try to be neither overly generous nor too stingy, +but moderate. The minimum timeouts should never be below 10 seconds.''' + +SHORT_DESC = 'Example stateless resource agent' + + +def touch(file_name): + with open(file_name, 'a'): + os.utime(file_name, None) + + +def dummy_start(state, fake): + rc = dummy_monitor(state, fake) + if rc == ocf.OCF_SUCCESS: + return ocf.OCF_SUCCESS + touch(state) + + +def dummy_stop(state, fake): + rc = dummy_monitor(state, fake) + if rc == ocf.OCF_SUCCESS: + os.remove(state) + return ocf.OCF_SUCCESS + + +def dummy_monitor(state, fake): + try: + if os.path.isfile(state): + return ocf.OCF_SUCCESS + + if (not ocf.is_probe()) and (ocf.OCF_ACTION == "monitor"): + ocf.ocf_exit_reason("No process state file found") + + return ocf.OCF_NOT_RUNNING + except: # noqa: E722 + return ocf.OCF_ERR_GENERIC + + +def dummy_validate(state, fake): + state_dir = os.path.dirname(state) + file_test = '{}/{}'.format(state_dir if state_dir != "" else ".", PARAM_INSTANCE) + + try: + touch(file_test) + except: # noqa: E722 + ocf.ocf_exit_reason('State file "{}" is not writable'.format(state)) + return ocf.OCF_ERR_ARGS + + os.remove(file_test) + return ocf.OCF_SUCCESS + + +def dummy_migrate_to(state, fake): + INSTANCE = ocf.OCF_RESOURCE_INSTANCE + TARGET = ocf.get_parameter("CRM_meta_migrate_target") + ocf.logger.info('Migrating {} to {}.'.format(INSTANCE, TARGET)) + dummy_stop(state, fake) + + +def dummy_migrate_from(state, fake): + INSTANCE = ocf.OCF_RESOURCE_INSTANCE + SOURCE = ocf.get_parameter("CRM_meta_migrate_source") + ocf.logger.info('Migrating {} from {}.'.format(INSTANCE, SOURCE)) + dummy_start(state, fake) + + +def dummy_reload(state, fake): + ocf.logger.info('Reloading {} ...'.format(ocf.OCF_RESOURCE_INSTANCE)) + + +def dummy_validate_all(state, fake): + dummy_validate(state, fake) + + +def main(): + dummy_agent = ocf.Agent('dummypy', SHORT_DESC, LONG_DESC) + + dummy_agent.add_parameter( + name="state", + shortdesc="State file", + longdesc="Location to store the resource state in.", + content_type="string", + unique=True, + default=ocf.get_parameter("state_default") + ) + + dummy_agent.add_parameter( + name="fake", + shortdesc="Fake attribute that can be changed to cause a reload", + longdesc="Fake attribute that can be changed to cause a reload", + content_type="string", + unique=False, + default=ocf.get_parameter("fake_default") + ) + + dummy_agent.add_action(name="start", timeout=20, handler=dummy_start) + dummy_agent.add_action(name="stop", timeout=20, handler=dummy_stop) + dummy_agent.add_action(name="monitor", timeout=20, handler=dummy_monitor, interval=10, depth=0) # noqa: E501 + dummy_agent.add_action(name="reload", timeout=20, handler=dummy_reload) + dummy_agent.add_action(name="migrate_to", timeout=20, handler=dummy_migrate_to) # noqa: E501 + dummy_agent.add_action(name="migrate_from", timeout=20, handler=dummy_migrate_from) # noqa: E501 + dummy_agent.add_action(name="validate-all", timeout=20, handler=dummy_validate_all) # noqa: E501 + + dummy_agent.run() + + +if __name__ == "__main__": + main() |