Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | 518f357 | 2024-02-11 07:56:25 -0500 | [diff] [blame] | 3 | // Copyright 2023-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -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/pgrep_proc.groovy' |
| 25 | String iam = [src, func].join('::') |
| 26 | return iam |
| 27 | } |
| 28 | |
| 29 | // ----------------------------------------------------------------------- |
| 30 | // Intent: Log progress message |
| 31 | // ----------------------------------------------------------------------- |
| 32 | void 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 | // ----------------------------------------------------------------------- |
| 42 | void leave(String name) { |
| 43 | // Announce ourselves for log usability |
| 44 | String iam = getIam(name) |
| 45 | println("${iam}: LEAVE") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // ----------------------------------------------------------------------- |
Joey Armstrong | 9884462 | 2023-09-20 16:43:52 -0400 | [diff] [blame] | 50 | // Intent: Display a process by name |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 51 | // ----------------------------------------------------------------------- |
| 52 | Boolean 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 Armstrong | 92ffba6 | 2023-09-07 16:48:51 -0400 | [diff] [blame] | 62 | ].join(' ') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 63 | |
| 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 Luethi | ef8e280 | 2023-09-27 15:24:09 +0200 | [diff] [blame] | 79 | pgrep --uid \$(id -u) --list-full --full 'port-forw' || true |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 80 | """, |
| 81 | ) |
| 82 | return(ans) |
| 83 | } |
| 84 | |
| 85 | // ----------------------------------------------------------------------- |
| 86 | // Install: Display a list of port-forwarding processes. |
| 87 | // ----------------------------------------------------------------------- |
| 88 | // groovylint-disable-next-line None, UnusedMethodParameter |
| 89 | Boolean 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 Armstrong | 9884462 | 2023-09-20 16:43:52 -0400 | [diff] [blame] | 103 | String iam = getIam('main') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 104 | println("** ${iam}: EXCEPTION ${err}") |
| 105 | throw err |
| 106 | } |
| 107 | finally { |
Roger Luethi | ff337d8 | 2023-09-20 12:09:44 +0200 | [diff] [blame] | 108 | leave('main') |
Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame] | 109 | } |
| 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] |