@@ -223,7 +223,7 @@ def on_change():
223
223
return False
224
224
225
225
if is_mongo_primary :
226
- return mongo_update_replset_config (local_mongo , hostname )
226
+ return mongo_update_replset_config (local_mongo , ip )
227
227
else :
228
228
return True
229
229
@@ -277,14 +277,14 @@ def mongo_update_replset_config(local_mongo, hostname):
277
277
if not repl_config ['ok' ]:
278
278
raise Exception ('could not get replica config: %s' % repl_config ['errmsg' ])
279
279
repl_config = repl_config ['config' ]
280
-
280
+
281
281
# TODO use consul.agent.health() instead?
282
282
# get list of mongo servers from consul
283
283
consul_services = consul .agent .services ()
284
284
285
285
# translate the name stored by consul to be the "host" name stored
286
286
# 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 ]
288
288
mongos_in_consul = [svc for svc in mongos_in_consul if svc ]
289
289
# empty list from consul means we have nothing to compare against
290
290
if not mongos_in_consul :
@@ -332,17 +332,14 @@ def mongo_update_replset_config(local_mongo, hostname):
332
332
log .exception (e )
333
333
sys .exit (1 )
334
334
335
- def consul_to_mongo_hostname (name ):
335
+ def consul_to_mongo_hostname (service ):
336
336
# if name.startswith(SECONDARY + '-'):
337
337
# prefix = SECONDARY + '-'
338
- if name .startswith (PRIMARY + '-' ):
339
- prefix = PRIMARY + '-'
338
+ if service [ 'ID' ] .startswith (PRIMARY + '-' ):
339
+ return service [ 'Address' ] + ':' + str ( service [ 'Port' ])
340
340
else :
341
341
return None
342
342
343
- name = name [len (prefix ):] + ':27017'
344
- return name
345
-
346
343
# ---------------------------------------------------------
347
344
348
345
@debug
@@ -393,7 +390,7 @@ def get_session(no_cache=False):
393
390
try :
394
391
with open (SESSION_CACHE_FILE , 'r' ) as f :
395
392
session_id = f .read ()
396
-
393
+
397
394
# ensure the session_id is valid and refresh it
398
395
consul .session .renew (session_id )
399
396
except (IOError , pyconsul .base .NotFound ) as e :
0 commit comments