blob: cbd69e3bc44423319d3723a1154521c994510197 [file] [log] [blame]
Sergio Slobodriancab0a392017-07-13 08:42:10 -04001#!/bin/bash
2
David K. Bainbridgebba65ff2018-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. Bainbridgebba65ff2018-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. Bainbridgebba65ff2018-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/"
67
68# Attempt to count Ready Docker Swarm managers
69export SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l | sed -e 's/ //g')
70hostName=$(hostname)
71
72echo -n "[network] voltha-net ... "
73if [ $(docker network ls | grep voltha_net | wc -l) -eq 0 ]; then
74 OUT=$(docker network create --driver overlay \
75 --subnet="172.29.19.0/24" \
76 $ENCRYPT_VNET voltha_net 2>&1)
77 if [ $? -ne 0 ]; then
78 echo -e "${RED}ERROR: $OUT${NC}"
79 else
80 echo -e "${GREEN}created${NC}"
81 fi
82else
83 # Verify that the current encrypted state is the desired encrypted state
84 # and if not, tear down and recreate
85 CURRENT=$(docker network inspect --format '{{.Options.encrypted}}' voltha_net | grep -v "<no value>")
86 if [ "$ENCRYPT_VNET X" != " X" -a "$CURRENT" != "true" -o "$ENCRYPT_VNET X" == " X" -a "$CURRENT" == "true" ]; then
87 echo -en "${YELLOW}delete${NC} ... "
88 docker network rm voltha_net > /dev/null || exit 1
89 OUT=$(docker network create --driver overlay \
90 --subnet="172.29.19.0/24" \
91 $ENCRYPT_VNET voltha_net 2>&1)
92 if [ $? -ne 0 ]; then
93 echo -e "${RED}ERROR: $OUT${NC}"
94 else
95 echo -e "${GREEN}created${NC}"
96 fi
97 else
98 echo -e "${WHITE}already exists${NC}"
99 fi
100fi
101
102echo -n "[network] kafka_net ... "
103if [ $(docker network ls | grep kafka_net | wc -l) -eq 0 ]; then
104 OUT=$(docker network create --driver overlay --opt encrypted kafka_net 2>&1)
105 if [ $? -ne 0 ]; then
106 echo -e "${RED}ERROR: $OUT${NC}"
107 else
108 echo -e "${GREEN}created${NC}"
109 fi
110else
111 echo -e "${WHITE}already exists${NC}"
112fi
113
114docker stack deploy -c $BASE_DIR/compose/docker-compose-kafka-cluster.yml kafka
115docker stack deploy -c $BASE_DIR/compose/docker-compose-consul-cluster.yml consul
116echo -n "Waiting for consul to start ... "
117wait_for_service consul_consul
118echo -e "${GREEN}done${NC}"
119
120echo -n "Waiting for consul leader election ... "
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400121patience=10
122while true
123do
David K. Bainbridgebba65ff2018-01-19 09:26:09 -0800124 leader=`curl -v http://${hostName}:8500/v1/status/leader 2>/dev/null | sed -e 's/"//g'`
125 if [ ! -z "$leader" ] ; then
126 echo -e "${GREEN}Leader elected is on ${leader}${NC}"
127 break
128 fi
129 sleep 10
130 patience=`expr $patience - 1`
131 if [ $patience -eq 0 ]; then
132 echo -e "${RED}Consul leader election taking too long... aborting${NC}"
133 echo "Stopping VOLTHA ... "
134 ./voltha-swarm-stop.sh
135 exit 1
136 fi
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400137done
138
David K. Bainbridgebba65ff2018-01-19 09:26:09 -0800139docker stack deploy -c $BASE_DIR/compose/docker-compose-fluentd-agg-cluster.yml fluentd
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400140
David K. Bainbridgebba65ff2018-01-19 09:26:09 -0800141echo -n "Waiting for fluentd aggregation services to start ... "
142wait_for_service fluentd_fluentdstby
143wait_for_service fluentd_fluentdactv
144echo -e "${GREEN}done${NC}"
Sergio Slobodrian36cd85f2017-08-24 11:01:11 -0400145sleep 2
Sergio Slobodriancab0a392017-07-13 08:42:10 -0400146
David K. Bainbridgebba65ff2018-01-19 09:26:09 -0800147
148TMP_STACK_FILE=$(mktemp -u)
149cat $BASE_DIR/compose/docker-compose-all.yml.j2 2>&1 | docker run -e SWARM_MANAGER_COUNT=$SWARM_MANAGER_COUNT --rm -i voltha/j2 - 2>&1 > $TMP_STACK_FILE
150docker stack deploy -c $TMP_STACK_FILE voltha
151rm -f $TMP_STACK_FILE