- name: Check if distribution tar already exists
  become: yes
  stat:
    path: "{{ jfrog_home_directory }}/{{ distribution_tar_file_name }}"
  register: distribution_tar_check

- name: Download distribution for upgrade
  become: yes
  get_url:
    url: "{{ distribution_tar }}"
    timeout: "{{ distribution_download_timeout }}"
    dest: "{{ jfrog_home_directory }}"
  register: download_distribution
  until: download_distribution is succeeded
  retries: 3
  when: not distribution_tar_check.stat.exists

- name: Extract distribution tar
  become: yes
  unarchive:
    src: "{{ jfrog_home_directory }}/{{ distribution_tar_file_name }}"
    dest: "{{ jfrog_home_directory }}"
    remote_src: true
    owner: "{{ distribution_user }}"
    group: "{{ distribution_group }}"
    creates: "{{ distribution_untar_home }}"
  when: download_distribution is succeeded

- name: stop distribution
  meta: flush_handlers

- name: Delete distribution app
  become: yes
  file:
    path: "{{ distribution_home }}/app"
    state: absent
  when: download_distribution.changed

- name: Copy new app to distribution app
  become: yes
  command: "cp -r {{ distribution_untar_home }}/app/. {{ distribution_home }}/app"
  when: download_distribution.changed

- name: Check if system.yaml exists
  become: yes
  stat:
    path: "{{ distribution_home }}/var/etc/system.yaml"
  register: systemyaml

- name: Configure system.yaml
  become: yes
  template:
    src: "{{ distribution_system_yaml_template }}"
    dest: "{{ distribution_home }}/var/etc/system.yaml"
    mode: 0644
  when:
    - distribution_systemyaml is defined
    - distribution_systemyaml | length > 0
    - distribution_systemyaml_override or (not systemyaml.stat.exists)
  notify: restart distribution

- name: Check if install.sh wrapper script exist
  become: yes
  stat:
    path: "{{ distribution_install_script_path }}/install.sh"
  register: install_wrapper_script
  when: download_distribution.changed

- name: Include interactive installer scripts
  include_vars: script/archive.yml

- name: Install Distribution
  include_tasks: expect.yml
  vars:
    exp_executable_cmd: "./install.sh -u {{ distribution_user }} -g {{ distribution_group }}"
    exp_dir: "{{ distribution_install_script_path }}"
    exp_scenarios: "{{ distribution_installer_scenario['main'] }}"
  args:
    apply:
      environment:
        YQ_PATH: "{{ distribution_thirdparty_path }}/yq"
  when:
    - install_wrapper_script.stat.exists
    - download_distribution.changed

- name: Ensure redis config directory exists
  become: yes
  file:
    path: "{{ distribution_home }}/var/etc/redis/"
    state: directory
    owner: "{{ distribution_user }}"
    group: "{{ distribution_group }}"
    mode: 0644

- name: Configure redis config
  become: yes
  template:
    src: "redis.conf.j2"
    dest: "{{ distribution_home }}/var/etc/redis/redis.conf"
    mode: 0644
  notify: restart distribution

- name: Configure installer info
  become: yes
  template:
    src: installer-info.json.j2
    dest: "{{ distribution_home }}/var/etc/info/installer-info.json"
    mode: 0644
  notify: restart distribution

- name: Update distribution permissions
  become: yes
  file:
    path: "{{ distribution_home }}"
    state: directory
    recurse: yes
    owner: "{{ distribution_user }}"
    group: "{{ distribution_group }}"

- name: Restart distribution
  meta: flush_handlers

- name: Make sure distribution is up and running
  uri:
    url: http://127.0.0.1:8082/router/api/v1/system/health
    timeout: 130
    status_code: 200
  register: result
  until: result is succeeded
  retries: 25
  delay: 5
  when: not ansible_check_mode
