From 89ecfc164ed92696de4c5cd2c0f90f23c1ad3784 Mon Sep 17 00:00:00 2001
From: Sam Yaple <sam@yaple.net>
Date: Fri, 25 Sep 2015 09:19:07 +0000
Subject: [PATCH] Replace config-external - haproxy keepalived

Change-Id: I668c31401305713e720f0f0ae8ec18e320ea1c76
Partially-Implements: blueprint replace-config-external
---
 ansible/roles/haproxy/tasks/config.yml             | 10 ++++++++++
 ansible/roles/haproxy/tasks/start.yml              |  4 ++--
 ansible/roles/haproxy/templates/haproxy.json.j2    | 11 +++++++++++
 ansible/roles/haproxy/templates/keepalived.json.j2 | 11 +++++++++++
 docker/haproxy/ensure_latest_config.sh             |  2 +-
 docker/haproxy/start.sh                            | 10 ++++------
 docker/keepalived/start.sh                         | 10 ++++------
 7 files changed, 43 insertions(+), 15 deletions(-)
 create mode 100644 ansible/roles/haproxy/templates/haproxy.json.j2
 create mode 100644 ansible/roles/haproxy/templates/keepalived.json.j2

diff --git a/ansible/roles/haproxy/tasks/config.yml b/ansible/roles/haproxy/tasks/config.yml
index d71ae7232b..633e96c6f6 100755
--- a/ansible/roles/haproxy/tasks/config.yml
+++ b/ansible/roles/haproxy/tasks/config.yml
@@ -10,6 +10,11 @@
     src: "haproxy.cfg.j2"
     dest: "{{ node_config_directory }}/haproxy/haproxy.cfg"
 
+- name: Copying haproxy JSON configuration file
+  template:
+    src: "haproxy.json.j2"
+    dest: "{{ node_config_directory }}/haproxy/config.json"
+
 - name: Allowing non-local IP binding
   sysctl: name="net.ipv4.ip_nonlocal_bind" value=1 sysctl_set=yes
 
@@ -19,6 +24,11 @@
     state: "directory"
     recurse: "yes"
 
+- name: Copying keepalived JSON configuration file
+  template:
+    src: "keepalived.json.j2"
+    dest: "{{ node_config_directory }}/keepalived/config.json"
+
 - name: Copying over config(s)
   template:
     src: "keepalived.conf.j2"
diff --git a/ansible/roles/haproxy/tasks/start.yml b/ansible/roles/haproxy/tasks/start.yml
index 317c497a19..84a545d2b4 100755
--- a/ansible/roles/haproxy/tasks/start.yml
+++ b/ansible/roles/haproxy/tasks/start.yml
@@ -15,7 +15,7 @@
     image: "{{ keepalived_image_full }}"
     privileged: True
     volumes:
-      - "{{ node_config_directory }}/keepalived/:/opt/kolla/keepalived/:ro"
+      - "{{ node_config_directory }}/keepalived/:/opt/kolla/config_files/:ro"
       - "/run:/run"
       - "/lib/modules:/lib/modules:ro"
     env:
@@ -37,7 +37,7 @@
     image: "{{ haproxy_image_full }}"
     privileged: True
     volumes:
-      - "{{ node_config_directory }}/haproxy/:/opt/kolla/haproxy/:ro"
+      - "{{ node_config_directory }}/haproxy/:/opt/kolla/config_files/:ro"
       - "/run:/run"
     env:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
diff --git a/ansible/roles/haproxy/templates/haproxy.json.j2 b/ansible/roles/haproxy/templates/haproxy.json.j2
new file mode 100644
index 0000000000..62a30b3dd1
--- /dev/null
+++ b/ansible/roles/haproxy/templates/haproxy.json.j2
@@ -0,0 +1,11 @@
+{
+    "command": "/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid",
+    "config_files": [
+        {
+            "source": "/opt/kolla/config_files/haproxy.cfg",
+            "dest": "/etc/haproxy/haproxy.cfg",
+            "owner": "root",
+            "perm": "0644"
+        }
+    ]
+}
diff --git a/ansible/roles/haproxy/templates/keepalived.json.j2 b/ansible/roles/haproxy/templates/keepalived.json.j2
new file mode 100644
index 0000000000..27ebf7c96f
--- /dev/null
+++ b/ansible/roles/haproxy/templates/keepalived.json.j2
@@ -0,0 +1,11 @@
+{
+    "command": "/usr/sbin/keepalived -nld -p /run/keepalived.pid",
+    "config_files": [
+        {
+            "source": "/opt/kolla/config_files/keepalived.conf",
+            "dest": "/etc/keepalived/keepalived.conf",
+            "owner": "root",
+            "perm": "0644"
+        }
+    ]
+}
diff --git a/docker/haproxy/ensure_latest_config.sh b/docker/haproxy/ensure_latest_config.sh
index b91189ca57..af20175a6d 100755
--- a/docker/haproxy/ensure_latest_config.sh
+++ b/docker/haproxy/ensure_latest_config.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 CURRENT_CONFIG_HASH=$(sha1sum /etc/haproxy/haproxy.cfg | cut -f1 -d' ')
-NEW_CONFIG_HASH=$(sha1sum /opt/kolla/haproxy/haproxy.cfg | cut -f1 -d' ')
+NEW_CONFIG_HASH=$(sha1sum /opt/kolla/config_files/haproxy.cfg | cut -f1 -d' ')
 
 if [[ $CURRENT_CONFIG_HASH != $NEW_CONFIG_HASH ]]; then
     changed=changed
diff --git a/docker/haproxy/start.sh b/docker/haproxy/start.sh
index d1da4a607f..629c852544 100755
--- a/docker/haproxy/start.sh
+++ b/docker/haproxy/start.sh
@@ -1,17 +1,15 @@
 #!/bin/bash
 set -o errexit
 
-CMD='/usr/sbin/haproxy'
-ARGS="-f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid"
-
 # Loading common functions.
 source /opt/kolla/kolla-common.sh
 
-# Execute config strategy
-set_configs
+# Generate run command
+python /opt/kolla/set_configs.py
+CMD=$(cat /run_command)
 
 # We are intentionally not using exec so we can reload the haproxy config later
-$CMD $ARGS
+$CMD
 
 # TODO(SamYaple): This has the potential for a race condition triggered by a
 #                 config reload that could cause the container to exit
diff --git a/docker/keepalived/start.sh b/docker/keepalived/start.sh
index a122526e48..8c41a5f7de 100755
--- a/docker/keepalived/start.sh
+++ b/docker/keepalived/start.sh
@@ -1,14 +1,12 @@
 #!/bin/bash
 set -o errexit
 
-CMD="/usr/sbin/keepalived"
-ARGS="-nld -p /run/keepalived.pid"
-
 # Loading common functions.
 source /opt/kolla/kolla-common.sh
 
-# Execute config strategy
-set_configs
+# Generate run command
+python /opt/kolla/set_configs.py
+CMD=$(cat /run_command)
 
 modprobe ip_vs
 
@@ -17,4 +15,4 @@ if [ -f /run/keepalived.pid ]; then
     rm /run/keepalived.pid
 fi
 
-exec $CMD $ARGS
+exec $CMD