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