blob: 80ce89b71e1642eb9457511a6133c598d59f5d01 [file] [log] [blame]
Joey Armstrong96158a92022-11-25 10:36:06 -05001#!/usr/bin/env groovy
Joey Armstrong3d444c62022-11-26 21:42:20 -05002// -----------------------------------------------------------------------
3// -----------------------------------------------------------------------
Joey Armstrong96158a92022-11-25 10:36:06 -05004
Joey Armstrong3d444c62022-11-26 21:42:20 -05005// -----------------------------------------------------------------------
6// -----------------------------------------------------------------------
7def getIam(String func)
8{
9 String src = 'vars/waitForAdapters.groovy'
10 String iam = [src, func].join('::')
11 return
12}
Andrea Campanella45b8eb72021-09-28 10:50:01 +020013
Joey Armstrong3d444c62022-11-26 21:42:20 -050014// -----------------------------------------------------------------------
15// -----------------------------------------------------------------------
16def getAdapters()
17{
18 String iam = getIam('getAdapters')
19
20 def adapters = ""
21 try
22 {
23 adapters = sh (
24 script: 'voltctl adapter list --format "{{gosince .LastCommunication}}"',
25 returnStdout: true,
26 ).trim()
27 }
28 catch (err)
29 {
30 // in some older versions of voltctl the command results in
31 // ERROR: Unexpected error while attempting to format results
32 // as table : template: output:1: function "gosince" not defined
33 // if that's the case we won't be able to check the timestamp so
34 // it's useless to wait
35 println("voltctl can't parse LastCommunication, skip waiting")
36 adapters = 'SKIP'
37 }
38
39 print("** ${iam}: returned $adapters")
40 return adapters
41}
42
43// -----------------------------------------------------------------------
44// -----------------------------------------------------------------------
45def process(Map config)
46{
47 String iam = getIam('process')
Joey Armstrong96158a92022-11-25 10:36:06 -050048 println("** ${iam}: ENTER")
49
Andrea Campanella45b8eb72021-09-28 10:50:01 +020050 def defaultConfig = [
51 volthaNamespace: "voltha",
Matteo Scandolo721d08b2021-09-30 17:42:40 -070052 stackName: "voltha",
Andrea Campanella45b8eb72021-09-28 10:50:01 +020053 adaptersToWait: 2,
54 ]
55
Andrea Campanella45b8eb72021-09-28 10:50:01 +020056 def cfg = defaultConfig + config
57
Andrea Campanella365ea1e2021-09-28 10:50:01 +020058 if (cfg.adaptersToWait == 0){
59 //no need to wait
60 println "No need to wait for adapters to be registered"
61 return
62 }
63
Joey Armstrong3d444c62022-11-26 21:42:20 -050064 println("** ${iam}: Wait for adapters to be registered")
Andrea Campanella45b8eb72021-09-28 10:50:01 +020065
66 // guarantee that at least the specified number of adapters are registered with VOLTHA before proceeding
67 sh """
68 set +x
Matteo Scandolo721d08b2021-09-30 17:42:40 -070069 _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 +020070 """
71
72 sh """
73 set +x
74 adapters=\$(voltctl adapter list -q | wc -l)
75 while [[ \$adapters -lt ${cfg.adaptersToWait} ]]; do
76 sleep 5
77 adapters=\$(voltctl adapter list -q | wc -l)
78 done
Andrea Campanella45b8eb72021-09-28 10:50:01 +020079 """
80
Matteo Scandolo28722ea2021-10-01 15:48:42 -070081 // NOTE that we need to wait for LastCommunication to be equal or shorter that 5s
82 // as that means the core can talk to the adapters
83 // if voltctl can't read LastCommunication we skip this check
Joey Armstrong3d444c62022-11-26 21:42:20 -050084
85 println("** ${iam}: Wait for adapter LastCommunication")
Matteo Scandolo28722ea2021-10-01 15:48:42 -070086 def done = false;
Joey Armstrong3d444c62022-11-26 21:42:20 -050087 while (!done)
88 {
89 sleep 1
90 def adapters = getAdapters()
91 if (adapters == 'SKIP') { break }
Andrea Campanella45b8eb72021-09-28 10:50:01 +020092
Joey Armstrong3d444c62022-11-26 21:42:20 -050093 def waitingOn = adapters.split( '\n' ).find{since ->
94 since = since.replaceAll('s','') //remove seconds from the string
Andrea Campanella45b8eb72021-09-28 10:50:01 +020095
Joey Armstrong3d444c62022-11-26 21:42:20 -050096 // it has to be a single digit
97 if (since.length() > 1) {
98 return true
99 }
100 if ((since as Integer) > 5) {
101 return true
102 }
103 return false
104 }
Andrea Campanella45b8eb72021-09-28 10:50:01 +0200105
Joey Armstrong3d444c62022-11-26 21:42:20 -0500106 done = (waitingOn == null || waitingOn.length() == 0)
Matteo Scandolo28722ea2021-10-01 15:48:42 -0700107 }
Andrea Campanella45b8eb72021-09-28 10:50:01 +0200108
Joey Armstrong3d444c62022-11-26 21:42:20 -0500109 println("** ${iam}: Wait for adapter LastCommunication")
110 sh("""
Matteo Scandolo28722ea2021-10-01 15:48:42 -0700111 set +x
Joey Armstronga1915cf2022-11-26 15:58:49 -0500112 pgrep --list-full port-forw
Joey Armstrong96158a92022-11-25 10:36:06 -0500113
Joey Armstrong3d444c62022-11-26 21:42:20 -0500114 ps aux \
115 | grep port-forw \
116 | grep -v grep \
117 | awk '{print \$2}' \
118 | xargs --no-run-if-empty kill -9 || true
119 """)
Joey Armstrong96158a92022-11-25 10:36:06 -0500120
121 println("** ${iam}: LEAVE")
Joey Armstrong3d444c62022-11-26 21:42:20 -0500122 return
Andrea Campanella45b8eb72021-09-28 10:50:01 +0200123}
Joey Armstrong3d444c62022-11-26 21:42:20 -0500124
125// -----------------------------------------------------------------------
126// -----------------------------------------------------------------------
127def call(Map config)
128{
129 String iam = getIam('process')
130 println("** ${iam}: ENTER")
131
132 if (!config) {
133 config = [:]
134 }
135
136 try
137 {
138 process(config)
139 }
140 catch (Exception err)
141 {
142 println("** ${iam}: EXCEPTION ${err}")
143 throw err
144 }
145 finally
146 {
147 println("** ${iam}: LEAVE")
148 }
149 return
150}
151
152// EOF