149
150
return local_unit().split('/')[0]
154
"""The name of the currently executing hook"""
155
return os.path.basename(sys.argv[0])
159
"""A Juju charm config dictionary that can write itself to
160
disk (as json) and track which values have changed since
161
the previous hook invocation.
163
Do not instantiate this object directly - instead call
169
>>> from charmhelpers.core import hookenv
170
>>> config = hookenv.config()
173
>>> config['mykey'] = 'myval'
177
>>> # user runs `juju set mycharm foo=baz`
178
>>> # now we're inside subsequent config-changed hook
179
>>> config = hookenv.config()
182
>>> # test to see if this val has changed since last hook
183
>>> config.changed('foo')
185
>>> # what was the previous value?
186
>>> config.previous('foo')
188
>>> # keys/values that we add are preserved across hooks
191
>>> # don't forget to save at the end of hook!
195
CONFIG_FILE_NAME = '.juju-persistent-config'
197
def __init__(self, *args, **kw):
198
super(Config, self).__init__(*args, **kw)
199
self._prev_dict = None
200
self.path = os.path.join(charm_dir(), Config.CONFIG_FILE_NAME)
201
if os.path.exists(self.path):
204
def load_previous(self, path=None):
205
"""Load previous copy of config from disk so that current values
206
can be compared to previous values.
210
File path from which to load the previous config. If `None`,
211
config is loaded from the default location. If `path` is
212
specified, subsequent `save()` calls will write to the same
216
self.path = path or self.path
217
with open(self.path) as f:
218
self._prev_dict = json.load(f)
220
def changed(self, key):
221
"""Return true if the value for this key has changed since
225
if self._prev_dict is None:
227
return self.previous(key) != self.get(key)
229
def previous(self, key):
230
"""Return previous value for this key, or None if there
231
is no "previous" value.
235
return self._prev_dict.get(key)
239
"""Save this config to disk.
241
Preserves items in _prev_dict that do not exist in self.
245
for k, v in self._prev_dict.iteritems():
248
with open(self.path, 'w') as f:
153
253
def config(scope=None):
154
254
"""Juju charm configuration"""
392
def is_relation_made(relation, keys='private-address'):
394
Determine whether a relation is established by checking for
395
presence of key(s). If a list of keys is provided, they
396
must all be present for the relation to be identified as made
398
if isinstance(keys, str):
400
for r_id in relation_ids(relation):
401
for unit in related_units(r_id):
404
context[k] = relation_get(k, rid=r_id,
406
if None not in context.values():
288
411
def open_port(port, protocol="TCP"):
289
412
"""Open a service network port"""
290
413
_args = ['open-port']