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