Skip to content

Commit

Permalink
Story 11979: Fix replicaset cluster deployment for mongo-vitamui.
Browse files Browse the repository at this point in the history
Run once mongo_init tasks to avoid multiple execution on multi cluster node for mongo-vitamui.
  • Loading branch information
GiooDev committed Nov 30, 2023
1 parent 2461601 commit c692ee4
Showing 1 changed file with 112 additions and 108 deletions.
220 changes: 112 additions & 108 deletions deployment/roles/mongo_init/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,110 +1,114 @@
---

- fail: msg="Variable '{{ mongod_source_template_dir }}' is not defined"
when: mongod_source_template_dir is undefined

- 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'] }}"

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

- name: Set mongod_output_dir_entry_point
set_fact:
mongod_output_dir_entry_point: "{{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/"

- import_tasks: check_auth.yml

- name: Initialize directory if it doesn't exist.
file:
path: "{{ mongod_output_dir_entry_point }}"
state: directory

- name: "Clean directory {{ mongod_output_dir_entry_point }}"
shell: "rm -Rf {{ mongod_output_dir_entry_point }}/*"

# We sort directories by theirs versions
- name: List script files versions in the directory {{ mongod_source_template_dir }}
delegate_to: localhost
shell:
cmd: find * -maxdepth 1 -type d | sort -V
chdir: "{{ mongod_source_template_dir }}"
register: versions

