summaryrefslogtreecommitdiff
path: root/hooks
diff options
authorMario Splivalo <mario.splivalo@canonical.com>2014-12-19 16:26:13 +0100
committerMario Splivalo <mario.splivalo@canonical.com>2014-12-19 16:26:13 +0100
commit128a0f25ad0badf587d1148d63caac7423281354 (patch)
tree0118489e454009da245793236b6dfe4951884035 /hooks
parent8380d4951db1197a60f4b93fc296ce1b23136196 (diff)
More merge request fixes, lint fixes... more work to do
Diffstat (limited to 'hooks')
-rwxr-xr-xhooks/hooks.py70
1 files changed, 42 insertions, 28 deletions
diff --git a/hooks/hooks.py b/hooks/hooks.py
index da5a26a..a978152 100755
--- a/hooks/hooks.py
+++ b/hooks/hooks.py
@@ -30,26 +30,21 @@ from charmhelpers.fetch import (
)
import json
+from distutils.log import fatal
try:
from pymongo import MongoClient
from pymongo.errors import OperationFailure
except ImportError:
- try:
- apt_install("python-pymongo", fatal=True)
- from pymongo import MongoClient
- from pymongo.errors import OperationFailure
- except:
- pass
+ apt_install("python-pymongo", fatal=True)
+ from pymongo import MongoClient
+ from pymongo.errors import OperationFailure
try:
from portalocker import Lock
except ImportError:
- try:
- apt_install("python-portalocker", fatal=True)
- from portalocker import Lock
- except:
- pass
+ apt_install("python-portalocker", fatal=True)
+ from portalocker import Lock
from charmhelpers.core.hookenv import (
close_port,
@@ -95,9 +90,25 @@ INSTALL_PACKAGES = ['mongodb-server',
'python-portalocker']
INIT_LOCKFILE = '/tmp/mongodb-charm.lock'
+
+
MONGO_CLIENT_RETRIES = 10 # number of times mongo_client_smart will
# try to execute given statement
+
+# These are MongoDB ReplicaSet states, for convenience:
+MONGO_STARTUP = 0
+MONGO_PRIMARY = 1
+MONGO_SECONDARY = 2
+MONGO_RECOVERING = 3
+MONGO_FATAL = 4
+MONGO_STARTUP2 = 5
+MONGO_UNKNOWN = 6
+MONGO_ARBITER = 7
+MONGO_DOWN = 8
+MONGO_ROLLBACK = 9
+MONGO_REMOVED = 10
+
###############################################################################
# Supporting functions
###############################################################################
@@ -387,14 +398,14 @@ def mongo_client(host=None, command=None):
def mongo_client_smart(host='localhost', command=None):
'''
- Rework of mongo_client function, but retries the command
+ Rework of mongo_client function, but retries the command
MONGO_CLIENT_RETRIES times
-
+
:param host: The host to connect to. Defaults to localhost
:param command: The command to be executed. Can't be None
- :returns True if command succeeded, False if it failed
+ :returns True if command succeeded, False if it failed
'''
-
+
if command is None:
return False
@@ -427,27 +438,30 @@ def init_replset(master_node=None):
juju_log("init_replset: master_node must be defined.")
retVal = False
else:
- # merged from:
- # lp:~jjo/charms/trusty/mongodb/
- # jjo-lp1274947-use_private-address_at_rs-initiate
config_data = config()
# Use my IP at rs.initiate(), voids issues with invalid (and/or
# not resolvable by peers) hostnames
- init_json = ('{{"_id": "{}",'
- '"members": [{{"_id": 0, "host": "{}:{}"}}]}}')
- init_json = init_json.format(config_data['replicaset'],
- unit_get('private-address'),
- config_data['port'])
- retVal = mongo_client(master_node, 'rs.initiate(%s)' % init_json)
+ init = {}
+ init['_id:'] = config_data['replicaset']
+ init['members'] = {'_id': 0,
+ 'host': unit_get['private_addes']+':'
+ +config_data['port']
+ }
+
+ retVal = mongo_client(master_node, 'rs.initiate(%s)' % str(init))
time.sleep(1) # give mongod some time to become primary
c = MongoClient('localhost')
while True:
try:
r = admin_command(c, 'replSetGetStatus')
juju_log('init_replset: myState: %s' % r['myState'])
- if r['myState'] == 1: # we're primary!
+ if r['myState'] == MONGO_PRIMARY: # we're primary!
break
- elif r['myState'] in (0, 2, 3, 4): # we are still initializing
+ elif r['myState'] in (MONGO_STARTUP,
+ MONGO_SECONDARY,
+ MONGO_RECOVERING,
+ MONGO_FATAL
+ ): # we are still initializing
continue
except OperationFailure as e:
juju_log('init_replset: OperationFailure: %s' % e)
@@ -492,9 +506,9 @@ def leave_replset(master_node=None, host=None):
def enable_replset(replicaset_name=None):
+ retVal = False
if replicaset_name is None:
juju_log('enable_replset: replicaset_name is None, exiting', level=DEBUG)
- retVal = False
try:
juju_log('enable_replset: trying to get lock on: %s' %
default_mongodb_init_config)
@@ -1136,7 +1150,7 @@ def am_i_primary():
r = admin_command(c, 'replSetGetStatus')
juju_log('am_i_primary: replSetGetStatus returned: %s' % str(r),
level=DEBUG)
- return r['myState'] == 1
+ return r['myState'] == MONGO_PRIMARY
except OperationFailure as e:
juju_log('am_i_primary: OperationError: %s' % str(e), level=DEBUG)
if 'replSetInitiate - should come online shortly' in str(e):