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: loop until adapter list', |
| 183 | script : """ |
| 184 | # set +x |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 185 | adapters=\$(voltctl adapter list -q | wc -l) |
| 186 | while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do |
| 187 | sleep 5 |
| 188 | adapters=\$(voltctl adapter list -q | wc -l) |
| 189 | done |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 190 | |
| 191 | cat << EOM |
| 192 | ** ----------------------------------------------------------------------- |
| 193 | ** ${iam}: Available adapters: |
| 194 | ** ----------------------------------------------------------------------- |
| 195 | EOM |
| 196 | voltctl adapter list -q |
| 197 | """) |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 198 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 199 | // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s |
| 200 | // as that means the core can talk to the adapters |
| 201 | // if voltctl can't read LastCommunication we skip this check |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 202 | |
| 203 | println("** ${iam}: Wait for adapter LastCommunication") |
Joey Armstrong | 14a67a1 | 2022-11-28 12:28:23 -0500 | [diff] [blame] | 204 | Integer countdown = 60 * 10 |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 205 | while (true) { |
| 206 | sleep 1 |
| 207 | def adapters = getAdapters() |
| 208 | // Why are we not failing hard on this condition ? |
| 209 | // Adapters in an unknown state == testing: roll the dice |
| 210 | if (adapters == 'SKIP') { break } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 211 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 212 | def state = getAdaptersState(adapters) |
| 213 | if (state == 'VALID') { break } // IFF |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 214 | |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 215 | // ---------------------------------------------------------- |
| 216 | // Excessive timeout but unsure where startup time boundry is |
| 217 | // [TODO] profile for a baseline |
| 218 | // [TODO] groovy.transform.TimedInterrupt |
| 219 | // ---------------------------------------------------------- |
| 220 | countdown -= 1 |
| 221 | if (1 > countdown) { |
| 222 | throw new Exception('ERROR: Timed out waiting on adapter startup') |
| 223 | } |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 224 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 225 | |
Joey Armstrong | f0c7690 | 2022-11-28 17:14:45 -0500 | [diff] [blame] | 226 | println("** ${iam}: Tearing down port forwarding") |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 227 | Map pkpfArgs =\ |
| 228 | [ |
| 229 | 'banner' : true, // display banner for logging |
| 230 | 'show_procs' : true, // display procs under consideration |
| 231 | 'filler' : true // fix conditional trailing comma |
| 232 | ] |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 233 | |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 234 | // [TODO] 'kubectl.*port-forward' |
| 235 | pkill_port_forward('port-forward', pkpfArgs) |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 236 | |
| 237 | leave('process') |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 238 | return |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 239 | } |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 240 | |
| 241 | // ----------------------------------------------------------------------- |
| 242 | // ----------------------------------------------------------------------- |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 243 | def call(Map config=[:]) |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 244 | { |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 245 | try { |
| 246 | enter('main') |
| 247 | process(config) |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 248 | } |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 249 | catch (Exception err) { // groovylint-disable-line CatchException |
Joey Armstrong | e93c3fa | 2023-09-11 09:34:24 -0400 | [diff] [blame] | 250 | String iam = getIam('process') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 251 | println("** ${iam}: EXCEPTION ${err}") |
| 252 | throw err |
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 | finally { |
| 255 | leave('main') |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame] | 256 | } |
| 257 | return |
| 258 | } |
| 259 | |
Joey Armstrong | af679da | 2023-01-31 14:22:41 -0500 | [diff] [blame] | 260 | // [EOF] |