From 07e4bb17b9da25fbfb049f57a90bd90318f10abe Mon Sep 17 00:00:00 2001 From: James Montalvo Date: Tue, 3 Oct 2023 06:07:26 -0500 Subject: [PATCH] feat: change push_backup props to flat env-var-compatible vars e.g. push_backup.db.addr is now push_backup_db_addr Backwards compatibility shim is in set-vars role --- src/playbooks/push-backup.yml | 4 +- .../templates/meza-autodeployer-cron.j2 | 4 +- src/roles/backup-db-wikis-push/tasks/main.yml | 12 ++-- src/roles/backup-uploads-push/tasks/main.yml | 12 ++-- src/roles/set-vars/tasks/main.yml | 6 ++ src/roles/set-vars/tasks/push-backup.yml | 71 +++++++++++++++++++ 6 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/roles/set-vars/tasks/push-backup.yml diff --git a/src/playbooks/push-backup.yml b/src/playbooks/push-backup.yml index b04f6b7e..da7817c9 100644 --- a/src/playbooks/push-backup.yml +++ b/src/playbooks/push-backup.yml @@ -22,7 +22,7 @@ - name: Notify push backup is starting slack: token: "{{ autodeployer.slack_token }}" - msg: "Push backup to {{ push_backup.short_name | default('remote server') }} starting" + msg: "Push backup to {{ push_backup_short_name | default('remote server') }} starting" channel: "{{ autodeployer.slack_channel }}" username: "{{ autodeployer.slack_username }}" icon_url: "{{ autodeployer_slack_icon_url }}" @@ -70,7 +70,7 @@ - name: Notify push backup is complete slack: token: "{{ autodeployer.slack_token }}" - msg: "Push backup to {{ push_backup.short_name | default('remote server') }} complete" + msg: "Push backup to {{ push_backup_short_name | default('remote server') }} complete" channel: "{{ autodeployer.slack_channel }}" username: "{{ autodeployer.slack_username }}" icon_url: "{{ autodeployer_slack_icon_url }}" diff --git a/src/roles/autodeployer/templates/meza-autodeployer-cron.j2 b/src/roles/autodeployer/templates/meza-autodeployer-cron.j2 index f06d67cf..00be308f 100644 --- a/src/roles/autodeployer/templates/meza-autodeployer-cron.j2 +++ b/src/roles/autodeployer/templates/meza-autodeployer-cron.j2 @@ -18,10 +18,10 @@ MAILTO=root {{ autodeployer.crontime }} root meza autodeploy "{{ env }}" "Deploy" "" >> {{ m_logs }}/deploy/check-for-changes-`date "+\%Y\%m\%d"`.log 2>&1 -{% if push_backup is defined and push_backup.crontime is defined %} +{% if push_backup is defined and push_backup_crontime is defined %} # # Push backup (db and uploads) to another server periodically # -{{ push_backup.crontime }} root meza push-backup "{{ env }}" >> {{ m_logs }}/deploy/push-backup-`date "+\%Y\%m\%d"`.log 2>&1 +{{ push_backup_crontime }} root meza push-backup "{{ env }}" >> {{ m_logs }}/deploy/push-backup-`date "+\%Y\%m\%d"`.log 2>&1 {% endif %} {% endif %} diff --git a/src/roles/backup-db-wikis-push/tasks/main.yml b/src/roles/backup-db-wikis-push/tasks/main.yml index 78986334..0853a3d9 100644 --- a/src/roles/backup-db-wikis-push/tasks/main.yml +++ b/src/roles/backup-db-wikis-push/tasks/main.yml @@ -14,29 +14,29 @@ - name: Set remote_server_base_path if set in configuration set_fact: - remote_server_base_path: "{{ push_backup.db.path }}" + remote_server_base_path: "{{ push_backup_db_path }}" when: - - push_backup.db.path is defined + - push_backup_db_path is defined - name: Set remote_server_base_path if NOT set in configuration set_fact: remote_server_base_path: "{{ m_backups }}/{{ env }}//" when: - - push_backup.db.path is not defined + - push_backup_db_path is not defined - name: Output value of remote_server_base_path ( will be replaced by each wiki_id) debug: { var: remote_server_base_path } -- name: "Run role:rsync-push - Copy SQL files to {{ push_backup.db.addr }}" +- name: "Run role:rsync-push - Copy SQL files to {{ push_backup_db_addr }}" include_role: name: rsync-push vars: pushing_from_server: "{{ inventory_hostname }}" pushing_from_path: "{{ m_tmp }}/{{ env }}_{{ item }}.sql" - pushing_to_server: "{{ push_backup.db.addr }}" + pushing_to_server: "{{ push_backup_db_addr }}" # remote_server_base_path + backup_timestamp + _wiki.sql, but replace with wiki_id (item) pushing_to_path: "{{ remote_server_base_path | regex_replace('', item) }}{{ backup_timestamp }}_wiki_push.sql" - pushing_to_user: "{{ push_backup.remote_user }}" + pushing_to_user: "{{ push_backup_remote_user }}" with_items: "{{ wiki_dirs.files | map(attribute='path') | map('basename') | list }}" # Remove temp SQL files, only needs to be done on first backup server diff --git a/src/roles/backup-uploads-push/tasks/main.yml b/src/roles/backup-uploads-push/tasks/main.yml index fa88fc65..40f8b59e 100644 --- a/src/roles/backup-uploads-push/tasks/main.yml +++ b/src/roles/backup-uploads-push/tasks/main.yml @@ -20,9 +20,9 @@ set_fact: # Likely path if pushing to a live Meza uploads directory: # /opt/data-meza/uploads// - remote_server_base_path: "{{ push_backup.uploads.path }}" + remote_server_base_path: "{{ push_backup_uploads_path }}" when: - - push_backup.uploads.path is defined + - push_backup_uploads_path is defined - name: Set remote_server_base_path if NOT set in configuration set_fact: @@ -30,19 +30,19 @@ # /opt/data-meza/backups///uploads/ remote_server_base_path: "{{ m_backups }}/{{ env }}//uploads/" when: - - push_backup.uploads.path is not defined + - push_backup_uploads_path is not defined - name: Output value of remote_server_base_path ( will be replaced by each wiki_id) debug: { var: remote_server_base_path } -- name: "Run role:rsync-push - Copy uploads directory to {{ push_backup.uploads.addr }}" +- name: "Run role:rsync-push - Copy uploads directory to {{ push_backup_uploads_addr }}" include_role: name: rsync-push vars: pushing_from_server: "{{ inventory_hostname }}" pushing_from_path: "{{ m_uploads_dir }}/{{ item }}/" - pushing_to_server: "{{ push_backup.uploads.addr }}" + pushing_to_server: "{{ push_backup_uploads_addr }}" # remote_server_base_path + backup_timestamp, but replace with wiki_id (item) pushing_to_path: "{{ remote_server_base_path | regex_replace('', item) }}" - pushing_to_user: "{{ push_backup.remote_user }}" + pushing_to_user: "{{ push_backup_remote_user }}" with_items: "{{ wiki_dirs.files | map(attribute='path') | map('basename') | list }}" diff --git a/src/roles/set-vars/tasks/main.yml b/src/roles/set-vars/tasks/main.yml index 7105dfc9..c9c1a06c 100644 --- a/src/roles/set-vars/tasks/main.yml +++ b/src/roles/set-vars/tasks/main.yml @@ -75,3 +75,9 @@ # Can't load secret.yml when it doesn't exist yet, so skip this while setting # up an environment when: not allow_missing_secret_config|default(false) + +# +# Misc +# +- name: Include push_backup backwards compatibility + include: push-backup.yml diff --git a/src/roles/set-vars/tasks/push-backup.yml b/src/roles/set-vars/tasks/push-backup.yml new file mode 100644 index 00000000..7c23351f --- /dev/null +++ b/src/roles/set-vars/tasks/push-backup.yml @@ -0,0 +1,71 @@ +--- +# +# Set variables associated with pushing backups. Previously these variables were only +# set like: +# +# push_backup: +# crontime: "0 18 * * *" +# remote_user: meza-ansible +# short_name: "staging" +# db: +# addr: "staging.example.com" +# path: /opt/data-meza/backups/staging// +# uploads: +# addr: "staging.example.com" +# path: /opt/data-meza/uploads/ +# +# In order to support running push-backup only with environment variables, the above +# variables (which are all properties based off push_backup root variable) have been +# re-written in the following form: +# +# push_backup.db.addr --> push_backup_db_addr +# +# This file enables backwards compatibility by taking values like `push_backup.db.addr` +# and creating `push_backup_db_addr`. +# + +- set_fact: + push_backup_crontime: "{{ push_backup.crontime }}" + when: + - push_backup is defined + - push_backup.crontime is defined + +- set_fact: + push_backup_remote_user: "{{ push_backup.remote_user }}" + when: + - push_backup is defined + - push_backup.remote_user is defined + +- set_fact: + push_backup_short_name: "{{ push_backup.short_name }}" + when: + - push_backup is defined + - push_backup.short_name is defined + +- set_fact: + push_backup_db_addr: "{{ push_backup.db.addr }}" + when: + - push_backup is defined + - push_backup.db is defined + - push_backup.db.addr is defined + +- set_fact: + push_backup_db_path: "{{ push_backup.db.path }}" + when: + - push_backup is defined + - push_backup.db is defined + - push_backup.db.path is defined + +- set_fact: + push_backup_uploads_addr: "{{ push_backup.uploads.addr }}" + when: + - push_backup is defined + - push_backup.uploads is defined + - push_backup.uploads.addr is defined + +- set_fact: + push_backup_uploads_path: "{{ push_backup.uploads.path }}" + when: + - push_backup is defined + - push_backup.uploads is defined + - push_backup.uploads.path is defined