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