9
9
from parameter import Parameter, STRING, INTEGER, BOOLEAN
10
10
from errors import *
11
11
from xml.etree import ElementTree
12
from collections import defaultdict
17
def __getattr__(self, name):
17
class _ocf_reskeys(dict):
18
def __init__(self, params):
21
def __missing__(self, key):
24
return p.datatype.topython(os.environ[p.envname])
23
class _ocfenv(object):
25
self.instance = os.environ.get("OCF_RESOURCE_INSTANCE", "_test_instance")
26
if p.default is not None:
27
env.args[p.name] = p.default
31
__getattr__ = dict.__getitem__
33
class OCFEnvironment(object):
34
def __init__(self, params):
35
self.instance = os.environ.get("OCF_RESOURCE_INSTANCE", "__noinstance__")
36
self.tempdir = os.environ.get("HA_RSCTMP", "/tmp")
27
39
self.check_level = int(os.environ["OCF_CHECK_LEVEL"])
30
self.args = _dictobj()
43
self.params = _ocf_reskeys(params)
32
45
class ResourceAgent(object):
46
"""ResourceAgent is the main class for creating your OCF resource agent.
47
@name The name of your resource agent (`basename $0` in the shell script wrapper)
48
@version A version string (number?) for this agent. 1.0 would be a good start."""
33
49
def __init__(self, name, version):
35
51
self.version = version
38
54
self.validations = []
40
self.add_action("meta-data", {"timeout": "10s"}, self.meta_data, False)
41
self.add_action("validate-all", {"timeout": "10s"}, self.validate_all, False)
56
self.add_action("meta-data", {"timeout": "10s"}, self.meta_data)
57
self.add_action("validate-all", {"timeout": "10s"}, self.validate_all)
42
58
self.add_validation(self.validate_parameters)
44
def get_environment(self):
46
for p in self.parameters:
48
env.args[p.name] = p.datatype.topython(os.environ[p.envname])
50
if p.default is not None:
51
env.args[p.name] = p.default
54
60
def add_action(self, *args, **kwargs):
61
"""Add an action that the script supports
62
@name The name of the action (eg. start) - Must have at-least start, stop. meta-data and validate-all are
64
@parameters A dict of default parameters for this action. timeout is mandatory. example: { "timeout": "10s" }
65
@callback A callable (function or method) to be executed on this action. Is always an OCFEnvironment object"""
55
67
a = Action(*args, **kwargs)
56
68
self.actions[a.name] = a
58
70
def add_parameter(self, *args, **kwargs):
59
71
p = Parameter(*args, **kwargs)
60
self.parameters.append(p)
72
self.parameters[p.name] = p
62
74
def add_validation(self, callback):
63
75
self.validations.append(callback)
109
121
def validate_all(self, env=None):
110
for validator in self.validators:
122
for validator in self.validations:
113
125
def validate_parameters(self, env=None):
114
for parameter in self.parameters:
126
for parameter in self.parameters.itervalues():
115
127
if not os.environ.has_key(parameter.envname):
116
128
if parameter.required:
117
129
raise OCF_ERR_CONFIGURED("Required parameter \"%s\" missing" % parameter.name)