blob: 452fb7d450d783d38cc13af046df8747364860eb [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 Armstronge93c3fa2023-09-11 09:34:24 -0400143while true; do
144
Joey Armstrong358f2242023-09-15 09:49:21 -0400145 # Gather
146 kubectl get pods -n ${cfg.volthaNamespace} \
147 -l app.kubernetes.io/part-of=voltha --no-headers \
148 > \$vsd_log
149
150 count=\$((\$count - 1))
151
152 # Display activity every iteration ?
153 [[ -v verbose ]] && { count=0; }
154
155 # Display activity every minute or so {sleep(5) x count=10}
156 if [[ \$count -lt 1 ]]; then
157 count=10
158 cat \$vsd_log
159 fi
160
161 ## -----------------------
162 ## Probe for cluster state
163 ## -----------------------
164 if grep -q -e 'ContainerCreating' \$vsd_log; then
165 echo -e '\nvolthaStackDeploy.groovy: ContainerCrating active'
166 [[ -v debug ]] && grep -e 'ContainerCreating' \$vsd_log
167 elif grep -q -e '0/' \$vsd_log; then
168 echo -e '\nvolthaStackDeploy.groovy: Waiting for status=Running'
169 [[ -v debug ]] && grep -e '0/' \$vsd_log
170 elif ! grep -q '/' \$vsd_log; then
171 echo -e '\nvolthaStackDeploy.groovy: Waiting for initial pod activity'
172 [[ ! -v verbose ]] && { cat \$vsd_log; }
173 # -----------------------------------------------------------------------
174 # An extra conditon needed here but shell coding is tricky:
175 # "svc x/y Running 0 6s
176 # Verify (x == y) && (x > 0)
177 # Wait until job failure/we have an actual need for it.
178 # -----------------------------------------------------------------------
179 else
180 echo -e '\nvolthaStackDeploy.groovy: Voltha stack has launched'
181 [[ ! -v verbose ]] && { cat \$vsd_log; }
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400182 break
183 fi
184
Joey Armstrong358f2242023-09-15 09:49:21 -0400185 ## Support argument --timeout (?)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400186 sleep 5
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400187
188done
189rm -f \$vsd_log
190""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500191
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400192 leave('launchVolthaStack')
193 return
194}
195
196// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400197// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400198// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400199void waitForOnosDeploy(Map cfg) {
200 enter('waitForOnosDeploy')
201
202 sh(label : 'Wait for ONOS full deployment',
203 Scriptx : """
204
205cat <<EOM
206
207** -----------------------------------------------------------------------
208** Wait for ONOS full deployment
209** -----------------------------------------------------------------------
210EOM
211
212# set -euo pipefail
213set +x # # Noisy when commented (default: uncommented)
214
215declare -i count=0
Joey Armstrong8da08942023-09-14 13:52:49 -0400216vsd_log='volthaStackDeploy.tmp'
Joey Armstrong8d12f652023-09-13 18:25:33 -0400217touch \$vsd_log
218while true; do
219
220 ## Exit when the server begins showing signs of life
221 if grep -q '0/' \$vsd_log; then
222 echo 'volthaStackDeploy.groovy: Detected kubectl pods =~ 0/'
223 grep '0/' \$vsd_log
224 break
225 fi
226
227 sleep 5
228 count=\$((\$count - 1))
229
230 if [[ \$count -lt 1 ]]; then # [DEBUG] Display activity every minute or so
231 count=10
232 kubectl get pods -l app=onos-config-loader \
233 -n ${cfg.infraNamespace} --no-headers \
234 --field-selector=status.phase=Running \
235 | tee \$vsd_log
236 else
237 kubectl get pods -l app=onos-config-loader \
238 -n ${cfg.infraNamespace} --no-headers \
239 --field-selector=status.phase=Running \
240 > \$vsd_log
241 fi
242
243done
244rm -f \$vsd_log
245""")
246
247 leave('waitForOnosDeploy')
248 return
249}
250
251// -----------------------------------------------------------------------
252// -----------------------------------------------------------------------
253void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400254 enter('process')
255
256 // note that I can't define this outside the function as there's no global scope in Groovy
257 Map defaultConfig = [
258 bbsimReplica: 1,
259 infraNamespace: 'infra',
260 volthaNamespace: 'voltha',
261 stackName: 'voltha',
262 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
263 workflow: 'att',
264 withMacLearning: false,
265 withFttb: false,
266 extraHelmFlags: '',
267 localCharts: false,
268 onosReplica: 1,
269 adaptersToWait: 2,
270 ]
271
272 Map cfg = defaultConfig + config
273
274 // Augment config map
275 cfg.volthaStackChart = 'onf/voltha-stack'
276 cfg.bbsimChart = 'onf/bbsim'
277
278 if (cfg.localCharts) {
279 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
280 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
281
282 sh(label : 'HELM: Update voltha-stack deps',
283 script : """
284 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
285 helm dep update
286 popd
287""")
288 }
289
290 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
291 deployVolthaStack(cfg)
292 launchVolthaStack(cfg)
293 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400294 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400295 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400296
Joey Armstrong7987c112022-12-05 12:42:43 -0500297 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800298}
Joey Armstrong7987c112022-12-05 12:42:43 -0500299
300// -----------------------------------------------------------------------
301// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400302def call(Map config=[:]) {
303 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400304 enter('main')
305 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500306 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400307 catch (Exception err) { // groovylint-disable-line CatchException
308 ans = false
309 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
310 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500311 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400312 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400313 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500314 }
315 return
316}
317
318// [EOF]