blob: 14b8cdb532752fcf5441ff76ea9f147ffb80591c [file] [log] [blame]
Andrea Campanella45b8eb72021-09-28 10:50:01 +02001def call(Map config) {
2
3 def defaultConfig = [
4 volthaNamespace: "voltha",
Matteo Scandolo721d08b2021-09-30 17:42:40 -07005 stackName: "voltha",
Andrea Campanella45b8eb72021-09-28 10:50:01 +02006 adaptersToWait: 2,
7 ]
8
9 if (!config) {
10 config = [:]
11 }
12
13 def cfg = defaultConfig + config
14
Andrea Campanella365ea1e2021-09-28 10:50:01 +020015 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 Campanella45b8eb72021-09-28 10:50:01 +020021 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 Scandolo721d08b2021-09-30 17:42:40 -070026 _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 Campanella45b8eb72021-09-28 10:50:01 +020027 """
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 Campanella45b8eb72021-09-28 10:50:01 +020036 """
37
Matteo Scandolo28722ea2021-10-01 15:48:42 -070038 // 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 Campanella45b8eb72021-09-28 10:50:01 +020042
Matteo Scandolo28722ea2021-10-01 15:48:42 -070043 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 Campanella45b8eb72021-09-28 10:50:01 +020059
Matteo Scandolo28722ea2021-10-01 15:48:42 -070060 def waitingOn = adapters.split( '\n' ).find{since ->
61 since = since.replaceAll('s','') //remove seconds from the string
Andrea Campanella45b8eb72021-09-28 10:50:01 +020062
Matteo Scandolo28722ea2021-10-01 15:48:42 -070063 // it has to be a single digit
64 if (since.length() > 1) {
65 return true
Andrea Campanella45b8eb72021-09-28 10:50:01 +020066 }
Matteo Scandolo28722ea2021-10-01 15:48:42 -070067 if ((since as Integer) > 5) {
68 return true
69 }
70 return false
71 }
72 done = (waitingOn == null || waitingOn.length() == 0)
73 }
Andrea Campanella45b8eb72021-09-28 10:50:01 +020074
Matteo Scandolo28722ea2021-10-01 15:48:42 -070075 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 Campanella45b8eb72021-09-28 10:50:01 +020079}