Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -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. |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 16 | // ----------------------------------------------------------------------- |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 17 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 18 | // ----------------------------------------------------------------------- |
| 19 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -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 |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 22 | String src = 'vars/waitForAdapters.groovy' |
| 23 | String iam = [src, func].join('::') |
Joey Armstrong | f0c7690 | 2022-11-28 17:14:45 -0500 | [diff] [blame] | 24 | return iam |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 25 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 26 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 27 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 28 | // Intent: Log progress message |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 29 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 30 | void enter(String name) { |
| 31 | // Announce ourselves for log usability |
| 32 | String iam = getIam(name) |
| 33 | println("${iam}: ENTER") |
| 34 | return |
| 35 | } |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 36 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 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 | // ----------------------------------------------------------------------- |
| 48 | // ----------------------------------------------------------------------- |
| 49 | def getAdapters() { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 50 | String adapters = '' |
| 51 | |
| 52 | try { |
| 53 | adapters = sh( |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 54 | script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"', |
| 55 | returnStdout: true, |
| 56 | ).trim() |
| 57 | } |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 58 | catch (err) { |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 59 | // in some older versions of voltctl the command results in |
| 60 | // ERROR: Unexpected error while attempting to format results |
| 61 | // as table : template: output:1: function "gosince" not defined |
| 62 | // if that's the case we won't be able to check the timestamp so |
| 63 | // it's useless to wait |
| 64 | println("voltctl can't parse LastCommunication, skip waiting") |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 65 | adapters = 'SKIP' // why not just retry a few times (?) |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 68 | leave("returned $adapters") |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 69 | return adapters |
| 70 | } |
| 71 | |
| 72 | // ----------------------------------------------------------------------- |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 73 | // Intent: Interrogate adapter states to determine if we should continue |
| 74 | // looping waiting for adapter to start. Passed values must all be |
| 75 | // Integral, Valid and within range to assert we are done waiting. |
| 76 | // Function will return incomplete at the first sign of an invalid value. |
| 77 | // ----------------------------------------------------------------------- |
| 78 | // NOTE: list.find{} was replaced with a for loop to simplify |
| 79 | // early loop termination and return. |
| 80 | // ----------------------------------------------------------------------- |
| 81 | // RETURN: scalar |
| 82 | // 'DOUBLE-DIGIT' - value exceeds 0-5sec response window. |
| 83 | // 'NON-NUMERIC' - garbage in the stream (letter, symbol, ctrl) |
| 84 | // 'NULL' - empty or null string detected |
| 85 | // 'VALID' - succes, all adapters are functional |
| 86 | // 'SIX-NINE' - detected a single numeric digit between 6 and 9. |
| 87 | // ----------------------------------------------------------------------- |
| 88 | def getAdaptersState(String adapters0) |
| 89 | { |
| 90 | String iam = getIam('getAdaptersState') |
| 91 | Boolean debug = true // for now |
| 92 | |
| 93 | def adapters = adapters0.split('\n') |
| 94 | |
| 95 | String ans = null |
| 96 | def found = [] |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 97 | for(i=0; i<adapters.size(); i++) { |
| 98 | String elapsed = adapters[i] |
| 99 | if (debug) { |
| 100 | println("** ${iam} Checking elapsed[$i]: $elapsed") |
| 101 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 102 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 103 | if (! elapsed) { // empty string or null |
| 104 | ans = 'NULL' |
| 105 | break |
| 106 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 107 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 108 | Integer size = elapsed.length() |
| 109 | if (size > 2) { // 463765h58m52(s) |
| 110 | // higlander: there can be only one |
| 111 | ans = 'DOUBLE-DIGIT' |
| 112 | break |
| 113 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 114 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 115 | if (elapsed.endsWith('s')) { |
| 116 | // safer than replaceAll('s'): ssss1s => 1 |
| 117 | elapsed = elapsed.substring(0, size-1) |
| 118 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 119 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 120 | // Line noise guard: 'a', 'f', '#', '!' |
| 121 | if (! elapsed.isInteger()) { |
| 122 | ans = 'NON-NUMERIC' |
| 123 | break |
| 124 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 125 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 126 | // Is value in range: |
| 127 | // discard negative integers as just plain wonky. |
| 128 | // HMMM(?): zero/no latency response is kinda odd to include imho. |
| 129 | Integer val = elapsed.toInteger() |
| 130 | if (5 >= val && val >= 0) { |
| 131 | found.add(elapsed) |
| 132 | } |
| 133 | else { |
| 134 | ans = 'SIX-NINE' |
| 135 | break |
| 136 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 137 | } // for() |
| 138 | |
| 139 | // Declare success IFF all values are: |
| 140 | // o integral |
| 141 | // o valid |
| 142 | // o within range |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 143 | if (ans == null) { |
| 144 | Integer got = found.size() |
| 145 | Integer exp = adapters.size() |
| 146 | ans = (! exp) ? 'NO-ADAPTERS' |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 147 | : (got == exp) ? 'VALID' |
| 148 | : 'CONTINUE' |
| 149 | } |
| 150 | |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 151 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 152 | if (debug) { |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 153 | leave("return: [$ans]") |
Joey Armstrong | 7987c11 | 2022-12-05 12:42:43 -0500 | [diff] [blame] | 154 | } |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 155 | return ans |
| 156 | } // getAdaptersState |
| 157 | |
| 158 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 159 | // Intent: Poll until adapter status is known. |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 160 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 161 | def process(Map config) { |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 162 | String iam = getIam('process') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 163 | enter('process') |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 164 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 165 | def defaultConfig = [ |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 166 | volthaNamespace : 'voltha', |
| 167 | stackName : 'voltha', |
| 168 | adaptersToWait : 2, |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 169 | ] |
| 170 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 171 | def cfg = defaultConfig + config |
| 172 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 173 | if (cfg.adaptersToWait == 0) { |
| 174 | //no need to wait |
| 175 | println "No need to wait for adapters to be registered" |
| 176 | return |
Andrea Campanella | 365ea1e | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 179 | println("** ${iam}: Wait for adapters to be registered") |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 180 | |
| 181 | // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 182 | sh(label : 'waitForAdapters: Initiate port forwarding', |
| 183 | script : """ |
| 184 | _TAG="voltha-voltha-api" bash -c "while true; do kubectl port-forward --address 0.0.0.0 -n ${cfg.volthaNamespace} svc/${cfg.stackName}-voltha-api 55555:55555; done" & |
| 185 | """) |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 186 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 187 | sh(label : 'waitForAdapters: loop until adapter list', |
| 188 | script : """ |
| 189 | # set +x |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 190 | adapters=\$(voltctl adapter list -q | wc -l) |
| 191 | while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do |
| 192 | sleep 5 |
| 193 | adapters=\$(voltctl adapter list -q | wc -l) |
| 194 | done |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 195 | |
| 196 | cat << EOM |
| 197 | ** ----------------------------------------------------------------------- |
| 198 | ** ${iam}: Available adapters: |
| 199 | ** ----------------------------------------------------------------------- |
| 200 | EOM |
| 201 | voltctl adapter list -q |
| 202 | """) |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 203 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 204 | // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s |
| 205 | // as that means the core can talk to the adapters |
| 206 | // if voltctl can't read LastCommunication we skip this check |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 207 | |
| 208 | println("** ${iam}: Wait for adapter LastCommunication") |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 209 | Integer countdown = 60 * 10 |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 210 | while (true) { |
| 211 | sleep 1 |
| 212 | def adapters = getAdapters() |
| 213 | // Why are we not failing hard on this condition ? |
| 214 | // Adapters in an unknown state == testing: roll the dice |
| 215 | if (adapters == 'SKIP') { break } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 216 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 217 | def state = getAdaptersState(adapters) |
| 218 | if (state == 'VALID') { break } // IFF |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 219 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 220 | // ---------------------------------------------------------- |
| 221 | // Excessive timeout but unsure where startup time boundry is |
| 222 | // [TODO] profile for a baseline |
| 223 | // [TODO] groovy.transform.TimedInterrupt |
| 224 | // ---------------------------------------------------------- |
| 225 | countdown -= 1 |
| 226 | if (1 > countdown) { |
| 227 | throw new Exception('ERROR: Timed out waiting on adapter startup') |
| 228 | } |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 229 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 230 | |
Joey Armstrong | f0c7690 | 2022-11-28 17:14:45 -0500 | [diff] [blame] | 231 | println("** ${iam}: Tearing down port forwarding") |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 232 | Map pkpfArgs =\ |
| 233 | [ |
| 234 | 'banner' : true, // display banner for logging |
| 235 | 'show_procs' : true, // display procs under consideration |
| 236 | 'filler' : true // fix conditional trailing comma |
| 237 | ] |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 238 | |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 239 | // [TODO] 'kubectl.*port-forward' |
| 240 | pkill_port_forward('port-forward', pkpfArgs) |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 241 | |
| 242 | leave('process') |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 243 | return |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 244 | } |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 245 | |
| 246 | // ----------------------------------------------------------------------- |
| 247 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 248 | def call(Map config=[:]) |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 249 | { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 250 | try { |
| 251 | enter('main') |
| 252 | process(config) |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 253 | } |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 254 | catch (Exception err) { // groovylint-disable-line CatchException |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 255 | String iam = getIam('process') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 256 | println("** ${iam}: EXCEPTION ${err}") |
| 257 | throw err |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 258 | } |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 259 | finally { |
| 260 | leave('main') |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 261 | } |
| 262 | return |
| 263 | } |
| 264 | |
Joey Armstrong | af679da | 2023-01-31 14:22:41 -0500 | [diff] [blame] | 265 | // [EOF] |