blob: 22d63f668aec2c670dc2c1c2d3d12ad33ddf4433 [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
Joey Armstrong74ec08c2023-08-31 11:25:57 -040053 sh(label : "Create VOLTHA Stack ${cfg.stackName}, (namespace=${cfg.volthaNamespace})",
54 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040055
56helm 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 \
Matteo Scandolo529e8822021-07-21 10:20:18 -070060 --set voltha.onos_classic.replicas=${cfg.onosReplica} \
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""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -080067
Joey Armstrongcd3f0752023-09-11 18:10:12 -040068 for (int i = 0; i < cfg.bbsimReplica; i++) {
Joey Armstrong74ec08c2023-08-31 11:25:57 -040069 // NOTE we don't need to update the tag for DT
70 script {
71 sh(label : "Create config[$i]: bbsimCfg${cfg.stackId}${i}.yaml",
72 script : "rm -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml",
73 )
Joey Armstronge93c3fa2023-09-11 09:34:24 -040074
Joey Armstrong8d12f652023-09-13 18:25:33 -040075 if (cfg.workflow == 'att' || cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040076 int startingStag = 900
Joey Armstrong6f507652023-09-18 10:35:07 -040077 def serviceConfigFile = cfg.workflow // type(?) String
Joey Armstrong74ec08c2023-08-31 11:25:57 -040078 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040079 serviceConfigFile = 'tt-maclearner'
Joey Armstrong74ec08c2023-08-31 11:25:57 -040080 }
Joey Armstrong6f507652023-09-18 10:35:07 -040081 // bbsimCfg: type(?) Map
Joey Armstrong74ec08c2023-08-31 11:25:57 -040082 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
83 // 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 -040084 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -040085 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
86 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
87 } else {
88 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -040089 sh(label : 'DT install',
90 script : """
91cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
92 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
93 """)
94 } // if (cfg)
95 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -040096
Joey Armstrong74ec08c2023-08-31 11:25:57 -040097 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
98 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040099 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -0700100 --set olt_id="${cfg.stackId}${i}" \
101 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
102 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400103""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400104 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400105
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400106 leave('deployVolthaStack')
107 return
108}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700109
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400110// -----------------------------------------------------------------------
111// Intent: Wait until the pod completed, meaning ONOS fully deployed
112// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400113// Todo: Move logic like this into a standalone script.
114// Use jenkins stash or native JJB logic to publish out to nodes.
115// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400116void launchVolthaStack(Map cfg) {
117 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800118
Joey Armstrong358f2242023-09-15 09:49:21 -0400119 /* -----------------------------------------------------------------------
120 * % kubectl get pods
121 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
122 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
123
124 * % kubectl port-forward --address 0.0.0.0
125 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
126 * -----------------------------------------------------------------------
127 */
128 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400129 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400130
131cat <<EOM
132
133** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400134** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400135** -----------------------------------------------------------------------
136EOM
137
138# set -euo pipefail
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500139set +x # # Logs are noisy with set -x
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400140
141declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400142declare -i debug=1 # uncomment to enable debugging
143# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400144vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400145echo > \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400146
Joey Armstrongcfb85942023-09-15 11:47:02 -0400147declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400148while true; do
149
Joey Armstrong358f2242023-09-15 09:49:21 -0400150 # Gather
151 kubectl get pods -n ${cfg.volthaNamespace} \
152 -l app.kubernetes.io/part-of=voltha --no-headers \
153 > \$vsd_log
154
155 count=\$((\$count - 1))
156
157 # Display activity every iteration ?
158 [[ -v verbose ]] && { count=0; }
159
160 # Display activity every minute or so {sleep(5) x count=10}
161 if [[ \$count -lt 1 ]]; then
162 count=10
163 cat \$vsd_log
164 fi
165
166 ## -----------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400167 ## Probe for cluster state
Joey Armstrong358f2242023-09-15 09:49:21 -0400168 ## -----------------------
169 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200170 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrong358f2242023-09-15 09:49:21 -0400171 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400172
Joey Armstrong358f2242023-09-15 09:49:21 -0400173 elif grep -q -e '0/' \$vsd_log; then
174 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
175 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400176
Joey Armstrong358f2242023-09-15 09:49:21 -0400177 elif ! grep -q '/' \$vsd_log; then
178 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
179 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400180
181 # -----------------------------------------------------------------------
182 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
183 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
184 # -----------------------------------------------------------------------
185 elif grep -q 'Error' \$vsd_log; then
186 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
187 cat \$vsd_log
188 rc=1 # fatal
189 break
190
Joey Armstrong358f2242023-09-15 09:49:21 -0400191 # -----------------------------------------------------------------------
192 # An extra conditon needed here but shell coding is tricky:
193 # "svc x/y Running 0 6s
194 # Verify (x == y) && (x > 0)
Joey Armstrong6f507652023-09-18 10:35:07 -0400195 # Wait until job failure/we have an actual need for it.
Joey Armstrong358f2242023-09-15 09:49:21 -0400196 # -----------------------------------------------------------------------
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400197 # Could check for all services 'Running', is that reliable (?)
198 # -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400199 else
200 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
201 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400202 break
203 fi
204
Joey Armstrong358f2242023-09-15 09:49:21 -0400205 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400206 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400207
208done
209rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400210exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400211""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500212
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400213 leave('launchVolthaStack')
214 return
215}
216
217// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400218// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400219// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400220void waitForOnosDeploy(Map cfg) {
221 enter('waitForOnosDeploy')
222
223 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400224 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400225
226cat <<EOM
227
228** -----------------------------------------------------------------------
229** Wait for ONOS full deployment
230** -----------------------------------------------------------------------
231EOM
232
233# set -euo pipefail
234set +x # # Noisy when commented (default: uncommented)
235
236declare -i count=0
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400237declare -i debug=1 # uncomment to enable debugging
Joey Armstrong7f6c2c02024-02-11 14:15:24 -0500238declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400239vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400240echo > \$vsd_log
241
242declare -i rc=0 # exit status
Joey Armstrong8d12f652023-09-13 18:25:33 -0400243while true; do
244
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400245 # Gather -- should we check for count(svc > 1) ?
246 kubectl get pods -l app=onos-config-loader \
247 -n ${cfg.infraNamespace} --no-headers \
248 --field-selector=status.phase=Running \
249 > \$vsd_log
250
251 count=\$((\$count - 1))
252
253 # Display activity every iteration ?
254 [[ -v verbose ]] && { count=0; }
255
256 # Display activity every minute or so {sleep(5) x count=10}
257 if [[ \$count -lt 1 ]]; then
258 count=10
259 cat \$vsd_log
260 fi
261
262 ## -----------------------
263 ## Probe for cluster state
264 ## -----------------------
265 if grep -q -e 'ContainerCreating' \$vsd_log; then
Jan Klare415f3b72023-10-25 09:23:15 +0200266 echo -e '\nvolthaStackDeploy.groovy: ContainerCreating active'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400267 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
268
269 elif grep -q -e '0/' \$vsd_log; then
270 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
271 [[ -v debug ]] && grep -e '0/' \$vsd_log
272
273 elif ! grep -q '/' \$vsd_log; then
274 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
275 [[ ! -v verbose ]] && { cat \$vsd_log; }
276
277 # -----------------------------------------------------------------------
278 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
279 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
280 # -----------------------------------------------------------------------
281 elif grep -q 'Error' \$vsd_log; then
282 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
283 cat \$vsd_log
284 rc=1 # fatal
285 break
286
287 # -----------------------------------------------------------------------
288 # An extra conditon needed here but shell coding is tricky:
289 # "svc x/y Running 0 6s
290 # Verify (x == y) && (x > 0)
291 # Wait until job failure/we have an actual need for it.
292 # -----------------------------------------------------------------------
293 # Could check for all services 'Running', is that reliable (?)
294 # -----------------------------------------------------------------------
295 else
296 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
297 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400298 break
299 fi
300
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400301 ## Support argument --timeout (?)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400302 sleep 5
Joey Armstrong8d12f652023-09-13 18:25:33 -0400303
Joey Armstrong8d12f652023-09-13 18:25:33 -0400304done
305rm -f \$vsd_log
Joey Armstrong8c347122023-09-18 17:04:49 -0400306exit \$rc
Joey Armstrong8d12f652023-09-13 18:25:33 -0400307""")
308
309 leave('waitForOnosDeploy')
310 return
311}
312
313// -----------------------------------------------------------------------
314// -----------------------------------------------------------------------
315void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400316 enter('process')
317
318 // note that I can't define this outside the function as there's no global scope in Groovy
319 Map defaultConfig = [
320 bbsimReplica: 1,
321 infraNamespace: 'infra',
322 volthaNamespace: 'voltha',
323 stackName: 'voltha',
324 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
325 workflow: 'att',
326 withMacLearning: false,
327 withFttb: false,
328 extraHelmFlags: '',
329 localCharts: false,
330 onosReplica: 1,
331 adaptersToWait: 2,
Jan Klareb4789282024-11-15 13:19:10 +0100332 cluster: 'voltha.local',
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400333 ]
334
335 Map cfg = defaultConfig + config
336
337 // Augment config map
338 cfg.volthaStackChart = 'onf/voltha-stack'
339 cfg.bbsimChart = 'onf/bbsim'
340
341 if (cfg.localCharts) {
342 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
343 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
344
345 sh(label : 'HELM: Update voltha-stack deps',
346 script : """
347 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
348 helm dep update
349 popd
350""")
351 }
352
353 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
354 deployVolthaStack(cfg)
355 launchVolthaStack(cfg)
356 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400357 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400358 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400359
Joey Armstrong7987c112022-12-05 12:42:43 -0500360 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800361}
Joey Armstrong7987c112022-12-05 12:42:43 -0500362
363// -----------------------------------------------------------------------
364// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400365def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400366 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400367 enter('main')
368 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500369 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400370 catch (Exception err) { // groovylint-disable-line CatchException
371 ans = false
372 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
373 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500374 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400375 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400376 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500377 }
378 return
379}
380
381// [EOF]