Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | 518f357 | 2024-02-11 07:56:25 -0500 | [diff] [blame] | 3 | // Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 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/pkill_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 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 Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 53 | String cmd = """if [[ \$(pgrep --count "${proc}") -gt 0 ]]; then ${cmdKill}; fi""" |
Joey Armstrong | 5d65efe | 2023-08-25 09:43:18 -0400 | [diff] [blame] | 54 | |
| 55 | print(""" |
| 56 | ** ----------------------------------------------------------------------- |
| 57 | ** Running: $cmd |
| 58 | ** ----------------------------------------------------------------------- |
| 59 | """) |
| 60 | |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 61 | sh( |
| 62 | label : 'pkill_proc', // jenkins usability: label log entry 'step' |
Joey Armstrong | be8c59c | 2023-08-28 12:43:45 -0400 | [diff] [blame] | 63 | script : cmd.toString(), |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 64 | ) |
| 65 | |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 66 | 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 Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 74 | // groovylint-disable-next-line None, UnusedMethodParameter |
| 75 | void call\ |
| 76 | ( |
| 77 | String proc, // name of process or arguments to terminate |
| 78 | Map args=[:], |
| 79 | // Groovy, why special case list comma handling (?) |
Joey Armstrong | 2da94cc | 2023-08-28 10:57:31 -0400 | [diff] [blame] | 80 | Boolean filler = true // groovylint-disable-line UnusedMethodParameter |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 81 | ) { |
| 82 | |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 83 | String iam = getIam('main') |
Joey Armstrong | 2da94cc | 2023-08-28 10:57:31 -0400 | [diff] [blame] | 84 | Boolean ans = true |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 85 | |
| 86 | println("** ${iam}: ENTER") |
| 87 | |
Joey Armstrong | e9725b1 | 2023-08-28 18:15:12 -0400 | [diff] [blame] | 88 | // var = Gstring throws exception due to cast problem. |
| 89 | // var = [ 'foo', 'bar' 'tans' ].join(' ').toString() fails |
Joey Armstrong | 2563719 | 2023-08-30 09:53:05 -0400 | [diff] [blame] | 90 | throw new Exception("UNUSED: Not yet implemented") |
Joey Armstrong | e9725b1 | 2023-08-28 18:15:12 -0400 | [diff] [blame] | 91 | |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 92 | try { |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 93 | // Limit process matching by default |
| 94 | if (! mymap.containsKey('command_only')) { |
Joey Armstrong | 2da94cc | 2023-08-28 10:57:31 -0400 | [diff] [blame] | 95 | mymap['command_only'] = true |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 96 | } |
| 97 | process(proc, args) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 98 | } |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 99 | catch (Exception err) { // groovylint-disable-line CatchException |
Joey Armstrong | 9341c9a | 2023-08-28 12:09:19 -0400 | [diff] [blame] | 100 | ans = false |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 101 | println("** ${iam}: EXCEPTION ${err}") |
| 102 | throw err |
| 103 | } |
| 104 | finally { |
| 105 | println("** ${iam}: LEAVE") |
| 106 | } |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 107 | |
| 108 | return(ans) |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Joey Armstrong | be8c59c | 2023-08-28 12:43:45 -0400 | [diff] [blame] | 111 | // [SEE ALSO] |
| 112 | // ----------------------------------------------------------------------- |
| 113 | // o String cmd = [ ... ].join('') -- GString cannot cast to java.String |
| 114 | // o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot |
Joey Armstrong | ebc1802 | 2023-08-26 13:20:49 -0400 | [diff] [blame] | 115 | // ----------------------------------------------------------------------- |
| 116 | // [TODO] - Combine pkill_proc and pgrep_proc |
| 117 | // - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl' |
| 118 | // o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected |
| 119 | // o cmd && args (or command containing args) (or list of patterns passed) |
| 120 | // - pass arg --full to match entire command line. |
| 121 | // ----------------------------------------------------------------------- |
Joey Armstrong | b9a6f12 | 2023-08-24 22:00:04 -0400 | [diff] [blame] | 122 | // [EOF] |