---

- name: Install glusterfs packages (Ubuntu)
  apt: name={{item}} state=installed update_cache=yes
  with_items:
    - glusterfs-server
    - glusterfs-client
    - attr
  when: is_debian_or_ubuntu
  tags:
    - gluster

- name: Install glusterfs packages (CentOS)
  yum: name={{item}} state=installed
  with_items:
    - glusterfs-server
    - glusterfs-client
  when: is_centos
  tags:
    - gluster

- name: Ensure the GlusterFS service is running (CentOS)
  service: name=glusterd state=started
  when: is_centos
  tags:
    - gluster

- file: path={{gluster_brick_dir}} state=directory
  tags:
    - gluster

- name: Configure peers (Only on the first host)
  shell: gluster peer probe {{item}}
  with_items: groups.gluster_data
  when: inventory_hostname == groups.gluster_data[0]
  tags:
    - gluster

- name: Set default stripe options
  shell: if [ "{{gluster_stripes|default('x')}}" = "x" ]; then echo ""; else echo "stripe {{gluster_stripes|default('')}}"; fi
  register: gluster_stripes
  connection: local
  sudo: false
  tags:
    - gluster

- name: Set default replica options
  shell: if [ "{{gluster_replicas | default('x')}}" = "x" ]; then echo ""; else echo "replica {{gluster_replicas|default('')}}"; fi
  register: gluster_replicas
  connection: local
  sudo: false
  tags:
    - gluster

- name: Set default transport options
  shell: if [  "{{gluster_transport|default('x')}}" = "x" ]; then echo "transport tcp"; else echo "transport {{gluster_transport|default('')}}"; fi
  register: gluster_transport
  connection: local
  sudo: false
  tags:
    - gluster

- name: Build bricks list
  shell: echo {{groups.gluster_data | join(',')}} | sed "s%,%:{{gluster_brick_dir}} %g; s%$%:{{gluster_brick_dir}}%"
  register: gluster_bricks
  connection: local
  sudo: false
  tags:
    - gluster

- name: Create default volume
  shell: gluster volume info {{gluster_volume}} || 
          gluster volume create {{gluster_volume}} {{gluster_stripes.stdout}}
          {{gluster_replicas.stdout}} {{gluster_transport.stdout}}
          {{gluster_bricks.stdout}} force
  when: inventory_hostname == groups.gluster_data[0]
  tags:
    - gluster

- name: Check if all the bricks have been added
  shell: for brick in {{gluster_bricks.stdout}}; do gluster volume info {{gluster_volume}} | grep -q $brick || echo -n "$brick "; done; true
  register: missing_bricks
  when: inventory_hostname == groups.gluster_data[0]
  tags:
    - gluster

- name: Ensure bricks are clean
  shell: gluster volume info | grep -q $(hostname):{{gluster_brick_dir}} ||        (setfattr -x trusted.glusterfs.volume-id {{gluster_brick_dir}}; 
        setfattr -x trusted.gfid {{gluster_brick_dir}}; 
        rm -rf {{gluster_brick_dir}}/.glusterfs)
  tags:
    - gluster

- name: Add missing bricks
  shell: test -n "{{missing_bricks.stdout}}" && gluster volume add-brick {{gluster_volume}} {{missing_bricks.stdout}} force
  when: inventory_hostname == groups.gluster_data[0]
  tags:
    - gluster

- name: Start default volume
  shell: 'gluster volume info {{gluster_volume}} | grep "Status: Started" || 
          gluster volume start {{gluster_volume}}'
  when: inventory_hostname == groups.gluster_data[0]
  tags:
    - gluster

