~ibmcharmers/charms/trusty/ibm-was-nd-node/trunk

« back to all changes in this revision

Viewing changes to lib/charms/leadership.py

  • Committer: Geetha S
  • Date: 2016-08-31 04:35:05 UTC
  • Revision ID: geethas1@in.ibm.com-20160831043505-0fv7lgbom4gixdj3
Initial check in for IBM WAS ND Node charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015-2016 Canonical Ltd.
 
2
#
 
3
# This file is part of the Leadership Layer for Juju.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
from charmhelpers.core import hookenv
 
18
from charmhelpers.core import unitdata
 
19
 
 
20
from charms import reactive
 
21
from charms.reactive import not_unless
 
22
 
 
23
 
 
24
__all__ = ['leader_get', 'leader_set']
 
25
 
 
26
 
 
27
@not_unless('leadership.is_leader')
 
28
def leader_set(settings=None, **kw):
 
29
    '''Change leadership settings, per charmhelpers.core.hookenv.leader_set.
 
30
 
 
31
    The leadership.set.{key} reactive state will be set while the
 
32
    leadership hook environment setting remains set.
 
33
 
 
34
    Changed leadership settings will set the leadership.changed.{key}
 
35
    and leadership.changed states. These states will remain set until
 
36
    the following hook.
 
37
 
 
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.
 
41
    '''
 
42
    settings = settings or {}
 
43
    settings.update(kw)
 
44
    previous = unitdata.kv().getrange('leadership.settings.', strip=True)
 
45
 
 
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),
 
51
                                      value is not None)
 
52
    hookenv.leader_set(settings)
 
53
    unitdata.kv().update(settings, prefix='leadership.settings.')
 
54
 
 
55
 
 
56
def leader_get(attribute=None):
 
57
    '''Return leadership settings, per charmhelpers.core.hookenv.leader_get.'''
 
58
    return hookenv.leader_get(attribute)