blob: 36f63407eb007d6dac84e5541b76523cdd495a77 [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} \
Matteo Scandolo42f6e572021-01-25 15:11:34 -080061 --set global.voltha_infra_namespace=${cfg.infraNamespace} \
Matteo Scandoloed1afdd2021-04-02 16:25:45 -070062 ${cfg.extraHelmFlags}
Joey Armstrongcd3f0752023-09-11 18:10:12 -040063""")
Matteo Scandolo42f6e572021-01-25 15:11:34 -080064
Joey Armstrongcd3f0752023-09-11 18:10:12 -040065 for (int i = 0; i < cfg.bbsimReplica; i++) {
Joey Armstrong74ec08c2023-08-31 11:25:57 -040066 // NOTE we don't need to update the tag for DT
67 script {
68 sh(label : "Create config[$i]: bbsimCfg${cfg.stackId}${i}.yaml",
69 script : "rm -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml",
70 )
Joey Armstronge93c3fa2023-09-11 09:34:24 -040071
Joey Armstrong8d12f652023-09-13 18:25:33 -040072 if (cfg.workflow == 'att' || cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040073 int startingStag = 900
Joey Armstrong6f507652023-09-18 10:35:07 -040074 def serviceConfigFile = cfg.workflow // type(?) String
Joey Armstrong74ec08c2023-08-31 11:25:57 -040075 if (cfg.withMacLearning && cfg.workflow == 'tt') {
Joey Armstrongcd3f0752023-09-11 18:10:12 -040076 serviceConfigFile = 'tt-maclearner'
Joey Armstrong74ec08c2023-08-31 11:25:57 -040077 }
Joey Armstrong6f507652023-09-18 10:35:07 -040078 // bbsimCfg: type(?) Map
Joey Armstrong74ec08c2023-08-31 11:25:57 -040079 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
80 // 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 -040081 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -040082 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
83 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
84 } else {
85 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -040086 sh(label : 'DT install',
87 script : """
88cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
89 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
90 """)
91 } // if (cfg)
92 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -040093
Joey Armstrong74ec08c2023-08-31 11:25:57 -040094 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
95 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040096 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -070097 --set olt_id="${cfg.stackId}${i}" \
98 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
99 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400100""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400101 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400102
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400103 leave('deployVolthaStack')
104 return
105}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700106
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400107// -----------------------------------------------------------------------
108// Intent: Wait until the pod completed, meaning ONOS fully deployed
109// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400110// Todo: Move logic like this into a standalone script.
111// Use jenkins stash or native JJB logic to publish out to nodes.
112// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400113void launchVolthaStack(Map cfg) {
114 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800115
Joey Armstrong358f2242023-09-15 09:49:21 -0400116 /* -----------------------------------------------------------------------
117 * % kubectl get pods
118 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
119 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
120
121 * % kubectl port-forward --address 0.0.0.0
122 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
123 * -----------------------------------------------------------------------
124 */
125 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400126 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400127
128cat <<EOM
129
130** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400131** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400132** -----------------------------------------------------------------------
133EOM
134
135# set -euo pipefail
Joey Armstrong358f2242023-09-15 09:49:21 -0400136set +x # # Logs are noisy when commented
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400137
138declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400139declare -i debug=1 # uncomment to enable debugging
140# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400141vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400142echo > \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400143
Joey Armstrongcfb85942023-09-15 11:47:02 -0400144declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400145while true; do
146
Joey Armstrong358f2242023-09-15 09:49:21 -0400147 # Gather
148 kubectl get pods -n ${cfg.volthaNamespace} \
149 -l app.kubernetes.io/part-of=voltha --no-headers \
150 > \$vsd_log
151
152 count=\$((\$count - 1))
153
154 # Display activity every iteration ?
155 [[ -v verbose ]] && { count=0; }
156
157 # Display activity every minute or so {sleep(5) x count=10}
158 if [[ \$count -lt 1 ]]; then
159 count=10
160 cat \$vsd_log
161 fi
162
163 ## -----------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400164 ## Probe for cluster state
Joey Armstrong358f2242023-09-15 09:49:21 -0400165 ## -----------------------
166 if grep -q -e 'ContainerCreating' \$vsd_log; then
167 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
168 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400169
Joey Armstrong358f2242023-09-15 09:49:21 -0400170 elif grep -q -e '0/' \$vsd_log; then
171 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
172 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400173
Joey Armstrong358f2242023-09-15 09:49:21 -0400174 elif ! grep -q '/' \$vsd_log; then
175 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
176 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400177
178 # -----------------------------------------------------------------------
179 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
180 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
181 # -----------------------------------------------------------------------
182 elif grep -q 'Error' \$vsd_log; then
183 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
184 cat \$vsd_log
185 rc=1 # fatal
186 break
187
Joey Armstrong358f2242023-09-15 09:49:21 -0400188 # -----------------------------------------------------------------------
189 # An extra conditon needed here but shell coding is tricky:
190 # "svc x/y Running 0 6s
191 # Verify (x == y) && (x > 0)
Joey Armstrong6f507652023-09-18 10:35:07 -0400192 # Wait until job failure/we have an actual need for it.
Joey Armstrong358f2242023-09-15 09:49:21 -0400193 # -----------------------------------------------------------------------
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400194 # Could check for all services 'Running', is that reliable (?)
195 # -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400196 else
197 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
198 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400199 break
200 fi
201
Joey Armstrong358f2242023-09-15 09:49:21 -0400202 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400203 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400204
205done
206rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400207exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400208""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500209
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400210 leave('launchVolthaStack')
211 return
212}
213
214// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400215// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400216// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400217void waitForOnosDeploy(Map cfg) {
218 enter('waitForOnosDeploy')
219
220 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400221 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400222
223cat <<EOM
224
225** -----------------------------------------------------------------------
226** Wait for ONOS full deployment
227** -----------------------------------------------------------------------
228EOM
229
230# set -euo pipefail
231set +x # # Noisy when commented (default: uncommented)
232
233declare -i count=0
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400234declare -i debug=1 # uncomment to enable debugging
235# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400236vsd_log='volthaStackDeploy.tmp'
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400237echo > \$vsd_log
238
239declare -i rc=0 # exit status
Joey Armstrong8d12f652023-09-13 18:25:33 -0400240while true; do
241
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400242 # Gather -- should we check for count(svc > 1) ?
243 kubectl get pods -l app=onos-config-loader \
244 -n ${cfg.infraNamespace} --no-headers \
245 --field-selector=status.phase=Running \
246 > \$vsd_log
247
248 count=\$((\$count - 1))
249
250 # Display activity every iteration ?
251 [[ -v verbose ]] && { count=0; }
252
253 # Display activity every minute or so {sleep(5) x count=10}
254 if [[ \$count -lt 1 ]]; then
255 count=10
256 cat \$vsd_log
257 fi
258
259 ## -----------------------
260 ## Probe for cluster state
261 ## -----------------------
262 if grep -q -e 'ContainerCreating' \$vsd_log; then
263 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
264 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
265
266 elif grep -q -e '0/' \$vsd_log; then
267 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
268 [[ -v debug ]] && grep -e '0/' \$vsd_log
269
270 elif ! grep -q '/' \$vsd_log; then
271 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
272 [[ ! -v verbose ]] && { cat \$vsd_log; }
273
274 # -----------------------------------------------------------------------
275 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
276 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
277 # -----------------------------------------------------------------------
278 elif grep -q 'Error' \$vsd_log; then
279 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
280 cat \$vsd_log
281 rc=1 # fatal
282 break
283
284 # -----------------------------------------------------------------------
285 # An extra conditon needed here but shell coding is tricky:
286 # "svc x/y Running 0 6s
287 # Verify (x == y) && (x > 0)
288 # Wait until job failure/we have an actual need for it.
289 # -----------------------------------------------------------------------
290 # Could check for all services 'Running', is that reliable (?)
291 # -----------------------------------------------------------------------
292 else
293 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
294 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrong8d12f652023-09-13 18:25:33 -0400295 break
296 fi
297
Joey Armstrongd4af72d2023-09-18 14:23:35 -0400298 ## Support argument --timeout (?)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400299 sleep 5
Joey Armstrong8d12f652023-09-13 18:25:33 -0400300
Joey Armstrong8d12f652023-09-13 18:25:33 -0400301done
302rm -f \$vsd_log
Joey Armstrong8c347122023-09-18 17:04:49 -0400303exit \$rc
Joey Armstrong8d12f652023-09-13 18:25:33 -0400304""")
305
306 leave('waitForOnosDeploy')
307 return
308}
309
310// -----------------------------------------------------------------------
311// -----------------------------------------------------------------------
312void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400313 enter('process')
314
315 // note that I can't define this outside the function as there's no global scope in Groovy
316 Map defaultConfig = [
317 bbsimReplica: 1,
318 infraNamespace: 'infra',
319 volthaNamespace: 'voltha',
320 stackName: 'voltha',
321 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
322 workflow: 'att',
323 withMacLearning: false,
324 withFttb: false,
325 extraHelmFlags: '',
326 localCharts: false,
327 onosReplica: 1,
328 adaptersToWait: 2,
329 ]
330
331 Map cfg = defaultConfig + config
332
333 // Augment config map
334 cfg.volthaStackChart = 'onf/voltha-stack'
335 cfg.bbsimChart = 'onf/bbsim'
336
337 if (cfg.localCharts) {
338 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
339 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
340
341 sh(label : 'HELM: Update voltha-stack deps',
342 script : """
343 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
344 helm dep update
345 popd
346""")
347 }
348
349 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
350 deployVolthaStack(cfg)
351 launchVolthaStack(cfg)
352 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400353 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400354 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400355
Joey Armstrong7987c112022-12-05 12:42:43 -0500356 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800357}
Joey Armstrong7987c112022-12-05 12:42:43 -0500358
359// -----------------------------------------------------------------------
360// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400361def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400362 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400363 enter('main')
364 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500365 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400366 catch (Exception err) { // groovylint-disable-line CatchException
367 ans = false
368 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
369 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500370 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400371 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400372 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500373 }
374 return
375}
376
377// [EOF]