blob: 0a1fe5ddfff9694f036ffb37d43fa37e8d7f3cb4 [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/pkill_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 cmdKill = [
40 'pkill',
41 '--uid', '$(id -u)', // no stray signals
42 '--echo',
43 '--full', // hmmm: conditional use (?)
44 "'${proc}",
45 ].join(' ')
46
47 /*
48 String cmdKill = (args['command_only')
49 ? "pkill --echo '${proc}'"
50 : "pkill --echo --full '${proc}'"
51 */
52
Joey Armstrongb9a6f122023-08-24 22:00:04 -040053 String cmd = """if [[ \$(pgrep --count "${proc}") -gt 0 ]]; then ${cmdKill}; fi"""
Joey Armstrong5d65efe2023-08-25 09:43:18 -040054
55 print("""
56** -----------------------------------------------------------------------
57** Running: $cmd
58** -----------------------------------------------------------------------
59""")
60
Joey Armstrongb9a6f122023-08-24 22:00:04 -040061 sh(
62 label : 'pkill_proc', // jenkins usability: label log entry 'step'
Joey Armstrongbe8c59c2023-08-28 12:43:45 -040063 script : cmd.toString(),
Joey Armstrongb9a6f122023-08-24 22:00:04 -040064 )
65
Joey Armstrongb9a6f122023-08-24 22:00:04 -040066 return(ans)
67}
68
69// -----------------------------------------------------------------------
70// Install: Jenkins/groovy callback for installing the kind command.
71// o Paramter branch is passed but not yet used.
72// o Installer should be release friendly and checkout a frozen version
73// -----------------------------------------------------------------------
Joey Armstrongebc18022023-08-26 13:20:49 -040074// groovylint-disable-next-line None, UnusedMethodParameter
75void call\
76(
77 String proc, // name of process or arguments to terminate
78 Map args=[:],
79 // Groovy, why special case list comma handling (?)
Joey Armstrong2da94cc2023-08-28 10:57:31 -040080 Boolean filler = true // groovylint-disable-line UnusedMethodParameter
Joey Armstrongebc18022023-08-26 13:20:49 -040081) {
82
Joey Armstrongb9a6f122023-08-24 22:00:04 -040083 String iam = getIam('main')
Joey Armstrong2da94cc2023-08-28 10:57:31 -040084 Boolean ans = true
Joey Armstrongb9a6f122023-08-24 22:00:04 -040085
86 println("** ${iam}: ENTER")
87
88 try {
Joey Armstrongebc18022023-08-26 13:20:49 -040089 // Limit process matching by default
90 if (! mymap.containsKey('command_only')) {
Joey Armstrong2da94cc2023-08-28 10:57:31 -040091 mymap['command_only'] = true
Joey Armstrongebc18022023-08-26 13:20:49 -040092 }
93 process(proc, args)
Joey Armstrongb9a6f122023-08-24 22:00:04 -040094 }
Joey Armstrongebc18022023-08-26 13:20:49 -040095 catch (Exception err) { // groovylint-disable-line CatchException
Joey Armstrong9341c9a2023-08-28 12:09:19 -040096 ans = false
Joey Armstrongb9a6f122023-08-24 22:00:04 -040097 println("** ${iam}: EXCEPTION ${err}")
98 throw err
99 }
100 finally {
101 println("** ${iam}: LEAVE")
102 }
Joey Armstrongebc18022023-08-26 13:20:49 -0400103
104 return(ans)
Joey Armstrongb9a6f122023-08-24 22:00:04 -0400105}
106
Joey Armstrongbe8c59c2023-08-28 12:43:45 -0400107// [SEE ALSO]
108// -----------------------------------------------------------------------
109// o String cmd = [ ... ].join('') -- GString cannot cast to java.String
110// o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot
Joey Armstrongebc18022023-08-26 13:20:49 -0400111// -----------------------------------------------------------------------
112// [TODO] - Combine pkill_proc and pgrep_proc
113// - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl'
114// o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected
115// o cmd && args (or command containing args) (or list of patterns passed)
116// - pass arg --full to match entire command line.
117// -----------------------------------------------------------------------
Joey Armstrongb9a6f122023-08-24 22:00:04 -0400118// [EOF]