Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_leader requires juju 1.22.0 or better #16

Merged
merged 5 commits into from Aug 10, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions hooks/hooks.py
Expand Up @@ -24,6 +24,10 @@

@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 @@ -152,6 +156,23 @@ 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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the entire try block here, since OSError will simply raise just like any other exception. There really isn't any exception handling here. You can also simplify the if block with

return ret == 0

Finally, it appears that charmhelpers has some way of translating whether or not is-leader is available. Doing this

from charmhelpers.core.hookenv import is_leader

try:
    is_leader()
except NotImplementedError:
    print("panic message")

would achieve the same goal.

# If we aren't finding the which command, we have bigger issues.
raise

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