blob: 1705ce0959935ad2271362f1297281d0a1f73461 [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong7987c112022-12-05 12:42:43 -05002// -----------------------------------------------------------------------
Joey Armstrongaf679da2023-01-31 14:22:41 -05003// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
4//
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} \
Jan Klare7b065232023-09-22 16:23:48 +020061 --set voltha.ingress.enabled=true
62 --set voltha.ingress.enableVirtualHosts=true
Matteo Scandolo42f6e572021-01-25 15:11:34 -080063 --set global.voltha_infra_namespace=${cfg.infraNamespace} \
Matteo Scandoloed1afdd2021-04-02 16:25:45 -070064 ${cfg.extraHelmFlags}
Joey Armstrongcd3f0752023-09-11 18:10:12 -040065""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -080066
Joey Armstrongcd3f0752023-09-11 18:10:12 -040067 for (int i = 0; i < cfg.bbsimReplica; i++) {
Joey Armstrong74ec08c2023-08-31 11:25:57 -040068 // NOTE we don't need to update the tag for DT
69 script {
70 sh(label : "Create config[$i]: bbsimCfg${cfg.stackId}${i}.yaml",
71 script : "rm -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml",
72 )
Joey Armstronge93c3fa2023-09-11 09:34:24 -040073
Joey Armstrong8d12f652023-09-13 18:25:33 -040074 if (cfg.workflow == 'att' || cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040075 int startingStag = 900
Joey Armstrong6f507652023-09-18 10:35:07 -040076 def serviceConfigFile = cfg.workflow // type(?) String
Joey Armstrong74ec08c2023-08-31 11:25:57 -040077 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040078 serviceConfigFile = 'tt-maclearner'
Joey Armstrong74ec08c2023-08-31 11:25:57 -040079 }
Joey Armstrong6f507652023-09-18 10:35:07 -040080 // bbsimCfg: type(?) Map
Joey Armstrong74ec08c2023-08-31 11:25:57 -040081 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
82 // 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 -040083 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -040084 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
85 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
86 } else {
87 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -040088 sh(label : 'DT install',
89 script : """
90cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
91 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
92 """)
93 } // if (cfg)
94 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -040095
Joey Armstrong74ec08c2023-08-31 11:25:57 -040096 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
97 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040098 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -070099 --set olt_id="${cfg.stackId}${i}" \
100 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
101 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400102""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400103 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400104
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400105 leave('deployVolthaStack')
106 return
107}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700108
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400109// -----------------------------------------------------------------------
110// Intent: Wait until the pod completed, meaning ONOS fully deployed
111// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400112// Todo: Move logic like this into a standalone script.
113// Use jenkins stash or native JJB logic to publish out to nodes.
114// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400115void launchVolthaStack(Map cfg) {
116 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800117
Joey Armstrong358f2242023-09-15 09:49:21 -0400118 /* -----------------------------------------------------------------------
119 * % kubectl get pods
120 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
121 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
122
123 * % kubectl port-forward --address 0.0.0.0
124 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
125 * -----------------------------------------------------------------------
126 */
127 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400128 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400129
130cat <<EOM
131
132** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400133** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400134** -----------------------------------------------------------------------
135EOM
136
137# set -euo pipefail
Joey Armstrong358f2242023-09-15 09:49:21 -0400138set +x # # Logs are noisy when commented
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400139
140declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400141declare -i debug=1 # uncomment to enable debugging
142# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400143vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400144echo > \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400145
Joey Armstrongcfb85942023-09-15 11:47:02 -0400146declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400147while true; do
148
Joey Armstrong358f2242023-09-15 09:49:21 -0400149 # Gather
150 kubectl get pods -n ${cfg.volthaNamespace} \
151 -l app.kubernetes.io/part-of=voltha --no-headers \
152 > \$vsd_log
153
154 count=\$((\$count - 1))
155
156 # Display activity every iteration ?
157 [[ -v verbose ]] && { count=0; }
158
159 # Display activity every minute or so {sleep(5) x count=10}
160 if [[ \$count -lt 1 ]]; then
161 count=10
162 cat \$vsd_log
163 fi
164
165 ## -----------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400166 ## Probe for cluster state
Joey Armstrong358f2242023-09-15 09:49:21 -0400167 ## -----------------------
168 if grep -q -e 'ContainerCreating' \$vsd_log; then
169 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
170 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400171
Joey Armstrong358f2242023-09-15 09:49:21 -0400172 elif grep -q -e '0/' \$vsd_log; then
173 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
174 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400175
Joey Armstrong358f2242023-09-15 09:49:21 -0400176 elif ! grep -q '/' \$vsd_log; then
177 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
178 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400179
180 # -----------------------------------------------------------------------
181 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
182 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
183 # -----------------------------------------------------------------------
184 elif grep -q 'Error' \$vsd_log; then
185 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
186 cat \$vsd_log
187 rc=1 # fatal
188 break
189
Joey Armstrong358f2242023-09-15 09:49:21 -0400190 # -----------------------------------------------------------------------
191 # An extra conditon needed here but shell coding is tricky:
192 # "svc x/y Running 0 6s
193 # Verify (x == y) && (x > 0)
Joey Armstrong6f507652023-09-18 10:35:07 -0400194 # Wait until job failure/we have an actual need for it.
Joey Armstrong358f2242023-09-15 09:49:21 -0400195 # -----------------------------------------------------------------------
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400196 # Could check for all services 'Running', is that reliable (?)
197 # -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400198 else
199 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
200 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400201 break
202 fi
203
Joey Armstrong358f2242023-09-15 09:49:21 -0400204 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400205 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400206
207done
208rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400209exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400210""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500211
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400212 leave('launchVolthaStack')
213 return
214}
215
216// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400217// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400218// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400219void waitForOnosDeploy(Map cfg) {
220 enter('waitForOnosDeploy')
221
222 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400223 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400224
225cat <<EOM
226
227** -----------------------------------------------------------------------
228** Wait for ONOS full deployment
229** -----------------------------------------------------------------------
230EOM
231
232# set -euo pipefail
233set +x # # Noisy when commented (default: uncommented)
234
235declare -i count=0
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400236declare -i debug=1 # uncomment to enable debugging
237# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400238vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400239echo > \$vsd_log
240
241declare -i rc=0 # exit status
Joey Armstrong8d12f652023-09-13 18:25:33 -0400242while true; do
243
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400244 # Gather -- should we check for count(svc > 1) ?
245 kubectl get pods -l app=onos-config-loader \
246 -n ${cfg.infraNamespace} --no-headers \
247 --field-selector=status.phase=Running \
248 > \$vsd_log
249
250 count=\$((\$count - 1))
251
252 # Display activity every iteration ?
253 [[ -v verbose ]] && { count=0; }
254
255 # Display activity every minute or so {sleep(5) x count=10}
256 if [[ \$count -lt 1 ]]; then
257 count=10
258 cat \$vsd_log
259 fi
260
261 ## -----------------------
262 ## Probe for cluster state
263 ## -----------------------
264 if grep -q -e 'ContainerCreating' \$vsd_log; then
265 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
266 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
267
268 elif grep -q -e '0/' \$vsd_log; then
269 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
270 [[ -v debug ]] && grep -e '0/' \$vsd_log
271
272 elif ! grep -q '/' \$vsd_log; then
273 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
274 [[ ! -v verbose ]] && { cat \$vsd_log; }
275
276 # -----------------------------------------------------------------------
277 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
278 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
279 # -----------------------------------------------------------------------
280 elif grep -q 'Error' \$vsd_log; then
281 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
282 cat \$vsd_log
283 rc=1 # fatal
284 break
285
286 # -----------------------------------------------------------------------
287 # An extra conditon needed here but shell coding is tricky:
288 # "svc x/y Running 0 6s
289 # Verify (x == y) && (x > 0)
290 # Wait until job failure/we have an actual need for it.
291 # -----------------------------------------------------------------------
292 # Could check for all services 'Running', is that reliable (?)
293 # -----------------------------------------------------------------------
294 else
295 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
296 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400297 break
298 fi
299
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400300 ## Support argument --timeout (?)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400301 sleep 5
Joey Armstrong8d12f652023-09-13 18:25:33 -0400302
Joey Armstrong8d12f652023-09-13 18:25:33 -0400303done
304rm -f \$vsd_log
Joey Armstrong8c347122023-09-18 17:04:49 -0400305exit \$rc
Joey Armstrong8d12f652023-09-13 18:25:33 -0400306""")
307
308 leave('waitForOnosDeploy')
309 return
310}
311
312// -----------------------------------------------------------------------
313// -----------------------------------------------------------------------
314void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400315 enter('process')
316
317 // note that I can't define this outside the function as there's no global scope in Groovy
318 Map defaultConfig = [
319 bbsimReplica: 1,
320 infraNamespace: 'infra',
321 volthaNamespace: 'voltha',
322 stackName: 'voltha',
323 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
324 workflow: 'att',
325 withMacLearning: false,
326 withFttb: false,
327 extraHelmFlags: '',
328 localCharts: false,
329 onosReplica: 1,
330 adaptersToWait: 2,
331 ]
332
333 Map cfg = defaultConfig + config
334
335 // Augment config map
336 cfg.volthaStackChart = 'onf/voltha-stack'
337 cfg.bbsimChart = 'onf/bbsim'
338
339 if (cfg.localCharts) {
340 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
341 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
342
343 sh(label : 'HELM: Update voltha-stack deps',
344 script : """
345 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
346 helm dep update
347 popd
348""")
349 }
350
351 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
352 deployVolthaStack(cfg)
353 launchVolthaStack(cfg)
354 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400355 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400356 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400357
Joey Armstrong7987c112022-12-05 12:42:43 -0500358 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800359}
Joey Armstrong7987c112022-12-05 12:42:43 -0500360
361// -----------------------------------------------------------------------
362// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400363def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400364 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400365 enter('main')
366 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500367 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400368 catch (Exception err) { // groovylint-disable-line CatchException
369 ans = false
370 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
371 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500372 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400373 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400374 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500375 }
376 return
377}
378
379// [EOF]