summaryrefslogtreecommitdiff
diff options
authorMario Splivalo <mario.splivalo@canonical.com>2018-03-12 00:55:39 +0100
committerMario Splivalo <mario.splivalo@canonical.com>2018-03-12 00:55:39 +0100
commitf8bb79791cefd33d1e96281b4f5162f428c80006 (patch)
tree000f8a2c90ce3d4fd789e33b64d59f485087a07c
parent001d9b56d9597c8b20dc3b5b8cf7927a12a243b2 (diff)
Added update_status hook. Added get_replicaset_status support
function, for easy parsing the replicaset state for a particular unit. Added calls to status_set in install, config-changed and replicaset-changed hooks. Fixes-bug: LP: #1676730
-rwxr-xr-xhooks/hooks.py65
l---------hooks/update-status1
2 files changed, 63 insertions, 3 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py
index d3774a7..19945e5 100755
--- a/hooks/hooks.py
+++ b/hooks/hooks.py
@@ -58,7 +58,9 @@ from charmhelpers.core.hookenv import (
Hooks,
DEBUG,
WARNING,
- is_leader
+ is_leader,
+ status_set,
+ application_version_set,
)
from charmhelpers.core.hookenv import log as juju_log
@@ -66,7 +68,6 @@ from charmhelpers.core.hookenv import log as juju_log
from charmhelpers.payload.execd import execd_preinstall
from charmhelpers.contrib.hahelpers.cluster import (
- oldest_peer,
peer_units
)
@@ -920,6 +921,7 @@ def arm64_trusty_quirk():
@hooks.hook('install.real')
def install_hook():
juju_log('Begin install hook.')
+ status_set('maintenance', 'Installing packages')
execd_preinstall()
juju_log("Installing mongodb")
add_source(config('source'), config('key'))
@@ -935,7 +937,7 @@ def install_hook():
@hooks.hook('config-changed')
def config_changed():
juju_log("Entering config_changed")
- print "Entering config_changed"
+ status_set('maintenance', 'Configuring unit')
config_data = config()
print "config_data: ", config_data
mongodb_config = open(default_mongodb_config).read()
@@ -1009,6 +1011,7 @@ def config_changed():
write_logrotate_config(config_data)
# restart mongodb
+ status_set('maintenance', 'Restarting mongod')
restart_mongod()
# attach to replSet ( if needed )
@@ -1069,6 +1072,8 @@ def config_changed():
open_port(config_data['mongos_port'])
update_nrpe_config()
+ application_version_set(get_mongod_version())
+ update_status()
print "About to leave config_changed"
return(True)
@@ -1148,6 +1153,7 @@ def replica_set_relation_joined():
if enable_replset(my_replset):
juju_log('Restarting mongodb after config change (enable replset)',
level=DEBUG)
+ status_set('maintenance', 'Restarting mongod to enable replicaset')
restart_mongod()
relation_set(relation_id(), {
@@ -1157,6 +1163,8 @@ def replica_set_relation_joined():
'install-order': my_install_order,
'type': 'replset',
})
+
+ update_status()
juju_log("replica_set_relation_joined-finish")
@@ -1191,6 +1199,37 @@ def am_i_primary():
raise TimeoutException('Unable to determine if local unit is primary')
+def get_replicaset_status():
+ """Connect to mongod and get the status of replicaset
+ This function is used mainly within update_status() to display
+ replicaset statue in 'juju status' output
+
+ :returns string: can be any of replicaset states
+ (https://docs.mongodb.com/manual/reference/replica-states/)
+ or can be the string of an exception while getting the status
+ """
+
+ c = MongoClient('localhost')
+ try:
+ r = run_admin_command(c, 'replSetGetStatus')
+ for member in r['members']:
+ if 'self' in member:
+ return member['stateStr']
+ except OperationFailure as e:
+ if 'not running with --replSet' in str(e):
+ return 'not in replicaset.'
+ else:
+ return str(e)
+
+def get_mongod_version():
+ """ Connects to mongod and get the db.version() output
+ Mainly used for application_set_version in config-changed hook
+ """
+
+ c = MongoClient('localhost')
+ return c.server_info()['version']
+
+
@hooks.hook('replica-set-relation-changed')
def replica_set_relation_changed():
private_address = unit_get('private-address')
@@ -1208,6 +1247,7 @@ def replica_set_relation_changed():
# Initialize the replicaset - we do this only on the leader!
if is_leader():
juju_log('Initializing replicaset')
+ status_set('maintenance', 'Initializing replicaset')
init_replset()
unit = "%s:%s" % (private_address, config('port'))
@@ -1219,6 +1259,7 @@ def replica_set_relation_changed():
join_replset(unit, unit_remote)
juju_log('replica_set_relation_changed-finish')
+ update_status()
@hooks.hook('replica-set-relation-departed')
@@ -1437,6 +1478,24 @@ def uprade_charm():
remove_replset_from_upstart()
juju_log('upgrade-charm: removing --rest from upstart script')
remove_rest_from_upstart()
+
+
+@hooks.hook('update-status')
+def update_status():
+ mongo_status = get_replicaset_status()
+
+ if mongo_status in ('PRIMARY', 'SECONDARY'):
+ workload = 'active'
+ status = 'Unit is ready as ' + mongo_status
+ elif mongo_status in ('not in replicaset'):
+ workload = 'active'
+ status = 'Unit is ready, ' + mongo_status
+ else:
+ workload = 'maintenance'
+ status = mongo_status
+
+ juju_log('Unit status: ' + status, 'DEBUG')
+ status_set(workload, status)
def run(command, exit_on_error=True):
diff --git a/hooks/update-status b/hooks/update-status
new file mode 120000
index 0000000..9416ca6
--- /dev/null
+++ b/hooks/update-status
@@ -0,0 +1 @@
+hooks.py \ No newline at end of file