Skip to content

Commit

Permalink
Story 11979: Fix is_small priority and arbiter condition.
Browse files Browse the repository at this point in the history
Avoid bootstrap execution on arbiter node.

Cleanup:
* Enable debuging for init-replica.
* Update default value for mongo_rs_bootstrap.
* Cleanup tags & notify.
  • Loading branch information
GiooDev committed Nov 27, 2023
1 parent ab728ae commit f413c8d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 58 deletions.
13 changes: 9 additions & 4 deletions deployment/environments/hosts-ui.example
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,19 @@ hosts_vitamui_mongod

[hosts_vitamui_mongod]
# EDIT: Mandatory
# Mandatory params
# - mongo_rs_bootstrap=true (default: false)
# Optional params
# - mongo_rs_bootstrap=true (default: false) ; mandatory for 1 node of the shard, some init commands will be executed on it
# - mongo_cluster_name=mongo-vitamui (default: mongo-vitamui)
# - mongo_shard_id=0 (default: 0)
# - mongod_memory=x ; this will force the wiredtiger cache size to x (unit is GB)
# - is_small=true (default: false) this will force the priority for this server to be lower when electing master ; hardware can be downgraded for this machine
# - mongo_arbiter=true ; the node will be only an arbiter, it will not store data ; do not add this parameter on a mongo_rs_bootstrap node, maximum 1 node per shard
# - mongo_express_enabled=true to deploy mongo_express (default: false)
# Example:
# vm-mongo-ui mongo_rs_bootstrap=true mongo_express_enabled=true
# Recommended practice in production: use 3 instances per shard
# Example for a PSSmin deployment:
# vm1-mongo-ui mongo_rs_bootstrap=true mongo_express_enabled=true
# vm2-mongo-ui
# vm3-mongo-ui mongod_memory=1 is_small=true


################################################################################
Expand Down
9 changes: 4 additions & 5 deletions deployment/environments/hosts.local
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,15 @@ hosts_vitamui_mongod

[hosts_vitamui_mongod]
# EDIT: Mandatory
# WARNING: put only one server for this service, not more !
# Mandatory params
# - mongo_rs_bootstrap=true (default: false)
# - mongo_rs_bootstrap=true (default: false) ; mandatory for 1 node of the shard, some init commands will be executed on it
# Optional params
# - mongo_cluster_name=mongo-vitamui (default: mongo-vitamui)
# - mongo_shard_id=0 (default: 0)
# - mongo_express=true to deploy mongo_express (default: false)
# - mongo_express_enabled=true to deploy mongo_express (default: false)
# Example:
# vm-mongo-ui mongo_rs_bootstrap=true mongo_express=true
localhost mongo_rs_bootstrap=true mongo_express=true
# vm-mongo-ui mongo_rs_bootstrap=true mongo_express_enabled=true
localhost mongo_rs_bootstrap=true mongo_express_enabled=true

################################################################################
# ZONE INFRA
Expand Down
18 changes: 5 additions & 13 deletions deployment/roles/mongo/tasks/check_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@
register: mongo_authent_enabled
failed_when: false
no_log: "{{ hide_passwords_during_deploy }}"
tags:
- update_mongodb_configuration

# Set mongo_no_auth facts

- name: Set default mongo facts
set_fact:
mongo_credentials: ""
mongo_no_auth: false
tags:
- update_mongodb_configuration
tags: update_mongodb_configuration

- name: Set mongo_no_auth fact to true
set_fact:
mongo_credentials: ""
mongo_no_auth: true
when: "mongo_authent_enabled.rc != 0"
tags:
- update_mongodb_configuration
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"
no_log: "{{ hide_passwords_during_deploy }}"
tags: update_mongodb_configuration

31 changes: 10 additions & 21 deletions deployment/roles/mongo/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@

- block:

- fail:
msg: "ERROR: mongo_rs_bootstrap node can't be mongo_arbiter !"
when: mongo_arbiter | default(false) | bool == true

- name: Wait for the service port to be open on all members of the replica
wait_for:
host: "{{ hostvars[item]['ip_service'] }}"
port: "{{ mongodb.mongod_port }}"
timeout: "{{ vitamui_defaults.services.start_timeout }}"
with_items:
- "{{ groups['hosts_vitamui_mongod'] }}"
tags:
- update_mongodb_configuration
tags: update_mongodb_configuration

- name: Copy the script which initiate the replica set
template:
Expand All @@ -162,23 +165,11 @@
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: "{{ vitamui_defaults.folder.conf_permission | default('0440') }}"
tags:
- update_mongodb_configuration

# - name: Copy script that restore configuration of mongod sharded cluster
# template:
# src: "restore-mongod.js.j2"
# dest: "{{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/restore-mongod.js"
# owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
# group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
# mode: "{{ vitamui_defaults.folder.conf_permission | default('0440') }}"
# tags:
# - update_mongodb_configuration
tags: update_mongodb_configuration

- name: Initiate the replica set
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
tags: update_mongodb_configuration

- import_tasks: check_auth.yml

Expand All @@ -190,12 +181,10 @@
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: "{{ vitamui_defaults.folder.conf_permission | default('0440') }}"
tags:
- update_mongodb_configuration
tags: update_mongodb_configuration

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

