Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 1 | #!/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 | // ----------------------------------------------------------------------- |
| 22 | String 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 Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 31 | Boolean process(String proc, Map args) { |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 32 | Boolean ans = true |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 33 | String iam = getIam('process') |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 34 | |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 35 | if (args.containsKey('debug')) { |
| 36 | println("** $iam [DEBUG]: proc=[$proc], args=[$args]") |
| 37 | } |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 38 | |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 39 | String cmd = [ |
| 40 | 'pgrep', |
| 41 | '--uid', '$(id -u)', // no stray signals |
| 42 | '--list-full', |
| 43 | '--full', // hmmm: conditional use (?) |
| 44 | "'${proc}", |
| 45 | ] |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 46 | |
Joey Armstrong | 5d65efe | 2023-08-25 09:43:18 -0400 | [diff] [blame] | 47 | print(""" |
| 48 | ** ----------------------------------------------------------------------- |
| 49 | ** Running: $cmd |
| 50 | ** ----------------------------------------------------------------------- |
| 51 | """) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 52 | sh( |
| 53 | label : 'pgrep_proc', // jenkins usability: label log entry 'step' |
| 54 | script : "${cmd}", |
| 55 | ) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 56 | 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 Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 64 | // groovylint-disable-next-line None, UnusedMethodParameter |
| 65 | Boolean call\ |
| 66 | ( |
| 67 | String proc, // name of process or arguments to terminate |
| 68 | Map args=[:], |
| 69 | Boolean filler=True // Groovy, why special case list comma handling (?) |
| 70 | ) { |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 71 | String iam = getIam('main') |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 72 | Boolean ans = True |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 73 | |
| 74 | println("** ${iam}: ENTER") |
| 75 | |
| 76 | try { |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 77 | process(proc, args) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 78 | } |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 79 | catch (Exception err) { // groovylint-disable-line CatchException |
| 80 | ans = False |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 81 | println("** ${iam}: EXCEPTION ${err}") |
| 82 | throw err |
| 83 | } |
| 84 | finally { |
| 85 | println("** ${iam}: LEAVE") |
| 86 | } |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 87 | |
| 88 | return(ans) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 91 | // ----------------------------------------------------------------------- |
| 92 | // [TODO] - Combine pkill_proc and pgrep_proc |
| 93 | // - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl' |
| 94 | // o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected |
| 95 | // o cmd && args (or command containing args) (or list of patterns passed) |
| 96 | // - pass arg --full to match entire command line. |
| 97 | // ----------------------------------------------------------------------- |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 98 | // [EOF] |