summaryrefslogtreecommitdiff
diff options
authorMario Splivalo <mario.splivalo@canonical.com>2017-09-14 22:37:10 +0200
committerMario Splivalo <mario.splivalo@canonical.com>2017-09-14 22:37:10 +0200
commit8b089c7a4d4de8a467d5216a904eb8a1478e5798 (patch)
tree2b784a84be4d85fe134bf6304bd5638d5242dbdd
parentd2fee0e2fab9ce07ac0a71b5ebfae16e79f7bfe9 (diff)
Make replicaset-relation-broken not fail
When units are being removed they are querying replicaset status to locate the primary unit. Then, they use primary unit to remove themselves from replicaset. However, when the last unit is being removed (during complete service removal) the 'primary' key in replicaseet status is no longer there (as the unit switched to secondary, and there is no primary). This code change anticipates missing 'primary' key. If so, it will cleanly exit the hook so that juju can remove the unit cleanly.
-rwxr-xr-xhooks/hooks.py27
1 files changed, 10 insertions, 17 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py
index eafce5a..a5dc09a 100755
--- a/hooks/hooks.py
+++ b/hooks/hooks.py
@@ -1166,12 +1166,6 @@ def replica_set_relation_joined():
juju_log("replica_set_relation_joined-finish")
-def in_replica_set():
- c = Connection('localhost')
- r = run_admin_command(c, 'isMaster')
- return 'setName' in r
-
-
def am_i_primary():
c = Connection('localhost')
for i in xrange(10):
@@ -1266,11 +1260,6 @@ def replica_set_relation_departed():
def replica_set_relation_broken():
juju_log('replica_set_relation_broken-start')
- if not in_replica_set():
- juju_log('not in replica set', level=DEBUG)
- juju_log('replica_set_relation_broken-finish')
- return
-
if am_i_primary():
juju_log('I was primary - removing myself via new primary.', 'DEBUG')
mongo_client('localhost', 'rs.stepDown()')
@@ -1278,12 +1267,16 @@ def replica_set_relation_broken():
c = Connection('localhost')
r = c.admin.command('isMaster')
- master_node = r['primary']
- unit = "%s:%s" % (unit_get('private-address'),
- config('port'))
-
- juju_log('Removing myself via %s' % (master_node), 'DEBUG')
- leave_replset(master_node, unit)
+
+ try:
+ master_node = r['primary']
+ except KeyError:
+ pass
+
+ if 'master_node' in locals(): # unit is part of replicaset, remove it!
+ unit = "%s:%s" % (unit_get('private-address'), config('port'))
+ juju_log('Removing myself via %s' % (master_node), 'DEBUG')
+ leave_replset(master_node, unit)
juju_log('replica_set_relation_broken-finish')