blob: 661e0894d22e9acf44d5273dbb4d8fa16b41e05f [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
Andrea Campanella365ea1e2021-09-28 10:50:01 +020014 if (cfg.adaptersToWait == 0){
15 //no need to wait
16 println "No need to wait for adapters to be registered"
17 return
18 }
19
Andrea Campanella45b8eb72021-09-28 10:50:01 +020020 println "Wait for adapters to be registered"
21
22 // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding
23 sh """
24 set +x
25 _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"&
26 """
27
28 sh """
29 set +x
30 adapters=\$(voltctl adapter list -q | wc -l)
31 while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do
32 sleep 5
33 adapters=\$(voltctl adapter list -q | wc -l)
34 done
35 ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
36 """
37
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;
42
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 }
59
60 def waitingOn = adapters.split( '\n' ).find{since ->
61 since = since.replaceAll('s','') //remove seconds from the string
62
63 // it has to be a single digit
64 if (since.length() > 1) {
65 return true
66 }
67 if ((since as Integer) > 5) {
68 return true
69 }
70 return false
71 }
72 done = (waitingOn == null || waitingOn.length() == 0)
73 }
74
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 """
79}