From a172546531cc1347b70304009b0c28933e93c256 Mon Sep 17 00:00:00 2001 From: Manuel Carrer Date: Thu, 10 Oct 2024 11:45:10 +0200 Subject: [PATCH] Add migrate and deploy playbooks --- ansible/deploy.yml | 7 ++++ ansible/group_vars/servers.yaml | 15 +++++++ ansible/migrate.yml | 30 ++++++++++++++ db/partitions_generated.sql | 73 +++++++++++++++++++++++++++++++++ deploy.sh | 11 +++++ 5 files changed, 136 insertions(+) create mode 100644 ansible/deploy.yml create mode 100644 ansible/group_vars/servers.yaml create mode 100644 ansible/migrate.yml create mode 100644 db/partitions_generated.sql create mode 100644 deploy.sh diff --git a/ansible/deploy.yml b/ansible/deploy.yml new file mode 100644 index 0000000..e17750a --- /dev/null +++ b/ansible/deploy.yml @@ -0,0 +1,7 @@ +--- +- name: Deploy binaries + # Deploy on both VMs, only the primary is "active" + hosts: servers + remote_user: ubuntu + roles: + - role: deploy diff --git a/ansible/group_vars/servers.yaml b/ansible/group_vars/servers.yaml new file mode 100644 index 0000000..abfaab5 --- /dev/null +++ b/ansible/group_vars/servers.yaml @@ -0,0 +1,15 @@ +$ANSIBLE_VAULT;1.1;AES256 +39323433373866646663333962396637323937663436333763373663306264363337383838313531 +3237623337373630343264663232346366316332326564330a343062633165363564616663373366 +38303633346231626433393232313332373933626432613534646538326638623339323830613465 +3135643661323930370a656136326637373933353665316462373938396338383831353039323731 +61363032653830613438313564303432613531636436306662666336383838616132666234616538 +34313861306432626237383464623134386565643831396537343232646237323764346633373461 +30333265653634313436323735633733623032333039303461633931383337333664333636386532 +66383830323265303334323163313862393466383362646634653764373230613464393332363336 +63346438306666633835316333323464623261643861646636316637346363626162303662303039 +38616335356663343762356465653635623330383532656464633537333933613632336433653838 +36633130356262383464653935653864323233346162656639303861643533643563376464633530 +62343336663961363566383438393866353336366438343365363663623162313838396666343539 +39363766316532626463363533653561333933663130353632353934393534333965346637626636 +3138623135623031386437353434643736323166623661666263 diff --git a/ansible/migrate.yml b/ansible/migrate.yml new file mode 100644 index 0000000..cf6019c --- /dev/null +++ b/ansible/migrate.yml @@ -0,0 +1,30 @@ +--- +- name: Perform DB migration + hosts: servers + remote_user: ubuntu + gather_facts: false + vars: + primary: lard-a + + tasks: + - name: Copy the db folder to the remote + ansible.builtin.copy: + src: "{{ playbook_dir }}/../db/" + dest: /etc/postgresql/16/db/ + mode: "0755" + become: true + + - name: Migrate primary + when: inventory_hostname == primary + become: true + become_user: postgres + block: + - name: Update public schema in lard + community.postgresql.postgresql_script: + db: lard + path: /etc/postgresql/16/db/public.sql + + - name: Create partition tables in lard + community.postgresql.postgresql_script: + db: lard + path: /etc/postgresql/16/db/partitions_generated.sql diff --git a/db/partitions_generated.sql b/db/partitions_generated.sql new file mode 100644 index 0000000..070a914 --- /dev/null +++ b/db/partitions_generated.sql @@ -0,0 +1,73 @@ +-- Generated by simple script for testing +CREATE TABLE IF NOT EXISTS data_y1950_to_y2000 PARTITION OF public.data +FOR VALUES FROM ('1950-01-01 00:00:00+00') TO ('2000-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2000_to_y2010 PARTITION OF public.data +FOR VALUES FROM ('2000-01-01 00:00:00+00') TO ('2010-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2010_to_y2015 PARTITION OF public.data +FOR VALUES FROM ('2010-01-01 00:00:00+00') TO ('2015-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2015_to_y2016 PARTITION OF public.data +FOR VALUES FROM ('2015-01-01 00:00:00+00') TO ('2016-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2016_to_y2017 PARTITION OF public.data +FOR VALUES FROM ('2016-01-01 00:00:00+00') TO ('2017-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2017_to_y2018 PARTITION OF public.data +FOR VALUES FROM ('2017-01-01 00:00:00+00') TO ('2018-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2018_to_y2019 PARTITION OF public.data +FOR VALUES FROM ('2018-01-01 00:00:00+00') TO ('2019-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2019_to_y2020 PARTITION OF public.data +FOR VALUES FROM ('2019-01-01 00:00:00+00') TO ('2020-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2020_to_y2021 PARTITION OF public.data +FOR VALUES FROM ('2020-01-01 00:00:00+00') TO ('2021-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2021_to_y2022 PARTITION OF public.data +FOR VALUES FROM ('2021-01-01 00:00:00+00') TO ('2022-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2022_to_y2023 PARTITION OF public.data +FOR VALUES FROM ('2022-01-01 00:00:00+00') TO ('2023-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2023_to_y2024 PARTITION OF public.data +FOR VALUES FROM ('2023-01-01 00:00:00+00') TO ('2024-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2024_to_y2025 PARTITION OF public.data +FOR VALUES FROM ('2024-01-01 00:00:00+00') TO ('2025-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2025_to_y2026 PARTITION OF public.data +FOR VALUES FROM ('2025-01-01 00:00:00+00') TO ('2026-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2026_to_y2027 PARTITION OF public.data +FOR VALUES FROM ('2026-01-01 00:00:00+00') TO ('2027-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2027_to_y2028 PARTITION OF public.data +FOR VALUES FROM ('2027-01-01 00:00:00+00') TO ('2028-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2028_to_y2029 PARTITION OF public.data +FOR VALUES FROM ('2028-01-01 00:00:00+00') TO ('2029-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS data_y2029_to_y2030 PARTITION OF public.data +FOR VALUES FROM ('2029-01-01 00:00:00+00') TO ('2030-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y1950_to_y2000 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('1950-01-01 00:00:00+00') TO ('2000-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2000_to_y2010 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2000-01-01 00:00:00+00') TO ('2010-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2010_to_y2015 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2010-01-01 00:00:00+00') TO ('2015-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2015_to_y2016 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2015-01-01 00:00:00+00') TO ('2016-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2016_to_y2017 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2016-01-01 00:00:00+00') TO ('2017-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2017_to_y2018 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2017-01-01 00:00:00+00') TO ('2018-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2018_to_y2019 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2018-01-01 00:00:00+00') TO ('2019-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2019_to_y2020 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2019-01-01 00:00:00+00') TO ('2020-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2020_to_y2021 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2020-01-01 00:00:00+00') TO ('2021-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2021_to_y2022 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2021-01-01 00:00:00+00') TO ('2022-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2022_to_y2023 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2022-01-01 00:00:00+00') TO ('2023-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2023_to_y2024 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2023-01-01 00:00:00+00') TO ('2024-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2024_to_y2025 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2024-01-01 00:00:00+00') TO ('2025-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2025_to_y2026 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2025-01-01 00:00:00+00') TO ('2026-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2026_to_y2027 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2026-01-01 00:00:00+00') TO ('2027-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2027_to_y2028 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2027-01-01 00:00:00+00') TO ('2028-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2028_to_y2029 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2028-01-01 00:00:00+00') TO ('2029-01-01 00:00:00+00'); +CREATE TABLE IF NOT EXISTS nonscalar_data_y2029_to_y2030 PARTITION OF public.nonscalar_data +FOR VALUES FROM ('2029-01-01 00:00:00+00') TO ('2030-01-01 00:00:00+00'); diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..5d0a8c2 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +if ! cargo build --workspace --release; then + exit 1 +fi + +pushd ansible || exit + +ansible-playbook -i inventory.yml deploy.yml --ask-vault-pass + +popd || exit