Merge pull request #1 from tcpcloud/notify

Notify action and master state - WIP
diff --git a/README.rst b/README.rst
index 639d612..249750e 100644
--- a/README.rst
+++ b/README.rst
@@ -17,12 +17,14 @@
         enabled: True
         instance:
           VIP1:
+            nopreempt: True
             priority: 100 (highest priority must be on primary server, different for cluster members)
             virtual_router_id: 51
             password: pass
             address: 192.168.10.1
             interface: eth0
           VIP2:
+            nopreempt: True
             priority: 150 (highest priority must be on primary server, different for cluster members)
             virtual_router_id: 52
             password: pass
@@ -38,6 +40,7 @@
         enabled: True
         instance:
           VIP1:
+            nopreempt: True
             priority: 100 (highest priority must be on primary server, different for cluster members)
             virtual_router_id: 51
             password: pass
@@ -46,6 +49,52 @@
             - 192.168.10.2
             interface: eth0
 
+Disable nopreempt mode to have Master. Highest priority is taken in all cases.
+
+.. code-block:: yaml
+
+    keepalived:
+      cluster:
+        enabled: True
+        instance:
+          VIP1:
+            nopreempt: False
+            priority: 100 (highest priority must be on primary server, different for cluster members)
+            virtual_router_id: 51
+            password: pass
+            addresses:
+            - 192.168.10.1
+            - 192.168.10.2
+            interface: eth0
+
+Notify action in keepalived.
+
+.. code-block:: yaml
+
+    keepalived:
+      cluster:
+        enabled: True
+        instance:
+          VIP1:
+            nopreempt: True
+            notify_action:
+              master:
+                - /usr/bin/docker start jenkins
+                - /usr/bin/docker start gerrit
+              backup:
+                - /usr/bin/docker stop jenkins
+                - /usr/bin/docker stop gerrit
+              fault:
+                - /usr/bin/docker stop jenkins
+                - /usr/bin/docker stop gerrit
+            priority: 100 # highest priority must be on primary server, different for cluster members
+            virtual_router_id: 51
+            password: pass
+            addresses:
+            - 192.168.10.1
+            - 192.168.10.2
+            interface: eth0
+
 Read more
 =========
 
diff --git a/keepalived/cluster.sls b/keepalived/cluster.sls
index 85b107f..a750bfa 100644
--- a/keepalived/cluster.sls
+++ b/keepalived/cluster.sls
@@ -14,6 +14,27 @@
   - require:
     - pkg: keepalived_packages
 
+{% for instance_name, instance in cluster.instance.iteritems() %}
+
+{%- if instance.notify_action is defined %}
+
+keepalived_{{ instance_name }}_notify:
+  file.managed:
+  - name: /usr/local/bin/keepalived_notify_{{ instance_name }}.sh
+  - mode: 755
+  - source: salt://keepalived/files/keepalived_notify.sh
+  - template: jinja
+  - defaults:
+      notify_action: {{ instance.notify_action }}
+  - require:
+    - pkg: keepalived_packages
+  - require_in:
+    - service: keepalived_service
+
+{%- endif %}
+
+{% endfor %}
+
 keepalived_service:
   service.running:
   - name: {{ cluster.service }}
@@ -22,4 +43,4 @@
   - watch:
     - file: keepalived_config
 
-{%- endif %}
\ No newline at end of file
+{%- endif %}
diff --git a/keepalived/files/keepalived.conf b/keepalived/files/keepalived.conf
index ed429be..66b5b8c 100644
--- a/keepalived/files/keepalived.conf
+++ b/keepalived/files/keepalived.conf
@@ -4,11 +4,18 @@
 
 {% for instance_name, instance in cluster.instance.iteritems() %}
 vrrp_instance {{ instance_name }} {
+    {%- if instance.get('nopreempt', True) %}
     state BACKUP
     nopreempt
     garp_master_delay 5
     garp_master_repeat 3
     garp_master_refresh 1
+    {%- else %}
+    state MASTER
+    {%- endif %}
+    {%- if instance.notify_action is defined %}
+    notify /usr/local/bin/keepalived_notify_{{ instance_name }}.sh
+    {%- endif %}
     interface {{ instance.interface }}
     virtual_router_id {{ instance.virtual_router_id }}
     priority {{ instance.priority }}
@@ -27,4 +34,4 @@
         {%- endif %}
     }
 }
-{% endfor %}
\ No newline at end of file
+{% endfor %}
diff --git a/keepalived/files/keepalived_notify.sh b/keepalived/files/keepalived_notify.sh
new file mode 100644
index 0000000..9fe6fe9
--- /dev/null
+++ b/keepalived/files/keepalived_notify.sh
@@ -0,0 +1,27 @@
+#!/bin/bash -e
+
+TYPE=$1
+NAME=$2
+STATE=$3
+
+case $STATE in
+    "MASTER")
+        {%- for action in notify_action.master %}
+        {{ action }}
+        {%- endfor %}
+        ;;
+    "BACKUP")
+        {%- for action in notify_action.backup %}
+        {{ action }}
+        {%- endfor %}
+        ;;
+    "FAULT")
+        {%- for action in notify_action.fault %}
+        {{ action }}
+        {%- endfor %}
+        ;;
+    *)
+      echo "Unknown state $STATE" 1>&2
+      exit 1
+      ;;
+esac