-
Notifications
You must be signed in to change notification settings - Fork 1
/
pydummy
executable file
·150 lines (118 loc) · 4.76 KB
/
pydummy
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
#!/usr/bin/env python
# This file is part of python-ocf.
# Copyright (C) 2015 Tiger Computing Ltd. <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import ocf
import os
class DummyAgent(ocf.ResourceAgent):
"""
Example stateless resource agent
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 defined
below. They should be meaningful for the kind of resource the agent
manages. They should be the minimum advised timeouts, but they should not /
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.
This is a Python re-implementation of the ``ocf:heartbeat:Dummy`` resource
agent using the *python-ocf* module (:mod:`ocf`).
"""
VERSION = '0.10'
state = ocf.Parameter(
unique=True, shortdesc='State file',
longdesc='Location to store the resource state in.',
default="{tmp}/Dummy-{inst}.state".format(
tmp=ocf.env.rsctmp, inst=ocf.env.resource_instance))
fake = ocf.Parameter(
shortdesc='Fake attribute that can be changed to cause a reload',
longdesc='Fake attribute that can be changed to cause a reload',
default='dummy')
@ocf.Action()
def start(self):
ocf.log.debug('Starting...')
ret = self.monitor()
# Check whether we are already running
if ret == ocf.OCF_SUCCESS:
return ret
# Touch our state file
with open(self.state, 'a'):
os.utime(self.state, None)
return ocf.OCF_SUCCESS
@ocf.Action()
def stop(self):
ocf.log.debug('Stopping...')
ret = self.monitor()
# If we are "running", remove our state file
if ret == ocf.OCF_SUCCESS:
os.remove(self.state)
return ocf.OCF_SUCCESS
@ocf.Action(timeout=20, depth=0, interval=10)
def monitor(self):
ocf.log.debug('Monitoring...')
# Monitor _MUST!_ differentiate correctly between running (SUCCESS),
# failed (ERROR) or _cleanly_ stopped (NOT RUNNING). That is THREE
# states, not just yes/no.
# If our state file exists, we are "running"
if os.path.isfile(self.state):
return ocf.OCF_SUCCESS
# Something has gone horribly wrong
# if False:
# return ocf.OCF_ERR_GENERIC
return ocf.OCF_NOT_RUNNING
def validate_all(self):
ocf.log.debug('Validating...')
ret = super(DummyAgent, self).validate_all()
if ret != ocf.OCF_SUCCESS:
return ret
# Is our state directory writable?
state_dir = os.path.dirname(self.state)
test_file = os.path.join(state_dir, str(os.getpid()))
try:
# Try to create a file in the directory
with open(test_file, 'a'):
os.utime(test_file, None)
except OSError as e:
ocf.log.debug('OSError while opening test_file', e)
# If this fails, our arguments are incorrect
return ocf.OCF_ERR_ARGS
else:
# It worked, return success
return ocf.OCF_SUCCESS
finally:
# Clean up the test file on the way out
try:
os.remove(test_file)
except OSError:
pass
@ocf.Action()
def reload(self):
ocf.log.info('Reloading...')
return ocf.OCF_SUCCESS
@ocf.Action()
def migrate_to(self):
ocf.log.info("Migrating to {tgt}.".format(
tgt=ocf.env.reskey.get('CRM_meta_migrate_target')))
return self.stop()
@ocf.Action()
def migrate_from(self):
ocf.log.info("Migrating from {src}.".format(
src=ocf.env.reskey.get('CRM_meta_migrate_source')))
return self.start()
if __name__ == "__main__":
DummyAgent.main()
# vi:tw=0:wm=0:nowrap:ai:et:ts=8:softtabstop=4:shiftwidth=4