Skip to content

Commit 5e052fb

Browse files
authored
Merge pull request #4 from jasonpincin/use-ip-addresses
Use IP addresses instead of hostnames
2 parents 918de6c + 011223e commit 5e052fb

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

bin/manage.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def on_change():
223223
return False
224224

225225
if is_mongo_primary:
226-
return mongo_update_replset_config(local_mongo, hostname)
226+
return mongo_update_replset_config(local_mongo, ip)
227227
else:
228228
return True
229229

@@ -277,14 +277,14 @@ def mongo_update_replset_config(local_mongo, hostname):
277277
if not repl_config['ok']:
278278
raise Exception('could not get replica config: %s' % repl_config['errmsg'])
279279
repl_config = repl_config['config']
280-
280+
281281
# TODO use consul.agent.health() instead?
282282
# get list of mongo servers from consul
283283
consul_services = consul.agent.services()
284284

285285
# translate the name stored by consul to be the "host" name stored
286286
# in mongo config, skipping any non-mongo services
287-
mongos_in_consul = [consul_to_mongo_hostname(svc) for svc in consul_services]
287+
mongos_in_consul = [consul_to_mongo_hostname(consul_services[svc]) for svc in consul_services]
288288
mongos_in_consul = [svc for svc in mongos_in_consul if svc]
289289
# empty list from consul means we have nothing to compare against
290290
if not mongos_in_consul:
@@ -332,17 +332,14 @@ def mongo_update_replset_config(local_mongo, hostname):
332332
log.exception(e)
333333
sys.exit(1)
334334

335-
def consul_to_mongo_hostname(name):
335+
def consul_to_mongo_hostname(service):
336336
# if name.startswith(SECONDARY + '-'):
337337
# prefix = SECONDARY + '-'
338-
if name.startswith(PRIMARY + '-'):
339-
prefix = PRIMARY + '-'
338+
if service['ID'].startswith(PRIMARY + '-'):
339+
return service['Address'] + ':' + str(service['Port'])
340340
else:
341341
return None
342342

343-
name = name[len(prefix):] + ':27017'
344-
return name
345-
346343
# ---------------------------------------------------------
347344

348345
@debug
@@ -393,7 +390,7 @@ def get_session(no_cache=False):
393390
try:
394391
with open(SESSION_CACHE_FILE, 'r') as f:
395392
session_id = f.read()
396-
393+
397394
# ensure the session_id is valid and refresh it
398395
consul.session.renew(session_id)
399396
except (IOError, pyconsul.base.NotFound) as e:

0 commit comments

Comments
 (0)