blob: 9629de404e16be744281e9f03ebf2a52ec1ebce6 [file] [log] [blame]
Joey Armstrongb9a6f122023-08-24 22:00:04 -04001#!/usr/bin/env groovy
2// -----------------------------------------------------------------------
3// Copyright 2021-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/pgrep_proc.groovy'
25 String iam = [src, func].join('::')
26 return iam
27}
28
29// -----------------------------------------------------------------------
30// -----------------------------------------------------------------------
Joey Armstrongebc18022023-08-26 13:20:49 -040031Boolean process(String proc, Map args) {
Joey Armstrongb9a6f122023-08-24 22:00:04 -040032 Boolean ans = true
Joey Armstrongebc18022023-08-26 13:20:49 -040033 String iam = getIam('process')
Joey Armstrongb9a6f122023-08-24 22:00:04 -040034
Joey Armstrongebc18022023-08-26 13:20:49 -040035 if (args.containsKey('debug')) {
36 println("** $iam [DEBUG]: proc=[$proc], args=[$args]")
37 }
Joey Armstrongb9a6f122023-08-24 22:00:04 -040038
Joey Armstrongebc18022023-08-26 13:20:49 -040039 String cmd = [
40 'pgrep',
41 '--uid', '$(id -u)', // no stray signals
42 '--list-full',
43 '--full', // hmmm: conditional use (?)
44 "'${proc}",
45 ]
Joey Armstrongb9a6f122023-08-24 22:00:04 -040046
Joey Armstrong5d65efe2023-08-25 09:43:18 -040047 print("""
48** -----------------------------------------------------------------------
49** Running: $cmd
50** -----------------------------------------------------------------------
51""")
Joey Armstrongb9a6f122023-08-24 22:00:04 -040052 sh(
53 label : 'pgrep_proc', // jenkins usability: label log entry 'step'
Joey Armstrongbe8c59c2023-08-28 12:43:45 -040054 script : ${cmd}.toString(),
Joey Armstrongb9a6f122023-08-24 22:00:04 -040055 )
Joey Armstrongb9a6f122023-08-24 22:00:04 -040056 return(ans)
57}
58
59// -----------------------------------------------------------------------
60// Install: Jenkins/groovy callback for installing the kind command.
61// o Paramter branch is passed but not yet used.
62// o Installer should be release friendly and checkout a frozen version
63// -----------------------------------------------------------------------
Joey Armstrongebc18022023-08-26 13:20:49 -040064// groovylint-disable-next-line None, UnusedMethodParameter
65Boolean call\
66(
67 String proc, // name of process or arguments to terminate
68 Map args=[:],
Joey Armstrong2da94cc2023-08-28 10:57:31 -040069 Boolean filler = true // Groovy, why special case list comma handling (?)
Joey Armstrongebc18022023-08-26 13:20:49 -040070) {
Joey Armstrongb9a6f122023-08-24 22:00:04 -040071 String iam = getIam('main')
Joey Armstrong2da94cc2023-08-28 10:57:31 -040072 Boolean ans = true
Joey Armstrongb9a6f122023-08-24 22:00:04 -040073
74 println("** ${iam}: ENTER")
75
Joey Armstronge9725b12023-08-28 18:15:12 -040076 // var = Gstring throws exception due to cast problem.
77 // var = [ 'foo', 'bar' 'tans' ].join(' ').toString() fails
78 throw new Exception("Not yet implemented")
79
Joey Armstrongb9a6f122023-08-24 22:00:04 -040080 try {
Joey Armstrongebc18022023-08-26 13:20:49 -040081 process(proc, args)
Joey Armstrongb9a6f122023-08-24 22:00:04 -040082 }
Joey Armstrongebc18022023-08-26 13:20:49 -040083 catch (Exception err) { // groovylint-disable-line CatchException
Joey Armstrong9341c9a2023-08-28 12:09:19 -040084 ans = false
Joey Armstrongb9a6f122023-08-24 22:00:04 -040085 println("** ${iam}: EXCEPTION ${err}")
86 throw err
87 }
88 finally {
89 println("** ${iam}: LEAVE")
90 }
Joey Armstrongebc18022023-08-26 13:20:49 -040091
92 return(ans)
Joey Armstrongb9a6f122023-08-24 22:00:04 -040093}
94
Joey Armstrongbe8c59c2023-08-28 12:43:45 -040095// [SEE ALSO]
96// -----------------------------------------------------------------------
97// o String cmd = [ ... ].join('') -- GString cannot cast to java.String
98// o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot
Joey Armstrongebc18022023-08-26 13:20:49 -040099// -----------------------------------------------------------------------
100// [TODO] - Combine pkill_proc and pgrep_proc
101// - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl'
102// o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected
103// o cmd && args (or command containing args) (or list of patterns passed)
104// - pass arg --full to match entire command line.
105// -----------------------------------------------------------------------
Joey Armstrongb9a6f122023-08-24 22:00:04 -0400106// [EOF]