From fc6ac8d177d2b00ad8504476893caf469dc25c30 Mon Sep 17 00:00:00 2001
From: Hongbin Lu <hongbin.lu@huawei.com>
Date: Mon, 17 Apr 2017 18:46:15 -0400
Subject: [PATCH] Avoid running multiple etcd process

If both Zun and Kuryr-libnetwork are enabled in devstack, the script
might fail since etcd will be installed and started twice. This commit
fixed it.

Change-Id: Ieffbb7a86e99a3e9a03cbfcadb2daf92d8f62cfb
---
 devstack/lib/zun               | 4 ++--
 devstack/local.conf.sample     | 2 ++
 zun/tests/contrib/gate_hook.sh | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/devstack/lib/zun b/devstack/lib/zun
index cc5312c12..404aed257 100644
--- a/devstack/lib/zun
+++ b/devstack/lib/zun
@@ -381,7 +381,7 @@ function install_etcd_server {
     if [ $(sudo docker ps -a | awk '{print $NF}' | grep -w etcd) ]; then
         sudo docker rm -f etcd || true
     fi
-    sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ZUN_ETCD_VERSION} \
+    [[ $(pgrep -x "etcd") ]] || sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ZUN_ETCD_VERSION} \
                     /usr/local/bin/etcd \
                     --data-dir=data.etcd \
                     --name node0 \
@@ -438,7 +438,7 @@ function start_zun_compute {
 
 function start_zun_etcd {
     echo "Start zun etcd..."
-    sudo docker start etcd
+    sudo docker start etcd || true
 }
 
 function stop_zun-etcd {
diff --git a/devstack/local.conf.sample b/devstack/local.conf.sample
index 79fce287c..df4151b01 100644
--- a/devstack/local.conf.sample
+++ b/devstack/local.conf.sample
@@ -5,6 +5,8 @@ SERVICE_TOKEN=password
 SERVICE_PASSWORD=password
 ADMIN_PASSWORD=password
 enable_plugin zun https://git.openstack.org/openstack/zun
+
+KURYR_ETCD_PORT=2379
 enable_plugin kuryr-libnetwork http://git.openstack.org/openstack/kuryr-libnetwork
 
 # Optional:  uncomment to enable the Zun UI plugin in Horizon
diff --git a/zun/tests/contrib/gate_hook.sh b/zun/tests/contrib/gate_hook.sh
index dc9ea55ec..f9d6e9089 100755
--- a/zun/tests/contrib/gate_hook.sh
+++ b/zun/tests/contrib/gate_hook.sh
@@ -33,6 +33,7 @@ fi
 
 if [ "$db" = "etcd" ]; then
     export DEVSTACK_LOCAL_CONFIG+=$'\n'"ZUN_DB_TYPE=etcd"
+    export DEVSTACK_LOCAL_CONFIG+=$'\n'"KURYR_ETCD_PORT=2379"
 elif [ "$db" = "sql" ]; then
     export DEVSTACK_LOCAL_CONFIG+=$'\n'"ZUN_DB_TYPE=sql"
 fi