---

- name: "Install {{ vitamui_struct.package_name }} package"
  package:
    name: "{{ vitamui_struct.package_name }}"
    state: latest
  register: result
  retries: "{{ packages_install_retries_number }}"
  until: result is succeeded
  delay: "{{ packages_install_retries_delay }}"
  tags:
    - update_package_vitamui
  notify:
    - restart service
  when: force_vitamui_version is not defined

- name: Ensure {{ service_name }} service is enabled at boot
  service:
    name: "{{ service_name }}"
    enabled: true
  notify:
    - restart service

#### Configuration ####

- name: Check that the directories exist (must be removed when the RPM plugin will be patched)
  file:
    path: "{{ vitamui_defaults.folder.root_path }}/{{ item }}/{{ vitamui_struct.vitamui_component }}"
    state: directory
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  with_items:
    - app
    - bin
    - conf
    - data
    - lib
    - log
    - script
    - tmp
  notify:
    - restart service

- name: Ensure that the sysconfig sub-directory exists
  file:
    path: "{{ vitamui_folder_conf }}/sysconfig"
    state: directory
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  notify:
    - restart service

- name: Ensure that the templates sub-directory exists
  file:
    path: "{{ vitamui_folder_conf }}/templates"
    state: directory
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  notify:
    - restart service

- name: Deploy common configuration files in sysconfig subdir
  template:
    src: "{{ item }}.j2"
    dest: "{{ vitamui_folder_conf }}/sysconfig/{{ item }}"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.conf_permission }}"
  with_items:
    - "java_opts"
  tags:
    - update_jvmoptions_vitamui
  notify:
    - restart service

- name: Deploy common configuration files
  template:
    src: "{{ item }}.j2"
    dest: "{{ vitamui_folder_conf }}/{{ item }}"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.conf_permission }}"
  with_items:
    - "logback.xml"

- name: Deploy specific configuration files
  template:
    src: "{{ item }}"
    dest: "{{ vitamui_folder_conf }}/{{ item | basename | regex_replace('\\.j2$') }}"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.conf_permission }}"
  with_fileglob:
    - "{{ role_path }}/templates/{{ vitamui_struct.vitamui_component }}/*"
  no_log: "{{ hide_passwords_during_deploy }}"
  tags:
    - update_vitam_configuration
  notify:
    - restart service

- name: "Copy {{ vitamui_struct.package_name }} jks keystore (server)"
  copy:
    src: "{{ inventory_dir }}/keystores/server/{{ inventory_hostname }}/keystore_{{ vitamui_struct.store_name }}.jks"
    dest: "{{ vitamui_folder_conf }}/keystore_{{ vitamui_struct.package_name }}.jks"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  when: "( vitamui_struct.secure|lower == 'true' ) and ({{ lookup('pipe', 'test -f {{ inventory_dir }}/keystores/server/{{ inventory_hostname }}/keystore_{{ vitamui_struct.store_name }}.jks || echo nofile') == \"\" }})"
  tags:
    - update_vitamui_certificates
  notify:
    - restart service

# Copy the trustore for all vitamui components in order to communicate between them.
- name: "Copy {{ vitamui_certificate_type }} truststore"
  copy:
    src: "{{ item.src }}"
    dest: "{{ vitamui_folder_conf }}/truststore_{{ vitamui_certificate_type }}.jks"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  when: 
    - vitamui_struct.secure|lower == 'true'
    - vitamui_certificate_type is defined 
    - "vitamui_certificate_type|lower == '{{ item.name }}'"
    - "{{ lookup('pipe', 'test -f {{ item.src }} || echo nofile') == \"\"}}"
  with_items:
    # Copy the trustore for all vitamui components in order to communicate between them.
    - { name: server, src: "{{ inventory_dir }}/keystores/server/truststore_server.jks" }
    # Copy the trustore for all external API in order to communicate with vitamui components (ui, external APIs, cas) and externals apps.
    - { name: external, src: "{{ inventory_dir }}/keystores/client-{{ vitamui_certificate_type }}/truststore_{{ vitamui_certificate_type }}.jks" }
  tags:
    - update_vitamui_certificates
  notify:
    - restart service

- name: "Copy vitam certificates"
  copy:
    src: "{{ item }}"
    dest: "{{ vitamui_folder_conf }}/{{ item | basename }}"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  with_fileglob:
    - "{{ inventory_dir }}/keystores/client-vitam/*.*"
  when:
    - vitam_cert is defined
  tags:
    - update_vitam_configuration
  notify:
    - restart service

- name: apply consul conf when cas-server ONLY
  template:
    src: "cas-server/vitamui-cas-server.json.j2"
    dest: "{{ vitamui_conf_consul }}/vitamui-cas-server.json"
    owner: "{{ vitamui_defaults.users.vitamui }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: "{{ vitamui_defaults.folder.folder_permission }}"
  when: vitamui_struct.service_name == 'vitamui-cas-server'
  notify:
    - restart service

- name: flush_handlers
  meta: flush_handlers
  tags:
    - always

- name: "Ensure {{ service_name }} service is started"
  service:
    name: "{{ service_name }}"
    state: started