blob: 70659c6cc83c0a371359896b1836b338146896c1 [file] [log] [blame]
David K. Bainbridgeb7285432019-07-02 22:05:24 -07001#!/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()
17trap ctrl_c INT
18
19function ctrl_c() {
20 tput cnorm
21 echo ""
22 echo "ctrl-c trapped"
23 echo "Thank you for trying 'votlha up'"
24 exit
25}
26
27if [ $# -ne 1 -o $(echo ":up:down:" | grep -c ":$1:") -ne 1 ]; then
28 >&2 echo "up or down?"
29 exit 1
30fi
31
32if [ "$1" == "down" ]; then
33 exec kind delete cluster --name voltha-$TYPE
34 exit
35fi
36
37LOG="install.log"
38date > $LOG
39
40spin() {
41 START=14852225
42 END=14852287
43 IDX=$START
44 tput civis
45 while true; do
46 printf '%02x' $IDX | xxd -r -p
47 echo -en " $IDX"
48 IDX=$(expr $IDX + 1)
49 if [ $IDX -gt $END ]; then
50 IDX=$START
51 fi
52 sleep .15
53 echo -en "\r"
54 done
55}
56
57IDX=14852225
58NOT_VERIFIED="\xe2\x9c\x97"
59VERIFIED="\xe2\x9c\x93"
60bspin() {
61 tput civis
62 IDX=14852225
63 local INDENT=
64 if [ "$1" == "-" ]; then
65 INDENT=" "
66 shift
67 fi
68 echo -en "$INDENT $*"
69}
70
71sspin() {
72 local INDENT=
73 if [ "$1" == "-" ]; then
74 INDENT=" "
75 shift
76 fi
77 echo -en "\r$INDENT"
78 printf " %02x" $IDX | xxd -r -p
79 echo -en " $*"
80 IDX=$(expr $IDX + 1)
81 if [ $IDX -gt 14852287 ]; then
82 IDX=14852225
83 fi
84}
85
86espin() {
87 local INDENT=
88 if [ "$1" == "-" ]; then
89 INDENT=" "
90 shift
91 fi
92 echo -e "\r$INDENT$*"
93 tput cnorm
94}
95
96count_pods() {
97 local NAMESPACE=$1; shift
98 local PODS=$(kubectl -n $NAMESPACE get pod -o go-template="{{range .items}}{{.metadata.name}}/{{.status.phase}}/_{{range .status.containerStatuses}}{{.ready}}_{{end}} {{end}}")
99 local COUNT=0
100 local PATTERNS=$*
101 for POD in $PODS; do
102 local NAME=$(echo $POD | cut -d/ -f 1)
103 local STATE=$(echo $POD | cut -d/ -f 2)
104 local CONTAINERS=$(echo $POD | cut -d/ -f 3 | sed -e 's/_/ /g')
105 if [ "$STATE" == "Running" ]; then
106 local TOTAL=$(echo $CONTAINERS | wc -w)
107 local FOUND=$(echo $CONTAINERS | grep -o true | wc -l)
108 if [ $TOTAL -eq $FOUND ]; then
109 for PATTERN in $PATTERNS; do
110 if [[ $NAME =~ $PATTERN ]]; then
111 COUNT=$(expr $COUNT + 1)
112 fi
113 done
114 fi
115 fi
116 done
117 echo $COUNT
118}
119
120wait_for_pods() {
121 local INDENT=
122 if [ "$1" == "-" ]; then
123 INDENT=$1; shift
124 fi
125 local NAMESPACE=$1; shift
126 local EXPECT=$1; shift
127 local RETRY=$1; shift
128 local MESSAGE=$1; shift
129 local PATTERNS=$*
130 local HAVE=$(count_pods $NAMESPACE $PATTERNS)
131 COUNT=$(expr 300 / 15)
132 bspin $INDENT $MESSAGE
133 sspin $INDENT
134 if [ $HAVE -ne $EXPECT ]; then
135 while [ $HAVE -ne $EXPECT ]; do
136 sspin $INDENT
137 COUNT=$(expr $COUNT - 1)
138 if [ $COUNT -eq 0 ]; then
139 HAVE=$(count_pods $NAMESPACE $PATTERNS)
140 COUNT=$(expr 300 / 15)
141 fi
142 sleep .15
143 done
144 fi
145 espin $INDENT $VERIFIED
146 if [ $HAVE -ne $EXPECT ]; then
147 return 1
148 fi
149 return 0
150}
151
152helm_install() {
153 local INDENT=
154 if [ "$1" == "-" ]; then
155 INDENT=$1; shift
156 fi
157 local NAMESPACE=$1; shift
158 local NAME=$1; shift
159 local CHART=$1; shift
160 local MESSAGE=$*
161
162 COUNT=$(expr 300 / 15)
163 bspin $INDENT $MESSAGE
164 (set -x; helm install -f $TYPE-values.yaml --namespace $NAMESPACE --name $NAME $CHART >>$LOG 2>&1) >>$LOG 2>&1
165 SUCCESS=$?
166 while [ $SUCCESS -ne 0 ]; do
167 sspin $INDENT
168 COUNT=$(expr $COUNT - 1)
169 if [ $COUNT -eq 0 ]; then
170 (set -x; helm install -f $TYPE-values.yaml --namespace $NAMESPACE --name $NAME $CHART >>$LOG 2>&1) >>$LOG 2>&1
171 COUNT=$(expr 300 / 15)
172 fi
173 sleep .15
174 done
175 espin $INDENT $VERIFIED
176}
177
178TYPE=${TYPE:-minimal}
179
180# Only minimal cluster works in this script currently
181TYPE=minimal
182echo "INSTALL TYPE: $TYPE" >> $LOG
183
184bspin "Verify GOPATH"
185export GOPATH=$(pwd)
186espin $VERIFIED
187
188bspin "Verify Kubernetes/Kind"
189if [ -x ./bin/kind ]; then
190 espin $VERIFIED
191else
192 espin $NOT_VERIFIED
193 bspin - "Download and build Kubernetes/Kind"
194 (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1
195 (set -x; GO111MODULE="on" go get sigs.k8s.io/kind@v0.4.0 >>$LOG 2>&1) >>$LOG 2>&1
196 espin - $VERIFIED
197fi
198
199bspin "Verify Helm"
200if [ -x ./bin/helm ]; then
201 espin $VERIFIED
202else
203 espin $NOT_VERIFIED
204 bspin - "Download and install Helm"
205 (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1
206 (set -x; curl -L https://git.io/get_helm.sh | HELM_INSTALL_DIR=$(go env GOPATH)/bin bash >>$LOG 2>&1) >>$LOG 2>&1
207 espin - $VERIFIED
208fi
209
210bspin "Verify voltctl"
211if [ -x ./bin/voltctl ]; then
212 espin $VERIFIED
213else
214 espin $NOT_VERIFIED
215 bspin - "Download and build voltctl"
216 (set -x; rm -rf $GOPATH/go.mod $GOPATH/go.sum >>$LOG 2>&1) >>$LOG 2>&1
217 (set -x; go get github.com/ciena/voltctl/cmd/voltctl >>$LOG 2>&1) >>$LOG 2>&1
218 espin - $VERIFIED
219fi
220
221bspin "Verify command PATH"
222export PATH=$(go env GOPATH)/bin:$PATH
223espin $VERIFIED
224exit
225
226HAVE=$(kind get clusters | grep -c voltha-$TYPE)
227bspin "Verify Kubernetes/Kind Cluster"
228sspin
229if [ $HAVE -eq 0 ]; then
230 espin $NOT_VERIFIED
231 kind create cluster --name voltha-$TYPE --config $TYPE-cluster.cfg
232else
233 espin $VERIFIED
234fi
235
236export KUBECONFIG="$(kind get kubeconfig-path --name="voltha-$TYPE")"
237P="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
245wait_for_pods - "kube-system" 12 -1 "Waiting for system PODs to start..." $P
246
247COUNT=$(count_pods "kube-system" "tiller-deploy-.*")
248bspin "Verify Helm"
249if [ $(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
279else
280 espin $VERIFIED
281fi
282wait_for_pods - "kube-system" 1 -1 "Waiting for Tiller POD to start..." "tiller-deploy-.*"
283
284bspin "Verify ETCD Operator"
285if [ $(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"
288else
289 espin $VERIFIED
290fi
291wait_for_pods - "voltha" 1 -1 "Waiting for ETCD Operator to start..." "etcd-operator-.*"
292
293bspin "Verify ONOS installed"
294if [ $(helm list onos | grep -c DEPLOYED) -ne 1 ]; then
295 espin $NOT_VERIFIED
296 helm_install - default onos onf/onos "Install ONOS"
297else
298 espin $VERIFIED
299fi
300wait_for_pods - "default" 1 -1 "Waiting for ONOS to start..." "onos-.*"
301
302bspin - "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
305espin - $VERIFIED
306bspin - "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
309espin - $VERIFIED
310bspin - "Install required ONOS applications"
311(set -x; ./onos-files/install-onos-applications.sh >>$LOG 2>&1) >>$LOG 2>&1
312espin - $VERIFIED
313
314bspin "Verify VOLTHA installed"
315if [ $(helm list voltha | grep -c DEPLOYED) -ne 1 ]; then
316 espin $NOT_VERIFIED
317 helm_install - voltha voltha onf/voltha "Install VOLTHA"
318else
319 espin $VERIFIED
320fi
321VOLTHA="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-.*"
329wait_for_pods - "voltha" 9 -1 "Waiting for VOLTHA CORE top start..." $VOLTHA
330
331echo "Verify Adapters"
332bspin - "Verify Simulated Adapters installed"
333if [ $(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"
336else
337 espin - $VERIFIED
338fi
339
340bspin - "Verify OpenOLT Adapter installed"
341if [ $(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"
344else
345 espin - $VERIFIED
346fi
347bspin - "Verify OpenONU Adapter installed"
348if [ $(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"
351else
352 espin - $VERIFIED
353fi
354
355ADAPTERS="adapter-.*"
356wait_for_pods - "voltha" 4 -1 "Waiting for adapters to start..." $ADAPTERS
357
358echo "Restart VOLTHA API"
359API="voltha-api-server-.* ofagent-.*"
360(set -x; kubectl scale --replicas=0 deployment -n voltha voltha-api-server ofagent >>$LOG 2>&1) >>$LOG 2>&1
361wait_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
363wait_for_pods - "voltha" 2 -1 "Wait for API to re-start ..." $API
364
365bspin - "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
368espin - $VERIFIED
369bspin - "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
372espin - $VERIFIED