From 13799455c20074cad967a0b6fbe50b883163b4fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Andr=C3=A9?= <martin.andre@kvhasia.com>
Date: Thu, 15 Jan 2015 10:09:30 +0900
Subject: [PATCH] Make scripts work on latest Atomic images

Remove git and jq dependency in most scripts, allowing to run them on
latest atomic images, or even distribute Kolla in the form of tarball.

The only remainings of git dependency are in the git pre-commit hook,
and in the image build scripts. We can remove the latter one and have
the scripts running in degraded mode if we really want to.

I opted for a python based approach to finding the top-level directory
for portability, ensuring consistent result on Linux and BSD, including
OSX.

Change-Id: I987174032d11b2e9d6a993c563b5dc877c15dd2d
---
 tools/start                      |  3 ++-
 tools/start-all-pods             |  3 ++-
 tools/start-all-replications     |  3 ++-
 tools/start-all-services         |  3 ++-
 tools/stop                       |  3 ++-
 tools/stop-all-pods              | 29 +++++++++++++----------------
 tools/stop-all-replications      |  3 ++-
 tools/stop-all-services          |  3 ++-
 tools/update-build-links         |  4 +++-
 tools/validate-all-json.sh       |  7 +++----
 tools/validate-all-maintainer.sh |  7 +++----
 tools/validate-all-yaml.sh       |  7 +++----
 12 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/tools/start b/tools/start
index bf7bd8735d..d0d3f7bc48 100755
--- a/tools/start
+++ b/tools/start
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 ./tools/start-all-services
 ./tools/start-all-replications
diff --git a/tools/start-all-pods b/tools/start-all-pods
index 72c5807816..8bad8d0ffe 100755
--- a/tools/start-all-pods
+++ b/tools/start-all-pods
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 pods='
 rabbitmq
diff --git a/tools/start-all-replications b/tools/start-all-replications
index a605d4fb75..8adc0430f2 100755
--- a/tools/start-all-replications
+++ b/tools/start-all-replications
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 services='
 nova-compute
diff --git a/tools/start-all-services b/tools/start-all-services
index 3c073465f6..23a8e4c1ca 100755
--- a/tools/start-all-services
+++ b/tools/start-all-services
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 services='
 ceilometer-api
diff --git a/tools/stop b/tools/stop
index 1a9b6fd106..2a914d9175 100755
--- a/tools/stop
+++ b/tools/stop
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 ./tools/stop-all-replications
 ./tools/stop-all-pods
diff --git a/tools/stop-all-pods b/tools/stop-all-pods
index 2a2d7d3abe..f18f822818 100755
--- a/tools/stop-all-pods
+++ b/tools/stop-all-pods
@@ -1,21 +1,18 @@
-#!/bin/sh
+#!/bin/bash
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
-# Delete the replication is not cleaning up its pods
-# These pods need to be deleted according to their UUID
-uuids=$(kubectl get pods -o json | jq '.[][].id' 2>/dev/null | grep -o -E '"[a-fA-F|0-9|\-]*' | cut -c 2- |  grep '\-')
+UUID_REGEX="[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}"
 
-for uuid in $uuids; do
-    if [ $uuid ]; then
-	kubectl delete pod $uuid
+pods=$(kubectl get pods -o template -t '{{range .items}}{{.id}} {{end}}')
+for pod in $pods; do
+    if [[ $pod =~ $UUID_REGEX ]]; then
+	# Stopping a k8s replicationController doesn't delete the associated
+	# pods, which names are UUIDs.
+	# Assuming all pods named by UUID are leftover replication pods.
+	kubectl delete pod $pod
+    else
+	kubectl delete -f "k8s/pod/${pod}-pod.yaml" 2>/dev/null
     fi
 done
-
-pods=$(kubectl get pods -o json| jq '.[][].id' 2>/dev/null)
-# Removes quotes from jquery
-pods=${pods//\"/}
-
-for pod in $pods; do
-    kubectl delete -f "k8s/pod/${pod}-pod.yaml" 2>/dev/null
-done
diff --git a/tools/stop-all-replications b/tools/stop-all-replications
index 6bea193fd7..014ecb3bf0 100755
--- a/tools/stop-all-replications
+++ b/tools/stop-all-replications
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 replication_ctrs=$(kubectl get replicationController | awk 'NR>1 {print $1}')
 
diff --git a/tools/stop-all-services b/tools/stop-all-services
index df5ce86d3e..14e4942b3c 100755
--- a/tools/stop-all-services
+++ b/tools/stop-all-services
@@ -1,6 +1,7 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
 services=$(kubectl get services | awk 'NR>1 {print $1}')
 
diff --git a/tools/update-build-links b/tools/update-build-links
index ac6fa53a89..ef1468485a 100755
--- a/tools/update-build-links
+++ b/tools/update-build-links
@@ -1,6 +1,8 @@
 #!/bin/sh
 
-cd $(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
+
 find docker -name Dockerfile | while read dockerfile; do
 	dir=${dockerfile%/*}
 	link=$(python -c 'import os,sys; print os.path.relpath(sys.argv[1], sys.argv[2])' \
diff --git a/tools/validate-all-json.sh b/tools/validate-all-json.sh
index ce85fadd69..87f1a9918c 100755
--- a/tools/validate-all-json.sh
+++ b/tools/validate-all-json.sh
@@ -1,9 +1,8 @@
 #!/bin/sh
 
-TOPLEVEL=$(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
-cd $TOPLEVEL
-
-git ls-files -z '*.json' |
+find . -name '*.json' -print0 |
     xargs -0 python tools/validate-json.py || exit 1
 
diff --git a/tools/validate-all-maintainer.sh b/tools/validate-all-maintainer.sh
index a488385a1e..7fc57422f4 100755
--- a/tools/validate-all-maintainer.sh
+++ b/tools/validate-all-maintainer.sh
@@ -1,9 +1,8 @@
 #!/bin/sh
 
-TOPLEVEL=$(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
-cd $TOPLEVEL
-
-git ls-files -z '*/Dockerfile' |
+find docker -name Dockerfile -print0 |
     xargs -0 tools/validate-maintainer.sh || exit 1
 
diff --git a/tools/validate-all-yaml.sh b/tools/validate-all-yaml.sh
index 6a16d9a980..b19cfe18af 100755
--- a/tools/validate-all-yaml.sh
+++ b/tools/validate-all-yaml.sh
@@ -1,9 +1,8 @@
 #!/bin/sh
 
-TOPLEVEL=$(git rev-parse --show-toplevel)
+REAL_PATH=$(python -c "import os,sys;print os.path.realpath('$0')")
+cd "$(dirname "$REAL_PATH")/.."
 
-cd $TOPLEVEL
-
-git ls-files -z '*.yaml' |
+find . -name '*.yaml' -print0 |
     xargs -0 python tools/validate-yaml.py || exit 1