19
21
the `name` attribute that are complete will used to populate the dictionary
20
22
values (see `get_data`, below).
22
The generated context will be namespaced under the interface type, to prevent
23
potential naming conflicts.
24
The generated context will be namespaced under the relation :attr:`name`,
25
to prevent potential naming conflicts.
27
:param str name: Override the relation :attr:`name`, since it can vary from charm to charm
28
:param list additional_required_keys: Extend the list of :attr:`required_keys`
29
def __init__(self, *args, **kwargs):
30
super(RelationContext, self).__init__(*args, **kwargs)
34
def __init__(self, name=None, additional_required_keys=None):
37
if additional_required_keys is not None:
38
self.required_keys.extend(additional_required_keys)
33
41
def __bool__(self):
112
class MysqlRelation(RelationContext):
114
Relation context for the `mysql` interface.
116
:param str name: Override the relation :attr:`name`, since it can vary from charm to charm
117
:param list additional_required_keys: Extend the list of :attr:`required_keys`
121
required_keys = ['host', 'user', 'password', 'database']
124
class HttpRelation(RelationContext):
126
Relation context for the `http` interface.
128
:param str name: Override the relation :attr:`name`, since it can vary from charm to charm
129
:param list additional_required_keys: Extend the list of :attr:`required_keys`
133
required_keys = ['host', 'port']
135
def provide_data(self):
137
'host': hookenv.unit_get('private-address'),
142
class RequiredConfig(dict):
144
Data context that loads config options with one or more mandatory options.
146
Once the required options have been changed from their default values, all
147
config options will be available, namespaced under `config` to prevent
148
potential naming conflicts (for example, between a config option and a
151
:param list *args: List of options that must be changed from their default values.
154
def __init__(self, *args):
155
self.required_options = args
156
self['config'] = hookenv.config()
157
with open(os.path.join(hookenv.charm_dir(), 'config.yaml')) as fp:
158
self.config = yaml.load(fp).get('options', {})
161
for option in self.required_options:
162
if option not in self['config']:
164
current_value = self['config'][option]
165
default_value = self.config[option].get('default')
166
if current_value == default_value:
168
if current_value in (None, '') and default_value in (None, ''):
172
def __nonzero__(self):
173
return self.__bool__()
176
class StoredContext(dict):
178
A data context that always returns the data that it was first created with.
180
This is useful to do a one-time generation of things like passwords, that
181
will thereafter use the same value that was originally generated, instead
182
of generating a new value each time it is run.
184
def __init__(self, file_name, config_data):
186
If the file exists, populate `self` with the data from the file.
187
Otherwise, populate with the given data and persist it to the file.
189
if os.path.exists(file_name):
190
self.update(self.read_context(file_name))
192
self.store_context(file_name, config_data)
193
self.update(config_data)
195
def store_context(self, file_name, config_data):
196
if not os.path.isabs(file_name):
197
file_name = os.path.join(hookenv.charm_dir(), file_name)
198
with open(file_name, 'w') as file_stream:
199
os.fchmod(file_stream.fileno(), 0o600)
200
yaml.dump(config_data, file_stream)
202
def read_context(self, file_name):
203
if not os.path.isabs(file_name):
204
file_name = os.path.join(hookenv.charm_dir(), file_name)
205
with open(file_name, 'r') as file_stream:
206
data = yaml.load(file_stream)
208
raise OSError("%s is empty" % file_name)
104
212
class TemplateCallback(ManagerCallback):
106
Callback class that will render a template, for use as a ready action.
214
Callback class that will render a Jinja2 template, for use as a ready
217
:param str source: The template source file, relative to
218
`$CHARM_DIR/templates`
220
:param str target: The target to write the rendered template to
221
:param str owner: The owner of the rendered file
222
:param str group: The group of the rendered file
223
:param int perms: The permissions of the rendered file
108
def __init__(self, source, target, owner='root', group='root', perms=0444):
225
def __init__(self, source, target,
226
owner='root', group='root', perms=0o444):
109
227
self.source = source
110
228
self.target = target
111
229
self.owner = owner