--- - name: Set Postgres Configuration Secret name set_fact: postgres_configuration_secret: "{{ spec['postgres_configuration_secret'] | default(postgres_configuration_secret) }}" - name: Check for specified PostgreSQL configuration k8s_info: kind: Secret namespace: '{{ ansible_operator_meta.namespace }}' name: '{{ postgres_configuration_secret }}' register: pg_config no_log: "{{ no_log }}" - name: Store Database Configuration set_fact: awx_postgres_user: "{{ pg_config['resources'][0]['data']['username'] | b64decode }}" awx_postgres_pass: "{{ pg_config['resources'][0]['data']['password'] | b64decode }}" awx_postgres_database: "{{ pg_config['resources'][0]['data']['database'] | b64decode }}" awx_postgres_port: "{{ pg_config['resources'][0]['data']['port'] | b64decode }}" awx_postgres_host: "{{ pg_config['resources'][0]['data']['host'] | b64decode }}" awx_postgres_type: "{{ pg_config['resources'][0]['data']['type'] | b64decode | default('unmanaged') }}" no_log: "{{ no_log }}" - name: Set Default label selector for custom resource generated postgres set_fact: postgres_label_selector: "app.kubernetes.io/instance=postgres-{{ supported_pg_version }}-{{ deployment_name }}" when: postgres_label_selector is not defined - block: - name: Get the postgres pod information k8s_info: kind: Pod namespace: '{{ ansible_operator_meta.namespace }}' label_selectors: - "{{ postgres_label_selector }}" register: postgres_pod until: - "postgres_pod['resources'] | length" - "postgres_pod['resources'][0]['status']['phase'] == 'Running'" - "postgres_pod['resources'][0]['status']['containerStatuses'][0]['ready'] == true" delay: 5 retries: 60 - name: Set the resource pod name as a variable. set_fact: postgres_pod_name: "{{ postgres_pod['resources'][0]['metadata']['name'] }}" when: awx_postgres_type == 'managed' - name: Check for presence of AWX Deployment k8s_info: api_version: apps/v1 kind: Deployment name: "{{ deployment_name }}-task" namespace: "{{ ansible_operator_meta.namespace }}" register: this_deployment - name: Scale down Deployment for migration k8s_scale: api_version: apps/v1 kind: Deployment name: "{{ item }}" namespace: "{{ ansible_operator_meta.namespace }}" replicas: 0 wait: yes loop: - "{{ deployment_name }}-task" - "{{ deployment_name }}-web" when: this_deployment['resources'] | length - name: Set full resolvable host name for postgres pod set_fact: resolvable_db_host: '{{ (awx_postgres_type == "managed") | ternary(awx_postgres_host + "." + ansible_operator_meta.namespace + ".svc." + cluster_name, awx_postgres_host) }}' # yamllint disable-line rule:line-length no_log: "{{ no_log }}" - name: Set pg_restore command set_fact: pg_restore: >- pg_restore --clean --if-exists -U {{ awx_postgres_user }} -h {{ resolvable_db_host }} -d {{ awx_postgres_database }} -p {{ awx_postgres_port }} no_log: "{{ no_log }}" - name: Force drop and create database if force_drop_db is true block: - name: Set drop db command set_fact: pg_drop_db: >- echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql -U {{ awx_postgres_user }} -h {{ resolvable_db_host }} -d postgres -p {{ awx_postgres_port }} ; no_log: "{{ no_log }}" - name: Set create db command set_fact: pg_create_db: >- echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql -U {{ awx_postgres_user }} -h {{ resolvable_db_host }} -d postgres -p {{ awx_postgres_port }} ; no_log: "{{ no_log }}" - name: Set complete pg restore command set_fact: pg_drop_create: >- {{ pg_drop_db }} {{ pg_create_db }} when: force_drop_db - name: Restore database dump to the new postgresql container k8s_exec: namespace: "{{ backup_pvc_namespace }}" pod: "{{ ansible_operator_meta.name }}-db-management" container: "{{ ansible_operator_meta.name }}-db-management" command: | bash -c " function end_keepalive { rc=$? rm -f \"$1\" kill $(cat /proc/$2/task/$2/children 2>/dev/null) 2>/dev/null || true wait $2 || true exit $rc } keepalive_file=\"$(mktemp)\" while [[ -f \"$keepalive_file\" ]]; do echo 'Migrating data from old database...' sleep 60 done & keepalive_pid=$! trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM echo keepalive_pid: $keepalive_pid set -e -o pipefail {{ pg_drop_create }} cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }} PG_RC=$? set +e +o pipefail exit $PG_RC " register: data_migration no_log: "{{ no_log }}"