Skip to content

Commit

Permalink
Bug #12074: Fix mongo isWritablePrimary check.
Browse files Browse the repository at this point in the history
Compute list of mongo nodes to properly execute bootstrap commands on the primary.
  • Loading branch information
GiooDev committed Dec 7, 2023
1 parent 028dfc5 commit a596ad0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
4 changes: 2 additions & 2 deletions deployment/roles/mongo/tasks/check_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
set_fact:
mongo_credentials: ""
mongo_no_auth: true
when: "mongo_authent_enabled.rc != 0"
when: mongo_authent_enabled.rc != 0
tags: update_mongodb_configuration

# When authentication is required, we set mongodb admin credentials
- name: Set mongodb authentication credentials
set_fact:
mongo_credentials: "-u {{ mongodb.admin.user }} -p {{ mongodb.admin.password }}"
mongo_no_auth: false
when: "mongo_authent_enabled.rc == 0"
when: mongo_authent_enabled.rc == 0
no_log: "{{ hide_passwords_during_deploy }}"
tags: update_mongodb_configuration

13 changes: 9 additions & 4 deletions deployment/roles/mongo/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@
host: "{{ hostvars[item]['ip_service'] }}"
port: "{{ mongodb.mongod_port }}"
timeout: "{{ vitamui_defaults.services.start_timeout }}"
with_items:
- "{{ groups['hosts_vitamui_mongod'] }}"
with_items: "{{ groups['hosts_vitamui_mongod'] }}"
tags: update_mongodb_configuration

- name: Copy the script which initiate the replica set
Expand All @@ -164,12 +163,18 @@
tags: update_mongodb_configuration

- name: Initiate the replica set
command: "mongo --host {{ ip_service }} --port {{ mongodb.mongod_port }} {{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/init-replica.js"
command: "mongosh --host {{ ip_service }} --port {{ mongodb.mongod_port }} --quiet --file {{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/init-replica.js"
tags: update_mongodb_configuration

- import_tasks: check_auth.yml

# Create the local shard admin user
- name: Compute list of mongo_nodes
set_fact:
mongo_nodes: "{{ mongo_nodes | default([]) + [ hostvars[item]['ip_service'] + ':' + mongodb.mongod_port | string ] }}"
loop: "{{ groups['hosts_vitamui_mongod'] }}"
when: hostvars[item]['mongo_arbiter'] | default(false) | bool == false

- name: Copy the script which create the local users
template:
src: local-user.js.j2
Expand All @@ -180,7 +185,7 @@
tags: update_mongodb_configuration

- name: Create the local shard user
command: "mongo --host {{ ip_service }}:{{ mongodb.mongod_port }} {{ mongo_credentials }} {{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/local-user.js"
command: "mongosh --host {{ mongo_nodes | join(',') }} {{ mongo_credentials }} --quiet --file {{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/local-user.js"
tags: update_mongodb_configuration

when: mongo_rs_bootstrap | default(false) | bool == true
28 changes: 1 addition & 27 deletions deployment/roles/mongo/templates/init-replica.js.j2
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,12 @@ function waitForReplicaSetPrimaryElection() {
throw "ERROR : Timeout - NO PRIMARY NODE FOUND";
}

function waitForWritablePrimary() {
print("INFO: waitForWritablePrimary");
var instanceStatus;
for (let i = 0; i < 60; i++) {

instanceStatus = db.hello();

if(!instanceStatus.ok) {
printjson(instanceStatus);
throw "ERROR : Cannot get db instance status";
}

if(instanceStatus.isWritablePrimary) {
print("OK : Primary node " + instanceStatus.primary + " is writable");
return;
}

// Retry later
sleep(1000);
}

printjson(instanceStatus);
throw "ERROR : Timeout - NO PRIMARY NODE FOUND";
}

if(checkExistingReplicaSet()) {
if (checkExistingReplicaSet()) {
checkExistingReplicaSetMembers();
print("INFO : mongod replica set already configured.");
} else {
print("INFO : Initializing mongod replica set");
initReplicaSetPrimary();
waitForReplicaSetPrimaryElection();
waitForWritablePrimary();
print("INFO : mongod replica set intitialization OK");
}
31 changes: 28 additions & 3 deletions deployment/roles/mongo/templates/local-user.js.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
function waitForWritablePrimary() {
print("INFO: waitForWritablePrimary");
var instanceStatus;
for (let i = 0; i < 60; i++) {

instanceStatus = db.hello();

if (!instanceStatus.ok) {
printjson(instanceStatus);
throw "ERROR : Cannot get db instance status";
}

if (instanceStatus.isWritablePrimary) {
print("OK : Primary node " + instanceStatus.primary + " is writable");
return;
}

// Retry later
sleep(1000);
}

printjson(instanceStatus);
throw "ERROR : Timeout - NO PRIMARY NODE FOUND";
}

waitForWritablePrimary();

admin = db.getSiblingDB("admin")

if (! admin.getUser("{{ mongodb.localadmin.user }}")) {
if (!admin.getUser("{{ mongodb.localadmin.user }}")) {
admin.createUser(
{
user: "{{ mongodb.localadmin.user }}",
Expand All @@ -17,8 +43,7 @@ if (! admin.getUser("{{ mongodb.localadmin.user }}")) {
]
}
)
}
else {
} else {
admin.updateUser(
"{{ mongodb.localadmin.user }}",
{
Expand Down
5 changes: 2 additions & 3 deletions deployment/roles/mongo_init/tasks/check_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
- name: Set mongo_no_auth fact to true
set_fact:
mongo_no_auth: true
when: "mongo_authent_enabled.rc != 0"
when: mongo_authent_enabled.rc != 0
when: mongodb.docker is not defined or not mongodb.docker.enable

- block:
- name: Load script in database (docker)
shell: "docker exec --tty {{ mongodb.docker.image_name }} /bin/bash -c \"mongo --host mongodb://{{ mongod_uri }}/admin?replicaSet={{ mongod_replicaset_name }} -u {{ mongodb.admin.user }} -p {{ mongodb.admin.password }} --quiet --eval 'db.help()'\""

failed_when: false
register: mongo_authent_enabled

- name: Set mongo_no_auth fact to true
set_fact:
mongo_no_auth: true
when: "mongo_authent_enabled.rc != 0"
when: mongo_authent_enabled.rc != 0
when: mongodb.docker is defined and mongodb.docker.enable

# When authentication is required, we set mongodb admin credentials
Expand Down
3 changes: 2 additions & 1 deletion deployment/roles/mongo_init/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
set_fact:
mongo_nodes: "{{ mongo_nodes | default([]) + [ hostvars[item]['ip_service'] + ':'+ mongodb.mongod_port | string ] }}"
loop: "{{ groups['hosts_vitamui_mongod'] }}"
when: hostvars[item]['mongo_arbiter'] | default(false) | bool == false

- name: Set Mongo URI
set_fact:
mongod_uri: "{{ mongo_nodes| join(',') }}"
mongod_uri: "{{ mongo_nodes | join(',') }}"

- name: Set mongod_output_dir_entry_point
set_fact:
Expand Down

0 comments on commit a596ad0

Please sign in to comment.