---
#    ******         **         ******    **   **   **     **   *******    #
#   /*////**       ****       **////**  /**  **   /**    /**  /**////**   #
#   /*   /**      **//**     **    //   /** **    /**    /**  /**   /**   #
#   /******      **  //**   /**         /****     /**    /**  /*******    #
#   /*//// **   **********  /**         /**/**    /**    /**  /**////     #
#   /*    /**  /**//////**  //**    **  /**//**   /**    /**  /**         #
#   /*******   /**     /**   //******   /** //**  //*******   /**         #
#   ///////    //      //     //////    //   //    ///////    //          #

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

# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. devnso, qanso, prodnso)
#     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

  tasks:
    #     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

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

  pre_tasks:
    - name: "Import constraints check"
      import_tasks: tasks/constraints_check.yml
      become: false
      tags:
        - always

    - name: "Import autodiscover pre-tasks"
      import_tasks: tasks/autodiscover_pre_tasks.yml
      become: false
      tags:
        - always

  tasks:
    - name: "Add {{ database_engine }} servers to hosts if necessary"
      add_host:
        name: "{{ item.name }}"
        groups:
          - "stage_{{ stage }}"
          - "{{ database_engine }}"
      when:
        - (database_engine == 'postgres' and item.service == 'postgres' and (item.role | default('')) == 'slave')
          or (database_engine == 'maria' and item.service == 'maria')
      loop: "{{ stage_server_infos }}"

    - name: "Add 'backup' servers to hosts if necessary"
      add_host:
        name: "{{ stage }}-backup-01"
        groups:
          - "stage_{{ stage }}"
          - "backup"
      when:
        - "'postgres' in groups or 'maria' in groups"

##############################################################
##   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 backup server
#############################################################

- hosts: "postgres:maria:backup"
  serial: "{{ serial_number | default(5) }}"
  gather_facts: false
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"
    backup_server_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: "{{ backup_server_system_user }}"
      shell: "/home/{{ backup_server_system_user }}/pull_remote_backups.sh {{ item }} {{ stage }} {{ database_engine }}"
      with_items: "{{ (groups['postgres'] | default([])) + (groups['maria'] | default([])) }}"
      when:
        - inventory_hostname in groups['backup']

    - 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['backup']
        - inventory_hostname in groups [database_engine]

#############################################################
#   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
