blob: d935701ed7e682003d9f9d7d756e956792e81d25 [file] [log] [blame]
Joey Armstrong43cb15a2023-09-01 14:32:27 -04001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
3// Copyright 2023 Open Networking Foundation (ONF) and the ONF Contributors
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// -----------------------------------------------------------------------
17// Install the voltctl command by branch name "voltha-xx"
18// -----------------------------------------------------------------------
19
20// -----------------------------------------------------------------------
21// -----------------------------------------------------------------------
22String getIam(String func) {
23 // Cannot rely on a stack trace due to jenkins manipulation
24 String src = 'vars/pkill_port_forward.groovy'
25 String iam = [src, func].join('::')
26 return iam
27}
28
29// -----------------------------------------------------------------------
30// Intent: Log progress message
31// -----------------------------------------------------------------------
32void enter(String name) {
33 // Announce ourselves for log usability
34 String iam = getIam(name)
35 println("${iam}: ENTER")
36 return
37}
38
39// -----------------------------------------------------------------------
40// Intent: Log progress message
41// -----------------------------------------------------------------------
42void leave(String name) {
43 // Announce ourselves for log usability
44 String iam = getIam(name)
45 println("${iam}: LEAVE")
46 return
47}
48
49// -----------------------------------------------------------------------
50// Intent: Terminate a process by name.
51// -----------------------------------------------------------------------
52// Note: Due to an exception casting GString to java.lang.string:
53// - Used for parameterized construction of a command line with args
54// - Passed to jenkins sh("${cmd}")
55// - Command line invoked is currently hardcoded.
56// -----------------------------------------------------------------------
57Boolean process(String proc, Map args) {
58 Boolean ans = true
59 String iam = getIam('process')
60
61 println("** ${iam}: args passed: ${args}")
62
63 String cmd = [
64 'pkill',
65 '--uid', '$(id -u)', // no stray signals
66 '--list-full',
67 '--full', // hmmm: conditional use (?)
68 "'${proc}",
Joey Armstrong92ffba62023-09-07 16:48:51 -040069 ].join(' ')
Joey Armstrong43cb15a2023-09-01 14:32:27 -040070
71 if (args['banner']) {
72 print("""
73** -----------------------------------------------------------------------
74** Running: $cmd
75** -----------------------------------------------------------------------
76""")
77 }
78
79 if (args['show_procs']) {
80 sh(
81 label : 'Display port forwarding (pre-pgrep-pkill)',
82 script : """
Joey Armstrongb4aee532023-09-07 17:52:32 -040083pgrep --uid \$(id -u) --list-full --full 'port-forw' || true
Joey Armstrong43cb15a2023-09-01 14:32:27 -040084""")
85 }
86
87 sh(
88 label : 'Display port forwarding',
89 // script : ${cmd}.toString(), -> Exception
90 script : """
Joey Armstrong6380bb92023-09-05 14:13:25 -040091echo -e "\n** vars/pkill_port_forward.groovy [DEBUG]: pgrep-pkill check"
Joey Armstrong43cb15a2023-09-01 14:32:27 -040092if [[ \$(pgrep --count 'port-forw') -gt 0 ]]; then
Joey Armstrong6be11992023-09-07 17:22:51 -040093 pkill --uid \$(id -u) --echo --list-full --full 'port-forw'
Joey Armstrong43cb15a2023-09-01 14:32:27 -040094fi
95""")
96
97 return(ans)
98}
99
100// -----------------------------------------------------------------------
101// Install: Display a list of port-forwarding processes.
102// -----------------------------------------------------------------------
103// groovylint-disable-next-line None, UnusedMethodParameter
104Boolean call\
105(
106 String proc, // name of process or arguments to terminate
107 Map args=[:],
108 Boolean filler = true // Groovy, why special case list comma handling (?)
109) {
110 Boolean ans = true
111
112 try {
113 enter('main')
114
115 // Assign defaults
Joey Armstrong6380bb92023-09-05 14:13:25 -0400116 ['banner', 'show_procs'].each { key ->
Joey Armstrong43cb15a2023-09-01 14:32:27 -0400117 if (!args.containsKey(key)) {
118 args[key] = true
119 }
120 }
121
122 process(proc, args)
123 }
124 catch (Exception err) { // groovylint-disable-line CatchException
125 ans = false
126 println("** ${iam}: EXCEPTION ${err}")
127 throw err
128 }
129 finally {
130 enter('main')
131 }
132
133 return(ans)
134}
135
136/* groovylint-disable */
137
138// [SEE ALSO]
139// -----------------------------------------------------------------------
140// o String cmd = [ ... ].join('') -- GString cannot cast to java.String
141// o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot
142// -----------------------------------------------------------------------
143// [TODO] - Combine pkill_proc and pkill_proc
144// - Usage: do_proc(pkill=true, pkill=true, args='proc-forward', cmd='kubectl'
145// o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected
146// o cmd && args (or command containing args) (or list of patterns passed)
147// - pass arg --full to match entire command line.
148// -----------------------------------------------------------------------
149// [EOF]