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 | // ----------------------------------------------------------------------- |
| 3 | // ----------------------------------------------------------------------- |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 4 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 5 | // ----------------------------------------------------------------------- |
| 6 | // ----------------------------------------------------------------------- |
| 7 | def getIam(String func) |
| 8 | { |
| 9 | String src = 'vars/waitForAdapters.groovy' |
| 10 | String iam = [src, func].join('::') |
| 11 | return |
| 12 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 13 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 14 | // ----------------------------------------------------------------------- |
| 15 | // ----------------------------------------------------------------------- |
| 16 | def getAdapters() |
| 17 | { |
| 18 | String iam = getIam('getAdapters') |
| 19 | |
| 20 | def adapters = "" |
| 21 | try |
| 22 | { |
| 23 | adapters = sh ( |
| 24 | script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"', |
| 25 | returnStdout: true, |
| 26 | ).trim() |
| 27 | } |
| 28 | catch (err) |
| 29 | { |
| 30 | // in some older versions of voltctl the command results in |
| 31 | // ERROR: Unexpected error while attempting to format results |
| 32 | // as table : template: output:1: function "gosince" not defined |
| 33 | // if that's the case we won't be able to check the timestamp so |
| 34 | // it's useless to wait |
| 35 | println("voltctl can't parse LastCommunication, skip waiting") |
| 36 | adapters = 'SKIP' |
| 37 | } |
| 38 | |
| 39 | print("** ${iam}: returned $adapters") |
| 40 | return adapters |
| 41 | } |
| 42 | |
| 43 | // ----------------------------------------------------------------------- |
| 44 | // ----------------------------------------------------------------------- |
| 45 | def process(Map config) |
| 46 | { |
| 47 | String iam = getIam('process') |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 48 | println("** ${iam}: ENTER") |
| 49 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 50 | def defaultConfig = [ |
| 51 | volthaNamespace: "voltha", |
Matteo Scandolo | 721d08b | 2021-09-30 17:42:40 -0700 | [diff] [blame] | 52 | stackName: "voltha", |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 53 | adaptersToWait: 2, |
| 54 | ] |
| 55 | |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 56 | def cfg = defaultConfig + config |
| 57 | |
Andrea Campanella | 365ea1e | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 58 | if (cfg.adaptersToWait == 0){ |
| 59 | //no need to wait |
| 60 | println "No need to wait for adapters to be registered" |
| 61 | return |
| 62 | } |
| 63 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 64 | println("** ${iam}: Wait for adapters to be registered") |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 65 | |
| 66 | // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding |
| 67 | sh """ |
| 68 | set +x |
Matteo Scandolo | 721d08b | 2021-09-30 17:42:40 -0700 | [diff] [blame] | 69 | _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] | 70 | """ |
| 71 | |
| 72 | sh """ |
| 73 | set +x |
| 74 | adapters=\$(voltctl adapter list -q | wc -l) |
| 75 | while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do |
| 76 | sleep 5 |
| 77 | adapters=\$(voltctl adapter list -q | wc -l) |
| 78 | done |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 79 | """ |
| 80 | |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 81 | // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s |
| 82 | // as that means the core can talk to the adapters |
| 83 | // if voltctl can't read LastCommunication we skip this check |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 84 | |
| 85 | println("** ${iam}: Wait for adapter LastCommunication") |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 86 | def done = false; |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 87 | while (!done) |
| 88 | { |
| 89 | sleep 1 |
| 90 | def adapters = getAdapters() |
| 91 | if (adapters == 'SKIP') { break } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 92 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 93 | def waitingOn = adapters.split( '\n' ).find{since -> |
| 94 | since = since.replaceAll('s','') //remove seconds from the string |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 95 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 96 | // it has to be a single digit |
| 97 | if (since.length() > 1) { |
| 98 | return true |
| 99 | } |
| 100 | if ((since as Integer) > 5) { |
| 101 | return true |
| 102 | } |
| 103 | return false |
| 104 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 105 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 106 | done = (waitingOn == null || waitingOn.length() == 0) |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 107 | } |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 108 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 109 | println("** ${iam}: Wait for adapter LastCommunication") |
| 110 | sh(""" |
Matteo Scandolo | 28722ea | 2021-10-01 15:48:42 -0700 | [diff] [blame] | 111 | set +x |
Joey Armstrong | a1915cf | 2022-11-26 15:58:49 -0500 | [diff] [blame] | 112 | pgrep --list-full port-forw |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 113 | |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 114 | ps aux \ |
| 115 | | grep port-forw \ |
| 116 | | grep -v grep \ |
| 117 | | awk '{print \$2}' \ |
| 118 | | xargs --no-run-if-empty kill -9 || true |
| 119 | """) |
Joey Armstrong | 96158a9 | 2022-11-25 10:36:06 -0500 | [diff] [blame] | 120 | |
| 121 | println("** ${iam}: LEAVE") |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 122 | return |
Andrea Campanella | 45b8eb7 | 2021-09-28 10:50:01 +0200 | [diff] [blame] | 123 | } |
Joey Armstrong | 3d444c6 | 2022-11-26 21:42:20 -0500 | [diff] [blame^] | 124 | |
| 125 | // ----------------------------------------------------------------------- |
| 126 | // ----------------------------------------------------------------------- |
| 127 | def call(Map config) |
| 128 | { |
| 129 | String iam = getIam('process') |
| 130 | println("** ${iam}: ENTER") |
| 131 | |
| 132 | if (!config) { |
| 133 | config = [:] |
| 134 | } |
| 135 | |
| 136 | try |
| 137 | { |
| 138 | process(config) |
| 139 | } |
| 140 | catch (Exception err) |
| 141 | { |
| 142 | println("** ${iam}: EXCEPTION ${err}") |
| 143 | throw err |
| 144 | } |
| 145 | finally |
| 146 | { |
| 147 | println("** ${iam}: LEAVE") |
| 148 | } |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | // EOF |