---
- name: Ensure octopus library is up to date using pip
  ansible.builtin.shell: |
    source /opt/squirro/virtualenv311/bin/activate
    pip install ../../../assets/octopus-*.whl --force-reinstall
  changed_when: true
  delay: 5
  retries: 3

- name: Ensure permission of virtualenv is correct
  ansible.builtin.file:
    path: /opt/squirro/virtualenv311
    group: squirro
    mode: "0755"
    owner: root
    recurse: true

- name: Ensure permission of topic is correct
  ansible.builtin.file:
    group: squirro
    mode: "0755"
    owner: sqtopic
    path: /var/lib/squirro/topic
    recurse: true

- name: Update query template
  ansible.builtin.copy:
    src: ../../query_templates/query_template.tmpl
    dest: /etc/squirro/query_templates/{{ project_id }}.tmpl
    mode: "0755"
    group: squirro
    owner: sqtopic
  changed_when: true

- name: Ensure emailsender config is readable by Squirro group
  ansible.builtin.file:
    path: /etc/squirro/emailsender.ini
    group: squirro
    mode: "0640"
    owner: sqemails
  changed_when: true

- name: Ensure permission of /flash/upload-tmp is correct
  ansible.builtin.file:
    group: squirro
    mode: "0775"
    owner: root
    path: /flash/upload-tmp
    state: directory

- name: Ensure permission of /flash/octopus is correct
  ansible.builtin.file:
    group: squirro
    mode: "0775"
    owner: root
    path: /flash/octopus
    recurse: true

- name: Restart sqfrontendd and sqplumberd
  block:
    - name: Restart sqfrontendd
      ansible.builtin.systemd:
        name: sqfrontendd
        state: restarted
    - name: Restart sqplumberd
      ansible.builtin.systemd:
        name: sqplumberd
        state: restarted
    - name: Pause for 3 minutes for frontend and plumber to restart
      ansible.builtin.pause:
        minutes: 3

################################################################################
# Tasks that are independent and can be run in parallel
################################################################################
- name: Applying patches
  ansible.builtin.shell: |
    ../../patches/patches.sh
  async: 300
  changed_when: true
  poll: 0
  register: patches

- name: Ensure system configurations are up to date
  ansible.builtin.shell: |
    ../../update_system_configs/update_system_configs.sh
  async: 300
  changed_when: true
  poll: 0
  register: update_system_configs

- name: Ensure squirro configurations are up to date
  ansible.builtin.shell: |
    ../../update_squirro_configs/update_squirro_configs.sh
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )
  async: 300
  changed_when: true
  poll: 0
  register: update_squirro_configs

################################################################################
# Tasks that are sequential and depend on the previous tasks
################################################################################
- name: Add document type mapping to redis
  ansible.builtin.shell: |
    ../../document_type_hash/document_type_hash.sh
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Generate company data index to ensure it is updated
  ansible.builtin.shell: |
    ../../../scheduled_jobs/cache_company_data/cache_company_data.sh
  changed_when: true
  delay: 5
  retries: 3
  when: env in ['PROD', 'DR', 'UAT']

- name: Generate company data index from mocked csv to ensure it is updated
  ansible.builtin.shell: |
    ../../../scheduled_jobs/cache_company_data/fake_cache_company_data.sh
  changed_when: true
  delay: 5
  retries: 3
  when: env == 'INTERNAL'

- name: Create labels
  ansible.builtin.shell: |
    ../../create_labels/create_labels.sh
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Deploy studio plugins
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../integration/studio

- name: Deploy dashboard loaders
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../dashboard/loaders

- name: Deploy dashboard themes
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../dashboard/themes

- name: Deploy dashboard widgets
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../dashboard/widgets

- name: Deploy data loaders
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../data

- name: Deploy pipelets
  ansible.builtin.include_tasks: upload_subdirs.yml
  vars:
    dir: ../../../enrich

- name: Import project components
  ansible.builtin.shell: |
    ../../import_project/import_project.sh
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Create user group
  ansible.builtin.shell: |
    ../../create_user_group/create_user_group.sh {{ item }}
  changed_when: false
  delay: 5
  retries: 3
  loop: "{{ user_groups }}"
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Create user group for reconciliation dashboard
  ansible.builtin.shell: |
    ../../create_user_group/create_user_group.sh recon_dashboard_users
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Set user group permissions
  ansible.builtin.shell: |
    ../../set_group_permissions/set_group_permissions.sh \
      --group {{ item }} \
      --role reader
  changed_when: false
  delay: 5
  retries: 3
  loop: "{{ user_groups }}"
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Set user group permissions for reconciliation dashboard
  ansible.builtin.shell: |
    ../../set_group_permissions/set_group_permissions.sh \
      --group recon_dashboard_users \
      --role reader
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

- name: Set dashboards' permissions to include Ops user group
  ansible.builtin.shell: |
    ../../set_dashboard_permissions/set_dashboard_permissions.sh \
      --dashboards WIP,Upload,Unclassified,Trash \
      --group Ops
  changed_when: false
  delay: 5
  retries: 3
  when: >
    env not in ['PROD', 'DR'] or
    (
      env in ['PROD', 'DR'] and
      (
        ansible_hostname.endswith('3') or
        ansible_hostname.endswith('C')
      )
    )

################################################################################
# Post deployment tasks
################################################################################
- name: Wait for all tasks to finish
  ansible.builtin.async_status:
    jid: "{{ item.ansible_job_id }}"
  changed_when: false
  loop: "{{ async_jobs }}"
  register: async_status
  until: async_status.finished
  vars:
    async_jobs: >
      {{
        [
          clear_logs,
          patches,
          update_squirro_configs,
          update_system_configs,
        ]
        | selectattr('ansible_job_id', 'defined')
        | list
      }}

- name: Ensure permission of octopus is correct
  ansible.builtin.file:
    group: squirro
    mode: "0775"
    owner: root
    path: /opt/squirro/octopus
    recurse: true

- name: Ensure permission of topic is correct
  ansible.builtin.file:
    group: squirro
    mode: "0755"
    owner: sqtopic
    path: /var/lib/squirro/topic
    recurse: true
