---
- name: Ensure /apps and /flash is setup
  ansible.builtin.file:
    group: squirro
    mode: "0775"
    owner: root
    path: "{{ item }}"
    state: directory
  loop:
    - /apps
    - /apps/ocr-tmp
    - /apps/octopus
    - /apps/octopus/logs
    - /apps/octopus/scripts
    - /flash
    - /flash/octopus
    - /flash/octopus/cache

- 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 octopus directory is setup
  ansible.builtin.file:
    group: squirro
    mode: "0755"
    owner: root
    path: "{{ item }}"
    state: directory
  loop:
    - /opt/squirro/octopus
    - /opt/squirro/octopus/config

- name: Populate main.ini
  ansible.builtin.template:
    dest: /opt/squirro/octopus/config/main.ini
    group: squirro
    mode: "0755"
    owner: root
    src: main.ini.j2

- name: Ensure query templates directory exists
  ansible.builtin.file:
    path: /etc/squirro/query_templates
    group: squirro
    mode: "0755"
    owner: sqtopic
    state: directory

- name: Update /etc/squirro/topic.ini with query_templates configuration
  ansible.builtin.blockinfile:
    path: /etc/squirro/topic.ini
    backup: true
    block: |
      [query_templates]
      project_ids = {{ project_id }}
      directory = /etc/squirro/query_templates
    state: present
  when: "'[query_templates]' not in lookup('file', '/etc/squirro/topic.ini')"

- name: Make a copy of post-install.sh as /apps/octopus/scripts/post_install.sh
  ansible.builtin.copy:
    src: ../../post-install.sh
    dest: /apps/octopus/scripts/post_install.sh
    group: squirro
    mode: "0755"
    owner: root

- name: Generate a fake csv for authentication
  ansible.builtin.copy:
    dest: /flash/octopus/ReconDashboardUsers.csv
    content: "Value.email"
    group: squirro
    mode: "0775"
    owner: root
  when: internal_node | default(false) | bool

- name: Encrypt sensitive values
  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 secure
  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

- name: Deploy email service
  block:
    - name: Ensure service file exists
      ansible.builtin.copy:
        src: /opt/squirro/octopus/services/uploader/ocemailsending.service
        dest: /usr/lib/systemd/system/ocemailsending.service
        mode: "0644"
    - name: Enable and start upload service
      ansible.builtin.systemd:
        name: ocemailsending
        daemon_reload: true
        enabled: true
        state: started

- name: Deploy upload service
  block:
    - name: Ensure service file exists
      ansible.builtin.copy:
        src: /opt/squirro/octopus/services/uploader/ocuploader.service
        dest: /usr/lib/systemd/system/ocuploader.service
        mode: "0644"
    - name: Enable and start upload service
      ansible.builtin.systemd:
        name: ocuploader
        daemon_reload: true
        enabled: true
        state: started

- name: Restart Squirro
  ansible.builtin.debug:
    msg: "Please ensure that the services are up and running before proceeding."
  notify:
    - Restart sqtopicd
    - Restart sqconfigurationd
    - Restart sqcontentd
    - Restart sqdatasourced
    - Restart sqdigestmailerd
    - Restart sqemailsenderd
    - Restart sqfilteringd
    - Restart sqfingerprintd
    - Restart sqindexmanagerd
    - Restart sqingesterd
    - Restart sqmachinelearningd
    - Restart sqnotesd
    - Restart sqpdfconversiond
    - Restart sqplumberd
    - Restart sqproviderd
    - Restart sqrelatedstoryd
    - Restart sqschedulerd
    - Restart sqsearchd
    - Restart sqthumblerd
    - Restart sqtopicproxyd
    - Restart sqtrendsd
    - Restart squserproxyd
    - Restart squserd
    - Restart sqwebshotd
    - Restart sqfrontendd
