blob: f287e41a48e970f62e377e0eeb9294a769ccbcc0 [file] [log] [blame]
Joey Armstrong74ec08c2023-08-31 11:25:57 -04001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
Joey Armstrong518f3572024-02-11 07:56:25 -05003// Copyright 2023-2024 Open Networking Foundation (ONF) and the ONF Contributors
Joey Armstrong74ec08c2023-08-31 11:25:57 -04004//
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/pgrep_proc.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// -----------------------------------------------------------------------
Joey Armstrong98844622023-09-20 16:43:52 -040050// Intent: Display a process by name
Joey Armstrong74ec08c2023-08-31 11:25:57 -040051// -----------------------------------------------------------------------
52Boolean process(String proc, Map args) {
53 Boolean ans = true
54 String iam = getIam('process')
55
56 String cmd = [
57 'pgrep',
58 '--uid', '$(id -u)', // no stray signals
59 '--list-full',
60 '--full', // hmmm: conditional use (?)
61 "'${proc}",
Joey Armstrong92ffba62023-09-07 16:48:51 -040062 ].join(' ')
Joey Armstrong74ec08c2023-08-31 11:25:57 -040063
64 print("""
65** -----------------------------------------------------------------------
66** Running: $cmd
67** -----------------------------------------------------------------------
68""")
69
70 sh(
71 label : 'pgrep_proc', // jenkins usability: label log entry 'step'
72 // script : ${cmd}.toString(),
73
74 // Cannot derefence cmd AMT. Value stored as a grovy String/GString.
75 // No native support for cast to java.lang.String, some objects can
76 // but logic is prone to exception so (YUCK!) hardcode for now.
77
78 script : """
Roger Luethief8e2802023-09-27 15:24:09 +020079pgrep --uid \$(id -u) --list-full --full 'port-forw' || true
Joey Armstrong74ec08c2023-08-31 11:25:57 -040080""",
81 )
82 return(ans)
83}
84
85// -----------------------------------------------------------------------
86// Install: Display a list of port-forwarding processes.
87// -----------------------------------------------------------------------
88// groovylint-disable-next-line None, UnusedMethodParameter
89Boolean call\
90(
91 String proc, // name of process or arguments to terminate
92 Map args=[:],
93 Boolean filler = true // Groovy, why special case list comma handling (?)
94) {
95 Boolean ans = true
96
97 try {
98 enter('main')
99 process(proc, args)
100 }
101 catch (Exception err) { // groovylint-disable-line CatchException
102 ans = false
Joey Armstrong98844622023-09-20 16:43:52 -0400103 String iam = getIam('main')
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400104 println("** ${iam}: EXCEPTION ${err}")
105 throw err
106 }
107 finally {
Roger Luethiff337d82023-09-20 12:09:44 +0200108 leave('main')
Joey Armstrong74ec08c2023-08-31 11:25:57 -0400109 }
110
111 return(ans)
112}
113
114// [SEE ALSO]
115// -----------------------------------------------------------------------
116// o String cmd = [ ... ].join('') -- GString cannot cast to java.String
117// o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot
118// -----------------------------------------------------------------------
119// [TODO] - Combine pkill_proc and pgrep_proc
120// - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl'
121// o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected
122// o cmd && args (or command containing args) (or list of patterns passed)
123// - pass arg --full to match entire command line.
124// -----------------------------------------------------------------------
125// [EOF]