- name: Check if xray tar exists
  become: yes
  stat:
    path: "{{ jfrog_home_directory }}/{{ xray_tar_file_name }}"
  register: xray_tar_check

- name: Download xray for upgrade
  become: yes
  get_url:
    url: "{{ xray_tar }}"
    timeout: "{{ xray_download_timeout }}"
    dest: "{{ jfrog_home_directory }}"
  register: download_xray
  until: download_xray is succeeded
  retries: 3
  when: not xray_tar_check.stat.exists

- name: Extract xray tar
  become: yes
  unarchive:
    src: "{{ jfrog_home_directory }}/{{ xray_tar_file_name }}"
    dest: "{{ jfrog_home_directory }}"
    remote_src: true
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
    creates: "{{ xray_untar_home }}"
  when: download_xray is succeeded

- name: stop xray
  meta: flush_handlers

- name: Delete xray app
  become: yes
  file:
    path: "{{ xray_home }}/app"
    state: absent
  when: download_xray.changed

- name: Copy new app to xray app
  become: yes
  command: "cp -r {{ xray_untar_home }}/app/. {{ xray_home }}/app"
  when: download_xray.changed

- name: Upgrade rabbitmq
  import_tasks: rabbitmq/upgrade/RedHat.yml
  when:
    - ansible_os_family == 'RedHat'
    - download_xray.changed

- name: Upgrade rabbitmq
  import_tasks: rabbitmq/upgrade/Debian.yml
  when:
    - ansible_os_family == 'Debian'
    - download_xray.changed

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

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

- name: Install xray
  include_tasks: expect.yml
  vars:
    exp_executable_cmd: "./install.sh -u {{ xray_user }} -g {{ xray_group }}"
    exp_dir: "{{ xray_install_script_path }}"
    exp_scenarios: "{{ xray_installer_scenario['main'] }}"
  args:
    apply:
      environment:
        YQ_PATH: "{{ xray_thirdparty_path }}/yq"
  when:
    - install_wrapper_script.stat.exists
    - download_xray.changed

- name: Configure rabbitmq config
  become: yes
  template:
    src: "rabbitmq.conf.j2"
    dest: "{{ xray_home }}/app/bin/rabbitmq/rabbitmq.conf"
    mode: 0644
  when:
    - download_xray.changed
  notify: restart xray

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

- name: Configure systemyaml
  become: yes
  template:
    src: "{{ xray_system_yaml_template }}"
    dest: "{{ xray_home }}/var/etc/system.yaml"
    mode: 0644
  when:
    - xray_systemyaml is defined
    - xray_systemyaml | length > 0
    - xray_systemyaml_override or (not systemyaml.stat.exists)
  notify: restart xray

- name: configure installer info
  become: yes
  template:
    src: installer-info.json.j2
    dest: "{{ xray_home }}/var/etc/info/installer-info.json"
    mode: 0644
  notify: restart xray

- name: Ensure permissions are correct
  become: yes
  file:
    path: "{{ jfrog_home_directory }}"
    state: directory
    owner: "{{ xray_user }}"
    group: "{{ xray_group }}"
    recurse: yes

- name: Restart xray
  meta: flush_handlers

- name: Make sure xray 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
