blob: 7a366bec706692bb65e386002e330ff050d9f44c [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} \
gst2ff418d2025-01-13 13:23:05 +053065 --set voltha.replicas.ofagent=0 \
Matteo Scandoloed1afdd2021-04-02 16:25:45 -070066 ${cfg.extraHelmFlags}
Joey Armstrongcd3f0752023-09-11 18:10:12 -040067""")
Gurua339a5a2024-12-22 03:07:45 +053068 } else {
69 sh(label: "Create VOLTHA Stack ${cfg.stackName}, (namespace=${cfg.volthaNamespace})",
70 script: """
71helm upgrade --install --create-namespace \
72 -n ${cfg.volthaNamespace} ${cfg.stackName} ${cfg.volthaStackChart} \
73 --set global.stack_name=${cfg.stackName} \
74 --set global.voltha_infra_name=voltha-infra \
75 --set voltha.onos_classic.replicas=${cfg.onosReplica} \
76 --set voltha.ingress.enabled=true \
77 --set voltha.ingress.hosts[0].host=voltha.${cfg.cluster} \
78 --set voltha.ingress.hosts[0].paths[0]='/voltha.VolthaService/' \
79 --set global.voltha_infra_namespace=${cfg.infraNamespace} \
80 ${cfg.extraHelmFlags}
81""")
82 }
Matteo Scandolo42f6e572021-01-25 15:11:34 -080083
Joey Armstrongcd3f0752023-09-11 18:10:12 -040084 for (int i = 0; i < cfg.bbsimReplica; i++) {
Joey Armstrong74ec08c2023-08-31 11:25:57 -040085 // NOTE we don't need to update the tag for DT
86 script {
87 sh(label : "Create config[$i]: bbsimCfg${cfg.stackId}${i}.yaml",
88 script : "rm -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml",
89 )
Joey Armstronge93c3fa2023-09-11 09:34:24 -040090
Joey Armstrong8d12f652023-09-13 18:25:33 -040091 if (cfg.workflow == 'att' || cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040092 int startingStag = 900
Joey Armstrong6f507652023-09-18 10:35:07 -040093 def serviceConfigFile = cfg.workflow // type(?) String
Joey Armstrong74ec08c2023-08-31 11:25:57 -040094 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040095 serviceConfigFile = 'tt-maclearner'
Joey Armstrong74ec08c2023-08-31 11:25:57 -040096 }
Joey Armstrong6f507652023-09-18 10:35:07 -040097 // bbsimCfg: type(?) Map
Joey Armstrong74ec08c2023-08-31 11:25:57 -040098 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
99 // 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 -0400100 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400101 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
102 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
103 } else {
104 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400105 sh(label : 'DT install',
106 script : """
107cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
108 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
109 """)
110 } // if (cfg)
111 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400112
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400113 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
114 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400115 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -0700116 --set olt_id="${cfg.stackId}${i}" \
117 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
118 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400119""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400120 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400121
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400122 leave('deployVolthaStack')
123 return
124}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700125
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400126// -----------------------------------------------------------------------
127// Intent: Wait until the pod completed, meaning ONOS fully deployed
128// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400129// Todo: Move logic like this into a standalone script.
130// Use jenkins stash or native JJB logic to publish out to nodes.
131// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400132void launchVolthaStack(Map cfg) {
133 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800134
Joey Armstrong358f2242023-09-15 09:49:21 -0400135 /* -----------------------------------------------------------------------
136 * % kubectl get pods
137 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
138 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
139
140 * % kubectl port-forward --address 0.0.0.0
141 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
142 * -----------------------------------------------------------------------
143 */
144 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400145 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400146
147cat <<EOM
148
149** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400150** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400151** -----------------------------------------------------------------------
152EOM
153
154# set -euo pipefail
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500155set +x # # Logs are noisy with set -x
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400156
157declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400158declare -i debug=1 # uncomment to enable debugging
159# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400160vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400161echo > \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400162
Joey Armstrongcfb85942023-09-15 11:47:02 -0400163declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400164while true; do
165
Joey Armstrong358f2242023-09-15 09:49:21 -0400166 # Gather
167 kubectl get pods -n ${cfg.volthaNamespace} \
168 -l app.kubernetes.io/part-of=voltha --no-headers \
169 > \$vsd_log
170
171 count=\$((\$count - 1))
172
173 # Display activity every iteration ?
174 [[ -v verbose ]] && { count=0; }
175
176 # Display activity every minute or so {sleep(5) x count=10}
177 if [[ \$count -lt 1 ]]; then
178 count=10
179 cat \$vsd_log
180 fi
181
182 ## -----------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400183 ## Probe for cluster state
Joey Armstrong358f2242023-09-15 09:49:21 -0400184 ## -----------------------
185 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200186 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrong358f2242023-09-15 09:49:21 -0400187 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400188
Joey Armstrong358f2242023-09-15 09:49:21 -0400189 elif grep -q -e '0/' \$vsd_log; then
190 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
191 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400192
Joey Armstrong358f2242023-09-15 09:49:21 -0400193 elif ! grep -q '/' \$vsd_log; then
194 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
195 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400196
197 # -----------------------------------------------------------------------
198 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
199 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
200 # -----------------------------------------------------------------------
201 elif grep -q 'Error' \$vsd_log; then
202 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
203 cat \$vsd_log
204 rc=1 # fatal
205 break
206
Joey Armstrong358f2242023-09-15 09:49:21 -0400207 # -----------------------------------------------------------------------
208 # An extra conditon needed here but shell coding is tricky:
209 # "svc x/y Running 0 6s
210 # Verify (x == y) && (x > 0)
Joey Armstrong6f507652023-09-18 10:35:07 -0400211 # Wait until job failure/we have an actual need for it.
Joey Armstrong358f2242023-09-15 09:49:21 -0400212 # -----------------------------------------------------------------------
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400213 # Could check for all services 'Running', is that reliable (?)
214 # -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400215 else
216 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
217 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400218 break
219 fi
220
Joey Armstrong358f2242023-09-15 09:49:21 -0400221 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400222 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400223
224done
225rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400226exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400227""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500228
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400229 leave('launchVolthaStack')
230 return
231}
232
233// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400234// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400235// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400236void waitForOnosDeploy(Map cfg) {
gst925133d2025-01-13 15:26:30 +0530237 if (!cfg.vgcEnabled) {
238 enter('waitForOnosDeploy')
Joey Armstrong8d12f652023-09-13 18:25:33 -0400239
240 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400241 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400242
243cat <<EOM
244
245** -----------------------------------------------------------------------
246** Wait for ONOS full deployment
247** -----------------------------------------------------------------------
248EOM
249
250# set -euo pipefail
251set +x # # Noisy when commented (default: uncommented)
252
253declare -i count=0
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400254declare -i debug=1 # uncomment to enable debugging
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500255declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400256vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400257echo > \$vsd_log
258
259declare -i rc=0 # exit status
Joey Armstrong8d12f652023-09-13 18:25:33 -0400260while true; do
261
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400262 # Gather -- should we check for count(svc > 1) ?
263 kubectl get pods -l app=onos-config-loader \
264 -n ${cfg.infraNamespace} --no-headers \
265 --field-selector=status.phase=Running \
266 > \$vsd_log
267
268 count=\$((\$count - 1))
269
270 # Display activity every iteration ?
271 [[ -v verbose ]] && { count=0; }
272
273 # Display activity every minute or so {sleep(5) x count=10}
274 if [[ \$count -lt 1 ]]; then
275 count=10
276 cat \$vsd_log
277 fi
278
279 ## -----------------------
280 ## Probe for cluster state
281 ## -----------------------
282 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200283 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400284 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
285
286 elif grep -q -e '0/' \$vsd_log; then
287 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
288 [[ -v debug ]] && grep -e '0/' \$vsd_log
289
290 elif ! grep -q '/' \$vsd_log; then
291 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
292 [[ ! -v verbose ]] && { cat \$vsd_log; }
293
294 # -----------------------------------------------------------------------
295 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
296 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
297 # -----------------------------------------------------------------------
298 elif grep -q 'Error' \$vsd_log; then
299 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
300 cat \$vsd_log
301 rc=1 # fatal
302 break
303
304 # -----------------------------------------------------------------------
305 # An extra conditon needed here but shell coding is tricky:
306 # "svc x/y Running 0 6s
307 # Verify (x == y) && (x > 0)
308 # Wait until job failure/we have an actual need for it.
309 # -----------------------------------------------------------------------
310 # Could check for all services 'Running', is that reliable (?)
311 # -----------------------------------------------------------------------
312 else
313 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
314 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400315 break
316 fi
317
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400318 ## Support argument --timeout (?)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400319 sleep 5
Joey Armstrong8d12f652023-09-13 18:25:33 -0400320
Joey Armstrong8d12f652023-09-13 18:25:33 -0400321done
322rm -f \$vsd_log
Joey Armstrong8c347122023-09-18 17:04:49 -0400323exit \$rc
Joey Armstrong8d12f652023-09-13 18:25:33 -0400324""")
Joey Armstrong8d12f652023-09-13 18:25:33 -0400325 leave('waitForOnosDeploy')
gst925133d2025-01-13 15:26:30 +0530326 } else {
327 println "VGC enabled, no need to wait for ONOS deployment"
328 }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400329 return
330}
331
332// -----------------------------------------------------------------------
333// -----------------------------------------------------------------------
334void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400335 enter('process')
336
337 // note that I can't define this outside the function as there's no global scope in Groovy
338 Map defaultConfig = [
339 bbsimReplica: 1,
340 infraNamespace: 'infra',
341 volthaNamespace: 'voltha',
342 stackName: 'voltha',
343 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
344 workflow: 'att',
345 withMacLearning: false,
346 withFttb: false,
347 extraHelmFlags: '',
348 localCharts: false,
349 onosReplica: 1,
350 adaptersToWait: 2,
Jan Klareb4789282024-11-15 13:19:10 +0100351 cluster: 'voltha.local',
Gurua339a5a2024-12-22 03:07:45 +0530352 vgcEnabled: 'false',
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400353 ]
354
355 Map cfg = defaultConfig + config
356
357 // Augment config map
358 cfg.volthaStackChart = 'onf/voltha-stack'
359 cfg.bbsimChart = 'onf/bbsim'
360
361 if (cfg.localCharts) {
362 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
363 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
364
365 sh(label : 'HELM: Update voltha-stack deps',
366 script : """
367 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
368 helm dep update
369 popd
370""")
371 }
372
373 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
374 deployVolthaStack(cfg)
375 launchVolthaStack(cfg)
376 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400377 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400378 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400379
Joey Armstrong7987c112022-12-05 12:42:43 -0500380 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800381}
Joey Armstrong7987c112022-12-05 12:42:43 -0500382
383// -----------------------------------------------------------------------
384// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400385def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400386 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400387 enter('main')
388 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500389 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400390 catch (Exception err) { // groovylint-disable-line CatchException
391 ans = false
392 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
393 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500394 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400395 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400396 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500397 }
398 return
399}
400
401// [EOF]