summaryrefslogtreecommitdiffstats
path: root/heartbeat/dummypy.in
blob: 3e2ff09dc682e98d19fe886aaaad81e0ea60df31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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()