{"action":"created","comment":{"url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/comments/24338119","id":24338119,"diff_hunk":"@@ -0,0 +1,218 @@\n+#!/usr/bin/python\n+# -*- coding: utf-8 -*-\n+\n+# (c) 2015, Jakub Jirutka <jakub@jirutka.cz>\n+#\n+# This file is part of Ansible\n+#\n+# Ansible is free software: you can redistribute it and/or modify\n+# it under the terms of the GNU General Public License as published by\n+# the Free Software Foundation, either version 3 of the License, or\n+# (at your option) any later version.\n+#\n+# Ansible is distributed in the hope that it will be useful,\n+# but WITHOUT ANY WARRANTY; without even the implied warranty of\n+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+# GNU General Public License for more details.\n+#\n+# You should have received a copy of the GNU General Public License\n+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.\n+\n+DOCUMENTATION = '''\n+---\n+module: mongodb_replset\n+author: Jakub Jirutka\n+version_added: \"1.8\"\n+short_description: Initiate or extend replica set.\n+description:\n+  - This module allows to initiate a new replica set or add new members to an\n+    existing one. It can't remove members from a replica set, this should be\n+    always done with caution by hand.\n+options:\n+  login_user:\n+    description:\n+      - The username used to authenticate with.\n+    aliases: [ \"user\" ]\n+    required: false\n+  login_password:\n+    description:\n+      - The password used to authenticate with.\n+    aliases: [ \"password\" ]\n+    required: false\n+  login_host:\n+    description:\n+      - The first host to connect when initiating the replica set.\n+    aliases: [ \"host\" ]\n+    required: false\n+    default: localhost\n+  login_port:\n+    description:\n+      - Port of the first host to connect when initiating the replica set.\n+    aliases: [ \"port\" ]\n+    required: false\n+    default: \"27017\"\n+  hosts:\n+    description:\n+      - A comma delimited list of replica set members.\n+    aliases: [ \"members\" ]\n+    required: true\n+    example: mongo0,mango1:27011,mango2:27012\n+  replica_set:\n+    description:\n+      - Name of the replica set to create or connect to.\n+    aliases: [ \"replset\" ]\n+    required: true\n+    default: \"rs0\"\n+'''\n+\n+import ConfigParser\n+try:\n+    from pymongo.errors import ConnectionFailure, OperationFailure, ConfigurationError\n+    from pymongo import MongoClient\n+    from pymongo.mongo_replica_set_client import MongoReplicaSetClient\n+    from pymongo.read_preferences import ReadPreference\n+    from pymongo.uri_parser import split_hosts\n+except ImportError:\n+    pymongo = None\n+\n+\n+def load_mongocnf_creds():\n+    \"\"\" Read credentials from ~/.mongodb.cnf file, when exists. \"\"\"\n+    config = ConfigParser.RawConfigParser()\n+    mongocnf = os.path.expanduser('~/.mongodb.cnf')\n+\n+    try:\n+        config.readfp(open(mongocnf))\n+        creds = {\n+            'user': config.get('client', 'user'),\n+            'password': config.get('client', 'pass')\n+        }\n+    except (ConfigParser.NoOptionError, IOError):\n+        return False\n+\n+    return creds\n+\n+\n+def replset_conf(client):\n+    \"\"\" Return replica set configuration; the same as rs.conf() in shell.\n+\n+    :param client: initialized Mongo client\n+    :return: replica set config\n+    \"\"\"\n+    return client['local'].system.replset.find_one()\n+\n+\n+def add_members(client, members):\n+    \"\"\" Add new members to the replica set.\n+\n+    :param client: initialized Mongo client\n+    :param members: list of tuples that defines hostnames and ports of the\n+                    replica set members;\n+                    example: `[(mango0, 27017), (mango1, 27018)]`\n+    \"\"\"\n+    conf = replset_conf(client)\n+    curr_hosts = [m['host'] for m in conf['members']]\n+    new_hosts = [join_colon(x) for x in members]\n+\n+    new_id = max([int(x['_id']) for x in conf['members']]) + 1\n+    for host in (set(new_hosts) - set(curr_hosts)):\n+        conf['members'] += [{'_id': new_id, 'host': host}]\n+        new_id += 1\n+\n+    conf['version'] += 1\n+    client.admin.command('replSetReconfig', conf)\n+\n+\n+def replset_initiate(client, name, members):\n+    \"\"\" Initiate replica set with the specified members.\n+\n+    :param client: initialized Mongo client\n+    :param name: name of the replica set to initiate\n+    :param members: list of tuples that defines hostnames and ports of the\n+                    replica set members;\n+                    example: `[(mango0, 27017), (mango1, 27018)]`\n+    \"\"\"\n+    hosts = [{'_id': idx, 'host': join_colon(val)} for idx, val in enumerate(members)]\n+    conf = {'_id': name, 'members': hosts}\n+    client.admin.command('replSetInitiate', conf)\n+\n+\n+def join_colon(iterable):\n+    return ':'.join(str(s) for s in iterable)\n+\n+\n+def main():\n+    module = AnsibleModule(\n+        argument_spec={\n+            'login_user':     {'aliases': ['user']},\n+            'login_password': {'aliases': ['password']},\n+            'login_host':     {'aliases': ['host'], 'default': 'localhost'},\n+            'login_port':     {'aliases': ['port'], 'default': 27017},\n+            'hosts':          {'aliases': ['members'], 'required': True},\n+            'replica_set':    {'aliases': ['replset'], 'required': True}\n+        }\n+    )\n+\n+    if not pymongo:\n+        module.fail_json(msg='Python module \"pymongo\" must be installed.')\n+\n+    user, password, host, port, hosts, replset = (","path":"database/misc/mongodb_replset.py","position":159,"original_position":159,"commit_id":"03739bb60d68d111efd70996922b1c5996ca702f","original_commit_id":"370301f010dc1d2ed3cbdcc9e1f3352ab76c6b88","user":{"login":"sivel","id":39340,"avatar_url":"https://avatars.githubusercontent.com/u/39340?v=3","gravatar_id":"","url":"https://api.github.com/users/sivel","html_url":"https://github.com/sivel","followers_url":"https://api.github.com/users/sivel/followers","following_url":"https://api.github.com/users/sivel/following{/other_user}","gists_url":"https://api.github.com/users/sivel/gists{/gist_id}","starred_url":"https://api.github.com/users/sivel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sivel/subscriptions","organizations_url":"https://api.github.com/users/sivel/orgs","repos_url":"https://api.github.com/users/sivel/repos","events_url":"https://api.github.com/users/sivel/events{/privacy}","received_events_url":"https://api.github.com/users/sivel/received_events","type":"User","site_admin":false},"body":"Is this necessary?  Seems overly complicated and too \"clever\".  I'd advocate for something more explicit if this is really needed.","created_at":"2015-02-09T15:57:33Z","updated_at":"2015-02-09T15:57:33Z","html_url":"https://github.com/ansible/ansible-modules-extras/pull/242#discussion_r24338119","pull_request_url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242","_links":{"self":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/comments/24338119"},"html":{"href":"https://github.com/ansible/ansible-modules-extras/pull/242#discussion_r24338119"},"pull_request":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242"}}},"pull_request":{"url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242","id":28889892,"html_url":"https://github.com/ansible/ansible-modules-extras/pull/242","diff_url":"https://github.com/ansible/ansible-modules-extras/pull/242.diff","patch_url":"https://github.com/ansible/ansible-modules-extras/pull/242.patch","issue_url":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/242","number":242,"state":"open","locked":false,"title":"Add new module mongodb_replset","user":{"login":"jirutka","id":949228,"avatar_url":"https://avatars.githubusercontent.com/u/949228?v=3","gravatar_id":"","url":"https://api.github.com/users/jirutka","html_url":"https://github.com/jirutka","followers_url":"https://api.github.com/users/jirutka/followers","following_url":"https://api.github.com/users/jirutka/following{/other_user}","gists_url":"https://api.github.com/users/jirutka/gists{/gist_id}","starred_url":"https://api.github.com/users/jirutka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jirutka/subscriptions","organizations_url":"https://api.github.com/users/jirutka/orgs","repos_url":"https://api.github.com/users/jirutka/repos","events_url":"https://api.github.com/users/jirutka/events{/privacy}","received_events_url":"https://api.github.com/users/jirutka/received_events","type":"User","site_admin":false},"body":"This module allows to initiate a new replica set or add new members to an existing one. It can’t remove members from a replica set, this should be always done with caution by hand.\r\n\r\n_Copied from https://github.com/ansible/ansible/pull/8153._","created_at":"2015-02-09T00:12:53Z","updated_at":"2015-02-09T15:57:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"53f0cb885f5b0306650657c1088eef581ac7d916","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242/commits","review_comments_url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242/comments","review_comment_url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/comments/{number}","comments_url":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/242/comments","statuses_url":"https://api.github.com/repos/ansible/ansible-modules-extras/statuses/03739bb60d68d111efd70996922b1c5996ca702f","head":{"label":"jirutka:mongodb_replset","ref":"mongodb_replset","sha":"03739bb60d68d111efd70996922b1c5996ca702f","user":{"login":"jirutka","id":949228,"avatar_url":"https://avatars.githubusercontent.com/u/949228?v=3","gravatar_id":"","url":"https://api.github.com/users/jirutka","html_url":"https://github.com/jirutka","followers_url":"https://api.github.com/users/jirutka/followers","following_url":"https://api.github.com/users/jirutka/following{/other_user}","gists_url":"https://api.github.com/users/jirutka/gists{/gist_id}","starred_url":"https://api.github.com/users/jirutka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jirutka/subscriptions","organizations_url":"https://api.github.com/users/jirutka/orgs","repos_url":"https://api.github.com/users/jirutka/repos","events_url":"https://api.github.com/users/jirutka/events{/privacy}","received_events_url":"https://api.github.com/users/jirutka/received_events","type":"User","site_admin":false},"repo":{"id":30510599,"name":"ansible-modules-extras","full_name":"jirutka/ansible-modules-extras","owner":{"login":"jirutka","id":949228,"avatar_url":"https://avatars.githubusercontent.com/u/949228?v=3","gravatar_id":"","url":"https://api.github.com/users/jirutka","html_url":"https://github.com/jirutka","followers_url":"https://api.github.com/users/jirutka/followers","following_url":"https://api.github.com/users/jirutka/following{/other_user}","gists_url":"https://api.github.com/users/jirutka/gists{/gist_id}","starred_url":"https://api.github.com/users/jirutka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jirutka/subscriptions","organizations_url":"https://api.github.com/users/jirutka/orgs","repos_url":"https://api.github.com/users/jirutka/repos","events_url":"https://api.github.com/users/jirutka/events{/privacy}","received_events_url":"https://api.github.com/users/jirutka/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jirutka/ansible-modules-extras","description":"Ansible extra modules - these modules ship with ansible","fork":true,"url":"https://api.github.com/repos/jirutka/ansible-modules-extras","forks_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/forks","keys_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/teams","hooks_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/hooks","issue_events_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/issues/events{/number}","events_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/events","assignees_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/assignees{/user}","branches_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/branches{/branch}","tags_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/tags","blobs_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/git/refs{/sha}","trees_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/statuses/{sha}","languages_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/languages","stargazers_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/stargazers","contributors_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/contributors","subscribers_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/subscribers","subscription_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/subscription","commits_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/commits{/sha}","git_commits_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/git/commits{/sha}","comments_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/comments{/number}","issue_comment_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/issues/comments/{number}","contents_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/contents/{+path}","compare_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/merges","archive_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/downloads","issues_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/issues{/number}","pulls_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/pulls{/number}","milestones_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/milestones{/number}","notifications_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/labels{/name}","releases_url":"https://api.github.com/repos/jirutka/ansible-modules-extras/releases{/id}","created_at":"2015-02-08T23:52:59Z","updated_at":"2015-02-08T23:53:00Z","pushed_at":"2015-02-09T15:55:07Z","git_url":"git://github.com/jirutka/ansible-modules-extras.git","ssh_url":"git@github.com:jirutka/ansible-modules-extras.git","clone_url":"https://github.com/jirutka/ansible-modules-extras.git","svn_url":"https://github.com/jirutka/ansible-modules-extras","homepage":"","size":9354,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"devel"}},"base":{"label":"ansible:devel","ref":"devel","sha":"d94d0ce70b5db5ecfafbc73bebc822c9e18734f3","user":{"login":"ansible","id":1507452,"avatar_url":"https://avatars.githubusercontent.com/u/1507452?v=3","gravatar_id":"","url":"https://api.github.com/users/ansible","html_url":"https://github.com/ansible","followers_url":"https://api.github.com/users/ansible/followers","following_url":"https://api.github.com/users/ansible/following{/other_user}","gists_url":"https://api.github.com/users/ansible/gists{/gist_id}","starred_url":"https://api.github.com/users/ansible/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ansible/subscriptions","organizations_url":"https://api.github.com/users/ansible/orgs","repos_url":"https://api.github.com/users/ansible/repos","events_url":"https://api.github.com/users/ansible/events{/privacy}","received_events_url":"https://api.github.com/users/ansible/received_events","type":"Organization","site_admin":false},"repo":{"id":24474138,"name":"ansible-modules-extras","full_name":"ansible/ansible-modules-extras","owner":{"login":"ansible","id":1507452,"avatar_url":"https://avatars.githubusercontent.com/u/1507452?v=3","gravatar_id":"","url":"https://api.github.com/users/ansible","html_url":"https://github.com/ansible","followers_url":"https://api.github.com/users/ansible/followers","following_url":"https://api.github.com/users/ansible/following{/other_user}","gists_url":"https://api.github.com/users/ansible/gists{/gist_id}","starred_url":"https://api.github.com/users/ansible/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ansible/subscriptions","organizations_url":"https://api.github.com/users/ansible/orgs","repos_url":"https://api.github.com/users/ansible/repos","events_url":"https://api.github.com/users/ansible/events{/privacy}","received_events_url":"https://api.github.com/users/ansible/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ansible/ansible-modules-extras","description":"Ansible extra modules - these modules ship with ansible","fork":false,"url":"https://api.github.com/repos/ansible/ansible-modules-extras","forks_url":"https://api.github.com/repos/ansible/ansible-modules-extras/forks","keys_url":"https://api.github.com/repos/ansible/ansible-modules-extras/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ansible/ansible-modules-extras/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ansible/ansible-modules-extras/teams","hooks_url":"https://api.github.com/repos/ansible/ansible-modules-extras/hooks","issue_events_url":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/events{/number}","events_url":"https://api.github.com/repos/ansible/ansible-modules-extras/events","assignees_url":"https://api.github.com/repos/ansible/ansible-modules-extras/assignees{/user}","branches_url":"https://api.github.com/repos/ansible/ansible-modules-extras/branches{/branch}","tags_url":"https://api.github.com/repos/ansible/ansible-modules-extras/tags","blobs_url":"https://api.github.com/repos/ansible/ansible-modules-extras/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ansible/ansible-modules-extras/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ansible/ansible-modules-extras/git/refs{/sha}","trees_url":"https://api.github.com/repos/ansible/ansible-modules-extras/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ansible/ansible-modules-extras/statuses/{sha}","languages_url":"https://api.github.com/repos/ansible/ansible-modules-extras/languages","stargazers_url":"https://api.github.com/repos/ansible/ansible-modules-extras/stargazers","contributors_url":"https://api.github.com/repos/ansible/ansible-modules-extras/contributors","subscribers_url":"https://api.github.com/repos/ansible/ansible-modules-extras/subscribers","subscription_url":"https://api.github.com/repos/ansible/ansible-modules-extras/subscription","commits_url":"https://api.github.com/repos/ansible/ansible-modules-extras/commits{/sha}","git_commits_url":"https://api.github.com/repos/ansible/ansible-modules-extras/git/commits{/sha}","comments_url":"https://api.github.com/repos/ansible/ansible-modules-extras/comments{/number}","issue_comment_url":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/comments/{number}","contents_url":"https://api.github.com/repos/ansible/ansible-modules-extras/contents/{+path}","compare_url":"https://api.github.com/repos/ansible/ansible-modules-extras/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ansible/ansible-modules-extras/merges","archive_url":"https://api.github.com/repos/ansible/ansible-modules-extras/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ansible/ansible-modules-extras/downloads","issues_url":"https://api.github.com/repos/ansible/ansible-modules-extras/issues{/number}","pulls_url":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls{/number}","milestones_url":"https://api.github.com/repos/ansible/ansible-modules-extras/milestones{/number}","notifications_url":"https://api.github.com/repos/ansible/ansible-modules-extras/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ansible/ansible-modules-extras/labels{/name}","releases_url":"https://api.github.com/repos/ansible/ansible-modules-extras/releases{/id}","created_at":"2014-09-25T20:28:42Z","updated_at":"2015-02-09T13:49:39Z","pushed_at":"2015-02-09T13:49:38Z","git_url":"git://github.com/ansible/ansible-modules-extras.git","ssh_url":"git@github.com:ansible/ansible-modules-extras.git","clone_url":"https://github.com/ansible/ansible-modules-extras.git","svn_url":"https://github.com/ansible/ansible-modules-extras","homepage":"","size":9354,"stargazers_count":106,"watchers_count":106,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":269,"mirror_url":null,"open_issues_count":126,"forks":269,"open_issues":126,"watchers":106,"default_branch":"devel"}},"_links":{"self":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242"},"html":{"href":"https://github.com/ansible/ansible-modules-extras/pull/242"},"issue":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/242"},"comments":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/issues/242/comments"},"review_comments":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242/comments"},"review_comment":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/pulls/242/commits"},"statuses":{"href":"https://api.github.com/repos/ansible/ansible-modules-extras/statuses/03739bb60d68d111efd70996922b1c5996ca702f"}}}}