- hosts: localhost
  gather_facts: no

  vars_files:
    - ./vars.yml

  tasks:
    - name: Write out image manifest
      action: template src=files/manifest.json dest=./image-manifest-{{ ami_release_version }}.json

    - name: Upload image manifest
      shell: |
        aws s3 cp ./image-manifest-{{ ami_release_version }}.json s3://{{ internal_artifacts_bucket }}/manifests/postgres-{{ ami_release_version }}/software-manifest.json

    # upload software artifacts of interest
    # Generally - download, extract, repack as xz archive, upload
    # currently, we upload gotrue, adminapi, postgrest
    - name: gotrue - download commit archive
      get_url:
        url: "https://github.com/supabase/gotrue/releases/download/v{{ gotrue_release }}/auth-v{{ gotrue_release }}-arm64.tar.gz"
        dest: /tmp/auth-v{{ gotrue_release }}-arm64.tar.gz
        checksum: "{{ gotrue_release_checksum }}"
        timeout: 60

    - name: PostgREST - download ubuntu binary archive (arm)
      get_url:
        url: "https://github.com/PostgREST/postgrest/releases/download/v{{ postgrest_release }}/postgrest-v{{ postgrest_release }}-ubuntu-aarch64.tar.xz"
        dest: /tmp/postgrest-{{ postgrest_release }}-arm64.tar.xz
        checksum: "{{ postgrest_arm_release_checksum }}"
        timeout: 60

    - name: Download adminapi archive
      get_url:
        url: "https://supabase-public-artifacts-bucket.s3.amazonaws.com/supabase-admin-api/v{{ adminapi_release }}/supabase-admin-api_{{ adminapi_release }}_linux_arm64.tar.gz"
        dest: "/tmp/adminapi.tar.gz"
        timeout: 90

    - name: adminapi - unpack archive in /tmp
      unarchive:
        remote_src: yes
        src: /tmp/adminapi.tar.gz
        dest: /tmp

    - name: adminapi - pack archive
      shell: |
        cd /tmp && tar -cJf supabase-admin-api-{{ adminapi_release }}-arm64.tar.xz supabase-admin-api

    - name: Download admin-mgr archive
      get_url:
        url: "https://supabase-public-artifacts-bucket.s3.amazonaws.com/admin-mgr/v{{ adminmgr_release }}/admin-mgr_{{ adminmgr_release }}_linux_arm64.tar.gz"
        dest: "/tmp/admin-mgr.tar.gz"
        timeout: 90

    - name: admin-mgr - unpack archive in /tmp
      unarchive:
        remote_src: yes
        src: /tmp/admin-mgr.tar.gz
        dest: /tmp

    - name: admin-mgr - pack archive
      shell: |
        cd /tmp && tar -cJf admin-mgr-{{ adminmgr_release }}-arm64.tar.xz admin-mgr

    - name: upload archives
      shell: |
        aws s3 cp /tmp/{{ item.file }} s3://{{ internal_artifacts_bucket }}/upgrades/{{ item.service }}/{{ item.file }}
      with_items:
        - service: gotrue
          file: auth-v{{ gotrue_release }}-arm64.tar.gz
        - service: postgrest
          file: postgrest-{{ postgrest_release }}-arm64.tar.xz
        - service: supabase-admin-api
          file: supabase-admin-api-{{ adminapi_release }}-arm64.tar.xz
        - service: admin-mgr
          file: admin-mgr-{{ adminmgr_release }}-arm64.tar.xz
