1
# Leadership Layer for Juju Charms
3
The Leadership layer is for charm-tools and 'charm build', making it
4
easier for layered charms to deal with Juju leadership.
6
This layer will initialize charms.reactive states, allowing you to
7
write handlers that will be activated by these states. It allows you
8
to completely avoid writing leader-elected and leader-settings-changed
9
hooks. As a simple example, these two handlers are all that is required
10
to make the leader unit generate a password if it is not already set,
11
and have the shared password stored in a file on all units:
14
import charms.leadership
15
from charmhelpers.core.host import pwgen
18
@when('leadership.is_leader')
19
@when_not('leadership.set.admin_password')
20
def generate_secret():
21
charms.leadership.leader_set(admin_password=pwgen())
24
@when('leadership.changed.admin_password')
26
write_file('/etc/foopass', leader_get('admin_password'))
32
The following states are set appropriately on startup, before any @hook
33
decorated methods are invoked:
35
* `leadership.is_leader`
37
This state is set when the unit is the leader. The unit will remain
38
the leader for the remainder of the hook, but may not be leader in
41
* `leadership.set.{varname}`
43
This state is set for each leadership setting (ie. the
44
`leadership.set.foo` state will be set if the leader has set
45
the foo leadership setting to any value). It will remain
46
set for the remainder of the hook, unless the unit is the leader
47
and calls `reactive.leadership.leader_set()` and resets the value
50
* `leadership.changed.{varname}`
52
This state is set for each leadership setting that has changed
53
since the last hook. It will remain set for the remainder of the
54
hook. It will not be set in the next hook, unless the leader has
55
changed the leadership setting yet again.
57
* `leadership.changed`
59
One or more leadership settings has changed since the last hook.
60
This state will remain set for the remainder of the hook. It will
61
not be set in the next hook, unless the leader has made further
67
The `charms.leadership` module exposes the `leader_set()` and
68
`leader_get()` methods, which match the methods found in the
69
`charmhelpers.core.hookenv` module. `reactive.leadership.leader_set()`
70
should be used instead of the charmhelpers function to ensure that
71
the reactive state is updated when the leadership settings are. If you
72
do not do this, then you risk handlers waiting on these states to not
73
be run on the leader (because when the leader changes settings, it
74
triggers leader-settings-changed hooks on the follower units but
80
This layer is maintained on Launchpad by
81
Stuart Bishop (stuart.bishop@canonical.com).
83
Code is available using git at git+ssh://git.launchpad.net/layer-leadership.
85
Bug reports can be made at https://bugs.launchpad.net/layer-leadership.
87
Queries and comments can be made on the Juju mailing list, Juju IRC
88
channels, or at https://answers.launchpad.net/layer-leadership.