Skip to content

Commit

Permalink
Merge pull request #97 from UnderGreen/mongodb_3_6
Browse files Browse the repository at this point in the history
MongoDB 3.6 full support
  • Loading branch information
UnderGreen authored Feb 26, 2018
2 parents b5257ff + 67082cc commit 6077df7
Show file tree
Hide file tree
Showing 18 changed files with 682 additions and 85 deletions.
45 changes: 42 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ sudo: required
language: python
python:
- "2.7"
- "3.6"
env:
- >
DISTRIBUTION=ubuntu
DIST_VERSION=16_04-builded
MONGODB_VERSION=3.6
- >
DISTRIBUTION=ubuntu
DIST_VERSION=16_04-builded
MONGODB_VERSION=3.4
- >
DISTRIBUTION=ubuntu
DIST_VERSION=16_04-builded
MONGODB_VERSION=3.2
- >
DISTRIBUTION=ubuntu-upstart
DIST_VERSION=14.04
MONGODB_VERSION=3.6
- >
DISTRIBUTION=ubuntu-upstart
DIST_VERSION=14.04
Expand All @@ -16,22 +31,46 @@ env:
DISTRIBUTION=ubuntu-upstart
DIST_VERSION=14.04
MONGODB_VERSION=3.2
- >
DISTRIBUTION=debian
DIST_VERSION=9-builded
MONGODB_VERSION=3.2
- >
DISTRIBUTION=debian
DIST_VERSION=8-builded
MONGODB_VERSION=3.6
- >
DISTRIBUTION=debian
DIST_VERSION=8-builded
MONGODB_VERSION=3.4
- >
DISTRIBUTION=debian
DIST_VERSION=8-builded
MONGODB_VERSION=3.2
- >
DISTRIBUTION=centos
DIST_VERSION=6-builded
MONGODB_VERSION=3.2
MONGODB_VERSION=3.6
- >
DISTRIBUTION=centos
DIST_VERSION=6-builded
MONGODB_VERSION=3.4
- >
DISTRIBUTION=centos
DIST_VERSION=7-builded
DIST_VERSION=6-builded
MONGODB_VERSION=3.2
- >
DISTRIBUTION=centos
DIST_VERSION=7-builded
MONGODB_VERSION=3.6
- >
DISTRIBUTION=centos
DIST_VERSION=7-builded
MONGODB_VERSION=3.4
- >
DISTRIBUTION=centos
DIST_VERSION=7-builded
MONGODB_VERSION=3.2
# - >
# distribution=debian
# version=7
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ Ansible role which manages [MongoDB](http://www.mongodb.org/).

MongoDB support matrix:

| Distribution | MongoDB 2.4 | MongoDB 2.6 | MongoDB 3.0 | MongoDB 3.2 | MongoDB 3.4 |
| ------------ |:-----------:|:-----------:|:-----------:|:-----------:|:-----------:|
| Ubuntu 14.04 | :no_entry: | :white_check_mark: | :white_check_mark: | :white_check_mark:| :x:|
| Ubuntu 16.04 | :no_entry: | :x: | :x: | :x:| :x:|
| Debian 8.x | :no_entry: | :x: | :x: | :x:| :x:|
| Debian 9.x | :no_entry: | :x: | :x: | :x:| :x:|
| RHEL 6.x | :no_entry: | :interrobang: | :interrobang: | :interrobang: | :interrobang: |
| RHEL 7.x | :no_entry: | :interrobang: | :interrobang: | :interrobang: | :interrobang: |

:white_check_mark: - fully tested, should work fine
:interrobang: - will be added testing suite soon
:x: - don't have official support
:no_entry: - does't have support, because used old format of configuration files
| Distribution | MongoDB 2.4 | MongoDB 2.6 | MongoDB 3.0 | MongoDB 3.2 | MongoDB 3.4 | MongoDB 3.6 |
| ------------ |:-----------:|:-----------:|:-----------:|:-----------:|:-----------:|:-----------:|
| Ubuntu 14.04 | :no_entry: | :no_entry: | :no_entry: | :white_check_mark:| :white_check_mark:| :white_check_mark:|
| Ubuntu 16.04 | :no_entry: | :no_entry: | :no_entry: | :white_check_mark:| :white_check_mark:| :white_check_mark:|
| Debian 8.x | :no_entry: | :no_entry: | :no_entry: | :white_check_mark:| :white_check_mark:| :white_check_mark:|
| Debian 9.x | :no_entry: | :no_entry: | :no_entry: | :white_check_mark:| :x:| :x:|
| RHEL 6.x | :no_entry: | :no_entry: | :no_entry: | :white_check_mark: | :white_check_mark: | :white_check_mark:|
| RHEL 7.x | :no_entry: | :no_entry: | :no_entry: | :white_check_mark: | :white_check_mark: | :white_check_mark:|

- :white_check_mark: - fully tested, should works fine
- :interrobang: - maybe works, not tested
- :x: - don't have official support
- :no_entry: - MongoDB has reached EOL

#### Variables

Expand All @@ -33,9 +33,9 @@ MongoDB support matrix:
mongodb_package: mongodb-org

# You can control installed version via this param.
# Should be '2.6', '3.0', '3.2' or '3.4'. This role does't support MongoDB < 2.4.
# Should be '3.2', '3.4', '3.6'. This role doesn't support MongoDB < 3.2.
# I will recommend you to use latest version of MongoDB.
mongodb_version: "3.4"
mongodb_version: "3.6"

mongodb_force_wait_for_port: false # When not forced, the role will wait for mongod port to become available only with systemd
mongodb_pymongo_from_pip: true # Install latest PyMongo via PIP or package manager
Expand Down
89 changes: 71 additions & 18 deletions library/mongodb_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
- The port to connect to
required: false
default: 27017
login_database:
description:
- The database where login credentials are stored
required: false
default: admin
replica_set:
description:
- Replica set to connect to (automatically connects to primary for writes)
Expand All @@ -69,6 +74,12 @@
description:
- Whether to use an SSL connection when connecting to the database
default: False
ssl_cert_reqs:
description:
- Specifies whether a certificate is required from the other side of the connection, and whether it will be validated if provided.
required: false
default: "CERT_REQUIRED"
choices: ["CERT_REQUIRED", "CERT_OPTIONAL", "CERT_NONE"]
build_indexes:
description:
- Determines whether the mongod builds indexes on this member.
Expand Down Expand Up @@ -146,7 +157,9 @@
sample: "replica"
'''
import ConfigParser
import ssl as ssl_lib
import time
from datetime import datetime as dtdatetime
from distutils.version import LooseVersion
try:
from pymongo.errors import ConnectionFailure
Expand All @@ -156,7 +169,6 @@
from pymongo.errors import ServerSelectionTimeoutError
from pymongo import version as PyMongoVersion
from pymongo import MongoClient
from pymongo import MongoReplicaSetClient
except ImportError:
pymongo_found = False
else:
Expand Down Expand Up @@ -203,6 +215,7 @@ def check_members(state, module, client, host_name, host_port, host_type):
module.exit_json(changed=False, host_name=host_name, host_port=host_port, host_type=host_type)

def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwargs):
start_time = dtdatetime.now()
while True:
try:
admin_db = client['admin']
Expand Down Expand Up @@ -240,12 +253,12 @@ def add_host(module, client, host_name, host_port, host_type, timeout=180, **kwa
admin_db.command('replSetReconfig', cfg)
return
except (OperationFailure, AutoReconnect) as e:
timeout = timeout - 5
if timeout <= 0:
if (dtdatetime.now() - start_time).seconds > timeout:
module.fail_json(msg='reached timeout while waiting for rs.reconfig(): %s' % str(e))
time.sleep(5)

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

Expand All @@ -289,14 +301,23 @@ def load_mongocnf():

return creds

def wait_for_ok_and_master(module, client, timeout = 60):
def wait_for_ok_and_master(module, connection_params, timeout = 180):
start_time = dtdatetime.now()
while True:
status = client.admin.command('replSetGetStatus', check=False)
if status['ok'] == 1 and status['myState'] == 1:
return
try:
client = MongoClient(**connection_params)
authenticate(client, connection_params["username"], connection_params["password"])

status = client.admin.command('replSetGetStatus', check=False)
if status['ok'] == 1 and status['myState'] == 1:
return

except ServerSelectionTimeoutError:
pass

timeout = timeout - 1
if timeout == 0:
client.close()

if (dtdatetime.now() - start_time).seconds > timeout:
module.fail_json(msg='reached timeout while waiting for rs.status() to become ok=1')

time.sleep(1)
Expand Down Expand Up @@ -324,11 +345,13 @@ def main():
login_password=dict(default=None, no_log=True),
login_host=dict(default='localhost'),
login_port=dict(default='27017'),
login_database=dict(default="admin"),
replica_set=dict(default=None),
host_name=dict(default='localhost'),
host_port=dict(default='27017'),
host_type=dict(default='replica', choices=['replica','arbiter']),
ssl=dict(default='false'),
ssl=dict(default=False, type='bool'),
ssl_cert_reqs=dict(default='CERT_REQUIRED', choices=['CERT_NONE', 'CERT_OPTIONAL', 'CERT_REQUIRED']),
build_indexes = dict(type='bool', default='yes'),
hidden = dict(type='bool', default='no'),
priority = dict(default='1.0'),
Expand All @@ -339,12 +362,13 @@ def main():
)

if not pymongo_found:
module.fail_json(msg='the python pymongo (>= 2.4) module is required')
module.fail_json(msg='the python pymongo (>= 3.2) module is required')

login_user = module.params['login_user']
login_password = module.params['login_password']
login_host = module.params['login_host']
login_port = module.params['login_port']
login_database = module.params['login_database']
replica_set = module.params['replica_set']
host_name = module.params['host_name']
host_port = module.params['host_port']
Expand All @@ -359,29 +383,58 @@ def main():
if replica_set is None:
module.fail_json(msg='replica_set parameter is required')
else:
client = MongoClient(login_host, int(login_port), replicaSet=replica_set,
ssl=ssl, serverSelectionTimeoutMS=5000)

connection_params = {
"host": login_host,
"port": int(login_port),
"username": login_user,
"password": login_password,
"authsource": login_database,
"serverselectiontimeoutms": 5000,
"replicaset": replica_set,
}

if ssl:
connection_params["ssl"] = ssl
connection_params["ssl_cert_reqs"] = getattr(ssl_lib, module.params['ssl_cert_reqs'])

client = MongoClient(**connection_params)
authenticate(client, login_user, login_password)
client['admin'].command('replSetGetStatus')

except ServerSelectionTimeoutError:
try:
client = MongoClient(login_host, int(login_port), ssl=ssl)
connection_params = {
"host": login_host,
"port": int(login_port),
"username": login_user,
"password": login_password,
"authsource": login_database,
"serverselectiontimeoutms": 10000,
}

if ssl:
connection_params["ssl"] = ssl
connection_params["ssl_cert_reqs"] = getattr(ssl_lib, module.params['ssl_cert_reqs'])

client = MongoClient(**connection_params)
authenticate(client, login_user, login_password)
if state == 'present':
new_host = { '_id': 0, 'host': "{0}:{1}".format(host_name, host_port) }
if priority != 1.0: new_host['priority'] = priority
config = { '_id': "{0}".format(replica_set), 'members': [new_host] }
client['admin'].command('replSetInitiate', config)
wait_for_ok_and_master(module, client)
client.close()
wait_for_ok_and_master(module, connection_params)
replica_set_created = True
module.exit_json(changed=True, host_name=host_name, host_port=host_port, host_type=host_type)
except OperationFailure as e:
module.fail_json(msg='Unable to initiate replica set: %s' % str(e))
except ConnectionFailure as e:
module.fail_json(msg='unable to connect to database: %s' % str(e))

# reconnect again
client = MongoClient(**connection_params)
authenticate(client, login_user, login_password)
check_compatibility(module, client)
check_members(state, module, client, host_name, host_port, host_type)

Expand Down
Loading

0 comments on commit 6077df7

Please sign in to comment.