---

- hosts: nodes
  become: yes
  become_user: root
  become_method: sudo
  gather_facts: yes


  vars:
    raft_repo: "https://github.com/Redislabs/redisraft"
    raft_branch: "master"
    deb_packages:
      - build-essential
      - autoconf
      - cmake
      - automake
      - libbsd-dev
      - libltdl-dev
      - libltdl7
      - libtool
      - libevent-openssl-2.1-6
      - libpcre3-dev 
      - libevent-dev
      - pkg-config
      - zlib1g-dev
      - libssl-dev
      - python3-pip
    redis_ports:
      - {"port": 19901, "start-hslot": 0, "end-hslot": 1365}
      - {"port": 19902, "start-hslot": 1366, "end-hslot": 2730}
      - {"port": 19903, "start-hslot": 2731, "end-hslot": 4096}
      - {"port": 19904, "start-hslot": 4097, "end-hslot": 5460}
      - {"port": 19905, "start-hslot": 5461, "end-hslot": 6826}
      - {"port": 19906, "start-hslot": 6827, "end-hslot": 8191}
      - {"port": 19907, "start-hslot": 8192, "end-hslot": 9557}
      - {"port": 19908, "start-hslot": 9558, "end-hslot": 10921}
      - {"port": 19909, "start-hslot": 10922, "end-hslot": 12287}
      - {"port": 19910, "start-hslot": 12288, "end-hslot": 13651}
      - {"port": 19911, "start-hslot": 13652, "end-hslot": 15017}
      - {"port": 19912, "start-hslot": 15018, "end-hslot": 16383}

  handlers:
    - name: reload_redis_services
      command: systemctl daemon-reload
    - name: restart_redis_services
      systemd:
        name: "redis-{{ item['port'] }}"
        state: restarted
      with_items: "{{ redis_ports }}"

  pre_tasks:
    - name: Update Apt Cache
      apt: update_cache=yes cache_valid_time=86400
      when: ansible_os_family == "Debian"
    - name: Ubuntu Packages
      package:
        name: "{{ deb_packages }}"
    - name: Set to Realistic Hostname
      hostname:
        name: "cpredisnode-{{ ansible_default_ipv4.address | replace('.', '-') }}"
    - name: Setup Hostsfile
      lineinfile:
        dest: /etc/hosts
        regexp: '^127\.0\.0\.1[ \t]+localhost'
        line: "127.0.0.1 localhost cpredisnode-{{ansible_default_ipv4.address | replace('.','-')}}"
        state: present
    - name: Disable THP
      shell: "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled"
    - name: Disable Swap
      ansible.posix.sysctl:
        name: vm.swappiness
        value: '0'
        state: present
    - name: Enable RPS
      shell: "echo '3' > /sys/class/net/{{ ansible_default_ipv4.alias}}/queues/rx-0/rps_cpus"
    - name: Install Redis Python pip
      pip:
        name: redis
        executable: pip3
    - name: Install Redis Cluster Python pip
      pip:
        name: redis-py-cluster
        executable: pip3


  tasks:
    - name: load vars
      include_vars: default.yaml
      tags:
        - cluster

    - name: create redis user
      user:
        name: redis
        uid: 4001
        shell: /bin/bash
        state: present
    - name: raft - clone the github repo
      git:
        repo: "{{raft_repo}}"
        dest: /home/redis/redisraft
        version: "{{raft_branch}}"
        recursive: yes
        update: yes
        force: yes
      notify:
        - restart_redis_services
    - name: raft - make
      make:
        chdir: /home/redis/redisraft
    - name: create raft directories
      file:
        path: /redis/persistant/cpredis
        owner: redis
        group: redis
        mode: '0755'
        state: directory
    - name: create raft directories
      file:
        path: /var/log/cpredis
        owner: redis
        group: redis
        mode: '0755'
        state: directory
    - name: checkout redis git repo
      git:
        repo: 'https://github.com/redis/redis'
        dest: /home/redis/redis
  
    - name: redis - Build the default target
      make:
        chdir: /home/redis/redis
  
    - name: redis - Run 'install' target as root
      make:
        chdir: /home/redis/redis
        target: install
      become: yes
    - name: Redis Systemd 
      template:
        src: systemd_redis.j2
        dest: /lib/systemd/system/redis-{{ item['port'] }}.service
        owner: root
        group: root
        mode: 0644
      with_items: "{{ redis_ports }}"
      notify:
        - reload_redis_services
        - restart_redis_services

    - name: Cluster Setup Script
      template:
        src: setup_raft_cluster.j2
        dest: /usr/local/bin/setup-raft-cluster
        owner: redis
        group: redis
        mode: 0755
      tags:
        - cluster

    - name: Enable Redis Services
      systemd:
        name: redis-{{ item['port'] }}
        enabled: yes
        state: started
      with_items: "{{ redis_ports }}"

    - name: Show RG script
      copy:
        src: rgshow.py
        dest: /usr/bin/rgshow
        owner: root
        group: root
        mode: "0755"
