Skip to content

Commit 98a8173

Browse files
authored
Change run_once to throttle to allow independent databases (#400)
* Change run_once to throttle to allow independent databases * Use throttle also for icingaweb schema import
1 parent e97e494 commit 98a8173

File tree

12 files changed

+131
-123
lines changed

12 files changed

+131
-123
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
bugfixes:
3+
- |
4+
Switch from :code:`run_once` which was used to ensure it runs only once in a cluster resulting in propagated the databases only on the first node even if nodes are independent to :code:`throttle`.
5+
This was also fixed for some cases where :code:`run_once` was missing in the current state.
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
- name: MySQL import Kubernetes schema
23
when: icinga_kubernetes_database_import_schema | default(False)
34
block:
@@ -13,19 +14,15 @@
1314
-p{{ icinga_kubernetes_database_password | quote }}
1415
"{{ icinga_kubernetes_database_name | default('kubernetes') }}"
1516
16-
- name: MySQL check for Kubernetes schema
17-
ansible.builtin.shell: >
18-
{{ mysqlcmd }}
19-
-Ns -e "select version from kubernetes_schema"
20-
failed_when: false
21-
changed_when: false
22-
check_mode: false
23-
register: _db_schema
24-
2517
- name: MySQL import Kubernetes schema
2618
ansible.builtin.shell: >
2719
{{ mysqlcmd }}
28-
< {{ icinga_kubernetes_database_schema }}
29-
when: _db_schema.rc != 0
30-
check_mode: false
31-
run_once: true
20+
-Ns -e "select version from kubernetes_schema" ||
21+
(
22+
{{ mysqlcmd }} < {{ icinga_kubernetes_database_schema }} &&
23+
echo "Schema imported"
24+
)
25+
register: _icingakubernetes_schema
26+
changed_when:
27+
- '"Schema imported" in _icingakubernetes_schema.stdout_lines'
28+
throttle: 1
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
# temporarily disabled, pgsql at the moment is not available for icinga kubernetes
23

34
# - name: PgSQL import Kubernetes schema
@@ -18,19 +19,16 @@
1819
# {% if icinga_kubernetes_database_key is defined %} sslkey={{ icinga_kubernetes_database_key }} {%- endif %}
1920
# {% if icinga_kubernetes_database_ssl_extra_options is defined %} {{ icinga_kubernetes_database_ssl_extra_options }} {%- endif %}"
2021

21-
# - name: PgSQL check for Kubernetes schema
22-
# ansible.builtin.shell: >
23-
# {{ _tmp_pgsqlcmd }}
24-
# -w -c "select version from kubernetes_schema"
25-
# failed_when: false
26-
# changed_when: false
27-
# check_mode: false
28-
# register: _db_schema
29-
3022
# - name: PgSQL import Kubernetes schema
3123
# ansible.builtin.shell: >
3224
# {{ _tmp_pgsqlcmd }}
33-
# -w -f {{ icinga_kubernetes_database_schema }}
34-
# when: _db_schema.rc != 0
35-
# check_mode: false
36-
# run_once: true
25+
# -w -c "select version from kubernetes_schema" ||
26+
# (
27+
# {{ _tmp_pgsqlcmd }}
28+
# -w -f {{ icinga_kubernetes_database_schema }} &&
29+
# echo "Schema imported"
30+
# )
31+
# register: _icingakubernetes_schema
32+
# changed_when:
33+
# - '"Schema imported" in _icingakubernetes_schema.stdout_lines'
34+
# throttle: 1
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
- name: MySQL import IcingaDB schema
23
when: icingadb_database_import_schema | default(False)
34
block:
@@ -13,19 +14,15 @@
1314
-p{{ icingadb_database_password | quote }}
1415
"{{ icingadb_database_name | default('icingadb') }}"
1516
16-
- name: MySQL check for IcingaDB schema
17-
ansible.builtin.shell: >
18-
{{ mysqlcmd }}
19-
-Ns -e "select version from icingadb_schema"
20-
failed_when: false
21-
changed_when: false
22-
check_mode: false
23-
register: _db_schema
24-
2517
- name: MySQL import IcingaDB schema
2618
ansible.builtin.shell: >
2719
{{ mysqlcmd }}
28-
< {{ icingadb_database_schema }}
29-
when: _db_schema.rc != 0
30-
check_mode: false
31-
run_once: yes
20+
-Ns -e "select version from icingadb_schema" ||
21+
(
22+
{{ mysqlcmd }} < {{ icingadb_database_schema }} &&
23+
echo "Schema imported"
24+
)
25+
register: _icingadb_schema
26+
changed_when:
27+
- '"Schema imported" in _icingadb_schema.stdout_lines'
28+
throttle: 1
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
- name: PgSQL import IcingaDB schema
23
when: icingadb_database_import_schema| default(False)
34
block:
@@ -16,19 +17,15 @@
1617
{% if icingadb_database_key is defined %} sslkey={{ icingadb_database_key }} {%- endif %}
1718
{% if icingadb_database_ssl_extra_options is defined %} {{ icingadb_database_ssl_extra_options }} {%- endif %}"
1819
19-
- name: PgSQL check for IcingaDB schema
20-
ansible.builtin.shell: >
21-
{{ _tmp_pgsqlcmd }}
22-
-w -c "select version from icingadb_schema"
23-
failed_when: false
24-
changed_when: false
25-
check_mode: false
26-
register: _db_schema
27-
2820
- name: PgSQL import IcingaDB schema
2921
ansible.builtin.shell: >
3022
{{ _tmp_pgsqlcmd }}
31-
-w -f {{ icingadb_database_schema }}
32-
when: _db_schema.rc != 0
33-
check_mode: false
34-
run_once: yes
23+
-w -c "select version from icingadb_schema" ||
24+
(
25+
{{ _tmp_pgsqlcmd }} -w -f {{ icingadb_database_schema }} &&
26+
echo "Schema imported"
27+
)
28+
register: _icingadb_schema
29+
changed_when:
30+
- '"Schema imported" in _icingadb_schema.stdout_lines'
31+
throttle: 1

roles/icingaweb2/handlers/main.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
---
22

3-
- name: Module Director | Apply pending migrations
4-
ansible.builtin.command:
5-
cmd: icingacli director migration run
3+
- name: Module Director | Apply pending migrations # noqa: command-instead-of-shell
4+
ansible.builtin.shell:
5+
cmd: icingacli director migration pending && icingacli director migration run || echo "No changes"
6+
throttle: 1
67
listen: "run_director_migrations"
8+
register: _tmp_director_migration
9+
changed_when: 'not "No changes" in _tmp_director_migration.stdout_lines'
10+
failed_when: _tmp_director_migration.stderr_lines | length > 0
711

8-
- name: Module Director | Run kickstart if required
9-
ansible.builtin.command:
10-
cmd: icingacli director kickstart run
12+
- name: Module Director | Run kickstart if required # noqa: command-instead-of-shell
13+
ansible.builtin.shell:
14+
cmd: icingacli director kickstart required && icingacli director kickstart run || echo "No changes"
15+
throttle: 1
1116
listen: "run_director_kickstart"
17+
register: _tmp_director_kickstart
18+
changed_when: 'not "No changes" in _tmp_director_kickstart.stdout_lines'
19+
failed_when: _tmp_director_kickstart.stderr_lines | length > 0 or "Kickstart has not been configured" in _tmp_director_kickstart.stdout_lines

roles/icingaweb2/tasks/modules/manage_mysql_imports.yml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,15 @@
2121
-p{{ _db['password'] | quote }}
2222
"{{ _db['name'] }}"
2323
24-
- name: MySQL check for db schema
24+
- name: MySQL import db schema
2525
ansible.builtin.shell: >
2626
{{ _tmp_mysqlcmd }}
27-
-Ns -e "{{ _db['select_query'] }}"
28-
failed_when: false
29-
changed_when: false
30-
check_mode: false
31-
register: _db_schema
32-
33-
- name: MySQL import db schema
34-
ansible.builtin.shell: >
35-
{{ _tmp_mysqlcmd }}
36-
< {{ _db['schema_path_mysql'] }}
37-
when: _db_schema.rc != 0
38-
run_once: yes
27+
-Ns -e "{{ _db['select_query'] }}" ||
28+
(
29+
{{ _tmp_mysqlcmd }} < {{ _db['schema_path_mysql'] }} &&
30+
echo "Schema imported"
31+
)
32+
register: _icingaweb2_schema
33+
changed_when:
34+
- '"Schema imported" in _icingaweb2_schema.stdout_lines'
35+
throttle: 1

roles/icingaweb2/tasks/modules/manage_pgsql_imports.yml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020
{% if _db['ssl_key'] is defined %} sslkey={{ _db['ssl_key'] }}{%- endif %}
2121
{% if _db['ssl_extra_options'] is defined %} {{ _db['ssl_extra_options'] }}{%- endif %}"
2222
23-
- name: PostgresQL check for db schema
23+
- name: PostgresQL import db schema
2424
ansible.builtin.shell: >
2525
{{ _tmp_pgsqlcmd }}
26-
-tq -c "{{ _db['select_query'] }}"
27-
failed_when: false
28-
changed_when: false
29-
check_mode: false
30-
register: _db_schema
31-
32-
- name: PostgresQL import db schema
33-
ansible.builtin.shell: >
34-
{{ _tmp_pgsqlcmd }}
35-
< {{ _db['schema_path_pgsql'] }}
36-
when: _db_schema.rc != 0
37-
run_once: yes
26+
-tq -c "{{ _db['select_query'] }}" ||
27+
(
28+
{{ _tmp_pgsqlcmd }} < {{ _db['schema_path_pgsql'] }} &&
29+
echo "Schema imported"
30+
)
31+
register: _icingaweb2_schema
32+
changed_when:
33+
- '"Schema imported" in _icingaweb2_schema.stdout_lines'
34+
throttle: 1

roles/icingaweb2/tasks/mysql/import_db.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
- name: MySQL check for icingaweb db schema
44
ansible.builtin.shell: >
55
{{ _tmp_mysqlcmd }}
6-
-Ns -e "select * from icingaweb_user"
7-
failed_when: false
8-
changed_when: false
9-
check_mode: false
10-
register: _icingaweb2_db_schema
6+
-Ns -e "select * from icingaweb_user" ||
7+
(
8+
{{ _tmp_mysqlcmd }} < /usr/share/icingaweb2/schema/mysql.schema.sql &&
9+
echo "Schema imported"
10+
)
11+
register: _icingaweb_schema
12+
changed_when:
13+
- '"Schema imported" in _icingaweb_schema.stdout_lines'
14+
throttle: 1
1115

12-
- name: MySQL import icingaweb db schema
13-
ansible.builtin.shell: >
14-
{{ _tmp_mysqlcmd }}
15-
< /usr/share/icingaweb2/schema/mysql.schema.sql
16-
when: _icingaweb2_db_schema.rc != 0
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
---
2-
3-
- name: MySQL check for icingaweb db schema
2+
- name: MySQL create Icinga Web 2 user
43
ansible.builtin.shell: >
54
{{ _tmp_mysqlcmd }}
65
-Ns -e "select name from icingaweb_user where name like '{{ _current_user.username }}'"
7-
failed_when: false
8-
changed_when: false
9-
check_mode: false
6+
| grep '{{ _current_user.username }}' ||
7+
(
8+
echo "INSERT INTO icingaweb_user (name, active, password_hash) VALUES ('{{ _current_user.username }}', 1,
9+
'"`php -r 'echo password_hash("{{ _current_user.password }}", PASSWORD_DEFAULT);'`"')
10+
ON DUPLICATE KEY UPDATE active = 1, password_hash = '"`php -r 'echo password_hash("{{ _current_user.password }}", PASSWORD_DEFAULT);'`"'" | {{ _tmp_mysqlcmd }} -Ns &&
11+
echo "User created"
12+
)
1013
register: _icingaweb2_db_user
14+
when: (_current_user.recreate | default(false)) is false
15+
changed_when:
16+
- '"User created" in _icingaweb2_db_user.stdout_lines'
17+
throttle: 1
1118

12-
- name: Create user in Icinga Web (or reenable user / reset password)
13-
run_once: true
14-
ansible.builtin.shell: >-
19+
- name: MySQL recreate Icinga Web 2 user
20+
ansible.builtin.shell: >
1521
echo "INSERT INTO icingaweb_user (name, active, password_hash) VALUES ('{{ _current_user.username }}', 1,
1622
'"`php -r 'echo password_hash("{{ _current_user.password }}", PASSWORD_DEFAULT);'`"')
1723
ON DUPLICATE KEY UPDATE active = 1, password_hash = '"`php -r 'echo password_hash("{{ _current_user.password }}", PASSWORD_DEFAULT);'`"'" | {{ _tmp_mysqlcmd }} -Ns
18-
when: (_icingaweb2_db_user.stdout_lines | length <= 0) or (_current_user.recreate is true)
24+
when: (_current_user.recreate | default(false)) is true
25+
throttle: 1

0 commit comments

Comments
 (0)