blob: 08a14c943b84a08ea6f1a6cb617a2a53e13c0ee5 [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
2
Andrea Campanella45b8eb72021-09-28 10:50:01 +02003def call(Map config) {
4
Joey Armstrong96158a92022-11-25 10:36:06 -05005 String iam = 'vars/waitForAdapters.groovy'
6 println("** ${iam}: ENTER")
7
Andrea Campanella45b8eb72021-09-28 10:50:01 +02008 def defaultConfig = [
9 volthaNamespace: "voltha",
Matteo Scandolo721d08b2021-09-30 17:42:40 -070010 stackName: "voltha",
Andrea Campanella45b8eb72021-09-28 10:50:01 +020011 adaptersToWait: 2,
12 ]
13
14 if (!config) {
15 config = [:]
16 }
17
18 def cfg = defaultConfig + config
19
Andrea Campanella365ea1e2021-09-28 10:50:01 +020020 if (cfg.adaptersToWait == 0){
21 //no need to wait
22 println "No need to wait for adapters to be registered"
23 return
24 }
25
Andrea Campanella45b8eb72021-09-28 10:50:01 +020026 println "Wait for adapters to be registered"
27
28 // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding
29 sh """
30 set +x
Matteo Scandolo721d08b2021-09-30 17:42:40 -070031 _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 +020032 """
33
34 sh """
35 set +x
36 adapters=\$(voltctl adapter list -q | wc -l)
37 while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do
38 sleep 5
39 adapters=\$(voltctl adapter list -q | wc -l)
40 done
Andrea Campanella45b8eb72021-09-28 10:50:01 +020041 """
42
Matteo Scandolo28722ea2021-10-01 15:48:42 -070043 // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s
44 // as that means the core can talk to the adapters
45 // if voltctl can't read LastCommunication we skip this check
46 def done = false;
Andrea Campanella45b8eb72021-09-28 10:50:01 +020047
Matteo Scandolo28722ea2021-10-01 15:48:42 -070048 while (!done) {
49 sleep 1
50 def adapters = ""
51 try {
52 adapters = sh (
53 script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"',
54 returnStdout: true,
55 ).trim()
56 } catch (err) {
57 // in some older versions of voltctl the command results in
58 // ERROR: Unexpected error while attempting to format results as table : template: output:1: function "gosince" not defined
59 // if that's the case we won't be able to check the timestamp so it's useless to wait
60 println("voltctl can't parse LastCommunication, skip waiting")
61 done = true
62 break
63 }
Andrea Campanella45b8eb72021-09-28 10:50:01 +020064
Matteo Scandolo28722ea2021-10-01 15:48:42 -070065 def waitingOn = adapters.split( '\n' ).find{since ->
66 since = since.replaceAll('s','') //remove seconds from the string
Andrea Campanella45b8eb72021-09-28 10:50:01 +020067
Matteo Scandolo28722ea2021-10-01 15:48:42 -070068 // it has to be a single digit
69 if (since.length() > 1) {
70 return true
Andrea Campanella45b8eb72021-09-28 10:50:01 +020071 }
Matteo Scandolo28722ea2021-10-01 15:48:42 -070072 if ((since as Integer) > 5) {
73 return true
74 }
75 return false
76 }
77 done = (waitingOn == null || waitingOn.length() == 0)
78 }
Andrea Campanella45b8eb72021-09-28 10:50:01 +020079
Matteo Scandolo28722ea2021-10-01 15:48:42 -070080 sh """
81 set +x
Joey Armstronga1915cf2022-11-26 15:58:49 -050082 pgrep --list-full port-forw
Joey Armstrong96158a92022-11-25 10:36:06 -050083
Matteo Scandolo28722ea2021-10-01 15:48:42 -070084 ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
85 """
Joey Armstrong96158a92022-11-25 10:36:06 -050086
87 println("** ${iam}: LEAVE")
Andrea Campanella45b8eb72021-09-28 10:50:01 +020088}