Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -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/pkill_port_forward.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 | // Intent: Terminate a process by name. |
| 51 | // ----------------------------------------------------------------------- |
| 52 | // Note: Due to an exception casting GString to java.lang.string: |
| 53 | // - Used for parameterized construction of a command line with args |
| 54 | // - Passed to jenkins sh("${cmd}") |
| 55 | // - Command line invoked is currently hardcoded. |
| 56 | // ----------------------------------------------------------------------- |
| 57 | Boolean process(String proc, Map args) { |
| 58 | Boolean ans = true |
| 59 | String iam = getIam('process') |
| 60 | |
| 61 | println("** ${iam}: args passed: ${args}") |
| 62 | |
| 63 | String cmd = [ |
| 64 | 'pkill', |
| 65 | '--uid', '$(id -u)', // no stray signals |
| 66 | '--list-full', |
| 67 | '--full', // hmmm: conditional use (?) |
| 68 | "'${proc}", |
Joey Armstrong | 92ffba6 | 2023-09-07 16:48:51 -0400 | [diff] [blame] | 69 | ].join(' ') |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 70 | |
| 71 | if (args['banner']) { |
| 72 | print(""" |
| 73 | ** ----------------------------------------------------------------------- |
| 74 | ** Running: $cmd |
| 75 | ** ----------------------------------------------------------------------- |
| 76 | """) |
| 77 | } |
| 78 | |
| 79 | if (args['show_procs']) { |
| 80 | sh( |
| 81 | label : 'Display port forwarding (pre-pgrep-pkill)', |
| 82 | script : """ |
Joey Armstrong | b4aee53 | 2023-09-07 17:52:32 -0400 | [diff] [blame] | 83 | pgrep --uid \$(id -u) --list-full --full 'port-forw' || true |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 84 | """) |
| 85 | } |
| 86 | |
| 87 | sh( |
Roger Luethi | aa61359 | 2023-10-02 11:17:56 +0200 | [diff] [blame] | 88 | label : 'Kill port forwarding', |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 89 | // script : ${cmd}.toString(), -> Exception |
| 90 | script : """ |
Joey Armstrong | 6380bb9 | 2023-09-05 14:13:25 -0400 | [diff] [blame] | 91 | echo -e "\n** vars/pkill_port_forward.groovy [DEBUG]: pgrep-pkill check" |
Roger Luethi | aa61359 | 2023-10-02 11:17:56 +0200 | [diff] [blame] | 92 | if pgrep --uid \$(id -u) --list-full --full 'port-forw'; then |
Joey Armstrong | 6be1199 | 2023-09-07 17:22:51 -0400 | [diff] [blame] | 93 | pkill --uid \$(id -u) --echo --list-full --full 'port-forw' |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 94 | fi |
| 95 | """) |
| 96 | |
| 97 | return(ans) |
| 98 | } |
| 99 | |
| 100 | // ----------------------------------------------------------------------- |
| 101 | // Install: Display a list of port-forwarding processes. |
| 102 | // ----------------------------------------------------------------------- |
| 103 | // groovylint-disable-next-line None, UnusedMethodParameter |
| 104 | Boolean call\ |
| 105 | ( |
| 106 | String proc, // name of process or arguments to terminate |
| 107 | Map args=[:], |
| 108 | Boolean filler = true // Groovy, why special case list comma handling (?) |
| 109 | ) { |
| 110 | Boolean ans = true |
| 111 | |
| 112 | try { |
| 113 | enter('main') |
| 114 | |
| 115 | // Assign defaults |
Joey Armstrong | 6380bb9 | 2023-09-05 14:13:25 -0400 | [diff] [blame] | 116 | ['banner', 'show_procs'].each { key -> |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 117 | if (!args.containsKey(key)) { |
| 118 | args[key] = true |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | process(proc, args) |
| 123 | } |
| 124 | catch (Exception err) { // groovylint-disable-line CatchException |
| 125 | ans = false |
Joey Armstrong | 9884462 | 2023-09-20 16:43:52 -0400 | [diff] [blame] | 126 | String iam = getIam('main') |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 127 | println("** ${iam}: EXCEPTION ${err}") |
| 128 | throw err |
| 129 | } |
| 130 | finally { |
Joey Armstrong | c33a0bf | 2023-09-08 14:41:23 -0400 | [diff] [blame] | 131 | leave('main') |
Joey Armstrong | 43cb15a | 2023-09-01 14:32:27 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | return(ans) |
| 135 | } |
| 136 | |
| 137 | /* groovylint-disable */ |
| 138 | |
| 139 | // [SEE ALSO] |
| 140 | // ----------------------------------------------------------------------- |
| 141 | // o String cmd = [ ... ].join('') -- GString cannot cast to java.String |
| 142 | // o https://stackoverflow.com/questions/60304068/artifactory-in-jenkins-pipeline-org-codehaus-groovy-runtime-gstringimpl-cannot |
| 143 | // ----------------------------------------------------------------------- |
| 144 | // [TODO] - Combine pkill_proc and pkill_proc |
| 145 | // - Usage: do_proc(pkill=true, pkill=true, args='proc-forward', cmd='kubectl' |
| 146 | // o When kill == grep == true: display procs, terminate, recheck: fatal if procs detected |
| 147 | // o cmd && args (or command containing args) (or list of patterns passed) |
| 148 | // - pass arg --full to match entire command line. |
| 149 | // ----------------------------------------------------------------------- |
| 150 | // [EOF] |