{"action":"created","comment":{"url":"https://api.github.com/repos/openshift/mongodb/pulls/comments/55018560","id":55018560,"diff_hunk":"@@ -119,25 +119,60 @@ function mongo_initiate() {\n \n # get the address of the current primary member\n function mongo_primary_member_addr() {\n-  local current_endpoints\n-  current_endpoints=$(endpoints)\n-  local mongo_node\n-  mongo_node=\"$(echo \"${current_endpoints}\" | grep -v \"$(container_addr)\" | head -1):${CONTAINER_PORT}\"\n-  echo -n $(mongo admin -u admin -p \"${MONGODB_ADMIN_PASSWORD}\" --host ${mongo_node} --quiet --eval \"print(rs.isMaster().primary);\")\n+  endpoints | grep -v \"$(container_addr)\" | while read mongo_node; do\n+    cmd_output=\"$(mongo admin -u admin -p \"$MONGODB_ADMIN_PASSWORD\" --host \"$mongo_node:$CONTAINER_PORT\" --eval 'print(rs.isMaster().primary)' --quiet || true)\"\n+\n+    # Trying to find IP:PORT in output and filter out error message because mongo prints it to stdout\n+    ip_and_port_regexp='[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+:[0-9]\\+'\n+    if addr=\"$(echo \"$cmd_output\" | grep -x \"$ip_and_port_regexp\")\"; then\n+      echo -n \"$addr\"\n+      exit 0\n+    fi\n+\n+    echo >&2 \"Cannot get address of primary from $mongo_node node: $cmd_output\"\n+    exit 1\n+  done && return\n+\n+  echo >&2 \"Cannot get address of primary node: after checking all nodes we don't have the address\"\n+\n+  # return empty string\n+  echo -n ''\n }\n \n # mongo_remove removes the current MongoDB from the cluster\n function mongo_remove() {\n-  echo \"=> Removing $(mongo_addr) on $(mongo_primary_member_addr) ...\"\n+  local primary_addr\n+  primary_addr=\"$(mongo_primary_member_addr)\"\n+\n+  if [ -z \"$primary_addr\" ]; then\n+    echo >&2 \"Cannot remove node from the replica set: primary node was not found\"\n+    return\n+  fi\n+\n+  local mongo_addr\n+  mongo_addr=\"$(mongo_addr)\"\n+\n+  echo \"=> Removing $mongo_addr on $primary_addr ...\"\n   mongo admin -u admin -p \"${MONGODB_ADMIN_PASSWORD}\" \\\n-    --host $(mongo_primary_member_addr) --eval \"rs.remove('$(mongo_addr)');\" &>/dev/null || true\n+    --host \"$primary_addr\" --eval \"rs.remove('$mongo_addr');\" || true\n }\n \n # mongo_add advertise the current container to other mongo replicas\n function mongo_add() {\n-  echo \"=> Adding $(mongo_addr) to $(mongo_primary_member_addr) ...\"\n+  local primary_addr\n+  primary_addr=\"$(mongo_primary_member_addr)\"\n+\n+  if [ -z \"$primary_addr\" ]; then\n+    echo >&2 \"Cannot add node to the replica set: primary node was not found\"\n+    return","path":"2.4/root/usr/share/container-scripts/mongodb/common.sh","position":57,"original_position":57,"commit_id":"6904701a0728517cd421e590a907bf91f26457f1","original_commit_id":"6904701a0728517cd421e590a907bf91f26457f1","user":{"login":"php-coder","id":87088,"avatar_url":"https://avatars.githubusercontent.com/u/87088?v=3","gravatar_id":"","url":"https://api.github.com/users/php-coder","html_url":"https://github.com/php-coder","followers_url":"https://api.github.com/users/php-coder/followers","following_url":"https://api.github.com/users/php-coder/following{/other_user}","gists_url":"https://api.github.com/users/php-coder/gists{/gist_id}","starred_url":"https://api.github.com/users/php-coder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-coder/subscriptions","organizations_url":"https://api.github.com/users/php-coder/orgs","repos_url":"https://api.github.com/users/php-coder/repos","events_url":"https://api.github.com/users/php-coder/events{/privacy}","received_events_url":"https://api.github.com/users/php-coder/received_events","type":"User","site_admin":false},"body":">How can the caller distinguish if mongo_add (and mongo_remove) succeeded or not?\r\n\r\nCurrently only by checking is the result empty.\r\n\r\n>If not exit 1, then it should be return 1?\r\n\r\nMaybe, but this change involves changing code of all the callers.","created_at":"2016-03-04T11:19:12Z","updated_at":"2016-03-04T11:19:12Z","html_url":"https://github.com/openshift/mongodb/pull/133#discussion_r55018560","pull_request_url":"https://api.github.com/repos/openshift/mongodb/pulls/133","_links":{"self":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/comments/55018560"},"html":{"href":"https://github.com/openshift/mongodb/pull/133#discussion_r55018560"},"pull_request":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/133"}}},"pull_request":{"url":"https://api.github.com/repos/openshift/mongodb/pulls/133","id":61284181,"html_url":"https://github.com/openshift/mongodb/pull/133","diff_url":"https://github.com/openshift/mongodb/pull/133.diff","patch_url":"https://github.com/openshift/mongodb/pull/133.patch","issue_url":"https://api.github.com/repos/openshift/mongodb/issues/133","number":133,"state":"open","locked":false,"title":"Fix getting of primary member","user":{"login":"php-coder","id":87088,"avatar_url":"https://avatars.githubusercontent.com/u/87088?v=3","gravatar_id":"","url":"https://api.github.com/users/php-coder","html_url":"https://github.com/php-coder","followers_url":"https://api.github.com/users/php-coder/followers","following_url":"https://api.github.com/users/php-coder/following{/other_user}","gists_url":"https://api.github.com/users/php-coder/gists{/gist_id}","starred_url":"https://api.github.com/users/php-coder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-coder/subscriptions","organizations_url":"https://api.github.com/users/php-coder/orgs","repos_url":"https://api.github.com/users/php-coder/repos","events_url":"https://api.github.com/users/php-coder/events{/privacy}","received_events_url":"https://api.github.com/users/php-coder/received_events","type":"User","site_admin":false},"body":"Modified logic for getting address of primary member: try to ask all neighbour nodes. Also mongo may print error message to stdout and I've added fix for that too. Plus minor improvements (like adding quotes).\r\n\r\nPTAL @bparees @rhcarvalho\r\n\r\nFixes #125","created_at":"2016-03-01T18:13:47Z","updated_at":"2016-03-04T11:19:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8aa6de059cb785c434f9c5351c1c86ac2b3252a0","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/openshift/mongodb/pulls/133/commits","review_comments_url":"https://api.github.com/repos/openshift/mongodb/pulls/133/comments","review_comment_url":"https://api.github.com/repos/openshift/mongodb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openshift/mongodb/issues/133/comments","statuses_url":"https://api.github.com/repos/openshift/mongodb/statuses/6904701a0728517cd421e590a907bf91f26457f1","head":{"label":"php-coder:fix_gettting_of_primary_member","ref":"fix_gettting_of_primary_member","sha":"6904701a0728517cd421e590a907bf91f26457f1","user":{"login":"php-coder","id":87088,"avatar_url":"https://avatars.githubusercontent.com/u/87088?v=3","gravatar_id":"","url":"https://api.github.com/users/php-coder","html_url":"https://github.com/php-coder","followers_url":"https://api.github.com/users/php-coder/followers","following_url":"https://api.github.com/users/php-coder/following{/other_user}","gists_url":"https://api.github.com/users/php-coder/gists{/gist_id}","starred_url":"https://api.github.com/users/php-coder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-coder/subscriptions","organizations_url":"https://api.github.com/users/php-coder/orgs","repos_url":"https://api.github.com/users/php-coder/repos","events_url":"https://api.github.com/users/php-coder/events{/privacy}","received_events_url":"https://api.github.com/users/php-coder/received_events","type":"User","site_admin":false},"repo":{"id":50997677,"name":"mongodb","full_name":"php-coder/mongodb","owner":{"login":"php-coder","id":87088,"avatar_url":"https://avatars.githubusercontent.com/u/87088?v=3","gravatar_id":"","url":"https://api.github.com/users/php-coder","html_url":"https://github.com/php-coder","followers_url":"https://api.github.com/users/php-coder/followers","following_url":"https://api.github.com/users/php-coder/following{/other_user}","gists_url":"https://api.github.com/users/php-coder/gists{/gist_id}","starred_url":"https://api.github.com/users/php-coder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/php-coder/subscriptions","organizations_url":"https://api.github.com/users/php-coder/orgs","repos_url":"https://api.github.com/users/php-coder/repos","events_url":"https://api.github.com/users/php-coder/events{/privacy}","received_events_url":"https://api.github.com/users/php-coder/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/php-coder/mongodb","description":"","fork":true,"url":"https://api.github.com/repos/php-coder/mongodb","forks_url":"https://api.github.com/repos/php-coder/mongodb/forks","keys_url":"https://api.github.com/repos/php-coder/mongodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/php-coder/mongodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/php-coder/mongodb/teams","hooks_url":"https://api.github.com/repos/php-coder/mongodb/hooks","issue_events_url":"https://api.github.com/repos/php-coder/mongodb/issues/events{/number}","events_url":"https://api.github.com/repos/php-coder/mongodb/events","assignees_url":"https://api.github.com/repos/php-coder/mongodb/assignees{/user}","branches_url":"https://api.github.com/repos/php-coder/mongodb/branches{/branch}","tags_url":"https://api.github.com/repos/php-coder/mongodb/tags","blobs_url":"https://api.github.com/repos/php-coder/mongodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/php-coder/mongodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/php-coder/mongodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/php-coder/mongodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/php-coder/mongodb/statuses/{sha}","languages_url":"https://api.github.com/repos/php-coder/mongodb/languages","stargazers_url":"https://api.github.com/repos/php-coder/mongodb/stargazers","contributors_url":"https://api.github.com/repos/php-coder/mongodb/contributors","subscribers_url":"https://api.github.com/repos/php-coder/mongodb/subscribers","subscription_url":"https://api.github.com/repos/php-coder/mongodb/subscription","commits_url":"https://api.github.com/repos/php-coder/mongodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/php-coder/mongodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/php-coder/mongodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/php-coder/mongodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/php-coder/mongodb/contents/{+path}","compare_url":"https://api.github.com/repos/php-coder/mongodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/php-coder/mongodb/merges","archive_url":"https://api.github.com/repos/php-coder/mongodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/php-coder/mongodb/downloads","issues_url":"https://api.github.com/repos/php-coder/mongodb/issues{/number}","pulls_url":"https://api.github.com/repos/php-coder/mongodb/pulls{/number}","milestones_url":"https://api.github.com/repos/php-coder/mongodb/milestones{/number}","notifications_url":"https://api.github.com/repos/php-coder/mongodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/php-coder/mongodb/labels{/name}","releases_url":"https://api.github.com/repos/php-coder/mongodb/releases{/id}","deployments_url":"https://api.github.com/repos/php-coder/mongodb/deployments","created_at":"2016-02-03T12:18:53Z","updated_at":"2016-02-03T12:18:55Z","pushed_at":"2016-03-03T18:39:44Z","git_url":"git://github.com/php-coder/mongodb.git","ssh_url":"git@github.com:php-coder/mongodb.git","clone_url":"https://github.com/php-coder/mongodb.git","svn_url":"https://github.com/php-coder/mongodb","homepage":null,"size":171,"stargazers_count":0,"watchers_count":0,"language":"Shell","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":"master"}},"base":{"label":"openshift:master","ref":"master","sha":"ea4fb19195ea11062f67368f4b9cad36c82c23e7","user":{"login":"openshift","id":792337,"avatar_url":"https://avatars.githubusercontent.com/u/792337?v=3","gravatar_id":"","url":"https://api.github.com/users/openshift","html_url":"https://github.com/openshift","followers_url":"https://api.github.com/users/openshift/followers","following_url":"https://api.github.com/users/openshift/following{/other_user}","gists_url":"https://api.github.com/users/openshift/gists{/gist_id}","starred_url":"https://api.github.com/users/openshift/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openshift/subscriptions","organizations_url":"https://api.github.com/users/openshift/orgs","repos_url":"https://api.github.com/users/openshift/repos","events_url":"https://api.github.com/users/openshift/events{/privacy}","received_events_url":"https://api.github.com/users/openshift/received_events","type":"Organization","site_admin":false},"repo":{"id":31970825,"name":"mongodb","full_name":"openshift/mongodb","owner":{"login":"openshift","id":792337,"avatar_url":"https://avatars.githubusercontent.com/u/792337?v=3","gravatar_id":"","url":"https://api.github.com/users/openshift","html_url":"https://github.com/openshift","followers_url":"https://api.github.com/users/openshift/followers","following_url":"https://api.github.com/users/openshift/following{/other_user}","gists_url":"https://api.github.com/users/openshift/gists{/gist_id}","starred_url":"https://api.github.com/users/openshift/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openshift/subscriptions","organizations_url":"https://api.github.com/users/openshift/orgs","repos_url":"https://api.github.com/users/openshift/repos","events_url":"https://api.github.com/users/openshift/events{/privacy}","received_events_url":"https://api.github.com/users/openshift/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openshift/mongodb","description":"","fork":false,"url":"https://api.github.com/repos/openshift/mongodb","forks_url":"https://api.github.com/repos/openshift/mongodb/forks","keys_url":"https://api.github.com/repos/openshift/mongodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openshift/mongodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openshift/mongodb/teams","hooks_url":"https://api.github.com/repos/openshift/mongodb/hooks","issue_events_url":"https://api.github.com/repos/openshift/mongodb/issues/events{/number}","events_url":"https://api.github.com/repos/openshift/mongodb/events","assignees_url":"https://api.github.com/repos/openshift/mongodb/assignees{/user}","branches_url":"https://api.github.com/repos/openshift/mongodb/branches{/branch}","tags_url":"https://api.github.com/repos/openshift/mongodb/tags","blobs_url":"https://api.github.com/repos/openshift/mongodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openshift/mongodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openshift/mongodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/openshift/mongodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openshift/mongodb/statuses/{sha}","languages_url":"https://api.github.com/repos/openshift/mongodb/languages","stargazers_url":"https://api.github.com/repos/openshift/mongodb/stargazers","contributors_url":"https://api.github.com/repos/openshift/mongodb/contributors","subscribers_url":"https://api.github.com/repos/openshift/mongodb/subscribers","subscription_url":"https://api.github.com/repos/openshift/mongodb/subscription","commits_url":"https://api.github.com/repos/openshift/mongodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/openshift/mongodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/openshift/mongodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/openshift/mongodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/openshift/mongodb/contents/{+path}","compare_url":"https://api.github.com/repos/openshift/mongodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openshift/mongodb/merges","archive_url":"https://api.github.com/repos/openshift/mongodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openshift/mongodb/downloads","issues_url":"https://api.github.com/repos/openshift/mongodb/issues{/number}","pulls_url":"https://api.github.com/repos/openshift/mongodb/pulls{/number}","milestones_url":"https://api.github.com/repos/openshift/mongodb/milestones{/number}","notifications_url":"https://api.github.com/repos/openshift/mongodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openshift/mongodb/labels{/name}","releases_url":"https://api.github.com/repos/openshift/mongodb/releases{/id}","deployments_url":"https://api.github.com/repos/openshift/mongodb/deployments","created_at":"2015-03-10T17:15:19Z","updated_at":"2016-02-25T17:29:54Z","pushed_at":"2016-03-03T20:51:29Z","git_url":"git://github.com/openshift/mongodb.git","ssh_url":"git@github.com:openshift/mongodb.git","clone_url":"https://github.com/openshift/mongodb.git","svn_url":"https://github.com/openshift/mongodb","homepage":null,"size":267,"stargazers_count":6,"watchers_count":6,"language":"Shell","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":48,"mirror_url":null,"open_issues_count":19,"forks":48,"open_issues":19,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/133"},"html":{"href":"https://github.com/openshift/mongodb/pull/133"},"issue":{"href":"https://api.github.com/repos/openshift/mongodb/issues/133"},"comments":{"href":"https://api.github.com/repos/openshift/mongodb/issues/133/comments"},"review_comments":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/133/comments"},"review_comment":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openshift/mongodb/pulls/133/commits"},"statuses":{"href":"https://api.github.com/repos/openshift/mongodb/statuses/6904701a0728517cd421e590a907bf91f26457f1"}}}}