Skip to content

Commit

Permalink
Refactored is_leader
Browse files Browse the repository at this point in the history
refactored from a bash based sniff to just consume charm helpers and abort
on NotImplementedError
  • Loading branch information
Charles Butler committed Aug 10, 2015
1 parent e374801 commit 23d17ca
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions hooks/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
hook_data = unitdata.HookData()
db = unitdata.kv()

leader_status = check_output(['is-leader']).rstrip() != "False"
private_address = hookenv.unit_get('private-address')
public_address = hookenv.unit_get('private-address')
unit_name = environ['JUJU_UNIT_NAME'].replace('/', '')

try:
leader_status = hookenv.is_leader()
except NotImplementedError:
hookenv.log('This charm requires Juju 1.22.0 or greater. Panic and exit!'
'CRITICAL')
sys.exit(1)


@hooks.hook('config-changed')
def config_changed():
if not isleader_available:
hookenv.log('This charm requires Juju 1.22.0 or greater. Panic and exit!'
'CRITICAL')
sys.exit(1)
if not db.get('installed') or hookenv.config().changed('source-sum'):
install_etcd()
if leader_status:
Expand Down Expand Up @@ -156,23 +158,6 @@ def install_etcd():
hookenv.open_port(4001)
db.set('installed', True)

def isleader_available():
"""Attempt to locate is_leader
predicate method to determine if we are on a Juju revision that maintains
the leader election codebase.
"""
cmd = ['which', 'is_leader']
try:
ret = subprocess.call(cmd, universal_newlines=True)
if ret == 0:
return True
else:
return False
except OSError as e:
# If we aren't finding the which command, we have bigger issues.
raise

if __name__ == '__main__':
with hook_data():
hooks.execute(sys.argv)

0 comments on commit 23d17ca

Please sign in to comment.