---
- name: Ignore pkg_resources PEP 420 warnings
  ansible.builtin.template:
    src: python/sitecustomize.txt
    dest: /opt/squirro/virtualenv311/lib/python3.11/site-packages/sitecustomize.py
    group: squirro
    mode: "0755"
    owner: root
  when: squirro_clusternode | default(false) | bool

- name: Disable GenAI
  ansible.builtin.include_tasks: disable_genai.yml
  when:
    - squirro_clusternode | default(false) | bool

- name: Remove default elasticsearch jobs
  ansible.builtin.include_tasks: remove_default_es_jobs.yml

- name: Ensure robots.txt does not exist
  ansible.builtin.file:
    path: "{{ sq_frontend_dir }}/static/robots.txt"
    state: absent
  when: squirro_clusternode | default(false) | bool

- name: Remove all .orig files
  ansible.builtin.include_tasks: remove_orig_files.yml
  when: squirro_clusternode | default(false) | bool

- name: Configure Nginx for multi node cluster
  ansible.builtin.include_tasks: configure_nginx_multi_node.yml
  when: squirro_clusternode | default(false) | bool

- 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
    chown root:squirro -R /opt/squirro/virtualenv311
  changed_when: true
  delay: 5
  retries: 3
  when: squirro_clusternode | default(false) | bool

- name: Encrypt sensitive values
  when: squirro_clusternode | default(false) | bool
  block:
    - name: Include SQ_DECRYPT_COMMAND profile.d
      ansible.builtin.copy:
        content: 'export SQ_DECRYPT_COMMAND="/opt/squirro/octopus/deployment/vault/fetch_ck_cmd_values/fetch_ck_cmd_values.sh"'
        dest: /etc/profile.d/squirro_decrypt.sh
        group: root
        mode: "0644"
        owner: root
    - name: Initialize vault
      ansible.builtin.command: ../../vault/init_vault.sh
      changed_when: true

- name: Setup Redis Sentinel
  ansible.builtin.include_role:
    name: redis_sentinel
  when:
    - squirro_clusternode | default(false) | bool
    - redis_sentinel_nodes is defined
    - redis_sentinel_nodes | length > 0

- name: Setup Redis secure
  when: squirro_clusternode | default(false) | bool
  block:
    - name: Ensure redis is part of squirro group
      ansible.builtin.user:
        append: true
        groups: redis,squirro
        name: redis
    - name: Update permissions of /etc/redis
      ansible.builtin.file:
        group: redis
        mode: "0775"
        owner: root
        path: /etc/redis
        state: directory
    - name: Update Redis services
      ansible.builtin.command: ../../redis_secure/run.sh
      changed_when: true
