---
- name: Allow gluster through firewall
  become: true
  ansible.posix.firewalld:
    service: glusterfs
    state: enabled
    zone: trusted
    immediate: true
    permanent: true

- name: Create datastore directory
  become: true
  ansible.builtin.file:
    path: /mnt/brick/datastore
    state: directory

- name: Start and disable glusterd
  become: true
  ansible.builtin.systemd:
    name: glusterd
    state: started
    enabled: false

- name: Fetch peer status
  become: true
  ansible.builtin.command:
    cmd: gluster peer status
  changed_when: false
  register: _gluster_peer_status_raw

- name: Check peer status
  ansible.builtin.assert:
    that:
      - not _gluster_peer_status_raw.stdout_lines[0].strip().endswith('0')
    fail_msg: >-
      ERROR: Datastore host '{{ inventory_hostname }}' is not joined to the gluster pool. Run the
      command 'gluster peer probe {{ inventory_hostname }}.local' from another datastore host to
      add it.
    success_msg: >-
      Datastore host {{ inventory_hostname }} is joined to the gluster pool

- name: Mount gluster volume
  become: true
  ansible.posix.mount:
    path: /mnt/datastore
    src: localhost:/datastore
    state: mounted
    fstype: glusterfs
    # Note that this just needs to be any path *other* than the actual
    # fstab. This is done just to prevent the devices from being
    # automatically mounted at boot
    fstab: "{{ skylab_state_dir }}/mounts"
