Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
David K. Bainbridge | 1246305 | 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 | 1246305 | 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 | 1246305 | 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/" |
David K. Bainbridge | 3d5b22d | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 67 | test -z "$REPOSITORY" -o "$(echo ${REPOSITORY: -1})" == "/" || REGISTRY="$REPOSITORY/" |
| 68 | TAG=${TAG:-latest} |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 69 | |
| 70 | # Attempt to count Ready Docker Swarm managers |
| 71 | export SWARM_MANAGER_COUNT=$(docker node ls | grep Ready | egrep '(Leader)|(Reachable)' | wc -l | sed -e 's/ //g') |
| 72 | hostName=$(hostname) |
| 73 | |
| 74 | echo -n "[network] voltha-net ... " |
| 75 | if [ $(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 |
| 84 | else |
| 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 |
| 102 | fi |
| 103 | |
| 104 | echo -n "[network] kafka_net ... " |
| 105 | if [ $(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 |
| 112 | else |
| 113 | echo -e "${WHITE}already exists${NC}" |
| 114 | fi |
| 115 | |
| 116 | docker stack deploy -c $BASE_DIR/compose/docker-compose-kafka-cluster.yml kafka |
| 117 | docker stack deploy -c $BASE_DIR/compose/docker-compose-consul-cluster.yml consul |
| 118 | echo -n "Waiting for consul to start ... " |
| 119 | wait_for_service consul_consul |
| 120 | echo -e "${GREEN}done${NC}" |
| 121 | |
| 122 | echo -n "Waiting for consul leader election ... " |
Sergio Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 123 | patience=10 |
| 124 | while true |
| 125 | do |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 126 | 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 Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 139 | done |
| 140 | |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 141 | 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] | 142 | |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 143 | echo -n "Waiting for fluentd aggregation services to start ... " |
| 144 | wait_for_service fluentd_fluentdstby |
| 145 | wait_for_service fluentd_fluentdactv |
| 146 | echo -e "${GREEN}done${NC}" |
Sergio Slobodrian | 36cd85f | 2017-08-24 11:01:11 -0400 | [diff] [blame] | 147 | sleep 2 |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 148 | |
David K. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 149 | |
| 150 | TMP_STACK_FILE=$(mktemp -u) |
David K. Bainbridge | 3d5b22d | 2018-01-22 12:57:52 -0800 | [diff] [blame] | 151 | cat $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. Bainbridge | 1246305 | 2018-01-19 09:26:09 -0800 | [diff] [blame] | 152 | docker stack deploy -c $TMP_STACK_FILE voltha |
| 153 | rm -f $TMP_STACK_FILE |