---
- name: Configure Squirro group
  ansible.builtin.group:
    gid: 4000
    name: squirro
    state: present

- name: Setup /apps directory
  ansible.builtin.file:
    group: root
    mode: "0755"
    owner: root
    path: "{{ item }}"
    state: directory
  loop:
    - /apps
    - /apps/squirro/
    - /apps/squirro/etc
    - /apps/squirro/etc/squirro
    - /apps/squirro/opt
    - /apps/squirro/opt/squirro
    - /apps/squirro/usr
    - /apps/squirro/usr/share
    - /apps/squirro/var
    - /apps/squirro/var/lib
    - /apps/squirro/var/lib/squirro
    - /apps/squirro/var/log
    - /apps/squirro/var/log/squirro

- name: Configure storage node users and groups
  when: squirro_storagenode | default(false) | bool
  block:
    - name: Ensure elasticsearch group exists
      ansible.builtin.group:
        name: elasticsearch
        gid: 3004
        state: present
    - name: Ensure elasticsearch user exists
      ansible.builtin.user:
        name: elasticsearch
        createhome: false
        groups: elasticsearch
        shell: /sbin/falselogin
        state: present
        uid: 3004
    - name: Create elasticsearch directories
      ansible.builtin.file:
        path: "/apps/squirro{{ item }}"
        group: elasticsearch
        mode: "0750"
        owner: elasticsearch
        state: directory
      loop:
        - /etc/elasticsearch
        - /usr/share/elasticsearch
        - /usr/share/elasticsearch/tmp
        - /var/lib/elasticsearch
        - /var/log/elasticsearch
    - name: Setup elasticsearch repo paths
      ansible.builtin.file:
        path: "{{ item }}"
        mode: "0750"
        group: elasticsearch
        owner: elasticsearch
        state: directory
      run_once: true
      when:
        - squirro_storagenode | default(false) | bool
        - elasticsearch_repo_paths is defined
      with_items: "{{ elasticsearch_repo_paths }}"
    - name: Set symlink for elasticsearch directories
      ansible.builtin.file:
        path: "{{ item }}"
        src: "/apps/squirro{{ item }}"
        state: link
      loop:
        - /etc/elasticsearch
        - /usr/share/elasticsearch
        - /var/lib/elasticsearch
        - /var/log/elasticsearch

- name: Configure redis user and group
  when:
    - not remote_redis_server | default(false) | bool
    - squirro_clusternode | default(false) | bool
  block:
    - name: Configure redis group
      ansible.builtin.group:
        name: redis
        gid: 3001
        state: present
    - name: Configure redis user
      ansible.builtin.user:
        name: redis
        comment: "Redis Database Server"
        createhome: false
        groups: redis
        shell: /sbin/falselogin
        state: present
        uid: 3001
    - name: Create redis directories
      ansible.builtin.file:
        path: "/apps/squirro{{ item }}"
        group: redis
        mode: "0755"
        owner: redis
        state: directory
      loop:
        - /etc/redis
        - /var/lib/redis
        - /var/log/redis
    - name: Set symlink for for Redis directories
      ansible.builtin.file:
        path: "{{ item }}"
        src: "/apps/squirro{{ item }}"
        state: link
      loop:
        - /etc/redis
        - /var/lib/redis
        - /var/log/redis

- name: Configure cluster node groups
  ansible.builtin.group:
    name: "{{ item.name }}"
    gid: "{{ item.gid }}"
  loop:
    - name: "zkeeper"
      gid: "3002"
    - name: "tika"
      gid: "3003"

- name: Set symlink for cluster node directories
  ansible.builtin.file:
    path: "{{ item }}"
    src: "/apps/squirro{{ item }}"
    state: link
  loop:
    - /etc/squirro
    - /opt/squirro
    - /var/log/squirro
    - /var/lib/squirro

