---
- name: Identify the IP address
  ansible.builtin.set_fact:
    ip: "{{ ansible_default_ipv4.address }}"
  when: env != "squirro"

- name: Identify the IP address on playground servers
  ansible.builtin.set_fact:
    ip: "{{ ansible_facts['enp7s0']['ipv4']['address'] }}"
  when: env == "squirro"

- name: Enable redis to listen to all interfaces
  ansible.builtin.lineinfile:
    path: "{{ item }}"
    regexp: "^bind"
    line: "# bind 127.0.0.1 -::1"
  loop:
    - /etc/redis/redis.conf
    - /etc/redis/cache.conf

- name: Announce replication to redis sentinel
  ansible.builtin.lineinfile:
    path: "{{ item }}"
    regexp: "^# replica-announced .*"
    line: "replica-announced yes"
  loop:
    - /etc/redis/redis.conf
    - /etc/redis/cache.conf

- name: Configure replica on slaves
  ansible.builtin.lineinfile:
    path: /etc/redis/redis.conf
    line: "replicaof {{ redis_initial_master }} {{ redis_port }}"
  when: ip != redis_initial_master

- name: Configure replica on redis server cache
  ansible.builtin.lineinfile:
    path: /etc/redis/cache.conf
    line: "replicaof {{ redis_initial_master }} {{ redis_cache_port }}"
  when: ip != redis_initial_master

- name: Restart redis server
  ansible.builtin.systemd:
    name: redis-server
    state: restarted

- name: Restart redis server cache
  ansible.builtin.systemd:
    name: redis-server-cache
    state: restarted

- name: Setup redis sentinel config
  ansible.builtin.template:
    src: sentinel.conf.j2
    dest: /etc/redis/sentinel.conf
    group: redis
    mode: "644"
    owner: redis

- name: Setup redis sentinel systemd service
  ansible.builtin.template:
    src: redis-sentinel.service.j2
    dest: /etc/systemd/system/redis-sentinel.service
    group: root
    mode: "0644"
    owner: root

- name: Ensure redis sentinel service is running
  ansible.builtin.systemd:
    name: redis-sentinel
    daemon_reload: true
    enabled: true
    state: started

- name: Patch Squirro to adopt redis sentinel
  ansible.builtin.shell: |
    mkdir -p /opt/squirro/virtualenv311/patches
    bash ../../../common/deployment/apply_patch ../../patches/squirro-common.patch
  changed_when: true

- name: Update redis host in Squirro configs
  community.general.ini_file:
    path: "{{ item.path }}"
    backup: true
    mode: "0644"
    section: "{{ item.section }}"
    option: "{{ item.option }}"
    value: "{{ redis_sentinel_nodes | join(',') }}"
  with_items:
    - path: "/etc/squirro/common.ini"
      option: "host"
      section: "redis"
    - path: "/etc/squirro/common.ini"
      option: "host"
      section: "redis_content"
    - path: "/etc/squirro/common.ini"
      option: "host"
      section: "redis_bloom"
    - path: "/etc/squirro/common.ini"
      section: "redis_cache"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_http_cache"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_provider_deduplication"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_studio"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_studio_cache"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_queue_file_system_streamer"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "redis_database_migrations"
      option: "host"
    - path: "/etc/squirro/common.ini"
      section: "queues_local"
      option: "redis_host"
    - path: "/etc/squirro/common.ini"
      section: "redis_key_value_cache_provider"
      option: "redis_host"
    - path: "/etc/squirro/common.ini"
      section: "redis_bytes_cache"
      option: "host"
    - path: "/etc/squirro/datasource.ini"
      section: "redis_key_value_store_provider"
      option: "redis_host"
    - path: "/etc/squirro/datasource.ini"
      section: "redis_key_value_cache_provider"
      option: "redis_host"
    - path: "/etc/squirro/datasource.ini"
      section: "queues_local"
      option: "redis_host"
    - path: "/etc/squirro/filtering.ini"
      section: "redis_email_history"
      option: "host"
    - path: "/etc/squirro/frontend.ini"
      section: "redis_studio_cache"
      option: "host"
    - path: "/etc/squirro/indexmanager.ini"
      section: "redis_index_management"
      option: "host"
    - path: "/etc/squirro/machinelearning.ini"
      section: "queues_local"
      option: "redis_host"
    - path: "/etc/squirro/relatedstory.ini"
      section: "redis_duplicate"
      option: "host"
    - path: "/etc/squirro/relatedstory.ini"
      section: "redis_nearduplicate"
      option: "host"
    - path: "/etc/squirro/topicproxy.ini"
      section: "queues_local"
      option: "redis_host"