when: mongo_rs_bootstrap is defined and mongo_rs_bootstrap|lower == "true"
when: mongo_rs_bootstrap | default(false) | bool == true
24 changes: 15 additions & 9 deletions deployment/roles/mongo/templates/init-replica.js.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#jinja2: lstrip_blocks: True
/*
* IMPORTANT :
* - This script creates mongod replica set on first Vitam installation.
Expand All @@ -8,7 +9,7 @@
*/

function checkExistingReplicaSet() {

print("INFO: checkExistingReplicaSet");
try {
var rsStatus = rs.status();

Expand All @@ -32,6 +33,7 @@ function checkExistingReplicaSet() {
}

function checkExistingReplicaSetMembers() {
print("INFO: checkExistingReplicaSetMembers");
try {
var rsConfig = rs.config();

Expand Down Expand Up @@ -59,21 +61,25 @@ function checkExistingReplicaSetMembers() {
}

function buildTargetReplicaSetMemberConfiguration() {
print("INFO: buildTargetReplicaSetMemberConfiguration");
var idCpt = 0;
let members = [ { _id: idCpt++, host: "{{ ip_service }}:{{ mongodb.mongod_port }}" } ];
let members = [];
{% for host in groups['hosts_vitamui_mongod'] %}
{% if hostvars[host]['mongo_rs_bootstrap'] is not defined or hostvars[host]['mongo_rs_bootstrap'] | lower != "true" %}
members.push({
_id: idCpt++,
host: "{{ hostvars[host]['ip_service'] }}:{{ mongodb.mongod_port }}"
host: "{{ hostvars[host]['ip_service'] }}:{{ mongodb.mongod_port }}",
{% if (hostvars[host]['mongo_rs_bootstrap'] | default(false) | bool != true) and (hostvars[host]['mongo_arbiter'] | default(false) | bool == true) %}
arbiterOnly: true
{% else %}
priority: {{ '1' if (hostvars[host]['is_small'] | default(false) | bool == true) else '10' }}
{% endif %}
});
{% endif %}
{% endfor %}
return members;
}

function initReplicaSetPrimary() {

print("INFO: initReplicaSetPrimary");
let members = buildTargetReplicaSetMemberConfiguration();

let rsInit = rs.initiate(
Expand All @@ -90,7 +96,7 @@ function initReplicaSetPrimary() {
}

function waitForReplicaSetPrimaryElection() {

print("INFO: waitForReplicaSetPrimaryElection");
var status;
for (let i = 0; i < 60; i++) {

Expand All @@ -115,7 +121,7 @@ function waitForReplicaSetPrimaryElection() {
}

function waitForWritablePrimary() {

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

Expand All @@ -127,7 +133,7 @@ function waitForWritablePrimary() {
}

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

Expand Down
4 changes: 2 additions & 2 deletions deployment/roles/mongo_init/tasks/check_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

- block:
- name: Check if authent is enabled
command: "mongosh --host \"mongodb://{{ mongod_uri }}/admin?replicaSet={{ mongod_replicaset_name }}\" -u {{ mongodb.admin.user }} -p {{ mongodb.admin.password }} --quiet --eval 'db.help()'"
command: "mongosh mongodb://{{ mongod_uri }}/admin?replicaSet={{ mongod_replicaset_name }} -u {{ mongodb.admin.user }} -p {{ mongodb.admin.password }} --quiet --eval 'db.help()'"
register: mongo_authent_enabled
failed_when: false
no_log: "{{ hide_passwords_during_deploy }}"
Expand All @@ -20,7 +20,7 @@

- block:
- name: Load script in database (docker)
shell: "docker exec --tty {{ mongodb.docker.image_name }} /bin/bash -c \"mongosh --host mongodb://{{ mongod_uri }}/admin?replicaSet={{ mongod_replicaset_name }} -u {{ mongodb.admin.user }} -p {{ mongodb.admin.password }} --quiet --eval 'db.help()'\""
shell: "docker exec --tty {{ mongodb.docker.image_name }} /bin/bash -c \"mongosh 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
Expand Down
8 changes: 4 additions & 4 deletions deployment/roles/mongo_init/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@
mode: 0755

- name: Load script in database
shell: "mongosh \"mongodb://{{ mongod_uri }}/admin\" {{ mongo_credentials }} --quiet --file {{ mongod_output_dir_entry_point }}/main_script.js"
# no_log: "{{ hide_passwords_during_deploy }}"
shell: "mongosh mongodb://{{ mongod_uri }}/admin {{ mongo_credentials }} --quiet --file {{ mongod_output_dir_entry_point }}/main_script.js"
no_log: "{{ hide_passwords_during_deploy }}"
when: mongodb.docker is not defined or not mongodb.docker.enable

- name: Load script in database test (docker)
command: "docker exec --tty {{ mongodb.docker.image_name }} /bin/bash -c \"mongosh \\\"mongodb://{{ mongod_uri }}/admin\\\" {{ mongo_credentials }} --quiet --file {{ mongodb.docker.internal_dir}}/app/mongod/main_script.js \""
#no_log: "{{ hide_passwords_during_deploy }}"
command: "docker exec --tty {{ mongodb.docker.image_name }} /bin/bash -c \"mongosh mongodb://{{ mongod_uri }}/admin {{ mongo_credentials }} --quiet --file {{ mongodb.docker.internal_dir}}/app/mongod/main_script.js\""
no_log: "{{ hide_passwords_during_deploy }}"
when: mongodb.docker is defined and mongodb.docker.enable

# - name: "Execute file"
Expand Down

0 comments on commit f413c8d

Please sign in to comment.