Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | af679da | 2023-01-31 14:22:41 -0500 | [diff] [blame] | 3 | // 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 Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 19 | // ----------------------------------------------------------------------- |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 20 | String getIam(String func) { |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 21 | // 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 Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 26 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 27 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 28 | // Intent: Log progress message |
| 29 | // ----------------------------------------------------------------------- |
| 30 | void 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 | // ----------------------------------------------------------------------- |
| 40 | void leave(String name) { |
| 41 | // Announce ourselves for log usability |
| 42 | String iam = getIam(name) |
| 43 | println("${iam}: LEAVE") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | // ----------------------------------------------------------------------- |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 48 | // Intent: |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 49 | // ----------------------------------------------------------------------- |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 50 | void deployVolthaStack(Map cfg) { |
| 51 | enter('deployVolthaStack') |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 52 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 53 | sh(label : "Create VOLTHA Stack ${cfg.stackName}, (namespace=${cfg.volthaNamespace})", |
| 54 | script : """ |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 55 | |
| 56 | helm upgrade --install --create-namespace \ |
| 57 | -n ${cfg.volthaNamespace} ${cfg.stackName} ${cfg.volthaStackChart} \ |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 58 | --set global.stack_name=${cfg.stackName} \ |
| 59 | --set global.voltha_infra_name=voltha-infra \ |
Matteo Scandolo | 529e882 | 2021-07-21 10:20:18 -0700 | [diff] [blame] | 60 | --set voltha.onos_classic.replicas=${cfg.onosReplica} \ |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 61 | --set global.voltha_infra_namespace=${cfg.infraNamespace} \ |
Matteo Scandolo | ed1afdd | 2021-04-02 16:25:45 -0700 | [diff] [blame] | 62 | ${cfg.extraHelmFlags} |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 63 | """) |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 64 | |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 65 | for (int i = 0; i < cfg.bbsimReplica; i++) { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 66 | // 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 Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 71 | |
Joey Armstrong | 8d12f65 | 2023-09-13 18:25:33 -0400 | [diff] [blame] | 72 | if (cfg.workflow == 'att' || cfg.workflow == 'tt') { |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 73 | int startingStag = 900 |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 74 | def serviceConfigFile = cfg.workflow |
| 75 | if (cfg.withMacLearning && cfg.workflow == 'tt') { |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 76 | serviceConfigFile = 'tt-maclearner' |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 77 | } |
| 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 Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 80 | bbsimCfg['servicesConfig']['services'][0]['s_tag'] = startingStag + i |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 81 | 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 Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 85 | sh(label : 'DT install', |
| 86 | script : """ |
| 87 | cp $WORKSPACE/voltha-helm-charts/examples/${cfg.workflow}-values.yaml \ |
| 88 | $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml |
| 89 | """) |
| 90 | } // if (cfg) |
| 91 | } // script |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 92 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 93 | sh(label : "HELM: Create namespace=${cfg.volthaNamespace} bbsim${i}", |
| 94 | script : """ |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 95 | helm upgrade --install --create-namespace -n ${cfg.volthaNamespace} bbsim${i} ${cfg.bbsimChart} \ |
Matteo Scandolo | 0ce69f1 | 2021-05-04 08:44:53 -0700 | [diff] [blame] | 96 | --set olt_id="${cfg.stackId}${i}" \ |
| 97 | -f $WORKSPACE/bbsimCfg${cfg.stackId}${i}.yaml \ |
| 98 | ${cfg.extraHelmFlags} |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 99 | """) |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 100 | } // for |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 101 | |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 102 | leave('deployVolthaStack') |
| 103 | return |
| 104 | } |
Matteo Scandolo | 937f479 | 2021-09-24 11:05:52 -0700 | [diff] [blame] | 105 | |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 106 | // ----------------------------------------------------------------------- |
| 107 | // Intent: Wait until the pod completed, meaning ONOS fully deployed |
| 108 | // ----------------------------------------------------------------------- |
| 109 | void launchVolthaStack(Map cfg) { |
| 110 | enter('launchVolthaStack') |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 111 | |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 112 | sh(label : "Wait for VOLTHA Stack ${cfg.stackName}::${cfg.volthaNamespace} to start", |
| 113 | script : """ |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 114 | |
| 115 | cat <<EOM |
| 116 | |
| 117 | ** ----------------------------------------------------------------------- |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 118 | ** Wait for VOLTHA Stack ${cfg.stackName}::${cfg.volthaNamespace} to start |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 119 | ** ----------------------------------------------------------------------- |
| 120 | EOM |
| 121 | |
| 122 | # set -euo pipefail |
Joey Armstrong | c20bd72 | 2023-09-11 15:25:12 -0400 | [diff] [blame] | 123 | set +x # # Noisy when commented (default: uncommented) |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 124 | |
| 125 | declare -i count=0 |
Joey Armstrong | 8da0894 | 2023-09-14 13:52:49 -0400 | [diff] [blame] | 126 | vsd_log='volthaStackDeploy.tmp' |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 127 | touch \$vsd_log |
| 128 | while 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 Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 142 | kubectl get pods -n ${cfg.volthaNamespace} \ |
| 143 | -l app.kubernetes.io/part-of=voltha --no-headers \ |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 144 | | tee \$vsd_log |
| 145 | else |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 146 | kubectl get pods -n ${cfg.volthaNamespace} \ |
| 147 | -l app.kubernetes.io/part-of=voltha --no-headers \ |
| 148 | > \$vsd_log |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 149 | fi |
| 150 | |
| 151 | done |
| 152 | rm -f \$vsd_log |
| 153 | """) |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 154 | |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 155 | leave('launchVolthaStack') |
| 156 | return |
| 157 | } |
| 158 | |
| 159 | // ----------------------------------------------------------------------- |
Joey Armstrong | 8d12f65 | 2023-09-13 18:25:33 -0400 | [diff] [blame] | 160 | // Intent: Wait until the pod completed, meaning ONOS fully deployed |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 161 | // ----------------------------------------------------------------------- |
Joey Armstrong | 8d12f65 | 2023-09-13 18:25:33 -0400 | [diff] [blame] | 162 | void waitForOnosDeploy(Map cfg) { |
| 163 | enter('waitForOnosDeploy') |
| 164 | |
| 165 | sh(label : 'Wait for ONOS full deployment', |
| 166 | Scriptx : """ |
| 167 | |
| 168 | cat <<EOM |
| 169 | |
| 170 | ** ----------------------------------------------------------------------- |
| 171 | ** Wait for ONOS full deployment |
| 172 | ** ----------------------------------------------------------------------- |
| 173 | EOM |
| 174 | |
| 175 | # set -euo pipefail |
| 176 | set +x # # Noisy when commented (default: uncommented) |
| 177 | |
| 178 | declare -i count=0 |
Joey Armstrong | 8da0894 | 2023-09-14 13:52:49 -0400 | [diff] [blame] | 179 | vsd_log='volthaStackDeploy.tmp' |
Joey Armstrong | 8d12f65 | 2023-09-13 18:25:33 -0400 | [diff] [blame] | 180 | touch \$vsd_log |
| 181 | while 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 | |
| 206 | done |
| 207 | rm -f \$vsd_log |
| 208 | """) |
| 209 | |
| 210 | leave('waitForOnosDeploy') |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | // ----------------------------------------------------------------------- |
| 215 | // ----------------------------------------------------------------------- |
| 216 | void process(Map config) { |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 217 | 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 Armstrong | 8d12f65 | 2023-09-13 18:25:33 -0400 | [diff] [blame] | 257 | waitForOnosDeploy(cfg) |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 258 | leave('process') |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 259 | |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 260 | return |
Matteo Scandolo | 42f6e57 | 2021-01-25 15:11:34 -0800 | [diff] [blame] | 261 | } |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 262 | |
| 263 | // ----------------------------------------------------------------------- |
| 264 | // ----------------------------------------------------------------------- |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 265 | def call(Map config=[:]) { |
| 266 | try { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 267 | enter('main') |
| 268 | process(config) |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 269 | } |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 270 | catch (Exception err) { // groovylint-disable-line CatchException |
| 271 | ans = false |
| 272 | println("** volthaStackDeploy.groovy: EXCEPTION ${err}") |
| 273 | throw err |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 274 | } |
Joey Armstrong | cd3f075 | 2023-09-11 18:10:12 -0400 | [diff] [blame] | 275 | finally { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 276 | leave('main') |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 277 | } |
| 278 | return |
| 279 | } |
| 280 | |
| 281 | // [EOF] |