blob: d432e030c4536eff8ad60cd511fbc440504ec7f1 [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// -----------------------------------------------------------------------
109void launchVolthaStack(Map cfg) {
110 enter('launchVolthaStack')
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800111
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400112 sh(label : "Wait for VOLTHA Stack ${cfg.stackName}::${cfg.volthaNamespace} to start",
113 script : """
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400114
115cat <<EOM
116
117** -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400118** Wait for VOLTHA Stack ${cfg.stackName}::${cfg.volthaNamespace} to start
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400119** -----------------------------------------------------------------------
120EOM
121
122# set -euo pipefail
Joey Armstrongc20bd722023-09-11 15:25:12 -0400123set +x # # Noisy when commented (default: uncommented)
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400124
125declare -i count=0
Joey Armstrong8da08942023-09-14 13:52:49 -0400126vsd_log='volthaStackDeploy.tmp'
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400127touch \$vsd_log
128while true; do
129
130 ## Exit when the server begins showing signs of life
131 if grep -q '0/' \$vsd_log; then
132 echo 'volthaStackDeploy.groovy: Detected kubectl pods =~ 0/'
133 grep '0/' \$vsd_log
134 break
135 fi
136
137 sleep 5
138 count=\$((\$count - 1))
139
140 if [[ \$count -lt 1 ]]; then # [DEBUG] Display activity every minute or so
141 count=10
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400142 kubectl get pods -n ${cfg.volthaNamespace} \
143 -l app.kubernetes.io/part-of=voltha --no-headers \
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400144 | tee \$vsd_log
145 else
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400146 kubectl get pods -n ${cfg.volthaNamespace} \
147 -l app.kubernetes.io/part-of=voltha --no-headers \
148 > \$vsd_log
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400149 fi
150
151done
152rm -f \$vsd_log
153""")
Joey Armstrong96158a92022-11-25 10:36:06 -0500154
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400155 leave('launchVolthaStack')
156 return
157}
158
159// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400160// Intent: Wait until the pod completed, meaning ONOS fully deployed
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400161// -----------------------------------------------------------------------
Joey Armstrong8d12f652023-09-13 18:25:33 -0400162void waitForOnosDeploy(Map cfg) {
163 enter('waitForOnosDeploy')
164
165 sh(label : 'Wait for ONOS full deployment',
166 Scriptx : """
167
168cat <<EOM
169
170** -----------------------------------------------------------------------
171** Wait for ONOS full deployment
172** -----------------------------------------------------------------------
173EOM
174
175# set -euo pipefail
176set +x # # Noisy when commented (default: uncommented)
177
178declare -i count=0
Joey Armstrong8da08942023-09-14 13:52:49 -0400179vsd_log='volthaStackDeploy.tmp'
Joey Armstrong8d12f652023-09-13 18:25:33 -0400180touch \$vsd_log
181while true; do
182
183 ## Exit when the server begins showing signs of life
184 if grep -q '0/' \$vsd_log; then
185 echo 'volthaStackDeploy.groovy: Detected kubectl pods =~ 0/'
186 grep '0/' \$vsd_log
187 break
188 fi
189
190 sleep 5
191 count=\$((\$count - 1))
192
193 if [[ \$count -lt 1 ]]; then # [DEBUG] Display activity every minute or so
194 count=10
195 kubectl get pods -l app=onos-config-loader \
196 -n ${cfg.infraNamespace} --no-headers \
197 --field-selector=status.phase=Running \
198 | tee \$vsd_log
199 else
200 kubectl get pods -l app=onos-config-loader \
201 -n ${cfg.infraNamespace} --no-headers \
202 --field-selector=status.phase=Running \
203 > \$vsd_log
204 fi
205
206done
207rm -f \$vsd_log
208""")
209
210 leave('waitForOnosDeploy')
211 return
212}
213
214// -----------------------------------------------------------------------
215// -----------------------------------------------------------------------
216void process(Map config) {
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400217 enter('process')
218
219 // note that I can't define this outside the function as there's no global scope in Groovy
220 Map defaultConfig = [
221 bbsimReplica: 1,
222 infraNamespace: 'infra',
223 volthaNamespace: 'voltha',
224 stackName: 'voltha',
225 stackId: 1, // NOTE this is used to differentiate between BBSims across multiple stacks
226 workflow: 'att',
227 withMacLearning: false,
228 withFttb: false,
229 extraHelmFlags: '',
230 localCharts: false,
231 onosReplica: 1,
232 adaptersToWait: 2,
233 ]
234
235 Map cfg = defaultConfig + config
236
237 // Augment config map
238 cfg.volthaStackChart = 'onf/voltha-stack'
239 cfg.bbsimChart = 'onf/bbsim'
240
241 if (cfg.localCharts) {
242 cfg.volthaStackChart = "$WORKSPACE/voltha-helm-charts/voltha-stack"
243 cfg.bbsimChart = "$WORKSPACE/voltha-helm-charts/bbsim"
244
245 sh(label : 'HELM: Update voltha-stack deps',
246 script : """
247 pushd $WORKSPACE/voltha-helm-charts/voltha-stack
248 helm dep update
249 popd
250""")
251 }
252
253 println "Deploying VOLTHA Stack with the following parameters: ${cfg}."
254 deployVolthaStack(cfg)
255 launchVolthaStack(cfg)
256 waitForAdapters(cfg)
Joey Armstrong8d12f652023-09-13 18:25:33 -0400257 waitForOnosDeploy(cfg)
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400258 leave('process')
Joey Armstronge93c3fa2023-09-11 09:34:24 -0400259
Joey Armstrong7987c112022-12-05 12:42:43 -0500260 return
Matteo Scandolo42f6e572021-01-25 15:11:34 -0800261}
Joey Armstrong7987c112022-12-05 12:42:43 -0500262
263// -----------------------------------------------------------------------
264// -----------------------------------------------------------------------
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400265def call(Map config=[:]) {
266 try {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400267 enter('main')
268 process(config)
Joey Armstrong7987c112022-12-05 12:42:43 -0500269 }
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400270 catch (Exception err) { // groovylint-disable-line CatchException
271 ans = false
272 println("** volthaStackDeploy.groovy: EXCEPTION ${err}")
273 throw err
Joey Armstrong7987c112022-12-05 12:42:43 -0500274 }
Joey Armstrongcd3f0752023-09-11 18:10:12 -0400275 finally {
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400276 leave('main')
Joey Armstrong7987c112022-12-05 12:42:43 -0500277 }
278 return
279}
280
281// [EOF]