Skip to content

Commit b03ff7b

Browse files
committed
Fix replication module
1 parent 0e7c080 commit b03ff7b

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

library/mongodb_replication.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
import ConfigParser
160160
import ssl as ssl_lib
161161
import time
162+
from datetime import datetime as dtdatetime
162163
from distutils.version import LooseVersion
163164
try:
164165
from pymongo.errors import ConnectionFailure
@@ -168,7 +169,6 @@
168169
from pymongo.errors import ServerSelectionTimeoutError
169170
from pymongo import version as PyMongoVersion
170171
from pymongo import MongoClient
171-
from pymongo import MongoReplicaSetClient
172172
except ImportError:
173173
pymongo_found = False
174174
else:
@@ -215,6 +215,7 @@ def check_members(state, module, client, host_name, host_port, host_type):
215215
module.exit_json(changed=False, host_name=host_name, host_port=host_port, host_type=host_type)
216216

217217
def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwargs):
218+
start_time = dtdatetime.now()
218219
while True:
219220
try:
220221
admin_db = client['admin']
@@ -252,12 +253,12 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
252253
admin_db.command('replSetReconfig', cfg)
253254
return
254255
except (OperationFailure, AutoReconnect) as e:
255-
timeout = timeout - 5
256-
if timeout <= 0:
256+
if (dtdatetime.now() - start_time).seconds > timeout:
257257
module.fail_json(msg='reached timeout while waiting for rs.reconfig(): %s' % str(e))
258258
time.sleep(5)
259259

260260
def remove_host(module, client, host_name, timeout=180):
261+
start_time = dtdatetime.now()
261262
while True:
262263
try:
263264
admin_db = client['admin']
@@ -281,8 +282,7 @@ def remove_host(module, client, host_name, timeout=180):
281282
fail_msg = "couldn't find member with hostname: {0} in replica set members list".format(host_name)
282283
module.fail_json(msg=fail_msg)
283284
except (OperationFailure, AutoReconnect) as e:
284-
timeout = timeout - 5
285-
if timeout <= 0:
285+
if (dtdatetime.now() - start_time).seconds > timeout:
286286
module.fail_json(msg='reached timeout while waiting for rs.reconfig(): %s' % str(e))
287287
time.sleep(5)
288288

@@ -301,14 +301,23 @@ def load_mongocnf():
301301

302302
return creds
303303

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()
305306
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()
309319

310-
timeout = timeout - 1
311-
if timeout == 0:
320+
if (dtdatetime.now() - start_time).seconds > timeout:
312321
module.fail_json(msg='reached timeout while waiting for rs.status() to become ok=1')
313322

314323
time.sleep(1)
@@ -353,7 +362,7 @@ def main():
353362
)
354363

355364
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')
357366

358367
login_user = module.params['login_user']
359368
login_password = module.params['login_password']
@@ -400,7 +409,7 @@ def main():
400409
"username": login_user,
401410
"password": login_password,
402411
"authsource": login_database,
403-
"serverselectiontimeoutms": 30000,
412+
"serverselectiontimeoutms": 10000,
404413
}
405414

406415
if ssl:
@@ -414,14 +423,18 @@ def main():
414423
if priority != 1.0: new_host['priority'] = priority
415424
config = { '_id': "{0}".format(replica_set), 'members': [new_host] }
416425
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)
418428
replica_set_created = True
419429
module.exit_json(changed=True, host_name=host_name, host_port=host_port, host_type=host_type)
420430
except OperationFailure as e:
421431
module.fail_json(msg='Unable to initiate replica set: %s' % str(e))
422432
except ConnectionFailure as e:
423433
module.fail_json(msg='unable to connect to database: %s' % str(e))
424434

435+
# reconnect again
436+
client = MongoClient(**connection_params)
437+
authenticate(client, login_user, login_password)
425438
check_compatibility(module, client)
426439
check_members(state, module, client, host_name, host_port, host_type)
427440

tasks/auth_initialization.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
- name: Move back mongod.conf
2+
- name: Use different mongod.conf for auth initialization
33
template: src=mongod_init.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
44

55
- name: Restart mongodb service
@@ -77,7 +77,3 @@
7777
- name: Wait MongoDB port is listening
7878
wait_for: host="{{ item }}" port="{{ mongodb_net_port }}" delay=5 state=started
7979
with_items: "{{ mongodb_net_bindip.split(',') | map('replace', '0.0.0.0', '127.0.0.1') | list }}"
80-
81-
- name: stop mongodb if was not started
82-
shell: "kill {{ pidof_mongod.stdout }}"
83-
when: mongodb_manage_service == false and pidof_mongod.rc == 0

0 commit comments

Comments
 (0)