- name: Configure cluster node users
  ansible.builtin.user:
    name: "{{ item.name }}"
    comment: "{{ item.comment }}"
    createhome: "{{ item.home is defined }}"
    group: "{{ item.group }}"
    home: "{{ item.home | default(omit) }}"
    shell: /sbin/nologin
    state: present
    system: true
    uid: "{{ item.uid }}"
  loop:
    - name: "scrapyd"
      comment: "Scrapyd Service"
      group: "squirro"
      home: "/var/lib/squirro/scrapyd"
      uid: 4028
    - name: "sqclustr"
      comment: "Squirro Cluster Service"
      group: "squirro"
      home: "/var/lib/squirro/cluster"
      uid: 4004
    - name: "sqconfig"
      comment: "Squirro Configuration Service"
      group: "squirro"
      home: "/var/lib/squirro/configuration"
      uid: 4005
    - name: "sqcontnt"
      comment: "Squirro Content Service"
      group: "squirro"
      home: "/var/lib/squirro/content"
      uid: 4006
    - name: "sqdatasource"
      comment: "Squirro Datasource Service"
      group: "squirro"
      home: "/var/lib/squirro/datasource"
      uid: 4007
    - name: "sqemails"
      comment: "Squirro Email Sender Service"
      group: "squirro"
      home: "/var/lib/squirro/emailsender"
      uid: 4009
    - name: "sqfilter"
      comment: "Squirro Filtering Service"
      group: "squirro"
      home: "/var/lib/squirro/filtering"
      uid: 4010
    - name: "sqfinger"
      comment: "Squirro Fingerprint Service"
      group: "squirro"
      home: "/var/lib/squirro/fingerprint"
      uid: 4011
    - name: "sqfront"
      comment: "Squirro Frontend Service"
      group: "squirro"
      home: "/var/lib/squirro/frontend"
      uid: 4022
    - name: "sqgenai"
      comment: "Squirro GenAI Service"
      group: "squirro"
      home: "/var/lib/squirro/genai"
      uid: 4029
    - name: "sqindexmanager"
      comment: "Squirro Index Manager Service"
      group: "squirro"
      home: "/var/lib/squirro/indexmanager"
      uid: 4027
    - name: "sqingest"
      comment: "Squirro Ingester Service"
      group: "squirro"
      home: "/var/lib/squirro/ingester"
      uid: 4012
    - name: "sqmailer"
      comment: "Squirro Digest Mailer Service"
      group: "squirro"
      home: "/var/lib/squirro/digestmailer"
      uid: 4008
    - name: "sqnlp"
      comment: "Squirro NLP Service"
      group: "squirro"
      home: "/var/lib/squirro/nlp"
      uid: 4026
    - name: "sqnotes"
      comment: "Squirro Notes Service"
      group: "squirro"
      home: "/var/lib/squirro/notes"
      uid: 4023
    - name: "sqpdfcon"
      comment: "Squirro PDF Conversion Service"
      group: "squirro"
      home: "/var/lib/squirro/pdfconversion"
      uid: 4013
    - name: "sqplumbr"
      uid: 4014
      group: "squirro"
      home: "/var/lib/squirro/plumber"
      comment: "Squirro Plumber Service"
    - name: "sqprovid"
      comment: "Squirro Provider Service"
      group: "squirro"
      home: "/var/lib/squirro/provider"
      uid: 4001
    - name: "sqrstory"
      comment: "Squirro Related Story Service"
      group: "squirro"
      home: "/var/lib/squirro/relatedstory"
      uid: 4015
    - name: "sqsched"
      comment: "Squirro Scheduler Service"
      group: "squirro"
      home: "/var/lib/squirro/scheduler"
      uid: 4016
    - name: "sqsearch"
      comment: "Squirro Search Service"
      group: "squirro"
      home: "/var/lib/squirro/search"
      uid: 4025
    - name: "sqthumb"
      comment: "Squirro Thumbler Service"
      group: "squirro"
      home: "/var/lib/squirro/thumbler"
      uid: 4017
    - name: "sqtopic"
      comment: "Squirro Topic Service"
      group: "squirro"
      home: "/var/lib/squirro/topic"
      uid: 4002
    - name: "sqtproxy"
      comment: "Squirro Topic Proxy Service"
      group: "squirro"
      home: "/var/lib/squirro/topicproxy"
      uid: 4018
    - name: "sqtrends"
      comment: "Squirro Trend Service"
      group: "squirro"
      home: "/var/lib/squirro/trends"
      uid: 4019
    - name: "squproxy"
      comment: "Squirro User Proxy Service"
      group: "squirro"
      home: "/var/lib/squirro/userproxy"
      uid: 4020
    - name: "squser"
      comment: "Squirro User Service"
      group: "squirro"
      home: "/var/lib/squirro/user"
      uid: 4003
    - name: "sqwebsht"
      comment: "Squirro Webshot Service"
      group: "squirro"
      home: "/var/lib/squirro/webshot"
      uid: 4021
    - name: "tika"
      comment: "Tika Service"
      group: "tika"
      uid: 3003
    - name: "zkeeper"
      comment: "ZooKeeper Server"
      group: "zkeeper"
      home: "/var/lib/zookeeper"
      uid: "3002"
  when: squirro_clusternode | default(false) | bool
