VOL-628: Move logic to run VOLTHA stack to a bash script.

The bash script can be run without needing the code to be checked out.

Change-Id: Id22d10f272982357d5e27226f35d10c5f81d1657
diff --git a/Makefile b/Makefile
index 5d56ebd..6e61121 100644
--- a/Makefile
+++ b/Makefile
@@ -334,16 +334,13 @@
 	docker build $(DOCKER_BUILD_ARGS) -t ${REGISTRY}${REPOSITORY}voltha-test_runner:${TAG} -f docker/Dockerfile.test_runner .
 endif
 
+@MAKE_ENV := $(shell echo '$(.VARIABLES)' | awk -v RS=' ' '/^[a-zA-Z0-9]+$$/')
+@SHELL_EXPORT := $(foreach v,$(MAKE_ENV),$(v)='$($(v))')
 start:
-	bash -c 'echo $$VOLTHA_LOGS &&  TMP_STACK_FILE=$$(mktemp -u) && \
-		echo $$TMP_STACK_FILE && \
-		SWARM_MANAGER_COUNT=$$(docker node ls | grep Ready | egrep "(Leader)|(Reachable)" | wc -l | sed -e "s/ //g") && \
-		cat ./compose/voltha-stack.yml.j2 2>&1 | docker run -e RADIUS_ROOT=$$RADIUS_ROOT -e CONSUL_ROOT=$$CONSUL_ROOT -e VOLTHA_LOGS=$$VOLTHA_LOGS -e SWARM_MANAGER_COUNT=$$SWARM_MANAGER_COUNT --rm -i ${REGISTRY}${REPOSITORY}voltha-j2:${TAG} - 2>&1 > $$TMP_STACK_FILE && \
-	        docker stack deploy -c $$TMP_STACK_FILE voltha && \
-	        rm -f $$TMP_STACK_FILE'
-
+	$(SHELL_EXPORT) STACK_TEMPLATE=./compose/voltha-stack.yml.j2 ./scripts/run-voltha.sh start
+	
 stop:
-	docker stack rm voltha
+	./scripts/run-voltha.sh stop
 
 tag: $(patsubst  %,%.tag,$(DOCKER_IMAGE_LIST))
 
diff --git a/scripts/run-voltha.sh b/scripts/run-voltha.sh
new file mode 100755
index 0000000..ea88c3b
--- /dev/null
+++ b/scripts/run-voltha.sh
@@ -0,0 +1,80 @@
+#!/bin/bash
+
+# Runs VOLTHA using a templatized docker stack file.
+# We need to use a templatized file to configure a few things that docker doesn't
+# make configurable through variables right now, namely number of replicas and
+# volume mounts. This runner script first substites some environment variables
+# in to the tempated stack file, then runs that stack file.
+#
+# Look in the stack file template to see what can be configured through environment
+# variables - these variable should be set in the environment that runs this script.
+
+set -e
+
+TAG=${TAG:-latest}
+STACK_TEMPLATE=${STACK_TEMPLATE:-"voltha-stack.yml.j2"}
+
+start () {
+    SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep "(Leader)|(Reachable)" | wc -l | sed -e "s/ //g")
+    if [ $SWARM_MANAGER_COUNT -lt 1 ]; then
+        echo "No swarm managers found. Run 'docker swarm init' to create a new manager"
+        exit 1
+    fi
+
+    downloaded=0
+    if [ ! -f "$STACK_TEMPLATE" ]; then
+        wget https://raw.githubusercontent.com/opencord/voltha/master/compose/voltha-stack.yml.j2
+        downloaded=1
+    fi
+
+    TMP_STACK_FILE=$(mktemp -u)
+
+    cat $STACK_TEMPLATE 2>&1 | docker run -e RADIUS_ROOT=$RADIUS_ROOT -e CONSUL_ROOT=$CONSUL_ROOT -e VOLTHA_LOGS=$VOLTHA_LOGS -e SWARM_MANAGER_COUNT=$SWARM_MANAGER_COUNT --rm -i ${REGISTRY}${REPOSITORY}voltha-j2:${TAG} - 2>&1 > $TMP_STACK_FILE
+    docker stack deploy -c $TMP_STACK_FILE voltha
+
+    rm -f $TMP_STACK_FILE
+
+    if [ $downloaded -eq 1 ]; then
+        rm -f $STACK_TEMPLATE
+    fi
+}
+
+stop () {
+    docker stack rm voltha
+}
+
+status() {
+    if [ -z "$(docker stack ls | grep voltha)" ]; then
+        echo "Stopped"
+        exit
+    fi
+
+    STATUS="Running"
+    for i in $(docker service ls --format '{{.Name}}/{{.Replicas}}' | grep "^voltha_" | grep -v voltha_config_push); do
+        NAME=$(echo $i | cut -d/ -f1)
+        HAVE=$(echo $i | cut -d/ -f2)
+        WANT=$(echo $i | cut -d/ -f3)
+        if [ $HAVE -ne $WANT ]; then
+            echo "$NAME not running: $HAVE of $WANT"
+            STATUS="Incomplete"
+        fi
+    done
+
+    echo $STATUS
+}
+
+case $1 in
+    start)
+        start
+        ;;
+    status)
+        status
+        ;;
+    stop)
+        stop
+        ;;
+    *)
+        echo "Usage: $0 {start|status|stop}" >&2
+        exit 1
+        ;;
+esac
\ No newline at end of file