---
- name: create sentinel working directory
  file:
    path: "{{ redis_sentinel_dir }}"
    state: directory
    recurse: yes
    owner: "{{ redis_user }}"

- name: create sentinel init script
  template:
    src: "{{ item }}"
    dest: /etc/init.d/sentinel_{{ redis_sentinel_port }}
    mode: 0755
  # Choose the distro-specific template. We must specify the templates
  # path here because with_first_found tries to find files in files/
  with_first_found:
    - files:
      - "{{ ansible_os_family }}/redis_sentinel.init.j2"
      - default/redis_sentinel.init.j2
      paths:
        - ../templates
  when: redis_as_service and ansible_service_mgr|default() != "systemd"

- name: create sentinel systemd service
  template:
    src: "{{ item }}"
    dest: /etc/systemd/system/sentinel_{{ redis_sentinel_port }}.service
    mode: 0644
  with_first_found:
    - files:
      - "{{ ansible_os_family }}/redis_sentinel.service.j2"
      - default/redis_sentinel.service.j2
      paths:
        - ../templates
  register: sentinel_unit_file
  when: redis_as_service and ansible_service_mgr|default() == "systemd"

- name: create systemd tmpfiles configuration
  template:
    src: etc/tmpfiles.d/redis.conf.j2
    dest: /etc/tmpfiles.d/redis.conf
    mode: 0644
  when:
    - redis_as_service
    - ansible_service_mgr|default() == "systemd"
    - (redis_sentinel_pidfile|dirname).startswith("/var/run") or (redis_sentinel_pidfile|dirname).startswith("/run")

- name: reload systemd daemon
  command: systemctl daemon-reload
  when:
    - redis_as_service
    - ansible_service_mgr|default() == "systemd"
    - sentinel_unit_file|changed

- name: set sentinel to start at boot
  service:
    name: sentinel_{{ redis_sentinel_port }}
    enabled: yes
  when: redis_as_service

# Check then create log dir to prevent aggressively overwriting permissions
- name: check if sentinel log directory exists
  stat:
    path: "{{ redis_sentinel_logfile|dirname }}"
  register: sentinel_logdir
  changed_when: false
  when: redis_sentinel_logfile != '""'

- name: create sentinel log directory if it does not exist
  file:
    state: directory
    path: "{{ redis_sentinel_logfile|dirname }}"
    owner: "{{ redis_user }}"
    group: "{{ redis_group }}"
  when:
    - redis_sentinel_logfile != '""'
    - not sentinel_logdir.stat.exists

- name: touch the sentinel log file
  file:
    state: touch
    path: "{{ redis_sentinel_logfile }}"
    owner: "{{ redis_user }}"
    group: "{{ redis_group }}"
  when: redis_sentinel_logfile != '""'

- name: check if sentinel pid directory exists
  stat:
    path: "{{ redis_sentinel_pidfile|dirname }}"
  register: sentinel_piddir
  changed_when: false
  when: redis_sentinel_pidfile != '""'

- name: create sentinel pid directory if it does not exist
  file:
    state: directory
    path: "{{ redis_sentinel_pidfile|dirname }}"
    owner: "{{ redis_user }}"
    group: "{{ redis_group }}"
  when:
    - redis_sentinel_pidfile != '""'
    - not sentinel_piddir.stat.exists

- name: create sentinel config file
  template:
    src: redis_sentinel.conf.j2
    dest: /etc/redis/sentinel_{{ redis_sentinel_port }}.conf
    owner: "{{ redis_user }}"
    mode: 0640
  notify: "restart sentinel {{ redis_sentinel_port }}"

- name: add sentinel init config file
  template:
    dest: /etc/sysconfig/sentinel_{{ redis_sentinel_port }}
    src: redis.init.conf.j2
  when: ansible_os_family == "RedHat"
  notify: "restart sentinel {{ redis_sentinel_port }}"

- name: add sentinel init config file
  template:
    dest: /etc/default/sentinel_{{ redis_sentinel_port }}
    src: redis.init.conf.j2
  when: ansible_os_family == "Debian"
  notify: "restart sentinel {{ redis_sentinel_port }}"

# Flush handlers before ensuring the service is started to prevent
# a start and then restart
- name: flush handlers to apply config changes
  meta: flush_handlers

- name: ensure sentinel is running
  service:
    name: sentinel_{{ redis_sentinel_port }}
    state: started
  when: redis_as_service
