---
# file: roles/minio/tasks/minio_server.yml

# https://github.com/minio/minio/issues/4632#issuecomment-313232005
- name: "get last version via github API"
  ansible.builtin.uri:
    url: https://api.github.com/repos/minio/minio/releases/latest
    method: GET
  register: github_server_lastest_version
  check_mode: false

- name: "set minio_server_latest_tag_version"
  ansible.builtin.set_fact:
    minio_server_latest_version_tag: "{{ github_server_lastest_version.json.tag_name.split('.').1  | regex_replace('T', '-') | regex_replace('Z', '') }}"

- name: "parse release tag"
  ansible.builtin.set_fact:
    minio_server_latest_version_year: "{{ minio_server_latest_version_tag.split('-')[0] }}"
    minio_server_latest_version_month: "{{ minio_server_latest_version_tag.split('-')[1] }}"
    minio_server_latest_version_day: "{{ minio_server_latest_version_tag.split('-')[2] }}"
    minio_server_latest_version_hour: "{{ minio_server_latest_version_tag.split('-')[3] }}"
    minio_server_latest_version_minute: "{{ minio_server_latest_version_tag.split('-')[4] }}"
    minio_server_latest_version_second: "{{ minio_server_latest_version_tag.split('-')[5] }}"

- name: "set minio_server_latest_version_deb"
  ansible.builtin.set_fact:
    minio_server_latest_version_deb: "{{ minio_server_latest_version_year }}{{ minio_server_latest_version_month }}{{ minio_server_latest_version_day }}{{ minio_server_latest_version_hour }}{{ minio_server_latest_version_minute }}{{ minio_server_latest_version_second }}.0.0"

- name: "set minio_version if latest"
  ansible.builtin.set_fact:
    minio_version: "{{ minio_server_latest_version_deb }}"
  when: minio_version == "latest"

- name: "gather the package facts"
  ansible.builtin.package_facts:

- name: "set minio_server_installed_version"
  ansible.builtin.set_fact:
    minio_server_installed_version: "{{ ansible_facts.packages['minio'][0].version | default(None) }}"
  when:
    - "'minio' in ansible_facts.packages"

- name: "do we need to update minio?"
  debug:
    msg: "Update needed for minio, from {{ minio_server_installed_version }} to {{ minio_server_latest_version_deb }} (use: --extra-vars '{ \"minio_update_now\" : true }')"
  when:
    - minio_server_installed_version is defined
    - minio_server_installed_version != minio_server_latest_version_deb

- name: "get the minio server checksum"
  ansible.builtin.uri:
    url: "https://dl.min.io/server/minio/release/linux-amd64/minio_{{ minio_version }}_amd64.deb.sha256sum"
    method: GET
    return_content: true
  register: minio_checksum_file
  when: >
    minio_server_installed_version is not defined
    or (minio_update_now and minio_server_installed_version != minio_server_latest_version_deb)

- name: "set minio_server_version_deb_checksum"
  ansible.builtin.set_fact:
    minio_server_version_deb_checksum: "{{ minio_checksum_file.content.split(' ')[0] }}"
  when: >
    minio_server_installed_version is not defined
    or (minio_update_now and minio_server_installed_version != minio_server_latest_version_deb)

- name: "get_url https://dl.min.io/server/minio/release/linux-amd64/minio_{{ minio_version }}_amd64.deb"
  ansible.builtin.get_url:
    url: "https://dl.min.io/server/minio/release/linux-amd64/minio_{{ minio_version }}_amd64.deb"
    dest: "/dev/shm/minio_server.deb"
    checksum: "sha256:{{ minio_server_version_deb_checksum }}"
    mode: "0640"
  when: >
    minio_server_installed_version is not defined
    or (minio_update_now and minio_server_installed_version != minio_server_latest_version_deb)

- name: "install minio server"
  ansible.builtin.apt:
    deb: "/dev/shm/minio_server.deb"
  notify:
    - reload daemon
    - restart minio
  when: >
    minio_server_installed_version is not defined
    or (minio_update_now and minio_server_installed_version != minio_server_latest_version_deb)

- name: "make sure /dev/shm/minio_server.deb is removed"
  ansible.builtin.file:
    path: "/dev/shm/minio_server.deb"
    state: absent
