David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright 2019 Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # trap ctrl-c and call ctrl_c() |
| 17 | trap ctrl_c INT |
| 18 | |
| 19 | function ctrl_c() { |
| 20 | tput cnorm |
| 21 | echo "" |
| 22 | echo "ctrl-c trapped" |
| 23 | echo "Thank you for trying 'votlha up'" |
| 24 | exit |
| 25 | } |
| 26 | |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 27 | TYPE=${TYPE:-minimal} |
| 28 | # Only minimal cluster works in this script currently |
| 29 | TYPE=minimal |
| 30 | |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 31 | if [ $# -ne 1 -o $(echo ":up:down:" | grep -c ":$1:") -ne 1 ]; then |
| 32 | >&2 echo "up or down?" |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | if [ "$1" == "down" ]; then |
| 37 | exec kind delete cluster --name voltha-$TYPE |
| 38 | exit |
| 39 | fi |
| 40 | |
| 41 | LOG="install.log" |
| 42 | date > $LOG |
| 43 | |
| 44 | spin() { |
| 45 | START=14852225 |
| 46 | END=14852287 |
| 47 | IDX=$START |
| 48 | tput civis |
| 49 | while true; do |
| 50 | printf '%02x' $IDX | xxd -r -p |
| 51 | echo -en " $IDX" |
| 52 | IDX=$(expr $IDX + 1) |
| 53 | if [ $IDX -gt $END ]; then |
| 54 | IDX=$START |
| 55 | fi |
| 56 | sleep .15 |
| 57 | echo -en "\r" |
| 58 | done |
| 59 | } |
| 60 | |
| 61 | IDX=14852225 |
| 62 | NOT_VERIFIED="\xe2\x9c\x97" |
| 63 | VERIFIED="\xe2\x9c\x93" |
| 64 | bspin() { |
| 65 | tput civis |
| 66 | IDX=14852225 |
| 67 | local INDENT= |
| 68 | if [ "$1" == "-" ]; then |
| 69 | INDENT=" " |
| 70 | shift |
| 71 | fi |
| 72 | echo -en "$INDENT $*" |
| 73 | } |
| 74 | |
| 75 | sspin() { |
| 76 | local INDENT= |
| 77 | if [ "$1" == "-" ]; then |
| 78 | INDENT=" " |
| 79 | shift |
| 80 | fi |
| 81 | echo -en "\r$INDENT" |
| 82 | printf " %02x" $IDX | xxd -r -p |
| 83 | echo -en " $*" |
| 84 | IDX=$(expr $IDX + 1) |
| 85 | if [ $IDX -gt 14852287 ]; then |
| 86 | IDX=14852225 |
| 87 | fi |
| 88 | } |
| 89 | |
| 90 | espin() { |
| 91 | local INDENT= |
| 92 | if [ "$1" == "-" ]; then |
| 93 | INDENT=" " |
| 94 | shift |
| 95 | fi |
| 96 | echo -e "\r$INDENT$*" |
| 97 | tput cnorm |
| 98 | } |
| 99 | |
| 100 | count_pods() { |
| 101 | local NAMESPACE=$1; shift |
| 102 | local PODS=$(kubectl -n $NAMESPACE get pod -o go-template="{{range .items}}{{.metadata.name}}/{{.status.phase}}/_{{range .status.containerStatuses}}{{.ready}}_{{end}} {{end}}") |
| 103 | local COUNT=0 |
| 104 | local PATTERNS=$* |
| 105 | for POD in $PODS; do |
| 106 | local NAME=$(echo $POD | cut -d/ -f 1) |
| 107 | local STATE=$(echo $POD | cut -d/ -f 2) |
| 108 | local CONTAINERS=$(echo $POD | cut -d/ -f 3 | sed -e 's/_/ /g') |
| 109 | if [ "$STATE" == "Running" ]; then |
| 110 | local TOTAL=$(echo $CONTAINERS | wc -w) |
| 111 | local FOUND=$(echo $CONTAINERS | grep -o true | wc -l) |
| 112 | if [ $TOTAL -eq $FOUND ]; then |
| 113 | for PATTERN in $PATTERNS; do |
| 114 | if [[ $NAME =~ $PATTERN ]]; then |
| 115 | COUNT=$(expr $COUNT + 1) |
| 116 | fi |
| 117 | done |
| 118 | fi |
| 119 | fi |
| 120 | done |
| 121 | echo $COUNT |
| 122 | } |
| 123 | |
| 124 | wait_for_pods() { |
| 125 | local INDENT= |
| 126 | if [ "$1" == "-" ]; then |
| 127 | INDENT=$1; shift |
| 128 | fi |
| 129 | local NAMESPACE=$1; shift |
| 130 | local EXPECT=$1; shift |
| 131 | local RETRY=$1; shift |
| 132 | local MESSAGE=$1; shift |
| 133 | local PATTERNS=$* |
| 134 | local HAVE=$(count_pods $NAMESPACE $PATTERNS) |
| 135 | COUNT=$(expr 300 / 15) |
| 136 | bspin $INDENT $MESSAGE |
| 137 | sspin $INDENT |
| 138 | if [ $HAVE -ne $EXPECT ]; then |
| 139 | while [ $HAVE -ne $EXPECT ]; do |
| 140 | sspin $INDENT |
| 141 | COUNT=$(expr $COUNT - 1) |
| 142 | if [ $COUNT -eq 0 ]; then |
| 143 | HAVE=$(count_pods $NAMESPACE $PATTERNS) |
| 144 | COUNT=$(expr 300 / 15) |
| 145 | fi |
| 146 | sleep .15 |
| 147 | done |
| 148 | fi |
| 149 | espin $INDENT $VERIFIED |
| 150 | if [ $HAVE -ne $EXPECT ]; then |
| 151 | return 1 |
| 152 | fi |
| 153 | return 0 |
| 154 | } |
| 155 | |
| 156 | helm_install() { |
| 157 | local INDENT= |
| 158 | if [ "$1" == "-" ]; then |
| 159 | INDENT=$1; shift |
| 160 | fi |
| 161 | local NAMESPACE=$1; shift |
| 162 | local NAME=$1; shift |
| 163 | local CHART=$1; shift |
| 164 | local MESSAGE=$* |
| 165 | |
| 166 | COUNT=$(expr 300 / 15) |
| 167 | bspin $INDENT $MESSAGE |
| 168 | (set -x; helm install -f $TYPE-values.yaml --namespace $NAMESPACE --name $NAME $CHART >>$LOG 2>&1) >>$LOG 2>&1 |
| 169 | SUCCESS=$? |
| 170 | while [ $SUCCESS -ne 0 ]; do |
| 171 | sspin $INDENT |
| 172 | COUNT=$(expr $COUNT - 1) |
| 173 | if [ $COUNT -eq 0 ]; then |
| 174 | (set -x; helm install -f $TYPE-values.yaml --namespace $NAMESPACE --name $NAME $CHART >>$LOG 2>&1) >>$LOG 2>&1 |
| 175 | COUNT=$(expr 300 / 15) |
| 176 | fi |
| 177 | sleep .15 |
| 178 | done |
| 179 | espin $INDENT $VERIFIED |
| 180 | } |
| 181 | |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 182 | echo "INSTALL TYPE: $TYPE" >> $LOG |
| 183 | |
| 184 | bspin "Verify GOPATH" |
| 185 | export GOPATH=$(pwd) |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 186 | mkdir -p $GOPATH/bin |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 187 | espin $VERIFIED |
| 188 | |
| 189 | bspin "Verify Kubernetes/Kind" |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 190 | if [ -x $GOPATH/bin/kind ]; then |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 191 | espin $VERIFIED |
| 192 | else |
| 193 | espin $NOT_VERIFIED |
| 194 | bspin - "Download and build Kubernetes/Kind" |
| 195 | (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1 |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 196 | (set -x; curl -o $GOPATH/bin/kind -sSL https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-$(go env GOHOSTOS)-$(go env GOARCH) >>$LOG 2>&1) >>$LOG 2>&1 |
Test User | 7c2be41 | 2019-07-03 06:20:30 +0000 | [diff] [blame^] | 197 | (set -x; chmod 755 $GOPATH/bin/kind >>$LOG 2>&1) >>$LOG 2>&1 |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 198 | espin - $VERIFIED |
| 199 | fi |
| 200 | |
| 201 | bspin "Verify Helm" |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 202 | if [ -x $GOPATH/bin/helm ]; then |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 203 | espin $VERIFIED |
| 204 | else |
| 205 | espin $NOT_VERIFIED |
| 206 | bspin - "Download and install Helm" |
| 207 | (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1 |
| 208 | (set -x; curl -L https://git.io/get_helm.sh | HELM_INSTALL_DIR=$(go env GOPATH)/bin bash >>$LOG 2>&1) >>$LOG 2>&1 |
| 209 | espin - $VERIFIED |
| 210 | fi |
| 211 | |
| 212 | bspin "Verify voltctl" |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 213 | if [ -x $GOPATH/bin/voltctl ]; then |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 214 | espin $VERIFIED |
| 215 | else |
| 216 | espin $NOT_VERIFIED |
| 217 | bspin - "Download and build voltctl" |
| 218 | (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1 |
| 219 | (set -x; go get github.com/ciena/voltctl/cmd/voltctl >>$LOG 2>&1) >>$LOG 2>&1 |
| 220 | espin - $VERIFIED |
| 221 | fi |
| 222 | |
| 223 | bspin "Verify command PATH" |
| 224 | export PATH=$(go env GOPATH)/bin:$PATH |
| 225 | espin $VERIFIED |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 226 | |
| 227 | HAVE=$(kind get clusters | grep -c voltha-$TYPE) |
| 228 | bspin "Verify Kubernetes/Kind Cluster" |
| 229 | sspin |
| 230 | if [ $HAVE -eq 0 ]; then |
| 231 | espin $NOT_VERIFIED |
| 232 | kind create cluster --name voltha-$TYPE --config $TYPE-cluster.cfg |
| 233 | else |
| 234 | espin $VERIFIED |
| 235 | fi |
| 236 | |
| 237 | export KUBECONFIG="$(kind get kubeconfig-path --name="voltha-$TYPE")" |
| 238 | P="coredns-.* \ |
| 239 | etcd-voltha-$TYPE-control-plane \ |
| 240 | kindnet-.* \ |
| 241 | kube-apiserver-voltha-$TYPE-control-plane \ |
| 242 | kube-controller-manager-voltha-$TYPE-control-plane \ |
| 243 | kube-proxy-.* \ |
| 244 | kube-scheduler-voltha-$TYPE-control-plane" |
| 245 | |
| 246 | wait_for_pods - "kube-system" 12 -1 "Waiting for system PODs to start..." $P |
| 247 | |
| 248 | COUNT=$(count_pods "kube-system" "tiller-deploy-.*") |
| 249 | bspin "Verify Helm" |
| 250 | if [ $(count_pods "kube-system" "tiller-deploy-.*") -ne 1 ]; then |
| 251 | espin $NOT_VERIFIED |
| 252 | echo Configuring" Helm ..." |
| 253 | bspin - "Initialize Helm" |
| 254 | (set -x; helm init --upgrade >>$LOG 2>&1) >>$LOG 2>&1 |
| 255 | espin - $VERIFIED |
| 256 | bspin - "Add Google Incubator repository to Helm ..." |
| 257 | (set -x; helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com >>$LOG 2>&1) >>$LOG 2>&1 |
| 258 | espin - $VERIFIED |
| 259 | |
| 260 | bspin - "Add Google Stable repository to Helm ..." |
| 261 | (set -x; helm repo add stable https://kubernetes-charts.storage.googleapis.com >>$LOG 2>&1) >>$LOG 2>&1 |
| 262 | espin - $VERIFIED |
| 263 | bspin - "Add ONF repository to Helm ..." |
| 264 | (set -x; helm repo add onf https://charts.opencord.org >>$LOG 2>&1) >>$LOG 2>&1 |
| 265 | espin - $VERIFIED |
| 266 | bspin - "Update Helm repository cache ..." |
| 267 | (set -x; helm repo update >>$LOG 2>&1) >>$LOG 2>&1 |
| 268 | espin - $VERIFIED |
| 269 | |
| 270 | # Create and k8s service account so that Helm can create pods |
| 271 | bspin - "Create Tiller ServiceAccount ..." |
| 272 | (set -x; kubectl create serviceaccount --namespace kube-system tiller >>$LOG 2>&1) >>$LOG 2>&1 |
| 273 | espin - $VERIFIED |
| 274 | bspin - "Create Tiller ClusterRoleBinding ..." |
| 275 | (set -x; kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller >>$LOG 2>&1) >>$LOG 2>&1 |
| 276 | espin - $VERIFIED |
| 277 | bspin - "Update Tiller Manifest ..." |
| 278 | (set -x; kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}' >>$LOG 2>&1) >>$LOG 2>&1 |
| 279 | espin - $VERIFIED |
| 280 | else |
| 281 | espin $VERIFIED |
| 282 | fi |
| 283 | wait_for_pods - "kube-system" 1 -1 "Waiting for Tiller POD to start..." "tiller-deploy-.*" |
| 284 | |
| 285 | bspin "Verify ETCD Operator" |
| 286 | if [ $(helm list etcd-operator | grep -c DEPLOYED) -ne 1 ]; then |
| 287 | espin $NOT_VERIFIED |
| 288 | helm_install - voltha etcd-operator stable/etcd-operator "Install ETCD Operator" |
| 289 | else |
| 290 | espin $VERIFIED |
| 291 | fi |
| 292 | wait_for_pods - "voltha" 1 -1 "Waiting for ETCD Operator to start..." "etcd-operator-.*" |
| 293 | |
| 294 | bspin "Verify ONOS installed" |
| 295 | if [ $(helm list onos | grep -c DEPLOYED) -ne 1 ]; then |
| 296 | espin $NOT_VERIFIED |
| 297 | helm_install - default onos onf/onos "Install ONOS" |
| 298 | else |
| 299 | espin $VERIFIED |
| 300 | fi |
| 301 | wait_for_pods - "default" 1 -1 "Waiting for ONOS to start..." "onos-.*" |
| 302 | |
| 303 | bspin - "Forward ONOS API port" |
| 304 | (set -x; screen -p 0 -X -S onos-ui stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 305 | (set -x; screen -dmS onos-ui kubectl port-forward service/onos-ui 8181:8181 >>$LOG 2>&1) >>$LOG 2>&1 |
| 306 | espin - $VERIFIED |
| 307 | bspin - "Forward ONOS SSH port" |
| 308 | (set -x; screen -p 0 -X -S onos-ssh stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 309 | (set -x; screen -dmS onos-ssh kubectl port-forward service/onos-ssh 8101:8101 >>$LOG 2>&1) >>$LOG 2>&1 |
| 310 | espin - $VERIFIED |
| 311 | bspin - "Install required ONOS applications" |
| 312 | (set -x; ./onos-files/install-onos-applications.sh >>$LOG 2>&1) >>$LOG 2>&1 |
| 313 | espin - $VERIFIED |
| 314 | |
| 315 | bspin "Verify VOLTHA installed" |
| 316 | if [ $(helm list voltha | grep -c DEPLOYED) -ne 1 ]; then |
| 317 | espin $NOT_VERIFIED |
| 318 | helm_install - voltha voltha onf/voltha "Install VOLTHA" |
| 319 | else |
| 320 | espin $VERIFIED |
| 321 | fi |
| 322 | VOLTHA="ofagent-.* \ |
| 323 | ro-core.* \ |
| 324 | rw-core.* \ |
| 325 | voltha-api-server-.* \ |
| 326 | voltha-cli-server-.* \ |
| 327 | voltha-etcd-cluster-.* \ |
| 328 | voltha-kafka-.* \ |
| 329 | voltha-zookeeper-.*" |
| 330 | wait_for_pods - "voltha" 9 -1 "Waiting for VOLTHA CORE top start..." $VOLTHA |
| 331 | |
| 332 | echo "Verify Adapters" |
| 333 | bspin - "Verify Simulated Adapters installed" |
| 334 | if [ $(helm list sim | grep -c DEPLOYED) -ne 1 ]; then |
| 335 | espin - $NOT_VERIFIED |
| 336 | helm_install - voltha sim onf/voltha-adapter-simulated "Install Simulated Adapters" |
| 337 | else |
| 338 | espin - $VERIFIED |
| 339 | fi |
| 340 | |
| 341 | bspin - "Verify OpenOLT Adapter installed" |
| 342 | if [ $(helm list open-olt | grep -c DEPLOYED) -ne 1 ]; then |
| 343 | espin - $NOT_VERIFIED |
| 344 | helm_install - voltha open-olt onf/voltha-adapter-openolt "Install OpenOLT Adapter" |
| 345 | else |
| 346 | espin - $VERIFIED |
| 347 | fi |
| 348 | bspin - "Verify OpenONU Adapter installed" |
| 349 | if [ $(helm list open-onu | grep -c DEPLOYED) -ne 1 ]; then |
| 350 | espin - $NOT_VERIFIED |
| 351 | helm_install - voltha open-onu onf/voltha-adapter-openonu "Install OpenONU Adapter" |
| 352 | else |
| 353 | espin - $VERIFIED |
| 354 | fi |
| 355 | |
| 356 | ADAPTERS="adapter-.*" |
| 357 | wait_for_pods - "voltha" 4 -1 "Waiting for adapters to start..." $ADAPTERS |
| 358 | |
| 359 | echo "Restart VOLTHA API" |
| 360 | API="voltha-api-server-.* ofagent-.*" |
| 361 | (set -x; kubectl scale --replicas=0 deployment -n voltha voltha-api-server ofagent >>$LOG 2>&1) >>$LOG 2>&1 |
| 362 | wait_for_pods - "voltha" 0 -1 "Wait for API to stop" $API |
| 363 | (set -x; kubectl scale --replicas=1 deployment -n voltha voltha-api-server ofagent >>$LOG 2>&1) >>$LOG 2>&1 |
| 364 | wait_for_pods - "voltha" 2 -1 "Wait for API to re-start ..." $API |
| 365 | |
| 366 | bspin - "Forward VOLTHA API port" |
| 367 | (set -x; screen -p 0 -X -S voltha-api stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 368 | (set -x; screen -dmS voltha-api kubectl port-forward -n voltha service/voltha-api 55555:55555 >>$LOG 2>&1) >>$LOG 2>&1 |
| 369 | espin - $VERIFIED |
| 370 | bspin - "Forward VOLTHA SSH port" |
| 371 | (set -x; screen -p 0 -X -S voltha-ssh stuff $'\003' 2>/dev/null >/dev/null >>$LOG 2>&1) >>$LOG 2>&1 |
| 372 | (set -x; screen -dmS voltha-ssh kubectl port-forward -n voltha service/voltha-cli 5022:5022 >>$LOG 2>&1) >>$LOG 2>&1 |
| 373 | espin - $VERIFIED |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame] | 374 | |
| 375 | bspin - "Create voltctl configuration file" |
| 376 | (set -x; mkdir -p $HOME/.volt >>$LOG 2>&1) >>$LOG 2>&1 |
| 377 | (set -x; voltctl -a v2 -s localhost:55555 config > $HOME/.volt/config 2>>$LOG) >>$LOG 2>&1 |
| 378 | espin - $VERIFIED |