diff --git a/docs/roles/database_backup/database_backup-mysql.md b/docs/roles/database_backup/database_backup-mysql.md index c52620f6..3a7ec471 100644 --- a/docs/roles/database_backup/database_backup-mysql.md +++ b/docs/roles/database_backup/database_backup-mysql.md @@ -25,6 +25,7 @@ mysql_backup: - database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit credentials_file: "/home/{{ deploy_user }}/.mysql.creds" + #handling: static # optional override to the main handling method on a per database basis ``` diff --git a/roles/database_backup/database_backup-mysql/README.md b/roles/database_backup/database_backup-mysql/README.md index c52620f6..3a7ec471 100644 --- a/roles/database_backup/database_backup-mysql/README.md +++ b/roles/database_backup/database_backup-mysql/README.md @@ -25,6 +25,7 @@ mysql_backup: - database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit credentials_file: "/home/{{ deploy_user }}/.mysql.creds" + #handling: static # optional override to the main handling method on a per database basis ``` diff --git a/roles/database_backup/database_backup-mysql/defaults/main.yml b/roles/database_backup/database_backup-mysql/defaults/main.yml index 1cbc2e11..777cc6f9 100644 --- a/roles/database_backup/database_backup-mysql/defaults/main.yml +++ b/roles/database_backup/database_backup-mysql/defaults/main.yml @@ -20,3 +20,4 @@ mysql_backup: - database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit credentials_file: "/home/{{ deploy_user }}/.mysql.creds" + #handling: static # optional override to the main handling method on a per database basis diff --git a/roles/database_backup/database_backup-mysql/tasks/deploy-rolling.yml b/roles/database_backup/database_backup-mysql/tasks/deploy-rolling.yml index aec45596..6a264be1 100644 --- a/roles/database_backup/database_backup-mysql/tasks/deploy-rolling.yml +++ b/roles/database_backup/database_backup-mysql/tasks/deploy-rolling.yml @@ -4,26 +4,41 @@ _mysql_build_database_name: "{{ database.database }}_{{ build_number }}" - ansible.builtin.set_fact: _mysql_previous_build_database_name: "{{ database.database }}_{{ previous_build_number }}" - # Note: we don't use the mysql_db Ansible module on purpose. - # If database already exists, we want to fail and not override it - # with previous build. - # @TODO fix this so we check if the database exists and exit with - # the proper plugin instead of using command. -- name: Create new database. - ansible.builtin.command: mysql --defaults-extra-file={{ database.credentials_file }} -e "CREATE DATABASE `{{ _mysql_build_database_name }}`;" + +- name: Check if the new database name exists already. + ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep {{ _mysql_build_database_name }}" + register: _build_db_status + failed_when: _build_db_status.rc == 0 # we want the build to fail if the database exists + run_once: true + +- name: Create a new database. + community.mysql.mysql_db: + name: "{{ _mysql_build_database_name }}" + state: present + config_file: "{{ database.credentials_file }}" + config_overrides_defaults: true run_once: true -#- name: Create a new database. -# community.mysql.mysql_db: -# name: "{{ _mysql_build_database_name }}" -# state: present -# config_file: "{{ database.credentials_file }}" -# config_overrides_defaults: true -# run_once: true +- name: Check if the previous database exists. + ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep {{ _mysql_previous_build_database_name }}" + register: _previous_db_status + failed_when: _previous_db_status.rc > 1 # only fail if we get an unexpected exitcode + run_once: true +# Importing with shell for speed, we cannot import with mysql_db unless we create a dump file first - name: Populate new database. ansible.builtin.shell: "set -o pipefail && mysqldump --defaults-extra-file={{ database.credentials_file }} {{ mysql_backup.mysqldump_params }} {{ _mysql_previous_build_database_name }} | mysql --defaults-extra-file={{ database.credentials_file }} {{ _mysql_build_database_name }}" args: executable: /bin/bash - when: previous_build_number > 0 + when: + - previous_build_number > 0 + - _previous_db_status.rc == 0 # only run if we have a database + run_once: true + +# Making it clear if we skipped the population of the new database +- name: Populating database skipped. + ansible.builtin.debug: "### Attention - the new database was NOT populated!" + when: + - previous_build_number > 0 + - _previous_db_status.rc != 0 run_once: true diff --git a/roles/database_backup/database_backup-mysql/tasks/deploy.yml b/roles/database_backup/database_backup-mysql/tasks/deploy.yml index 4160806c..6e60a96d 100644 --- a/roles/database_backup/database_backup-mysql/tasks/deploy.yml +++ b/roles/database_backup/database_backup-mysql/tasks/deploy.yml @@ -82,7 +82,15 @@ _mysql_build_password: "{{ lookup('password', '/dev/shm/{{ project_name }}_{{ build_type }}_{{ build_number }}') }}" when: mysql_backup.credentials_handling == 'rotate' -- ansible.builtin.include_tasks: "deploy-{{ mysql_backup.handling }}.yml" +- ansible.builtin.set_fact: + _mysql_handling: "{{ mysql_backup.handling }}" +# If we have a specific instruction for handling this database differently, use it. +- ansible.builtin.set_fact: + _mysql_handling: "{{ mysql_backup.database.handling }}" + when: + - mysql_backup.database.handling is defined + - mysql_backup.database.handling | length > 0 +- ansible.builtin.include_tasks: "deploy-{{ _mysql_handling }}.yml" # We append privileges instead of replacing, # to allow this role to be looped over,