---

# creates remote database backup
#   - postgres
#     - executed on stage specific server: {{ stage }}-postgres-02 (currently: slave)
#     - creates database backup for ALL databases in postgres-server
#   - mariadb
#     - executed on stage specific server: {{ stage }}-maria-01
#     - creates database backup for ALL databases in mariadb-server

# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. dev, int, qa, prod)
#     database_engine := the database engine to generate a complete backup for (e.g. postgres, maria)
#   smardigo message callback
#     scope_id := (scope id of the management process)
#     process_instance_id := (process instance id of the management process)
#     smardigo_management_action := (smardigo management action anme of the management process)

#############################################################
#   Creating inventory dynamically for given parameters
#############################################################

- hosts: localhost
  connection: local
  gather_facts: false

  pre_tasks:
    - name: "Check if ansible version is at least {{ ansible_minimal_version }}"
      assert:
        that:
          - ansible_version.string is version(ansible_minimal_version, ">=")
        msg: "The ansible version has to be at least {{ ansible_minimal_version }}"

#     add virtual server to load stage specific variables as context
    - name: "Add <{{ stage }}-virtual-host-to-read-groups-vars> to hosts"
      add_host:
        name: "{{ stage }}-virtual-host-to-read-groups-vars"
        groups:
        - "stage_{{ stage }}"
      changed_when: False

  tasks:
    - name: "Add {{ database_engine }} servers to hosts if necessary"
      add_host:
        name: "{{ stage }}-{{ database_engine }}-{{'02' if database_engine == 'postgres' else '01'}}"
        groups:
        - "stage_{{ stage }}"
        - '{{ database_engine }}'
      changed_when: False
    - name: "Add 'storage' servers to hosts if necessary"
      add_host:
        name: "{{ stage }}-backup-01"
        groups:
        - "stage_{{ stage }}"
        - storage
      changed_when: False

##############################################################
##   Creating remote database backups for created inventory
##############################################################

- hosts: "postgres:maria"
  serial: "{{ serial_number | default(1) }}"
  gather_facts: false
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"
    current_date_time: '{{ get_current_date_time }}'

  tasks:
    - name: "Trigger backup mechanism"
      include_role:
        name: '{{ database_engine }}'
        tasks_from: _create_backup

#############################################################
#   Syncing remote database backups to storage server
#############################################################

- hosts: "postgres:maria:storage"
  serial: "{{ serial_number | default(5) }}"
  gather_facts: false
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"
    storageserver_system_user: 'backuphamster'

  tasks:
    # I could not get it up and running with <synchronize> module
    # to sync data from remote server A to remote server B
    - name: "Syncing remote backups"
      become: yes
      become_user: '{{ storageserver_system_user }}'
      vars:
        # should work with non-fqdn due to existing entry in /etc/hosts
        database_server_ip: "{{ stage }}-{{ database_engine }}-{{'02' if database_engine == 'postgres' else '01'}}"
      shell: '/home/{{ storageserver_system_user }}/pull_remote_backups.sh {{ database_server_ip }} {{ stage }} {{ database_engine }}'
      when:
        - inventory_hostname in groups['storage']

    - name: "Cleanup remote backup dirs: {{ database_engine }}"
      become: yes
      file:
        path: '{{ backup_directory }}/{{ database_engine }}/{{ get_current_date }}'
        state: absent
      when:
        - not inventory_hostname in groups['storage']

#############################################################
#   Sending smardigo management message to process
#############################################################

- hosts: "{{ stage }}-virtual-host-to-read-groups-vars"
  serial: "{{ serial_number | default(1) }}"
  gather_facts: false
  connection: local
  run_once: true
  vars:
    connect_jwt_username: "{{ management_admin_username }}"

  tasks:
    - name: "Sending smardigo management message to <{{ shared_service_url_management }}>"
      include_tasks: tasks/smardigo_management_message.yml
