blob: 89881d9bed0241d381cee9f59eb1b33106b34a11 [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong7987c112022-12-05 12:42:43 -05002// -----------------------------------------------------------------------
Joey Armstrong518f3572024-02-11 07:56:25 -05003// Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrongaf679da2023-01-31 14:22:41 -05004//
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.
16// -----------------------------------------------------------------------
17
18// -----------------------------------------------------------------------
Joey Armstrong7987c112022-12-05 12:42:43 -050019// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -040020String getIam(String func) {
Joey Armstrong7987c112022-12-05 12:42:43 -050021 // Cannot rely on a stack trace due to jenkins manipulation
22 String src = 'vars/volthaStackDeploy.groovy'
23 String iam = [src, func].join('::')
24 return iam
25}
Matteo Scandolo42f6e572021-01-25 15:11:34 -080026
Joey Armstrong7987c112022-12-05 12:42:43 -050027// -----------------------------------------------------------------------
Joey Armstrong74ec08c2023-08-31 11:25:57 -040028// Intent: Log progress message
29// -----------------------------------------------------------------------
30void enter(String name) {
31 // Announce ourselves for log usability
32 String iam = getIam(name)
33 println("${iam}: ENTER")
34 return
35}
36
37// -----------------------------------------------------------------------
38// Intent: Log progress message
39// -----------------------------------------------------------------------
40void leave(String name) {
41 // Announce ourselves for log usability
42 String iam = getIam(name)
43 println("${iam}: LEAVE")
44 return
45}
46
47// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -040048// Intent:
Joey Armstrong7987c112022-12-05 12:42:43 -050049// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -040050void deployVolthaStack(Map cfg) {
51 enter('deployVolthaStack')
Joey Armstronge93c3fa2023-09-11 09:34:24 -040052
Gurua339a5a2024-12-22 03:07:45 +053053 if (cfg.vgcEnabled) {
54 sh(label: "Create VOLTHA Stack ${cfg.stackName}, (namespace=${cfg.volthaNamespace}), (flowController=VGC)",
55 script: """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040056helm upgrade --install --create-namespace \
57 -n ${cfg.volthaNamespace} ${cfg.stackName} ${cfg.volthaStackChart} \
Matteo Scandolo42f6e572021-01-25 15:11:34 -080058 --set global.stack_name=${cfg.stackName} \
59 --set global.voltha_infra_name=voltha-infra \
Gurua339a5a2024-12-22 03:07:45 +053060 --set voltha-go-controller.enabled=true \
Roger Luethi2403ef02023-09-27 06:27:22 +020061 --set voltha.ingress.enabled=true \
Jan Klareb4789282024-11-15 13:19:10 +010062 --set voltha.ingress.hosts[0].host=voltha.${cfg.cluster} \
63 --set voltha.ingress.hosts[0].paths[0]='/voltha.VolthaService/' \
Matteo Scandolo42f6e572021-01-25 15:11:34 -080064 --set global.voltha_infra_namespace=${cfg.infraNamespace} \
Matteo Scandoloed1afdd2021-04-02 16:25:45 -070065 ${cfg.extraHelmFlags}
Joey Armstrongcd3f0752023-09-11 18:10:12 -040066""")
Gurua339a5a2024-12-22 03:07:45 +053067 } else {
68 sh(label: "Create VOLTHA Stack ${cfg.stackName}, (namespace=${cfg.volthaNamespace})",
69 script: """
70helm upgrade --install --create-namespace \
71 -n ${cfg.volthaNamespace} ${cfg.stackName} ${cfg.volthaStackChart} \
72 --set global.stack_name=${cfg.stackName} \
73 --set global.voltha_infra_name=voltha-infra \
74 --set voltha.onos_classic.replicas=${cfg.onosReplica} \
75 --set voltha.ingress.enabled=true \
76 --set voltha.ingress.hosts[0].host=voltha.${cfg.cluster} \
77 --set voltha.ingress.hosts[0].paths[0]='/voltha.VolthaService/' \
78 --set global.voltha_infra_namespace=${cfg.infraNamespace} \
79 ${cfg.extraHelmFlags}
80""")
81 }
Matteo Scandolo42f6e572021-01-25 15:11:34 -080082
Joey Armstrongcd3f0752023-09-11 18:10:12 -040083 for (int i = 0; i < cfg.bbsimReplica; i++) {
Joey Armstrong74ec08c2023-08-31 11:25:57 -040084 // NOTE we don't need to update the tag for DT
85 script {
86 sh(label : "Create config[$i]: bbsimCfg${cfg.stackId}${i}.yaml",
87 script : "rm -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml",
88 )
Joey Armstronge93c3fa2023-09-11 09:34:24 -040089
Joey Armstrong8d12f652023-09-13 18:25:33 -040090 if (cfg.workflow == 'att' || cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040091 int startingStag = 900
Joey Armstrong6f507652023-09-18 10:35:07 -040092 def serviceConfigFile = cfg.workflow // type(?) String
Joey Armstrong74ec08c2023-08-31 11:25:57 -040093 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040094 serviceConfigFile = 'tt-maclearner'
Joey Armstrong74ec08c2023-08-31 11:25:57 -040095 }
Joey Armstrong6f507652023-09-18 10:35:07 -040096 // bbsimCfg: type(?) Map
Joey Armstrong74ec08c2023-08-31 11:25:57 -040097 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
98 // NOTE we assume that the only service that needs a different s_tag is the first one in the list
Joey Armstrongcd3f0752023-09-11 18:10:12 -040099 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400100 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
101 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
102 } else {
103 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400104 sh(label : 'DT install',
105 script : """
106cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
107 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
108 """)
109 } // if (cfg)
110 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400111
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400112 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
113 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400114 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -0700115 --set olt_id="${cfg.stackId}${i}" \
116 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
117 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400118""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400119 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400120
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400121 leave('deployVolthaStack')
122 return
123}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700124
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400125// -----------------------------------------------------------------------
126// Intent: Wait until the pod completed, meaning ONOS fully deployed
127// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400128// Todo: Move logic like this into a standalone script.
129// Use jenkins stash or native JJB logic to publish out to nodes.
130// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400131void launchVolthaStack(Map cfg) {
132 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800133
Joey Armstrong358f2242023-09-15 09:49:21 -0400134 /* -----------------------------------------------------------------------
135 * % kubectl get pods
136 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
137 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
138
139 * % kubectl port-forward --address 0.0.0.0
140 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
141 * -----------------------------------------------------------------------
142 */
143 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400144 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400145
146cat <<EOM
147
148** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400149** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400150** -----------------------------------------------------------------------
151EOM
152
153# set -euo pipefail
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500154set +x # # Logs are noisy with set -x
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400155
156declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400157declare -i debug=1 # uncomment to enable debugging
158# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400159vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400160echo > \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400161
Joey Armstrongcfb85942023-09-15 11:47:02 -0400162declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400163while true; do
164
Joey Armstrong358f2242023-09-15 09:49:21 -0400165 # Gather
166 kubectl get pods -n ${cfg.volthaNamespace} \
167 -l app.kubernetes.io/part-of=voltha --no-headers \
168 > \$vsd_log
169
170 count=\$((\$count - 1))
171
172 # Display activity every iteration ?
173 [[ -v verbose ]] && { count=0; }
174
175 # Display activity every minute or so {sleep(5) x count=10}
176 if [[ \$count -lt 1 ]]; then
177 count=10
178 cat \$vsd_log
179 fi
180
181 ## -----------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400182 ## Probe for cluster state
Joey Armstrong358f2242023-09-15 09:49:21 -0400183 ## -----------------------
184 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200185 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrong358f2242023-09-15 09:49:21 -0400186 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400187
Joey Armstrong358f2242023-09-15 09:49:21 -0400188 elif grep -q -e '0/' \$vsd_log; then
189 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
190 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400191
Joey Armstrong358f2242023-09-15 09:49:21 -0400192 elif ! grep -q '/' \$vsd_log; then
193 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
194 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400195
196 # -----------------------------------------------------------------------
197 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
198 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
199 # -----------------------------------------------------------------------
200 elif grep -q 'Error' \$vsd_log; then
201 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
202 cat \$vsd_log
203 rc=1 # fatal
204 break
205
Joey Armstrong358f2242023-09-15 09:49:21 -0400206 # -----------------------------------------------------------------------
207 # An extra conditon needed here but shell coding is tricky:
208 # "svc x/y Running 0 6s
209 # Verify (x == y) && (x > 0)
Joey Armstrong6f507652023-09-18 10:35:07 -0400210 # Wait until job failure/we have an actual need for it.
Joey Armstrong358f2242023-09-15 09:49:21 -0400211 # -----------------------------------------------------------------------
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400212 # Could check for all services 'Running', is that reliable (?)
213 # -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400214 else
215 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
216 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400217 break
218 fi
219
Joey Armstrong358f2242023-09-15 09:49:21 -0400220 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400221 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400222
223done
224rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400225exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400226""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500227
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400228 leave('launchVolthaStack')
229 return
230}
231
232// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400233// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400234// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400235void waitForOnosDeploy(Map cfg) {
236 enter('waitForOnosDeploy')
237
238 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400239 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400240
241cat <<EOM
242
243** -----------------------------------------------------------------------
244** Wait for ONOS full deployment
245** -----------------------------------------------------------------------
246EOM
247
248# set -euo pipefail
249set +x # # Noisy when commented (default: uncommented)
250
251declare -i count=0
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400252declare -i debug=1 # uncomment to enable debugging
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500253declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400254vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400255echo > \$vsd_log
256
257declare -i rc=0 # exit status
Joey Armstrong8d12f652023-09-13 18:25:33 -0400258while true; do
259
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400260 # Gather -- should we check for count(svc > 1) ?
261 kubectl get pods -l app=onos-config-loader \
262 -n ${cfg.infraNamespace} --no-headers \
263 --field-selector=status.phase=Running \
264 > \$vsd_log
265
266 count=\$((\$count - 1))
267
268 # Display activity every iteration ?
269 [[ -v verbose ]] && { count=0; }
270
271 # Display activity every minute or so {sleep(5) x count=10}
272 if [[ \$count -lt 1 ]]; then
273 count=10
274 cat \$vsd_log
275 fi
276
277 ## -----------------------
278 ## Probe for cluster state
279 ## -----------------------
280 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200281 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400282 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
283
284 elif grep -q -e '0/' \$vsd_log; then
285 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
286 [[ -v debug ]] && grep -e '0/' \$vsd_log
287
288 elif ! grep -q '/' \$vsd_log; then
289 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
290 [[ ! -v verbose ]] && { cat \$vsd_log; }
291
292 # -----------------------------------------------------------------------
293 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
294 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
295 # -----------------------------------------------------------------------
296 elif grep -q 'Error' \$vsd_log; then
297 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
298 cat \$vsd_log
299 rc=1 # fatal
300 break
301
302 # -----------------------------------------------------------------------
303 # An extra conditon needed here but shell coding is tricky:
304 # "svc x/y Running 0 6s
305 # Verify (x == y) && (x > 0)
306 # Wait until job failure/we have an actual need for it.
307 # -----------------------------------------------------------------------
308 # Could check for all services 'Running', is that reliable (?)
309 # -----------------------------------------------------------------------
310 else
311 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
312 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400313 break
314 fi
315
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400316 ## Support argument --timeout (?)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400317 sleep 5
Joey Armstrong8d12f652023-09-13 18:25:33 -0400318
Joey Armstrong8d12f652023-09-13 18:25:33 -0400319done
320rm -f \$vsd_log
Joey Armstrong8c347122023-09-18 17:04:49 -0400321exit \$rc
Joey Armstrong8d12f652023-09-13 18:25:33 -0400322""")
323
324 leave('waitForOnosDeploy')
325 return
326}
327
328// -----------------------------------------------------------------------
329// -----------------------------------------------------------------------
330void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400331 enter('process')
332
333 // note that I can't define this outside the function as there's no global scope in Groovy
334 Map defaultConfig = [
335 bbsimReplica: 1,
336 infraNamespace: 'infra',
337 volthaNamespace: 'voltha',
338 stackName: 'voltha',
339 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
340 workflow: 'att',
341 withMacLearning: false,
342 withFttb: false,
343 extraHelmFlags: '',
344 localCharts: false,
345 onosReplica: 1,
346 adaptersToWait: 2,
Jan Klareb4789282024-11-15 13:19:10 +0100347 cluster: 'voltha.local',
Gurua339a5a2024-12-22 03:07:45 +0530348 vgcEnabled: 'false',
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400349 ]
350
351 Map cfg = defaultConfig + config
352
353 // Augment config map
354 cfg.volthaStackChart = 'onf/voltha-stack'
355 cfg.bbsimChart = 'onf/bbsim'
356
357 if (cfg.localCharts) {
358 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
359 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
360
361 sh(label : 'HELM: Update voltha-stack deps',
362 script : """
363 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
364 helm dep update
365 popd
366""")
367 }
368
369 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
370 deployVolthaStack(cfg)
371 launchVolthaStack(cfg)
372 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400373 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400374 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400375
Joey Armstrong7987c112022-12-05 12:42:43 -0500376 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800377}
Joey Armstrong7987c112022-12-05 12:42:43 -0500378
379// -----------------------------------------------------------------------
380// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400381def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400382 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400383 enter('main')
384 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500385 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400386 catch (Exception err) { // groovylint-disable-line CatchException
387 ans = false
388 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
389 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500390 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400391 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400392 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500393 }
394 return
395}
396
397// [EOF]