diff options
author | Mario Splivalo <mario.splivalo@canonical.com> | 2018-03-15 14:07:39 +0100 |
---|---|---|
committer | Mario Splivalo <mario.splivalo@canonical.com> | 2018-03-15 14:07:39 +0100 |
commit | e652517adca3334cb88cb17b0d33fb619c70bc0c (patch) | |
tree | af51b9161b6892343c3556665d425be1cbb41c01 | |
parent | f8bb79791cefd33d1e96281b4f5162f428c80006 (diff) |
Made config-changed and replicaset-relation-changed hook wait.
As members are joined to replicaset from PRIMARY, when update_status() is called from -changed hooks, it ofter returns a temporary error state. However, that state stay in 'juju status' for cca 5 minutes, until update-status hook is called. This should make -changed hooks wait a bit until the replicaset state of a member changes so that 'juju status' provides more 'recent' info.
-rwxr-xr-x | hooks/hooks.py | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py index 19945e5..9824913 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -13,6 +13,7 @@ import socket import subprocess import sys import time +import pprint try: import yaml # flake8: noqa @@ -1173,7 +1174,8 @@ def am_i_primary(): for i in xrange(10): try: r = run_admin_command(c, 'replSetGetStatus') - juju_log('am_i_primary: replSetGetStatus returned: %s' % str(r), + pretty_r = pprint.pformat(r) + juju_log('am_i_primary: replSetGetStatus returned: %s' % pretty_r, level=DEBUG) return r['myState'] == MONGO_PRIMARY except OperationFailure as e: @@ -1212,12 +1214,17 @@ def get_replicaset_status(): c = MongoClient('localhost') try: r = run_admin_command(c, 'replSetGetStatus') + r_pretty = pprint.pformat(r) + juju_log('get_replicaset_status() failed to get replicaset state:' + + r_pretty, 'WARN') for member in r['members']: if 'self' in member: return member['stateStr'] + return 'Unknown' + except OperationFailure as e: if 'not running with --replSet' in str(e): - return 'not in replicaset.' + return 'not in replicaset' else: return str(e) @@ -1257,9 +1264,12 @@ def replica_set_relation_changed(): if am_i_primary(): juju_log('Adding new secondary... %s' % unit_remote, level=DEBUG) join_replset(unit, unit_remote) + + while update_status <> 'active': + time.sleep(5) juju_log('replica_set_relation_changed-finish') - update_status() + @hooks.hook('replica-set-relation-departed') @@ -1482,20 +1492,30 @@ def uprade_charm(): @hooks.hook('update-status') def update_status(): - mongo_status = get_replicaset_status() + """ + Returns: workload_state (so that some hooks know they need to re-run + update_status if needed) + """ + mongo_status = get_replicaset_status() if mongo_status in ('PRIMARY', 'SECONDARY'): + print ' ## mongo_status in PRI/SEC' workload = 'active' - status = 'Unit is ready as ' + mongo_status - elif mongo_status in ('not in replicaset'): + status = 'Unit is ready as ' + mongo_status + elif mongo_status in ('not in replicaset',): + print ' ## mongo_status in "not in replicaset"' workload = 'active' status = 'Unit is ready, ' + mongo_status else: workload = 'maintenance' status = mongo_status - + print ' ## mongo_status something else: ', status + print ' #> forcing "return False" for update_status()' + juju_log('Unit status: ' + status, 'DEBUG') - status_set(workload, status) + + return workload + def run(command, exit_on_error=True): |