Add ability to force peer uuid in case of recovery and add bricks

Change-Id: Ia2719043ee039976e32592a9d577c9697f06cfef
diff --git a/README.rst b/README.rst
index bea64d3..0d13c5b 100644
--- a/README.rst
+++ b/README.rst
@@ -120,6 +120,19 @@
             - 172.168.1.21:/srv/glusterfs/glance
        enabled: true
 
+Server with forced peer UUID (for peer recovery)
+------------------------------------------------
+
+.. code-block:: yaml
+
+   glusterfs:
+     server:
+       recover_peers:
+         kvm03.testserver.local:
+           enabled: true
+           uuid: ab6ac060-68f1-4f0b-8de4-70241dfb2279
+
+
 Client
 ------
 
diff --git a/glusterfs/files/glusterd.info b/glusterfs/files/glusterd.info
new file mode 100644
index 0000000..f49971f
--- /dev/null
+++ b/glusterfs/files/glusterd.info
@@ -0,0 +1,2 @@
+UUID={{ uuid }}
+operating-version=30800
\ No newline at end of file
diff --git a/glusterfs/server/service.sls b/glusterfs/server/service.sls
index f8203f7..4eea178 100644
--- a/glusterfs/server/service.sls
+++ b/glusterfs/server/service.sls
@@ -6,6 +6,37 @@
   pkg.installed:
     - names: {{ server.pkgs }}
 
+
+{%- if server.recover_peers is defined %}
+{%- for peer_name, peer_data in server.recover_peers.iteritems() %}
+{%- if peer_data.get('enabled', False) and grains.get('fqdn', 'unknown') == peer_name %}
+
+force_peer_uuid:
+  file.managed:
+    - source: salt://glusterfs/files/glusterd.info
+    - name: /var/lib/glusterd/glusterd.info
+    - template: jinja
+    - makedirs: True
+    - defaults:
+        uuid: {{ peer_data.uuid }}
+
+stop_glusterfs_service:
+  service.dead:
+    - name: {{ server.service }}
+    - onchanges:
+      - file: force_peer_uuid
+
+
+glusterfs_sleep:
+  cmd.wait:
+    - name: sleep 5
+    - watch-in:
+      - service: stop_glusterfs_service
+
+{%- endif %}
+{%- endfor %}
+{%- endif %}
+
 glusterfs_service:
   service.running:
     - name: {{ server.service }}
diff --git a/glusterfs/server/setup.sls b/glusterfs/server/setup.sls
index 1db37a3..b983810 100644
--- a/glusterfs/server/setup.sls
+++ b/glusterfs/server/setup.sls
@@ -114,4 +114,23 @@
 {%- endfor %}
 {%- endif %}
 
+{%- if server.recover_peers is defined %}
+{%- for vol_name, vol_data in server.volumes.iteritems() %}
+{%- for brick in vol_data.bricks %}
+
+add_gluster_bricks_{{ vol_name }}_{{ brick }}:
+  cmd.run:
+    - name: "gluster volume add-brick {{ vol_name }} replica {{ vol_data.replica }} {{ brick }} force"
+    - unless: "gluster volume info {{ vol_name }} | grep {{ brick }}"
+    - require:
+      {%- if force_compatibility %}
+      - cmd: glusterfs_vol_{{ vol_name }}
+      {%- else %}
+      - glusterfs: glusterfs_vol_{{ vol_name }}
+      {%- endif %}
+
+{%- endfor %}
+{%- endfor %}
+{%- endif %}
+
 {%- endif %}