From e7a2105f01583cc158fe8e8059a5f757d8d3a35d Mon Sep 17 00:00:00 2001 From: Patrick Simonian Date: Fri, 17 Dec 2021 15:50:51 -0800 Subject: [PATCH 1/3] add new playbook for data migrations --- .../aqua-post-config/playbook/migrate_db.yaml | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 apps/aqua-post-config/playbook/migrate_db.yaml diff --git a/apps/aqua-post-config/playbook/migrate_db.yaml b/apps/aqua-post-config/playbook/migrate_db.yaml new file mode 100644 index 00000000..328c0da9 --- /dev/null +++ b/apps/aqua-post-config/playbook/migrate_db.yaml @@ -0,0 +1,43 @@ +--- +- hosts: localhost + connection: local + gather_facts: no + vars: + # aqua_web_name: "{{ lookup('env', 'AQUA_WEB_NAME') }}" + # dump_path: "{{ lookup('env', 'DUMP_PATH') }}" + # dump_file: dump.sql + # target_db_username: "{{ lookup('env', 'TARGET_DB_USERNAME') }}" + # target_db_database: "{{ lookup('env', 'TARGET_DB_DATABASE') }}" + # target_db_host: "{{ lookup('env', 'TARGET_DB_HOST') }}" + # target_db_password: "{{ lookup('env', 'TARGET_DB_PASSWORD') }}" + # target_db_username: "{{ lookup('env', 'SOURCE_DB_USERNAME') }}" + # source_db_database: "{{ lookup('env', 'SOURCE_DB_DATABASE') }}" + # source_db_host: "{{ lookup('env', 'SOURCE_DB_HOST') }}" + # source_db_password: "{{ lookup('env', 'SOURCE_DB_PASSWORD') }}" + + tasks: + - name: does aqua web exist? + run: oc get deployment/aqua-web -o json -n openshift-bcgov-aqua + register: aqua_web + + - name: setting fact for original desired replicas + set_fact: + desired_replicas: (aqua_web.stdout | from_json).spec.replicas + - name: setting fact for aqua_web_exists + set_fact: aqua_web.stdout == aqua-web + aqua_web_exists: "Error from server (NotFound)" not in aqua_web.stdout + + - name: scale aqua service down to 0 to prevent new transactions to database + run: oc scale deployment/aqua-web --replicas=0 + when: aqua_web_exists + + - name: perform source db dump + run: pg_dump --dbname={{ source_db_database }} --host={{ source_db_host }} --port=5432 > {{ dump_path }}/{{ dump_file }} + + - name: perform target db recovery + run: psql --dbname={{ target_db_database }} --host={{ target_db_host }} --port=5432 < {{ dump_path }}/{{ dump_file }} + + - name: scale aqua backup + run: oc scale deployment/aqua-web --replicas={{ desired_replicas }} + when: aqua_web_exists + From c228b08da1dc73284929c5272a78bea4f8fe314f Mon Sep 17 00:00:00 2001 From: Patrick Simonian Date: Fri, 17 Dec 2021 15:54:31 -0800 Subject: [PATCH 2/3] include vars --- .../aqua-post-config/playbook/migrate_db.yaml | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/aqua-post-config/playbook/migrate_db.yaml b/apps/aqua-post-config/playbook/migrate_db.yaml index 328c0da9..35204abe 100644 --- a/apps/aqua-post-config/playbook/migrate_db.yaml +++ b/apps/aqua-post-config/playbook/migrate_db.yaml @@ -3,32 +3,32 @@ connection: local gather_facts: no vars: - # aqua_web_name: "{{ lookup('env', 'AQUA_WEB_NAME') }}" - # dump_path: "{{ lookup('env', 'DUMP_PATH') }}" - # dump_file: dump.sql - # target_db_username: "{{ lookup('env', 'TARGET_DB_USERNAME') }}" - # target_db_database: "{{ lookup('env', 'TARGET_DB_DATABASE') }}" - # target_db_host: "{{ lookup('env', 'TARGET_DB_HOST') }}" - # target_db_password: "{{ lookup('env', 'TARGET_DB_PASSWORD') }}" - # target_db_username: "{{ lookup('env', 'SOURCE_DB_USERNAME') }}" - # source_db_database: "{{ lookup('env', 'SOURCE_DB_DATABASE') }}" - # source_db_host: "{{ lookup('env', 'SOURCE_DB_HOST') }}" - # source_db_password: "{{ lookup('env', 'SOURCE_DB_PASSWORD') }}" + aqua_web_name: "{{ lookup('env', 'AQUA_WEB_NAME') }}" + dump_path: "{{ lookup('env', 'DUMP_PATH') }}" + dump_file: dump.sql + target_db_username: "{{ lookup('env', 'TARGET_DB_USERNAME') }}" + target_db_database: "{{ lookup('env', 'TARGET_DB_DATABASE') }}" + target_db_host: "{{ lookup('env', 'TARGET_DB_HOST') }}" + target_db_password: "{{ lookup('env', 'TARGET_DB_PASSWORD') }}" + target_db_username: "{{ lookup('env', 'SOURCE_DB_USERNAME') }}" + source_db_database: "{{ lookup('env', 'SOURCE_DB_DATABASE') }}" + source_db_host: "{{ lookup('env', 'SOURCE_DB_HOST') }}" + source_db_password: "{{ lookup('env', 'SOURCE_DB_PASSWORD') }}" tasks: - name: does aqua web exist? - run: oc get deployment/aqua-web -o json -n openshift-bcgov-aqua + run: oc get deployment/{{ aqua_web_name }} -o json -n openshift-bcgov-aqua register: aqua_web - name: setting fact for original desired replicas set_fact: desired_replicas: (aqua_web.stdout | from_json).spec.replicas - name: setting fact for aqua_web_exists - set_fact: aqua_web.stdout == aqua-web + set_fact: aqua_web_exists: "Error from server (NotFound)" not in aqua_web.stdout - name: scale aqua service down to 0 to prevent new transactions to database - run: oc scale deployment/aqua-web --replicas=0 + run: oc scale deployment/{{ aqua_web_name }} --replicas=0 when: aqua_web_exists - name: perform source db dump @@ -38,6 +38,6 @@ run: psql --dbname={{ target_db_database }} --host={{ target_db_host }} --port=5432 < {{ dump_path }}/{{ dump_file }} - name: scale aqua backup - run: oc scale deployment/aqua-web --replicas={{ desired_replicas }} + run: oc scale deployment/{{ aqua_web_name }} --replicas={{ desired_replicas }} when: aqua_web_exists From d882df6ee637a712b42c75f200792bcd5f808b2a Mon Sep 17 00:00:00 2001 From: Patrick Simonian Date: Thu, 30 Dec 2021 14:18:01 -0800 Subject: [PATCH 3/3] update db migration playbook --- apps/aqua-post-config/README.md | 1 + .../aqua-post-config/playbook/migrate_db.yaml | 63 ++++++++++++++----- apps/aqua-post-config/playbook/temp/.gitkeep | 0 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 apps/aqua-post-config/playbook/temp/.gitkeep diff --git a/apps/aqua-post-config/README.md b/apps/aqua-post-config/README.md index b558345a..0080c4b0 100644 --- a/apps/aqua-post-config/README.md +++ b/apps/aqua-post-config/README.md @@ -1,3 +1,4 @@ + ## Aqua Post Deployment Configuration This tool is a set of ansible playbooks to automate the post deployment steps needed for aqua to be configured correctly on fresh installs of the service diff --git a/apps/aqua-post-config/playbook/migrate_db.yaml b/apps/aqua-post-config/playbook/migrate_db.yaml index 35204abe..a5910a72 100644 --- a/apps/aqua-post-config/playbook/migrate_db.yaml +++ b/apps/aqua-post-config/playbook/migrate_db.yaml @@ -3,41 +3,70 @@ connection: local gather_facts: no vars: - aqua_web_name: "{{ lookup('env', 'AQUA_WEB_NAME') }}" - dump_path: "{{ lookup('env', 'DUMP_PATH') }}" - dump_file: dump.sql - target_db_username: "{{ lookup('env', 'TARGET_DB_USERNAME') }}" - target_db_database: "{{ lookup('env', 'TARGET_DB_DATABASE') }}" - target_db_host: "{{ lookup('env', 'TARGET_DB_HOST') }}" - target_db_password: "{{ lookup('env', 'TARGET_DB_PASSWORD') }}" - target_db_username: "{{ lookup('env', 'SOURCE_DB_USERNAME') }}" + aqua_web_name: aqua-web + source_dump_path: "{{ lookup('env', 'SOURCE_DUMP_PATH') }}" + target_restore_path: "{{ lookup('env', 'TARGET_RESTORE_PATH') }}" + dump_file: dump.sql + source_db_username: "{{ lookup('env', 'SOURCE_DB_USERNAME') }}" source_db_database: "{{ lookup('env', 'SOURCE_DB_DATABASE') }}" source_db_host: "{{ lookup('env', 'SOURCE_DB_HOST') }}" source_db_password: "{{ lookup('env', 'SOURCE_DB_PASSWORD') }}" - + source_db_pod_selector: "{{ lookup('env', 'SOURCE_DB_POD_SELECTOR') }}" + target_db_pod_selctor: "{{ lookup('env', 'TARGET_DB_POD_SELECTOR') }}" + target_db_username: mattdamon + target_db_database: scalock + target_db_host: aqua-db-patroni-master + target_db_password: "{{ lookup('env', 'TARGET_DB_PASSWORD') }}" + tasks: - name: does aqua web exist? - run: oc get deployment/{{ aqua_web_name }} -o json -n openshift-bcgov-aqua + shell: oc get deployment/{{ aqua_web_name }} -o json -n openshift-bcgov-aqua register: aqua_web - name: setting fact for original desired replicas set_fact: - desired_replicas: (aqua_web.stdout | from_json).spec.replicas + desired_replicas: "{{ (aqua_web.stdout | from_json).spec.replicas }}" + - name: setting fact for aqua_web_exists set_fact: - aqua_web_exists: "Error from server (NotFound)" not in aqua_web.stdout + aqua_web_exists: "{{ 'Error from server (NotFound)' not in aqua_web.stdout }}" - name: scale aqua service down to 0 to prevent new transactions to database - run: oc scale deployment/{{ aqua_web_name }} --replicas=0 + shell: oc scale deployment/{{ aqua_web_name }} --replicas=0 when: aqua_web_exists + - name: get source db pod name + shell: oc get pod -l {{ source_db_pod_selector }} -o json | jq -r '.items[0].metadata.name' + register: source_pod_name + + - name: get target db pod name + shell: oc get pod -l {{ target_db_pod_selctor }} -o json | jq -r '.items[0].metadata.name' + register: target_pod_name + - name: perform source db dump - run: pg_dump --dbname={{ source_db_database }} --host={{ source_db_host }} --port=5432 > {{ dump_path }}/{{ dump_file }} + shell: oc rsh -n openshift-bcgov-aqua {{ source_pod_name.stdout }} pg_dump -Fc {{ source_db_database }} --file={{ source_dump_path }}/{{ dump_file }} + + - name: copy dump to local + shell: oc cp -n openshift-bcgov-aqua "{{ source_pod_name.stdout }}:{{ source_dump_path }}/{{ dump_file }}" ./temp/{{ dump_file }} + + - name: cleanup dump file at source db + shell: oc rsh -n openshift-bcgov-aqua {{ source_pod_name.stdout }} rm {{ source_dump_path }}/{{ dump_file }} + + - name: move dump file to target db + shell: oc cp -n openshift-bcgov-aqua ./temp/{{ dump_file }} "{{ target_pod_name.stdout }}:{{ target_restore_path }}/{{ dump_file }}" - name: perform target db recovery - run: psql --dbname={{ target_db_database }} --host={{ target_db_host }} --port=5432 < {{ dump_path }}/{{ dump_file }} + shell: oc rsh -n openshift-bcgov-aqua {{ target_pod_name.stdout }} pg_restore {{ target_restore_path }}/{{ dump_file }} --dbname={{ source_db_database }} + + - name: cleanup dump file at target db + shell: oc rsh -n openshift-bcgov-aqua {{ target_pod_name.stdout }} rm {{ target_restore_path }}/{{ dump_file }} + + - name: cleanup dump file local + file: + state: absent + path: ./temp/{{ dump_file }} - - name: scale aqua backup - run: oc scale deployment/{{ aqua_web_name }} --replicas={{ desired_replicas }} + - name: scale aqua back up to {{ desired_replicas }} + shell: oc scale deployment/{{ aqua_web_name }} --replicas={{ desired_replicas }} when: aqua_web_exists diff --git a/apps/aqua-post-config/playbook/temp/.gitkeep b/apps/aqua-post-config/playbook/temp/.gitkeep new file mode 100644 index 00000000..e69de29b