159
159
import ConfigParser
160
160
import ssl as ssl_lib
161
161
import time
162
+ from datetime import datetime as dtdatetime
162
163
from distutils .version import LooseVersion
163
164
try :
164
165
from pymongo .errors import ConnectionFailure
168
169
from pymongo .errors import ServerSelectionTimeoutError
169
170
from pymongo import version as PyMongoVersion
170
171
from pymongo import MongoClient
171
- from pymongo import MongoReplicaSetClient
172
172
except ImportError :
173
173
pymongo_found = False
174
174
else :
@@ -215,6 +215,7 @@ def check_members(state, module, client, host_name, host_port, host_type):
215
215
module .exit_json (changed = False , host_name = host_name , host_port = host_port , host_type = host_type )
216
216
217
217
def add_host (module , client , host_name , host_port , host_type , timeout = 180 , ** kwargs ):
218
+ start_time = dtdatetime .now ()
218
219
while True :
219
220
try :
220
221
admin_db = client ['admin' ]
@@ -252,12 +253,12 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
252
253
admin_db .command ('replSetReconfig' , cfg )
253
254
return
254
255
except (OperationFailure , AutoReconnect ) as e :
255
- timeout = timeout - 5
256
- if timeout <= 0 :
256
+ if (dtdatetime .now () - start_time ).seconds > timeout :
257
257
module .fail_json (msg = 'reached timeout while waiting for rs.reconfig(): %s' % str (e ))
258
258
time .sleep (5 )
259
259
260
260
def remove_host (module , client , host_name , timeout = 180 ):
261
+ start_time = dtdatetime .now ()
261
262
while True :
262
263
try :
263
264
admin_db = client ['admin' ]
@@ -281,8 +282,7 @@ def remove_host(module, client, host_name, timeout=180):
281
282
fail_msg = "couldn't find member with hostname: {0} in replica set members list" .format (host_name )
282
283
module .fail_json (msg = fail_msg )
283
284
except (OperationFailure , AutoReconnect ) as e :
284
- timeout = timeout - 5
285
- if timeout <= 0 :
285
+ if (dtdatetime .now () - start_time ).seconds > timeout :
286
286
module .fail_json (msg = 'reached timeout while waiting for rs.reconfig(): %s' % str (e ))
287
287
time .sleep (5 )
288
288
@@ -301,14 +301,23 @@ def load_mongocnf():
301
301
302
302
return creds
303
303
304
- def wait_for_ok_and_master (module , client , timeout = 60 ):
304
+ def wait_for_ok_and_master (module , connection_params , timeout = 180 ):
305
+ start_time = dtdatetime .now ()
305
306
while True :
306
- status = client .admin .command ('replSetGetStatus' , check = False )
307
- if status ['ok' ] == 1 and status ['myState' ] == 1 :
308
- return
307
+ try :
308
+ client = MongoClient (** connection_params )
309
+ authenticate (client , connection_params ["username" ], connection_params ["password" ])
310
+
311
+ status = client .admin .command ('replSetGetStatus' , check = False )
312
+ if status ['ok' ] == 1 and status ['myState' ] == 1 :
313
+ return
314
+
315
+ except ServerSelectionTimeoutError :
316
+ pass
317
+
318
+ client .close ()
309
319
310
- timeout = timeout - 1
311
- if timeout == 0 :
320
+ if (dtdatetime .now () - start_time ).seconds > timeout :
312
321
module .fail_json (msg = 'reached timeout while waiting for rs.status() to become ok=1' )
313
322
314
323
time .sleep (1 )
@@ -353,7 +362,7 @@ def main():
353
362
)
354
363
355
364
if not pymongo_found :
356
- module .fail_json (msg = 'the python pymongo (>= 2.4 ) module is required' )
365
+ module .fail_json (msg = 'the python pymongo (>= 3.2 ) module is required' )
357
366
358
367
login_user = module .params ['login_user' ]
359
368
login_password = module .params ['login_password' ]
@@ -400,7 +409,7 @@ def main():
400
409
"username" : login_user ,
401
410
"password" : login_password ,
402
411
"authsource" : login_database ,
403
- "serverselectiontimeoutms" : 30000 ,
412
+ "serverselectiontimeoutms" : 10000 ,
404
413
}
405
414
406
415
if ssl :
@@ -414,14 +423,18 @@ def main():
414
423
if priority != 1.0 : new_host ['priority' ] = priority
415
424
config = { '_id' : "{0}" .format (replica_set ), 'members' : [new_host ] }
416
425
client ['admin' ].command ('replSetInitiate' , config )
417
- wait_for_ok_and_master (module , client )
426
+ client .close ()
427
+ wait_for_ok_and_master (module , connection_params )
418
428
replica_set_created = True
419
429
module .exit_json (changed = True , host_name = host_name , host_port = host_port , host_type = host_type )
420
430
except OperationFailure as e :
421
431
module .fail_json (msg = 'Unable to initiate replica set: %s' % str (e ))
422
432
except ConnectionFailure as e :
423
433
module .fail_json (msg = 'unable to connect to database: %s' % str (e ))
424
434
435
+ # reconnect again
436
+ client = MongoClient (** connection_params )
437
+ authenticate (client , login_user , login_password )
425
438
check_compatibility (module , client )
426
439
check_members (state , module , client , host_name , host_port , host_type )
427
440
0 commit comments