1
# Copyright 2015-2016 Canonical Ltd.
3
# This file is part of the Leadership Layer for Juju.
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
from charmhelpers.core import hookenv
18
from charmhelpers.core import unitdata
20
from charms import reactive
21
from charms.reactive import not_unless
24
__all__ = ['leader_get', 'leader_set']
27
@not_unless('leadership.is_leader')
28
def leader_set(settings=None, **kw):
29
'''Change leadership settings, per charmhelpers.core.hookenv.leader_set.
31
The leadership.set.{key} reactive state will be set while the
32
leadership hook environment setting remains set.
34
Changed leadership settings will set the leadership.changed.{key}
35
and leadership.changed states. These states will remain set until
38
These state changes take effect immediately on the leader, and
39
in future hooks run on non-leaders. In this way both leaders and
40
non-leaders can share handlers, waiting on these states.
42
settings = settings or {}
44
previous = unitdata.kv().getrange('leadership.settings.', strip=True)
46
for key, value in settings.items():
47
if value != previous.get(key):
48
reactive.set_state('leadership.changed.{}'.format(key))
49
reactive.set_state('leadership.changed')
50
reactive.helpers.toggle_state('leadership.set.{}'.format(key),
52
hookenv.leader_set(settings)
53
unitdata.kv().update(settings, prefix='leadership.settings.')
56
def leader_get(attribute=None):
57
'''Return leadership settings, per charmhelpers.core.hookenv.leader_get.'''
58
return hookenv.leader_get(attribute)