- name: Upload Zenith binaries
  hosts: storage
  gather_facts: False
  remote_user: admin

  tasks:

    - name: get latest version of Zenith binaries
      register: current_version_file
      set_fact:
        current_version: "{{ lookup('file', '.zenith_current_version') | trim }}"
      tags:
      - pageserver
      - safekeeper

    - name: inform about versions
      debug: msg="Version to deploy - {{ current_version }}"
      tags:
      - pageserver
      - safekeeper

    - name: upload and extract Zenith binaries to /usr/local
      ansible.builtin.unarchive:
        owner: root
        group: root
        src: zenith_install.tar.gz
        dest: /usr/local
      become: true
      tags:
      - pageserver
      - safekeeper
      - binaries
      - putbinaries

- name: Deploy pageserver
  hosts: pageservers
  gather_facts: False
  remote_user: admin

  tasks:

    - name: upload init script
      when: console_mgmt_base_url is defined
      ansible.builtin.template:
        src: scripts/init_pageserver.sh
        dest: /tmp/init_pageserver.sh
        owner: root
        group: root
        mode: '0755'
      become: true
      tags:
      - pageserver

    - name: init pageserver
      shell:
        cmd: /tmp/init_pageserver.sh
      args:
        creates: "/storage/pageserver/data/tenants"
      environment:
        ZENITH_REPO_DIR: "/storage/pageserver/data"
        LD_LIBRARY_PATH: "/usr/local/lib"
      become: true
      tags:
      - pageserver

    - name: update remote storage (s3) config
      lineinfile:
        path: /storage/pageserver/data/pageserver.toml
        line: "{{ item }}"
      loop:
        - "[remote_storage]"
        - "bucket_name = '{{ bucket_name }}'"
        - "bucket_region = '{{ bucket_region }}'"
        - "prefix_in_bucket = '{{ inventory_hostname }}'"
      become: true
      tags:
      - pageserver

    - name: upload systemd service definition
      ansible.builtin.template:
        src: systemd/pageserver.service
        dest: /etc/systemd/system/pageserver.service
        owner: root
        group: root
        mode: '0644'
      become: true
      tags:
      - pageserver

    - name: start systemd service
      ansible.builtin.systemd:
        daemon_reload: yes
        name: pageserver
        enabled: yes
        state: restarted
      become: true
      tags:
      - pageserver

    - name: post version to console
      when: console_mgmt_base_url is defined
      shell:
        cmd: |
          INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
          curl -sfS -d '{"version": {{ current_version }} }' -X PATCH {{ console_mgmt_base_url }}/api/v1/pageservers/$INSTANCE_ID
      tags:
      - pageserver

- name: Deploy safekeeper
  hosts: safekeepers
  gather_facts: False
  remote_user: admin

  tasks:

    - name: upload init script
      when: console_mgmt_base_url is defined
      ansible.builtin.template:
        src: scripts/init_safekeeper.sh
        dest: /tmp/init_safekeeper.sh
        owner: root
        group: root
        mode: '0755'
      become: true
      tags:
      - safekeeper

    - name: init safekeeper
      shell:
        cmd: /tmp/init_safekeeper.sh
      args:
        creates: "/storage/safekeeper/data/safekeeper.id"
      environment:
        ZENITH_REPO_DIR: "/storage/safekeeper/data"
        LD_LIBRARY_PATH: "/usr/local/lib"
      become: true
      tags:
      - safekeeper

    # in the future safekeepers should discover pageservers byself
    # but currently use first pageserver that was discovered
    - name: set first pageserver var for safekeepers
      set_fact:
        first_pageserver: "{{ hostvars[groups['pageservers'][0]]['inventory_hostname'] }}"
      tags:
      - safekeeper

    - name: upload systemd service definition
      ansible.builtin.template:
        src: systemd/safekeeper.service
        dest: /etc/systemd/system/safekeeper.service
        owner: root
        group: root
        mode: '0644'
      become: true
      tags:
      - safekeeper

    - name: start systemd service
      ansible.builtin.systemd:
        daemon_reload: yes
        name: safekeeper
        enabled: yes
        state: restarted
      become: true
      tags:
      - safekeeper

    - name: post version to console
      when: console_mgmt_base_url is defined
      shell:
        cmd: |
          INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
          curl -sfS -d '{"version": {{ current_version }} }' -X PATCH {{ console_mgmt_base_url }}/api/v1/safekeepers/$INSTANCE_ID
      tags:
      - safekeeper
