Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 3 | # 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 Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 16 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 17 | PROG=$(basename $0) |
| 18 | BASE_DIR=$(pwd) |
| 19 | |
| 20 | GREEN='\033[32;1m' |
| 21 | RED='\033[0;31m' |
| 22 | YELLOW='\033[0;33m' |
| 23 | WHITE='\033[1;37m' |
| 24 | NC='\033[0m' # No Color |
| 25 | |
| 26 | usage() { |
| 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 | |
| 35 | wait_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 | |
| 50 | ENCRYPT_VNET="" |
| 51 | |
| 52 | OPTIND=1 |
| 53 | while 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 Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 62 | done |
Sergio Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 63 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 64 | # If `REGISTRY` is set, but doesn't end in a `/`, then |
| 65 | # add one |
| 66 | test -z "$REGISTRY" -o "$(echo ${REGISTRY: -1})" == "/" || REGISTRY="$REGISTRY/" |
| 67 | |
| 68 | # Attempt to count Ready Docker Swarm managers |
| 69 | export SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l | sed -e 's/ //g') |
| 70 | hostName=$(hostname) |
| 71 | |
| 72 | echo -n "[network] voltha-net ... " |
| 73 | if [ $(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 |
| 82 | else |
| 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 |
| 100 | fi |
| 101 | |
| 102 | echo -n "[network] kafka_net ... " |
| 103 | if [ $(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 |
| 110 | else |
| 111 | echo -e "${WHITE}already exists${NC}" |
| 112 | fi |
| 113 | |
| 114 | docker stack deploy -c $BASE_DIR/compose/docker-compose-kafka-cluster.yml kafka |
| 115 | docker stack deploy -c $BASE_DIR/compose/docker-compose-consul-cluster.yml consul |
| 116 | echo -n "Waiting for consul to start ... " |
| 117 | wait_for_service consul_consul |
| 118 | echo -e "${GREEN}done${NC}" |
| 119 | |
| 120 | echo -n "Waiting for consul leader election ... " |
Sergio Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 121 | patience=10 |
| 122 | while true |
| 123 | do |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 124 | 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 Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 137 | done |
| 138 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 139 | docker stack deploy -c $BASE_DIR/compose/docker-compose-fluentd-agg-cluster.yml fluentd |
Sergio Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 140 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 141 | echo -n "Waiting for fluentd aggregation services to start ... " |
| 142 | wait_for_service fluentd_fluentdstby |
| 143 | wait_for_service fluentd_fluentdactv |
| 144 | echo -e "${GREEN}done${NC}" |
Sergio Slobodrian | 36cd85f | 2017-08-24 11:01:11 -0400 | [diff] [blame] | 145 | sleep 2 |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 146 | |
David K. Bainbridge | bba65ff | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 147 | |
| 148 | TMP_STACK_FILE=$(mktemp -u) |
| 149 | cat $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 |
| 150 | docker stack deploy -c $TMP_STACK_FILE voltha |
| 151 | rm -f $TMP_STACK_FILE |