blob: a0c3a8973f520d6d03a4885ff16aea45df44f974 [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 Armstronge93c3fa2023-09-11 09:34:24 -0400142touch \$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 # -----------------------------------------------------------------------
194 else
195 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
196 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400197 break
198 fi
199
Joey Armstrong358f2242023-09-15 09:49:21 -0400200 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400201 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400202
203done
204rm -f \$vsd_log
Joey Armstrongcfb85942023-09-15 11:47:02 -0400205exit \$rc
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400206""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500207
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400208 leave('launchVolthaStack')
209 return
210}
211
212// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400213// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400214// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400215void waitForOnosDeploy(Map cfg) {
216 enter('waitForOnosDeploy')
217
218 sh(label : 'Wait for ONOS full deployment',
Joey Armstrong6f507652023-09-18 10:35:07 -0400219 script : """
Joey Armstrong8d12f652023-09-13 18:25:33 -0400220
221cat <<EOM
222
223** -----------------------------------------------------------------------
224** Wait for ONOS full deployment
225** -----------------------------------------------------------------------
226EOM
227
228# set -euo pipefail
229set +x # # Noisy when commented (default: uncommented)
230
231declare -i count=0
Joey Armstrong8da08942023-09-14 13:52:49 -0400232vsd_log='volthaStackDeploy.tmp'
Joey Armstrong8d12f652023-09-13 18:25:33 -0400233touch \$vsd_log
234while true; do
235
236 ## Exit when the server begins showing signs of life
237 if grep -q '0/' \$vsd_log; then
238 echo 'volthaStackDeploy.groovy: Detected kubectl pods =~ 0/'
239 grep '0/' \$vsd_log
240 break
241 fi
242
243 sleep 5
244 count=\$((\$count - 1))
245
246 if [[ \$count -lt 1 ]]; then # [DEBUG] Display activity every minute or so
247 count=10
248 kubectl get pods -l app=onos-config-loader \
249 -n ${cfg.infraNamespace} --no-headers \
250 --field-selector=status.phase=Running \
251 | tee \$vsd_log
252 else
253 kubectl get pods -l app=onos-config-loader \
254 -n ${cfg.infraNamespace} --no-headers \
255 --field-selector=status.phase=Running \
256 > \$vsd_log
257 fi
258
259done
260rm -f \$vsd_log
261""")
262
263 leave('waitForOnosDeploy')
264 return
265}
266
267// -----------------------------------------------------------------------
268// -----------------------------------------------------------------------
269void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400270 enter('process')
271
272 // note that I can't define this outside the function as there's no global scope in Groovy
273 Map defaultConfig = [
274 bbsimReplica: 1,
275 infraNamespace: 'infra',
276 volthaNamespace: 'voltha',
277 stackName: 'voltha',
278 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
279 workflow: 'att',
280 withMacLearning: false,
281 withFttb: false,
282 extraHelmFlags: '',
283 localCharts: false,
284 onosReplica: 1,
285 adaptersToWait: 2,
286 ]
287
288 Map cfg = defaultConfig + config
289
290 // Augment config map
291 cfg.volthaStackChart = 'onf/voltha-stack'
292 cfg.bbsimChart = 'onf/bbsim'
293
294 if (cfg.localCharts) {
295 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
296 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
297
298 sh(label : 'HELM: Update voltha-stack deps',
299 script : """
300 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
301 helm dep update
302 popd
303""")
304 }
305
306 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
307 deployVolthaStack(cfg)
308 launchVolthaStack(cfg)
309 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400310 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400311 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400312
Joey Armstrong7987c112022-12-05 12:42:43 -0500313 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800314}
Joey Armstrong7987c112022-12-05 12:42:43 -0500315
316// -----------------------------------------------------------------------
317// -----------------------------------------------------------------------
Joey Armstrong6f507652023-09-18 10:35:07 -0400318def call(Map config=[:]) { // Function return type(?)
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400319 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400320 enter('main')
321 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500322 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400323 catch (Exception err) { // groovylint-disable-line CatchException
324 ans = false
325 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
326 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500327 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400328 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400329 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500330 }
331 return
332}
333
334// [EOF]