Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 3 | def call(Map config) { |
| 4 | |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 5 | String iam = 'vars/waitForAdapters.groovy' |
| 6 | println("** ${iam}: ENTER") |
| 7 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 8 | def defaultConfig = [ |
| 9 | volthaNamespace: "voltha", |
Matteo Scandolo | 721d08b | 2021-09-30 17:42:40 -0700 | [diff] [blame] | 10 | stackName: "voltha", |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 11 | adaptersToWait: 2, |
| 12 | ] |
| 13 | |
| 14 | if (!config) { |
| 15 | config = [:] |
| 16 | } |
| 17 | |
| 18 | def cfg = defaultConfig + config |
| 19 | |
Andrea Campanella | 365ea1e | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 20 | if (cfg.adaptersToWait == 0){ |
| 21 | //no need to wait |
| 22 | println "No need to wait for adapters to be registered" |
| 23 | return |
| 24 | } |
| 25 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 26 | println "Wait for adapters to be registered" |
| 27 | |
| 28 | // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding |
| 29 | sh """ |
| 30 | set +x |
Matteo Scandolo | 721d08b | 2021-09-30 17:42:40 -0700 | [diff] [blame] | 31 | _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] | 32 | """ |
| 33 | |
| 34 | sh """ |
| 35 | set +x |
| 36 | adapters=\$(voltctl adapter list -q | wc -l) |
| 37 | while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do |
| 38 | sleep 5 |
| 39 | adapters=\$(voltctl adapter list -q | wc -l) |
| 40 | done |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 41 | """ |
| 42 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 43 | // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s |
| 44 | // as that means the core can talk to the adapters |
| 45 | // if voltctl can't read LastCommunication we skip this check |
| 46 | def done = false; |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 47 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 48 | while (!done) { |
| 49 | sleep 1 |
| 50 | def adapters = "" |
| 51 | try { |
| 52 | adapters = sh ( |
| 53 | script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"', |
| 54 | returnStdout: true, |
| 55 | ).trim() |
| 56 | } catch (err) { |
| 57 | // in some older versions of voltctl the command results in |
| 58 | // ERROR: Unexpected error while attempting to format results as table : template: output:1: function "gosince" not defined |
| 59 | // if that's the case we won't be able to check the timestamp so it's useless to wait |
| 60 | println("voltctl can't parse LastCommunication, skip waiting") |
| 61 | done = true |
| 62 | break |
| 63 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 64 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 65 | def waitingOn = adapters.split( '\n' ).find{since -> |
| 66 | since = since.replaceAll('s','') //remove seconds from the string |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 67 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 68 | // it has to be a single digit |
| 69 | if (since.length() > 1) { |
| 70 | return true |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 71 | } |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 72 | if ((since as Integer) > 5) { |
| 73 | return true |
| 74 | } |
| 75 | return false |
| 76 | } |
| 77 | done = (waitingOn == null || waitingOn.length() == 0) |
| 78 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 79 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 80 | sh """ |
| 81 | set +x |
Joey Armstrong | a1915cf | 2022-11-26 15:58:49 -0500 | [diff] [blame^] | 82 | pgrep --list-full port-forw |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 83 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 84 | ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true |
| 85 | """ |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 86 | |
| 87 | println("** ${iam}: LEAVE") |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 88 | } |