Joey Armstrong | 74ec08c | 2023-08-31 11:25:57 -0400 | [diff] [blame^] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
| 3 | // Copyright 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 | // 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 | // ----------------------------------------------------------------------- |
| 50 | // ----------------------------------------------------------------------- |
| 51 | Boolean process(String proc, Map args) { |
| 52 | Boolean ans = true |
| 53 | String iam = getIam('process') |
| 54 | |
| 55 | String cmd = [ |
| 56 | 'pgrep', |
| 57 | '--uid', '$(id -u)', // no stray signals |
| 58 | '--list-full', |
| 59 | '--full', // hmmm: conditional use (?) |
| 60 | "'${proc}", |
| 61 | ] |
| 62 | |
| 63 | print(""" |
| 64 | ** ----------------------------------------------------------------------- |
| 65 | ** Running: $cmd |
| 66 | ** ----------------------------------------------------------------------- |
| 67 | """) |
| 68 | |
| 69 | sh( |
| 70 | label : 'pgrep_proc', // jenkins usability: label log entry 'step' |
| 71 | // script : ${cmd}.toString(), |
| 72 | |
| 73 | // Cannot derefence cmd AMT. Value stored as a grovy String/GString. |
| 74 | // No native support for cast to java.lang.String, some objects can |
| 75 | // but logic is prone to exception so (YUCK!) hardcode for now. |
| 76 | |
| 77 | script : """ |
| 78 | pgrep --uid \$(uid -u) --list-full --full 'port-forw' |
| 79 | """, |
| 80 | ) |
| 81 | return(ans) |
| 82 | } |
| 83 | |
| 84 | // ----------------------------------------------------------------------- |
| 85 | // Install: Display a list of port-forwarding processes. |
| 86 | // ----------------------------------------------------------------------- |
| 87 | // groovylint-disable-next-line None, UnusedMethodParameter |
| 88 | Boolean call\ |
| 89 | ( |
| 90 | String proc, // name of process or arguments to terminate |
| 91 | Map args=[:], |
| 92 | Boolean filler = true // Groovy, why special case list comma handling (?) |
| 93 | ) { |
| 94 | Boolean ans = true |
| 95 | |
| 96 | try { |
| 97 | enter('main') |
| 98 | process(proc, args) |
| 99 | } |
| 100 | catch (Exception err) { // groovylint-disable-line CatchException |
| 101 | ans = false |
| 102 | println("** ${iam}: EXCEPTION ${err}") |
| 103 | throw err |
| 104 | } |
| 105 | finally { |
| 106 | enter('main') |
| 107 | } |
| 108 | |
| 109 | return(ans) |
| 110 | } |
| 111 | |
| 112 | // [SEE ALSO] |
| 113 | // ----------------------------------------------------------------------- |
| 114 | // o String cmd = [ ... ].join('') -- GString cannot cast to java.String |
| 115 | // o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot |
| 116 | // ----------------------------------------------------------------------- |
| 117 | // [TODO] - Combine pkill_proc and pgrep_proc |
| 118 | // - Usage: do_proc(pkill=true, pgrep=true, args='proc-forward', cmd='kubectl' |
| 119 | // o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected |
| 120 | // o cmd && args (or command containing args) (or list of patterns passed) |
| 121 | // - pass arg --full to match entire command line. |
| 122 | // ----------------------------------------------------------------------- |
| 123 | // [EOF] |