blob: 9b198e50abc676a351a3b7c90feca818a20c97fb [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 Armstrong74ec08c2023-08-31 11:25:57 -040074 def serviceConfigFile = cfg.workflow
75 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 }
78 def bbsimCfg = readYaml file: "$WORKSPACE/voltha-helm-charts/examples/${serviceConfigFile}-values.yaml"
79 // 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 -040080 bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i
Joey Armstrong74ec08c2023-08-31 11:25:57 -040081 println "Using BBSim Service config ${bbsimCfg['servicesConfig']}"
82 writeYaml file: "$WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml", data: bbsimCfg
83 } else {
84 // NOTE if it's DT just copy the file over
Joey Armstrongcd3f0752023-09-11 18:10:12 -040085 sh(label : 'DT install',
86 script : """
87cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \
88 $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml
89 """)
90 } // if (cfg)
91 } // script
Joey Armstronge93c3fa2023-09-11 09:34:24 -040092
Joey Armstrong74ec08c2023-08-31 11:25:57 -040093 sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}",
94 script : """
Joey Armstrongcd3f0752023-09-11 18:10:12 -040095 helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \
Matteo Scandolo0ce69f12021-05-04 08:44:53 -070096 --set olt_id="${cfg.stackId}${i}" \
97 -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \
98 ${cfg.extraHelmFlags}
Joey Armstronge93c3fa2023-09-11 09:34:24 -040099""")
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400100 } // for
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400101
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400102 leave('deployVolthaStack')
103 return
104}
Matteo Scandolo937f4792021-09-24 11:05:52 -0700105
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400106// -----------------------------------------------------------------------
107// Intent: Wait until the pod completed, meaning ONOS fully deployed
108// -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400109// Todo: Move logic like this into a standalone script.
110// Use jenkins stash or native JJB logic to publish out to nodes.
111// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400112void launchVolthaStack(Map cfg) {
113 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800114
Joey Armstrong358f2242023-09-15 09:49:21 -0400115 /* -----------------------------------------------------------------------
116 * % kubectl get pods
117 17:40:15 bbsim0-868479698c-z66mk 0/1 ContainerCreating 0 6s
118 17:40:15 voltha-voltha-adapter-openolt-68c84bf786-z98rh 0/1 Running 0 8s
119
120 * % kubectl port-forward --address 0.0.0.0
121 17:40:15 error: unable to forward port because pod is not running. Current status=Pending
122 * -----------------------------------------------------------------------
123 */
124 sh(label : "Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start",
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400125 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400126
127cat <<EOM
128
129** -----------------------------------------------------------------------
Joey Armstrong358f2242023-09-15 09:49:21 -0400130** Wait for VOLTHA Stack (stack=${cfg.stackName}, namespace=${cfg.volthaNamespace}) to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400131** -----------------------------------------------------------------------
132EOM
133
134# set -euo pipefail
Joey Armstrong358f2242023-09-15 09:49:21 -0400135set +x # # Logs are noisy when commented
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400136
137declare -i count=0
Joey Armstrong358f2242023-09-15 09:49:21 -0400138declare -i debug=1 # uncomment to enable debugging
139# declare -i verbose=1 # uncomment to enable debugging
Joey Armstrong8da08942023-09-14 13:52:49 -0400140vsd_log='volthaStackDeploy.tmp'
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400141touch \$vsd_log
Joey Armstrong358f2242023-09-15 09:49:21 -0400142
Joey Armstrongcfb85942023-09-15 11:47:02 -0400143declare -i rc=0 # exit status
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400144while true; do
145
Joey Armstrong358f2242023-09-15 09:49:21 -0400146 # Gather
147 kubectl get pods -n ${cfg.volthaNamespace} \
148 -l app.kubernetes.io/part-of=voltha --no-headers \
149 > \$vsd_log
150
151 count=\$((\$count - 1))
152
153 # Display activity every iteration ?
154 [[ -v verbose ]] && { count=0; }
155
156 # Display activity every minute or so {sleep(5) x count=10}
157 if [[ \$count -lt 1 ]]; then
158 count=10
159 cat \$vsd_log
160 fi
161
162 ## -----------------------
163 ## Probe for cluster state
164 ## -----------------------
165 if grep -q -e 'ContainerCreating' \$vsd_log; then
166 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
167 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400168
Joey Armstrong358f2242023-09-15 09:49:21 -0400169 elif grep -q -e '0/' \$vsd_log; then
170 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
171 [[ -v debug ]] && grep -e '0/' \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400172
Joey Armstrong358f2242023-09-15 09:49:21 -0400173 elif ! grep -q '/' \$vsd_log; then
174 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
175 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstrongcfb85942023-09-15 11:47:02 -0400176
177 # -----------------------------------------------------------------------
178 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 CrashLoopBackOff 2 69s
179 # voltha-adapter-openolt-68c84bf786-8xsfc 0/1 Error 3 85s
180 # -----------------------------------------------------------------------
181 elif grep -q 'Error' \$vsd_log; then
182 echo -e '\nvolthaStackDeploy.groovy: Detected cluster state=Error'
183 cat \$vsd_log
184 rc=1 # fatal
185 break
186
Joey Armstrong358f2242023-09-15 09:49:21 -0400187 # -----------------------------------------------------------------------
188 # An extra conditon needed here but shell coding is tricky:
189 # "svc x/y Running 0 6s
190 # Verify (x == y) && (x > 0)
191 # Wait until job failure/we have an actual need for it.
192 # -----------------------------------------------------------------------
193 else
194 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
195 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400196 break
197 fi
198
Joey Armstrong358f2242023-09-15 09:49:21 -0400199 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400200 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400201
202done
203rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400204exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400205""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500206
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400207 leave('launchVolthaStack')
208 return
209}
210
211// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400212// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400213// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400214void waitForOnosDeploy(Map cfg) {
215 enter('waitForOnosDeploy')
216
217 sh(label : 'Wait for ONOS full deployment',
218 Scriptx : """
219
220cat <<EOM
221
222** -----------------------------------------------------------------------
223** Wait for ONOS full deployment
224** -----------------------------------------------------------------------
225EOM
226
227# set -euo pipefail
228set +x # # Noisy when commented (default: uncommented)
229
230declare -i count=0
Joey Armstrong8da08942023-09-14 13:52:49 -0400231vsd_log='volthaStackDeploy.tmp'
Joey Armstrong8d12f652023-09-13 18:25:33 -0400232touch \$vsd_log
233while true; do
234
235 ## Exit when the server begins showing signs of life
236 if grep -q '0/' \$vsd_log; then
237 echo 'volthaStackDeploy.groovy: Detected kubectl pods =~ 0/'
238 grep '0/' \$vsd_log
239 break
240 fi
241
242 sleep 5
243 count=\$((\$count - 1))
244
245 if [[ \$count -lt 1 ]]; then # [DEBUG] Display activity every minute or so
246 count=10
247 kubectl get pods -l app=onos-config-loader \
248 -n ${cfg.infraNamespace} --no-headers \
249 --field-selector=status.phase=Running \
250 | tee \$vsd_log
251 else
252 kubectl get pods -l app=onos-config-loader \
253 -n ${cfg.infraNamespace} --no-headers \
254 --field-selector=status.phase=Running \
255 > \$vsd_log
256 fi
257
258done
259rm -f \$vsd_log
260""")
261
262 leave('waitForOnosDeploy')
263 return
264}
265
266// -----------------------------------------------------------------------
267// -----------------------------------------------------------------------
268void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400269 enter('process')
270
271 // note that I can't define this outside the function as there's no global scope in Groovy
272 Map defaultConfig = [
273 bbsimReplica: 1,
274 infraNamespace: 'infra',
275 volthaNamespace: 'voltha',
276 stackName: 'voltha',
277 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
278 workflow: 'att',
279 withMacLearning: false,
280 withFttb: false,
281 extraHelmFlags: '',
282 localCharts: false,
283 onosReplica: 1,
284 adaptersToWait: 2,
285 ]
286
287 Map cfg = defaultConfig + config
288
289 // Augment config map
290 cfg.volthaStackChart = 'onf/voltha-stack'
291 cfg.bbsimChart = 'onf/bbsim'
292
293 if (cfg.localCharts) {
294 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
295 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
296
297 sh(label : 'HELM: Update voltha-stack deps',
298 script : """
299 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
300 helm dep update
301 popd
302""")
303 }
304
305 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
306 deployVolthaStack(cfg)
307 launchVolthaStack(cfg)
308 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400309 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400310 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400311
Joey Armstrong7987c112022-12-05 12:42:43 -0500312 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800313}
Joey Armstrong7987c112022-12-05 12:42:43 -0500314
315// -----------------------------------------------------------------------
316// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400317def call(Map config=[:]) {
318 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400319 enter('main')
320 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500321 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400322 catch (Exception err) { // groovylint-disable-line CatchException
323 ans = false
324 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
325 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500326 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400327 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400328 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500329 }
330 return
331}
332
333// [EOF]