# For each version, we apply a second sort on the index of the script file.
- name: List script files in the directory {{ mongod_source_template_dir }}
delegate_to: localhost
shell:
cmd: find {{ version }}/* -type f -print | sort -V -t '_' -k1
chdir: "{{ mongod_source_template_dir }}"
register: output
loop: "{{ versions.stdout_lines }}"
loop_control:
loop_var: version

- name: "Compute file scripts"
delegate_to: localhost
set_fact:
mongod_files: "{{ (mongod_files| default([])) + item.stdout_lines }}"
loop: "{{ output.results }}"

# We apply regex for included and excluded files in order to compute the eligible scripts.
- name: Compute list of excluded files
delegate_to: localhost
set_fact:
mongod_excluded_files : "{{ (mongod_excluded_files| default([])) + [ item.0 ] }}"
when: item.0 is not match(item.1) or item.0 is match(item.2)
with_nested:
- "{{ mongod_files }}"
- "{{ mongodb.included_scripts }}"
- "{{ mongodb.excluded_scripts }}"

- name: Compute list of eligible files
delegate_to: localhost
set_fact:
mongod_eligible_files : "{{ (mongod_eligible_files| default([])) + [ {'name': item, 'version': item | regex_replace('^(.+)/(.+)$', '\\1') ,'finalname': 'vitamui_' + item | regex_replace('/', '_') | basename | regex_replace('\\.j2$')} ] }}"
loop: "{{ mongod_files | difference(mongod_excluded_files| default([])) }}"

# We generate scripts and upload on remote host
- name: Compute and copy script files
template:
src: "{{ mongod_source_template_dir }}/{{ item.name }}"
dest: "{{ mongod_output_dir_entry_point }}/{{ item.finalname }}"
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: 0755
loop: "{{ mongod_eligible_files | unique }}"

- name: "Prepare file"
include_tasks: "prepare_script.yml"
when: mongodb.versioning is defined and mongodb.versioning.enable
loop: "{{ mongod_eligible_files | unique }}"
loop_control:
loop_var: mongo_file

- name: Compute main script file
template:
src: "main_script.js.j2"
dest: "{{ mongod_output_dir_entry_point }}/main_script.js"
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: 0755

- name: Load script in database
shell: "mongo \"mongodb://{{ mongod_uri }}/admin\" {{ mongo_credentials }} {{ 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 \"mongo \\\"mongodb://{{ mongod_uri }}/admin\\\" {{ mongo_credentials }} {{ 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"
# include_tasks: "execute_script.yml"
# loop: "{{ mongod_eligible_files | unique }}"
# loop_control:
# loop_var: mongo_file
- block:

- fail: msg="Variable '{{ mongod_source_template_dir }}' is not defined"
when: mongod_source_template_dir is undefined

- 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'] }}"

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

- name: Set mongod_output_dir_entry_point
set_fact:
mongod_output_dir_entry_point: "{{ vitamui_defaults.folder.root_path | default('/vitamui') }}/app/mongod/"

- import_tasks: check_auth.yml

- name: Initialize directory if it doesn't exist.
file:
path: "{{ mongod_output_dir_entry_point }}"
state: directory

- name: "Clean directory {{ mongod_output_dir_entry_point }}"
shell: "rm -Rf {{ mongod_output_dir_entry_point }}/*"

# We sort directories by theirs versions
- name: List script files versions in the directory {{ mongod_source_template_dir }}
delegate_to: localhost
shell:
cmd: find * -maxdepth 1 -type d | sort -V
chdir: "{{ mongod_source_template_dir }}"
register: versions

# For each version, we apply a second sort on the index of the script file.
- name: List script files in the directory {{ mongod_source_template_dir }}
delegate_to: localhost
shell:
cmd: find {{ version }}/* -type f -print | sort -V -t '_' -k1
chdir: "{{ mongod_source_template_dir }}"
register: output
loop: "{{ versions.stdout_lines }}"
loop_control:
loop_var: version

- name: "Compute file scripts"
delegate_to: localhost
set_fact:
mongod_files: "{{ (mongod_files| default([])) + item.stdout_lines }}"
loop: "{{ output.results }}"

# We apply regex for included and excluded files in order to compute the eligible scripts.
- name: Compute list of excluded files
delegate_to: localhost
set_fact:
mongod_excluded_files : "{{ (mongod_excluded_files| default([])) + [ item.0 ] }}"
when: item.0 is not match(item.1) or item.0 is match(item.2)
with_nested:
- "{{ mongod_files }}"
- "{{ mongodb.included_scripts }}"
- "{{ mongodb.excluded_scripts }}"

- name: Compute list of eligible files
delegate_to: localhost
set_fact:
mongod_eligible_files : "{{ (mongod_eligible_files| default([])) + [ {'name': item, 'version': item | regex_replace('^(.+)/(.+)$', '\\1') ,'finalname': 'vitamui_' + item | regex_replace('/', '_') | basename | regex_replace('\\.j2$')} ] }}"
loop: "{{ mongod_files | difference(mongod_excluded_files| default([])) }}"

# We generate scripts and upload on remote host
- name: Compute and copy script files
template:
src: "{{ mongod_source_template_dir }}/{{ item.name }}"
dest: "{{ mongod_output_dir_entry_point }}/{{ item.finalname }}"
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: 0755
loop: "{{ mongod_eligible_files | unique }}"

- name: "Prepare file"
include_tasks: "prepare_script.yml"
when: mongodb.versioning is defined and mongodb.versioning.enable
loop: "{{ mongod_eligible_files | unique }}"
loop_control:
loop_var: mongo_file

- name: Compute main script file
template:
src: "main_script.js.j2"
dest: "{{ mongod_output_dir_entry_point }}/main_script.js"
owner: "{{ vitamui_defaults.users.vitamuidb | default('vitamuidb') }}"
group: "{{ vitamui_defaults.users.group | default('vitamui') }}"
mode: 0755

- name: Load script in database
shell: "mongo \"mongodb://{{ mongod_uri }}/admin\" {{ mongo_credentials }} {{ 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 \"mongo \\\"mongodb://{{ mongod_uri }}/admin\\\" {{ mongo_credentials }} {{ 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"
# include_tasks: "execute_script.yml"
# loop: "{{ mongod_eligible_files | unique }}"
# loop_control:
# loop_var: mongo_file

run_once: true

0 comments on commit c692ee4

Please sign in to comment.