- name: Include distro specific variables
  include_vars: "{{ distro_vars_file }}"
  vars:
    distro_vars_file: "{{ lookup('first_found', distro_vars, errors='ignore') }}"
    distro_vars:
      files:
        - "vars/distro/{{ ansible_distribution ~ ansible_distribution_major_version }}.yml"
        - "vars/distro/{{ ansible_distribution }}.yml"
        - "vars/distro/{{ ansible_os_family }}.yml"
        - "vars/distro/default.yml"

- name: Install prerequisite packages
  include_tasks: "{{ ansible_os_family }}.yml"

- name: Install nginx
  include_role:
    name: artifactory_nginx
  when:
    - artifactory_nginx_enabled | bool
    - not artifactory_nginx_ssl_enabled | bool

- name: Install nginx with SSL
  include_role:
    name: artifactory_nginx_ssl
  when:
    - not artifactory_nginx_enabled | bool
    - artifactory_nginx_ssl_enabled | bool

- name: Ensure group artifactory exist
  become: yes
  group:
    name: "{{ artifactory_group }}"
    state: present

- name: Ensure user artifactory exist
  become: yes
  user:
    name: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
    create_home: yes
    home: "{{ artifactory_home }}"
    shell: /bin/bash
    state: present

- name: Check if artifactory tar already exists
  become: yes
  stat:
    path: "{{ jfrog_home_directory }}/{{ artifactory_tar_file_name }}"
  register: artifactory_tar_check

- name: Download artifactory
  become: yes
  get_url:
    url: "{{ artifactory_tar }}"
    timeout: "{{ artifactory_download_timeout }}"
    dest: "{{ jfrog_home_directory }}"
  register: download_artifactory
  until: download_artifactory is succeeded
  retries: 3
  when: not artifactory_tar_check.stat.exists

- name: Extract artifactory tar
  become: yes
  unarchive:
    src: "{{ jfrog_home_directory }}/{{ artifactory_tar_file_name }}"
    dest: "{{ jfrog_home_directory }}"
    owner: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
    creates: "{{ artifactory_untar_home }}"
    remote_src: true
  when: download_artifactory is succeeded

- name: Check if app directory exists
  become: yes
  stat:
    path: "{{ artifactory_home }}/app"
  register: app_dir_check

- name: Copy untar directory to artifactory home
  become: yes
  copy:
    src: "{{ artifactory_untar_home }}/"
    dest: "{{ artifactory_home }}"
    owner: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
    mode: 0755
    remote_src: yes
  when: not app_dir_check.stat.exists

- name: Create required directories
  become: yes
  file:
    path: "{{ item }}"
    state: directory
    recurse: yes
    owner: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
  loop:
    - "{{ artifactory_home }}/var/data"
    - "{{ artifactory_home }}/var/etc"
    - "{{ artifactory_home }}/var/etc/security/"
    - "{{ artifactory_home }}/var/etc/artifactory/info/"

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

- name: Configure system.yaml
  become: yes
  template:
    src: "{{ artifactory_system_yaml_template }}"
    dest: "{{ artifactory_home }}/var/etc/system.yaml"
    mode: 0644
  when:
    - artifactory_systemyaml is defined
    - artifactory_systemyaml | length > 0
    - artifactory_systemyaml_override or (not systemyaml.stat.exists)
  notify: restart artifactory

- name: Configure master key
  become: yes
  copy:
    dest: "{{ artifactory_home }}/var/etc/security/master.key"
    content: "{{ master_key }}"
    owner: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
    mode: 0640

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

- name: Configure binary store
  become: yes
  template:
    src: binarystore.xml.j2
    dest: "{{ artifactory_home }}/var/etc/artifactory/binarystore.xml"
    mode: 0644
  when:
    - artifactory_binarystore is defined
    - artifactory_binarystore | length > 0
  notify: restart artifactory

- name: Configure artifactory license(s)
  become: yes
  template:
    src: artifactory.cluster.license.j2
    dest: "{{ artifactory_home }}/var/etc/artifactory/artifactory.cluster.license"
    mode: 0644
  when:
   - artifactory_licenses is defined
   - artifactory_licenses | length > 0
  notify: restart artifactory

- name: Check if database driver exists
  become: yes
  stat:
    path: "{{ artifactory_home }}/app/artifactory/tomcat/lib/jf_postgresql-{{ postgres_driver_version }}.jar"
  register: database_driver

- name: Download database driver
  become: yes
  get_url:
    url: "{{ postgres_driver_download_url }}"
    dest: "{{ artifactory_home }}/var/bootstrap/artifactory/tomcat/lib"
    owner: "{{ artifactory_user }}"
    group: "{{ artifactory_group }}"
  when:
    - postgres_driver_download_url is defined
    - not database_driver.stat.exists
  notify: restart artifactory

- name: Run restore context to reload selinux
  become: yes
  shell: |
    restorecon -R -v "{{ jfrog_home_directory }}/artifactory/app/bin"
  when: ansible_distribution == 'RedHat'
  changed_when: false

- name: Create artifactory service
  become: yes
  command: "{{ artifactory_home }}/app/bin/installService.sh"
  args:
    creates: "{{ artifactory_service_file }}"

- name: Ensure permissions are correct
  include_tasks: shared/ensure_permissions_correct.yml

- name: Restart artifactory
  meta: flush_handlers

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