blob: 2dce5ad4394ebfcd57174d3ff1263e03a822e69e [file] [log] [blame]
Sergio Slobodriancab0a392017-07-13 08:42:10 -04001#!/bin/bash
2
David K. Bainbridge12463052018-01-19 09:26:09 -08003# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Sergio Slobodriancab0a392017-07-13 08:42:10 -040016
David K. Bainbridge12463052018-01-19 09:26:09 -080017PROG=$(basename $0)
18BASE_DIR=$(pwd)
19
20GREEN='\033[32;1m'
21RED='\033[0;31m'
22YELLOW='\033[0;33m'
23WHITE='\033[1;37m'
24NC='\033[0m' # No Color
25
26usage() {
27 echo >&2 "$PROG: [-d <dir>] [-l <log-dir>] [-h]"
28 echo >&2 " -d <dir> directory in which the 'compose file directory' is located, defaults to '$(pwd)'"
29 echo >&2 " -l <log-dir> directory into which fluentd logs will be written"
30 echo >&2 " -c <consul-dir> directory into which consul data is written"
31 echo >&2 " -e ensure voltha_net is encrypted"
32 echo >&2 " -h this message"
33}
34
35wait_for_service() {
36 while true
37 do
38 COUNT=$(docker service ls | grep $1 | awk '{print $4}')
39 if [ ! -z "$COUNT" ]; then
40 HAVE=$(echo $COUNT | cut -d/ -f1)
41 WANT=$(echo $COUNT | cut -d/ -f2)
42 if [ $WANT == $HAVE ]; then
43 break
44 fi
45 fi
46 sleep 2
47 done
48}
49
50ENCRYPT_VNET=""
51
52OPTIND=1
53while getopts d:l:c:eh OPT; do
54 case "$OPT" in
55 d) BASE_DIR="$OPTARG";;
56 l) export VOLTHA_LOGS="$OPTARG";;
57 c) export CONSUL_ROOT="$OPTARG";;
58 e) ENCRYPT_VNET="--opt encrypted=true";;
59 h) usage;
60 exit 1;;
61 esac
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -040062done
Sergio Slobodrian6e270c12017-08-09 23:06:49 -040063
David K. Bainbridge12463052018-01-19 09:26:09 -080064# If `REGISTRY` is set, but doesn't end in a `/`, then
65# add one
66test -z "$REGISTRY" -o "$(echo ${REGISTRY: -1})" == "/" || REGISTRY="$REGISTRY/"
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -080067test -z "$REPOSITORY" -o "$(echo ${REPOSITORY: -1})" == "/" || REGISTRY="$REPOSITORY/"
68TAG=${TAG:-latest}
David K. Bainbridge12463052018-01-19 09:26:09 -080069
70# Attempt to count Ready Docker Swarm managers
71export SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l | sed -e 's/ //g')
72hostName=$(hostname)
73
74echo -n "[network] voltha-net ... "
75if [ $(docker network ls | grep voltha_net | wc -l) -eq 0 ]; then
76 OUT=$(docker network create --driver overlay \
77 --subnet="172.29.19.0/24" \
78 $ENCRYPT_VNET voltha_net 2>&1)
79 if [ $? -ne 0 ]; then
80 echo -e "${RED}ERROR: $OUT${NC}"
81 else
82 echo -e "${GREEN}created${NC}"
83 fi
84else
85 # Verify that the current encrypted state is the desired encrypted state
86 # and if not, tear down and recreate
87 CURRENT=$(docker network inspect --format '{{.Options.encrypted}}' voltha_net | grep -v "<no value>")
88 if [ "$ENCRYPT_VNET X" != " X" -a "$CURRENT" != "true" -o "$ENCRYPT_VNET X" == " X" -a "$CURRENT" == "true" ]; then
89 echo -en "${YELLOW}delete${NC} ... "
90 docker network rm voltha_net > /dev/null || exit 1
91 OUT=$(docker network create --driver overlay \
92 --subnet="172.29.19.0/24" \
93 $ENCRYPT_VNET voltha_net 2>&1)
94 if [ $? -ne 0 ]; then
95 echo -e "${RED}ERROR: $OUT${NC}"
96 else
97 echo -e "${GREEN}created${NC}"
98 fi
99 else
100 echo -e "${WHITE}already exists${NC}"
101 fi
102fi
103
104echo -n "[network] kafka_net ... "
105if [ $(docker network ls | grep kafka_net | wc -l) -eq 0 ]; then
106 OUT=$(docker network create --driver overlay --opt encrypted kafka_net 2>&1)
107 if [ $? -ne 0 ]; then
108 echo -e "${RED}ERROR: $OUT${NC}"
109 else
110 echo -e "${GREEN}created${NC}"
111 fi
112else
113 echo -e "${WHITE}already exists${NC}"
114fi
115
116docker stack deploy -c $BASE_DIR/compose/docker-compose-kafka-cluster.yml kafka
117docker stack deploy -c $BASE_DIR/compose/docker-compose-consul-cluster.yml consul
118echo -n "Waiting for consul to start ... "
119wait_for_service consul_consul
120echo -e "${GREEN}done${NC}"
121
122echo -n "Waiting for consul leader election ... "
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400123patience=10
124while true
125do
David K. Bainbridge12463052018-01-19 09:26:09 -0800126 leader=`curl -v http://${hostName}:8500/v1/status/leader 2>/dev/null | sed -e 's/"//g'`
127 if [ ! -z "$leader" ] ; then
128 echo -e "${GREEN}Leader elected is on ${leader}${NC}"
129 break
130 fi
131 sleep 10
132 patience=`expr $patience - 1`
133 if [ $patience -eq 0 ]; then
134 echo -e "${RED}Consul leader election taking too long... aborting${NC}"
135 echo "Stopping VOLTHA ... "
136 ./voltha-swarm-stop.sh
137 exit 1
138 fi
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400139done
140
David K. Bainbridge12463052018-01-19 09:26:09 -0800141docker stack deploy -c $BASE_DIR/compose/docker-compose-fluentd-agg-cluster.yml fluentd
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400142
David K. Bainbridge12463052018-01-19 09:26:09 -0800143echo -n "Waiting for fluentd aggregation services to start ... "
144wait_for_service fluentd_fluentdstby
145wait_for_service fluentd_fluentdactv
146echo -e "${GREEN}done${NC}"
Sergio Slobodrian36cd85f2017-08-24 11:01:11 -0400147sleep 2
Sergio Slobodriancab0a392017-07-13 08:42:10 -0400148
David K. Bainbridge12463052018-01-19 09:26:09 -0800149
150TMP_STACK_FILE=$(mktemp -u)
David K. Bainbridge3d5b22d2018-01-22 12:57:52 -0800151cat $BASE_DIR/compose/docker-compose-all.yml.j2 2>&1 | docker run -e SWARM_MANAGER_COUNT=$SWARM_MANAGER_COUNT --rm -i ${REGISTRY}${REPOSITORY}voltha-j2:${TAG} - 2>&1 > $TMP_STACK_FILE
David K. Bainbridge12463052018-01-19 09:26:09 -0800152docker stack deploy -c $TMP_STACK_FILE voltha
153rm -f $TMP_STACK_FILE