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 |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 197 | espin - $VERIFIED |
| 198 | fi |
| 199 | |
| 200 | bspin "Verify Helm" |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame^] | 201 | if [ -x $GOPATH/bin/helm ]; then |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 202 | espin $VERIFIED |
| 203 | else |
| 204 | espin $NOT_VERIFIED |
| 205 | bspin - "Download and install Helm" |
| 206 | (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1 |
| 207 | (set -x; curl -L https://git.io/get_helm.sh | HELM_INSTALL_DIR=$(go env GOPATH)/bin bash >>$LOG 2>&1) >>$LOG 2>&1 |
| 208 | espin - $VERIFIED |
| 209 | fi |
| 210 | |
| 211 | bspin "Verify voltctl" |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame^] | 212 | if [ -x $GOPATH/bin/voltctl ]; then |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 213 | espin $VERIFIED |
| 214 | else |
| 215 | espin $NOT_VERIFIED |
| 216 | bspin - "Download and build voltctl" |
| 217 | (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1 |
| 218 | (set -x; go get github.com/ciena/voltctl/cmd/voltctl >>$LOG 2>&1) >>$LOG 2>&1 |
| 219 | espin - $VERIFIED |
| 220 | fi |
| 221 | |
| 222 | bspin "Verify command PATH" |
| 223 | export PATH=$(go env GOPATH)/bin:$PATH |
| 224 | espin $VERIFIED |
David K. Bainbridge | b728543 | 2019-07-02 22:05:24 -0700 | [diff] [blame] | 225 | |
| 226 | HAVE=$(kind get clusters | grep -c voltha-$TYPE) |
| 227 | bspin "Verify Kubernetes/Kind Cluster" |
| 228 | sspin |
| 229 | if [ $HAVE -eq 0 ]; then |
| 230 | espin $NOT_VERIFIED |
| 231 | kind create cluster --name voltha-$TYPE --config $TYPE-cluster.cfg |
| 232 | else |
| 233 | espin $VERIFIED |
| 234 | fi |
| 235 | |
| 236 | export KUBECONFIG="$(kind get kubeconfig-path --name="voltha-$TYPE")" |
| 237 | P="coredns-.* \ |
| 238 | etcd-voltha-$TYPE-control-plane \ |
| 239 | kindnet-.* \ |
| 240 | kube-apiserver-voltha-$TYPE-control-plane \ |
| 241 | kube-controller-manager-voltha-$TYPE-control-plane \ |
| 242 | kube-proxy-.* \ |
| 243 | kube-scheduler-voltha-$TYPE-control-plane" |
| 244 | |
| 245 | wait_for_pods - "kube-system" 12 -1 "Waiting for system PODs to start..." $P |
| 246 | |
| 247 | COUNT=$(count_pods "kube-system" "tiller-deploy-.*") |
| 248 | bspin "Verify Helm" |
| 249 | if [ $(count_pods "kube-system" "tiller-deploy-.*") -ne 1 ]; then |
| 250 | espin $NOT_VERIFIED |
| 251 | echo Configuring" Helm ..." |
| 252 | bspin - "Initialize Helm" |
| 253 | (set -x; helm init --upgrade >>$LOG 2>&1) >>$LOG 2>&1 |
| 254 | espin - $VERIFIED |
| 255 | bspin - "Add Google Incubator repository to Helm ..." |
| 256 | (set -x; helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com >>$LOG 2>&1) >>$LOG 2>&1 |
| 257 | espin - $VERIFIED |
| 258 | |
| 259 | bspin - "Add Google Stable repository to Helm ..." |
| 260 | (set -x; helm repo add stable https://kubernetes-charts.storage.googleapis.com >>$LOG 2>&1) >>$LOG 2>&1 |
| 261 | espin - $VERIFIED |
| 262 | bspin - "Add ONF repository to Helm ..." |
| 263 | (set -x; helm repo add onf https://charts.opencord.org >>$LOG 2>&1) >>$LOG 2>&1 |
| 264 | espin - $VERIFIED |
| 265 | bspin - "Update Helm repository cache ..." |
| 266 | (set -x; helm repo update >>$LOG 2>&1) >>$LOG 2>&1 |
| 267 | espin - $VERIFIED |
| 268 | |
| 269 | # Create and k8s service account so that Helm can create pods |
| 270 | bspin - "Create Tiller ServiceAccount ..." |
| 271 | (set -x; kubectl create serviceaccount --namespace kube-system tiller >>$LOG 2>&1) >>$LOG 2>&1 |
| 272 | espin - $VERIFIED |
| 273 | bspin - "Create Tiller ClusterRoleBinding ..." |
| 274 | (set -x; kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller >>$LOG 2>&1) >>$LOG 2>&1 |
| 275 | espin - $VERIFIED |
| 276 | bspin - "Update Tiller Manifest ..." |
| 277 | (set -x; kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}' >>$LOG 2>&1) >>$LOG 2>&1 |
| 278 | espin - $VERIFIED |
| 279 | else |
| 280 | espin $VERIFIED |
| 281 | fi |
| 282 | wait_for_pods - "kube-system" 1 -1 "Waiting for Tiller POD to start..." "tiller-deploy-.*" |
| 283 | |
| 284 | bspin "Verify ETCD Operator" |
| 285 | if [ $(helm list etcd-operator | grep -c DEPLOYED) -ne 1 ]; then |
| 286 | espin $NOT_VERIFIED |
| 287 | helm_install - voltha etcd-operator stable/etcd-operator "Install ETCD Operator" |
| 288 | else |
| 289 | espin $VERIFIED |
| 290 | fi |
| 291 | wait_for_pods - "voltha" 1 -1 "Waiting for ETCD Operator to start..." "etcd-operator-.*" |
| 292 | |
| 293 | bspin "Verify ONOS installed" |
| 294 | if [ $(helm list onos | grep -c DEPLOYED) -ne 1 ]; then |
| 295 | espin $NOT_VERIFIED |
| 296 | helm_install - default onos onf/onos "Install ONOS" |
| 297 | else |
| 298 | espin $VERIFIED |
| 299 | fi |
| 300 | wait_for_pods - "default" 1 -1 "Waiting for ONOS to start..." "onos-.*" |
| 301 | |
| 302 | bspin - "Forward ONOS API port" |
| 303 | (set -x; screen -p 0 -X -S onos-ui stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 304 | (set -x; screen -dmS onos-ui kubectl port-forward service/onos-ui 8181:8181 >>$LOG 2>&1) >>$LOG 2>&1 |
| 305 | espin - $VERIFIED |
| 306 | bspin - "Forward ONOS SSH port" |
| 307 | (set -x; screen -p 0 -X -S onos-ssh stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 308 | (set -x; screen -dmS onos-ssh kubectl port-forward service/onos-ssh 8101:8101 >>$LOG 2>&1) >>$LOG 2>&1 |
| 309 | espin - $VERIFIED |
| 310 | bspin - "Install required ONOS applications" |
| 311 | (set -x; ./onos-files/install-onos-applications.sh >>$LOG 2>&1) >>$LOG 2>&1 |
| 312 | espin - $VERIFIED |
| 313 | |
| 314 | bspin "Verify VOLTHA installed" |
| 315 | if [ $(helm list voltha | grep -c DEPLOYED) -ne 1 ]; then |
| 316 | espin $NOT_VERIFIED |
| 317 | helm_install - voltha voltha onf/voltha "Install VOLTHA" |
| 318 | else |
| 319 | espin $VERIFIED |
| 320 | fi |
| 321 | VOLTHA="ofagent-.* \ |
| 322 | ro-core.* \ |
| 323 | rw-core.* \ |
| 324 | voltha-api-server-.* \ |
| 325 | voltha-cli-server-.* \ |
| 326 | voltha-etcd-cluster-.* \ |
| 327 | voltha-kafka-.* \ |
| 328 | voltha-zookeeper-.*" |
| 329 | wait_for_pods - "voltha" 9 -1 "Waiting for VOLTHA CORE top start..." $VOLTHA |
| 330 | |
| 331 | echo "Verify Adapters" |
| 332 | bspin - "Verify Simulated Adapters installed" |
| 333 | if [ $(helm list sim | grep -c DEPLOYED) -ne 1 ]; then |
| 334 | espin - $NOT_VERIFIED |
| 335 | helm_install - voltha sim onf/voltha-adapter-simulated "Install Simulated Adapters" |
| 336 | else |
| 337 | espin - $VERIFIED |
| 338 | fi |
| 339 | |
| 340 | bspin - "Verify OpenOLT Adapter installed" |
| 341 | if [ $(helm list open-olt | grep -c DEPLOYED) -ne 1 ]; then |
| 342 | espin - $NOT_VERIFIED |
| 343 | helm_install - voltha open-olt onf/voltha-adapter-openolt "Install OpenOLT Adapter" |
| 344 | else |
| 345 | espin - $VERIFIED |
| 346 | fi |
| 347 | bspin - "Verify OpenONU Adapter installed" |
| 348 | if [ $(helm list open-onu | grep -c DEPLOYED) -ne 1 ]; then |
| 349 | espin - $NOT_VERIFIED |
| 350 | helm_install - voltha open-onu onf/voltha-adapter-openonu "Install OpenONU Adapter" |
| 351 | else |
| 352 | espin - $VERIFIED |
| 353 | fi |
| 354 | |
| 355 | ADAPTERS="adapter-.*" |
| 356 | wait_for_pods - "voltha" 4 -1 "Waiting for adapters to start..." $ADAPTERS |
| 357 | |
| 358 | echo "Restart VOLTHA API" |
| 359 | API="voltha-api-server-.* ofagent-.*" |
| 360 | (set -x; kubectl scale --replicas=0 deployment -n voltha voltha-api-server ofagent >>$LOG 2>&1) >>$LOG 2>&1 |
| 361 | wait_for_pods - "voltha" 0 -1 "Wait for API to stop" $API |
| 362 | (set -x; kubectl scale --replicas=1 deployment -n voltha voltha-api-server ofagent >>$LOG 2>&1) >>$LOG 2>&1 |
| 363 | wait_for_pods - "voltha" 2 -1 "Wait for API to re-start ..." $API |
| 364 | |
| 365 | bspin - "Forward VOLTHA API port" |
| 366 | (set -x; screen -p 0 -X -S voltha-api stuff $'\003' >>$LOG 2>&1) >>$LOG 2>&1 |
| 367 | (set -x; screen -dmS voltha-api kubectl port-forward -n voltha service/voltha-api 55555:55555 >>$LOG 2>&1) >>$LOG 2>&1 |
| 368 | espin - $VERIFIED |
| 369 | bspin - "Forward VOLTHA SSH port" |
| 370 | (set -x; screen -p 0 -X -S voltha-ssh stuff $'\003' 2>/dev/null >/dev/null >>$LOG 2>&1) >>$LOG 2>&1 |
| 371 | (set -x; screen -dmS voltha-ssh kubectl port-forward -n voltha service/voltha-cli 5022:5022 >>$LOG 2>&1) >>$LOG 2>&1 |
| 372 | espin - $VERIFIED |
Test User | 3d7ad8e | 2019-07-03 06:15:44 +0000 | [diff] [blame^] | 373 | |
| 374 | bspin - "Create voltctl configuration file" |
| 375 | (set -x; mkdir -p $HOME/.volt >>$LOG 2>&1) >>$LOG 2>&1 |
| 376 | (set -x; voltctl -a v2 -s localhost:55555 config > $HOME/.volt/config 2>>$LOG) >>$LOG 2>&1 |
| 377 | espin - $VERIFIED |