Skip to content

Commit ff67a90

Browse files
author
Tim Vaillancourt
committed
fallback for Mongodb 2.4 which has no 'replSetGetStatus' admin cmd
1 parent 94e3a04 commit ff67a90

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

MongoBackup/DB.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def connect(self):
3636
def auth_if_required(self):
3737
if self.username is not None and self.password is not None:
3838
try:
39-
logging.debug("Authenticating MongoDB connection")
39+
logging.debug("Authenticating connection with username: %s" % self.username)
4040
self._conn[self.authdb].authenticate(self.username, self.password)
4141
except Exception, e:
4242
logging.fatal("Unable to authenticate with host %s:%s: %s" % (self.host, self.port, e))
@@ -60,6 +60,14 @@ def admin_command(self, admin_command):
6060
raise Exception, "Could not get output from command: '%s' after %i retries!" % (admin_command, self.retries), None
6161
return status
6262

63+
def server_version(self):
64+
status = self.admin_command('serverStatus')
65+
if 'version' in status:
66+
version = status['version'].split('-')[0]
67+
return tuple(version.split('.'))
68+
else:
69+
raise Exception, "Could not get server version using admin command 'serverStatus'! Error: %s" % e, None
70+
6371
def connection(self):
6472
return self._conn
6573

MongoBackup/ReplsetHandler.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ def close(self):
2727
return self.connection.close()
2828

2929
def get_rs_status(self):
30-
return self.db.admin_command('replSetGetStatus')
30+
try:
31+
return self.db.admin_command('replSetGetStatus')
32+
except Exception, e:
33+
raise Exception, "Error getting replica set status! Error: %s" % e, None
3134

3235
def get_rs_config(self):
33-
return self.db.admin_command('replSetGetConfig')
36+
try:
37+
if self.db.server_version() > tuple("2.4.0".split(".")):
38+
output = self.db.admin_command('replSetGetConfig')
39+
return output['config']
40+
else:
41+
return self.connection['local'].system.replset.find_one()
42+
except Exception, e:
43+
raise Exception, "Error getting replica set config! Error: %s" % e, None
3444

3545
def find_desirable_secondary(self):
3646
rs_status = self.get_rs_status()
@@ -65,7 +75,7 @@ def find_desirable_secondary(self):
6575
log_data = {}
6676

6777
hidden_weight = 0.20
68-
for member_config in rs_config['config']['members']:
78+
for member_config in rs_config['members']:
6979
if member_config['host'] == member['name']:
7080
if 'hidden' in member_config and member_config['hidden'] == True:
7181
score += (score * hidden_weight)

0 commit comments

Comments
 (0)