---
- name: Group for Discourse present
  group:
    name: discourse
    system: yes
    state: present
    gid: 1000

- name: User for Discourse present
  user:
    name: discourse
    system: yes
    state: present
    shell: /bin/bash
    home: /home/discourse
    createhome: true
    groups: discourse,docker
    uid: 1000

- name: Stat /var/discourse/lost+found
  stat:
    path: "/var/discourse/lost+found"
  register: var_discourse_partition

- block:

  - name: Delete lost+found directory if /var/discourse is a partition 
    file:
      dest: /var/discourse/lost+found
      state: absent

  when: var_discourse_partition.stat.exists == True

- name: Directory for Discourse present
  file:
    dest: /var/discourse
    state: directory
    owner: discourse
    group: discourse

- name: ssl-cert group present for UID mappings
  group:
    name: ssl-cert
    system: yes
    state: present
    gid: 111

- name: postgres group present for UID mappings
  group:
    name: postgres
    system: yes
    state: present
    gid: 112

- name: postgres user persent for GID mappings
  user:
    name: postgres
    system: yes
    group: postgres
    createhome: false
    shell: /bin/false
    uid: 107

- name: haproxy group present for UID mappings
  group:
    name: haproxy
    system: yes
    state: present
    gid: 113

- name: haproxy user persent for GID mappings
  user:
    name: haproxy
    system: yes
    group: haproxy
    createhome: false
    shell: /bin/false
    uid: 108

- name: redis group present for UID mappings
  group:
    name: redis
    system: yes
    state: present
    gid: 114

- name: redis user persent for GID mappings
  user:
    name: redis 
    system: yes
    group: redis 
    createhome: false
    shell: /bin/false
    uid: 110

- name: Discourse checked out
  git:
    repo: https://github.com/discourse/discourse_docker.git
    dest: /var/discourse
    update: yes
  become: yes
  become_user: 'discourse' 

- block:

  - name: Create lost+found directory 
    command: mklost+found
    args:
      chdir: /var/discourse
      creates: /var/discourse/lost+found

  when: var_discourse_partition.stat.exists == True

- name: Count how much swap is available
  shell: "free -g --si | awk '/^Swap:/{print $2}'"
  args:
    executable: /bin/bash
  register: swap_space

- debug:
    msg: "There is {{ swap_space.stdout }}GB of swap space"

- name: Fail if less than 2GB of swap is available 
  fail:
    msg: "Please ensure that the server has at least 2G of swap available"
  when: swap_space < 2

- name: 25% of physical memory calculated
  shell: "echo $(( $(free -m | awk '/^Mem:/{print $2}') / 4 ))"
  args:
    executable: /bin/bash
  register: db_shared_buffers

- debug:
    msg: "db_shared_buffers to be set {{ db_shared_buffers.stdout }}MB, which is 25% of physical memory"

- name: Count the CPUs
  command: nproc --all 
  register: cpus

- debug:
    msg: "There are {{ cpus.stdout }} CPUs available"

- name: Count the memory in GB 
  shell: "free -g --si | awk '/^Mem:/{print $2}'"
  args:
    executable: /bin/bash
  register: memory

- debug:
    msg: "There is {{ memory.stdout }}GB of RAM available"

- block:

  - name: Number of Unicorn Workers set to 2 times RAM in GB
    shell: "echo $(( 2 * {{ memory.stdout }} ))"
    args:
      executable: /bin/bash
    register: unicorn_workers
  
  when: memory < 2 

- block:

  - name: Number of Unicorn Workers set to 2 times the number of CPUs 
    shell: "echo $(( 2 * {{ cpus.stdout }} ))"
    args:
      executable: /bin/bash
    register: unicorn_workers

  when: memory >= 2

- debug:
    msg: "Unicorn Workers set to {{ unicorn_workers.stdout }}"

- name: Get the IP address of the server
  command: hostname -i
  register: host_ip

- name: Generate app.yml
  template:
    src: templates/standalone.yml.j2
    dest: /var/discourse/containers/app.yml
  become: yes
  become_user: discourse 

- name: Rebuild Discourse app
  command: /var/discourse/launcher rebuild app
  become: yes
  become_user: discourse