blob: 37620c3a923a6f429482f8e8f79a20d4e12cb996 [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
36 ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
37 """
38
39 // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s
40 // as that means the core can talk to the adapters
41 // if voltctl can't read LastCommunication we skip this check
42 def done = false;
43
44 while (!done) {
45 sleep 1
46 def adapters = ""
47 try {
48 adapters = sh (
49 script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"',
50 returnStdout: true,
51 ).trim()
52 } catch (err) {
53 // in some older versions of voltctl the command results in
54 // ERROR: Unexpected error while attempting to format results as table : template: output:1: function "gosince" not defined
55 // if that's the case we won't be able to check the timestamp so it's useless to wait
56 println("voltctl can't parse LastCommunication, skip waiting")
57 done = true
58 break
59 }
60
61 def waitingOn = adapters.split( '\n' ).find{since ->
62 since = since.replaceAll('s','') //remove seconds from the string
63
64 // it has to be a single digit
65 if (since.length() > 1) {
66 return true
67 }
68 if ((since as Integer) > 5) {
69 return true
70 }
71 return false
72 }
73 done = (waitingOn == null || waitingOn.length() == 0)
74 }
75
76 sh """
77 set +x
78 ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
79 """
80}