---
# tasks file for provision_cluster

- name: Distribution
  ansible.builtin.debug:
    msg: "{{ ansible_distribution }}"

- name: Distribution version
  ansible.builtin.debug:
    msg: "{{ ansible_distribution_version }}"

- name: Distribution major version
  ansible.builtin.debug:
    msg: "{{ ansible_distribution_major_version }}"

- name: Include OS variables
  ansible.builtin.include_vars: "{{ ansible_os_family }}.yml"

- name: Deploy per OS prerequisites
  ansible.builtin.include_tasks: "install-{{ ansible_os_family }}.yml"

- name: Debug node count
  ansible.builtin.debug:
    var: nodecount
    verbosity: 2

- name: Gather the package facts
  ansible.builtin.package_facts:
    manager: auto

- name: Debug osvc_repo_url
  ansible.builtin.debug:
    var: osvc_repo_url
    verbosity: 2

- name: Download OpenSVC agent latest package
  ansible.builtin.get_url:
    url: "{{ osvc_repo_url }}"
    dest: "{{ osvc_pkg_dl_path }}"
    force: true
    mode: '0600'
  when: "'opensvc' not in ansible_facts.packages"

- name: Install OpenSVC agent package
  ansible.builtin.include_tasks: "install-{{ ansible_os_family }}-opensvc.yml"
  when: "'opensvc' not in ansible_facts.packages"

- name: Remove package file from /tmp
  ansible.builtin.file:
    path: "{{ osvc_pkg_dl_path }}"
    state: absent
  when: "'opensvc' not in ansible_facts.packages"

- name: Enable opensvc-agent.service unit file
  ansible.builtin.service:
    name: opensvc-agent.service
    enabled: true
    state: started

- name: Update /etc/hosts file
  ansible.builtin.blockinfile:
    dest: /etc/hosts
    content: "{{ lookup('template', 'templates/hosts.j2') }}"
    state: present
  when:
    - osvc_update_etc_hosts | bool

- name: Enable ssh root access accross cluster nodes
  ansible.builtin.replace:
    dest: /etc/ssh/sshd_config
    regexp: '^PermitRootLogin (yes|without-password)'
    replace: "PermitRootLogin prohibit-password"
  notify: Reload sshd

- name: Flush handlers
  ansible.builtin.meta: flush_handlers

- name: Enable sysrq trigger
  ansible.posix.sysctl:
    name: kernel.sysrq
    value: '1'
    state: present
    sysctl_file: /etc/sysctl.d/99-opensvc-sysrq.conf
    sysctl_set: true

########################## FIRST NODE ##########################

- name: Check hb cluster param exists on first node
  ansible.builtin.command: om cluster get --kw hb#1.type
  register: clusterhbtype
  changed_when: false
  failed_when: false
  when: inventory_hostname == ansible_play_hosts[0]

- name: Configure cluster with unicast hb
  ansible.builtin.command: om cluster set --kw hb#1.type=unicast --kw hb#1.timeout={{ osvc_hb_timeout }}
  register: output
  changed_when: output.rc == 0
  when:
    - inventory_hostname == ansible_play_hosts[0]
    - clusterhbtype.stdout == "None"

- name: Get current cluster name
  ansible.builtin.command: om cluster get --kw cluster.name
  register: clustername
  changed_when: false
  failed_when: false
  when:
    - inventory_hostname == ansible_play_hosts[0]

- name: Set cluster name
  ansible.builtin.command: "om cluster set --kw cluster.name={{ osvc_clustername }}"
  register: output
  changed_when: output.rc == 0
  when:
    - inventory_hostname == ansible_play_hosts[0]
    - clustername.stdout != osvc_clustername

- name: Get cluster secret on first node
  ansible.builtin.command: om cluster get --kw cluster.secret
  register: clustersecret
  changed_when: false
  failed_when: false
  when:
    - inventory_hostname == ansible_play_hosts[0]
    - nodecount | int > 1

########################## OTHER NODES ##########################

- name: Check other nodes are joined to a cluster
  ansible.builtin.command: om cluster get --kw cluster.nodes
  register: clusternodes
  changed_when: false
  failed_when: false
  when:
    - nodecount | int > 1

- name: Consider that we have join the cluster
  ansible.builtin.set_fact:
    joined: 'true'
  when:
    - nodecount | int > 1

- name: Check if we are actually joined
  ansible.builtin.set_fact:
    joined: 'false'
  when:
    - nodecount | int > 1
    - clusternodes.stdout == ansible_hostname
    - inventory_hostname != ansible_play_hosts[0]

- name: Join other nodes to the cluster
  ansible.builtin.command: >
    om daemon join
    --secret {{ hostvars[ansible_play_hosts[0]].clustersecret.stdout }}
    --node {{ hostvars[ansible_play_hosts[0]].ansible_hostname }}
  register: output
  changed_when: output.rc == 0
  when:
    - nodecount | int > 1
    - not joined

- name: Wait for join to be terminated
  ansible.builtin.command: 'om node wait --duration 20s --filter monitor.nodes.{{ ansible_hostname }}.monitor.status=idle'
  changed_when: false
  when:
    - nodecount | int > 1
    - not joined

- name: Add mutual root ssh trust
  ansible.builtin.command: 'om node update ssh authorized keys --node "*"'
  register: output
  changed_when: output.rc == 0
  when:
    - nodecount | int > 1
    - not joined

- name: Configure cluster vip
  ansible.builtin.include_tasks: "vip.yml"
  when:
    - osvc_configure_vip | bool

- name: Configure webapp/api access
  ansible.builtin.include_tasks: "webapp.yml"
  when:
    - osvc_configure_webapp | bool

- name: Disable iptables usage by docker
  ansible.builtin.include_tasks: "docker.yml"
  when:
    - osvc_deploy_docker_daemon_json | bool
