Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/env groovy |
| 2 | // ----------------------------------------------------------------------- |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 3 | // Copyright 2023-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [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 | // % npm-groovy-lint vars/iam.groovy |
| 18 | // ----------------------------------------------------------------------- |
| 19 | |
| 20 | // ----------------------------------------------------------------------- |
| 21 | // ----------------------------------------------------------------------- |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 22 | String getIam(Map argv, String func) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 23 | { |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 24 | String src = argv.containsKey('label') |
| 25 | ? argv.label |
| 26 | : [ // Cannot lookup, jenkins alters stack for serialization |
| 27 | 'repo:ci-management', |
| 28 | 'vars', |
| 29 | 'iam', |
| 30 | ].join('/') |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 31 | |
| 32 | String iam = [src, func].join('::') |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 33 | if (argv.containsKey('version')) |
| 34 | { |
Joey Armstrong | 41feb7c | 2023-04-14 11:28:04 -0400 | [diff] [blame] | 35 | iam += sprintf('[%s]', argv.version) |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 36 | } |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 37 | return(iam) |
| 38 | } |
| 39 | |
| 40 | // ----------------------------------------------------------------------- |
| 41 | // Intent: Display future enhancement list. |
| 42 | // ----------------------------------------------------------------------- |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 43 | void todo(Map argv) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 44 | { |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 45 | String iam = getIam(argv, 'todo') |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 46 | |
| 47 | println(""" |
| 48 | [TODO: $iam] |
| 49 | o Pass jenkins parameters so todo() function can be conditionally called. |
| 50 | o Add call parameters to: |
| 51 | - Specify {ENTER,LEAVE} strings for logging. |
| 52 | - Set methods for caller to specify an alternate getIam() path. |
| 53 | """) |
| 54 | |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | // ----------------------------------------------------------------------- |
| 59 | // Intent: Placeholder in case future enhancements are needed |
| 60 | // ----------------------------------------------------------------------- |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 61 | Boolean process(Map argv) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 62 | { |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 63 | String iam = getIam(argv, 'process') |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 64 | Boolean debug ?: argv.debug |
| 65 | Boolean leave ?: argv.leave |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 66 | |
| 67 | // Identify caller with a banner for logging |
| 68 | if (config.containsKey('enter')) { |
| 69 | println("** ${iam}: ENTER") |
| 70 | } |
| 71 | else if (config.containsKey('leave')) { |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 72 | leave = true |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 73 | } |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 74 | else if (debug) { |
| 75 | println("** ${iam}: DEBUG=true") |
| 76 | } |
| 77 | |
| 78 | // Invoke closure passed in |
| 79 | if (config.containsKey('invoke')) { |
| 80 | Closure invoke = argv.invoke |
| 81 | invoke() |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 82 | } |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 83 | |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 84 | // Display future enhancement list |
| 85 | if (config.containsKey('todo')) { |
| 86 | todo() |
| 87 | } |
| 88 | |
| 89 | // Maintain a sane logging enclosure block |
| 90 | if (leave) |
| 91 | { |
| 92 | println("** ${iam}: LEAVE") |
| 93 | } |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 94 | |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 95 | return(true) |
| 96 | } |
| 97 | |
| 98 | // ----------------------------------------------------------------------- |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 99 | // Intent: Wrap functions/DSL tokens within a closure to improve logging |
| 100 | // o Issue a startup banner |
| 101 | // o Invoke task wrapped within the iam(){} closure |
| 102 | // o Detect exception/early termination and hilight that detail. |
| 103 | // o Issue a shutdown banner |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 104 | // ----------------------------------------------------------------------- |
| 105 | // Given: |
| 106 | // self jenkins environment pointer 'this' |
| 107 | // config groovy closure used to pass key/value pairs into argv |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 108 | // invoke Groovy closure to invoke: (ENTER, closure, LEAVE) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 109 | // enter Display "** {iam} ENTER" |
| 110 | // leave Display "** {iam} LEAVE" |
| 111 | // todo Display future enhancement list |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 112 | // label path/to/src[ver:1.0] |
| 113 | // version specify version and label separately |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 114 | // ----------------------------------------------------------------------- |
| 115 | // Usage: |
| 116 | // o called from a jenkins {pipeline,stage,script} block. |
| 117 | // o iam(this) |
| 118 | // { |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 119 | // debug = false |
| 120 | // enter = true |
| 121 | // invoke = { println('hello from closure') } |
| 122 | // leave = true |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 123 | // } |
| 124 | // ----------------------------------------------------------------------- |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 125 | Boolean call\ |
| 126 | ( |
Joey Armstrong | 41feb7c | 2023-04-14 11:28:04 -0400 | [diff] [blame] | 127 | Closure body // jenkins closure attached to the call iam() {closure} |
Joey Armstrong | 9ed18e1 | 2023-03-07 10:40:14 -0500 | [diff] [blame] | 128 | // def self, // jenkins env object for access to primitives like echo() |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 129 | ) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 130 | { |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 131 | // evaluate the body block and collect configuration into the object |
| 132 | Map argv = [:] // {ternary,elvis} operator |
| 133 | body.resolveStrategy = Closure.DELEGATE_FIRST |
Joey Armstrong | 9ed18e1 | 2023-03-07 10:40:14 -0500 | [diff] [blame] | 134 | body.delegate = argv |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 135 | body() |
| 136 | |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 137 | // Running within closure |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 138 | String iam = getIam(argv, 'main') |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 139 | Booelan debug ?: argv.debug |
Joey Armstrong | 1fa8cb8 | 2023-03-03 14:05:51 -0500 | [diff] [blame] | 140 | |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 141 | println("** ${iam}: argv=${argv}") |
| 142 | |
| 143 | Boolean ranToCompletion = false |
| 144 | try |
| 145 | { |
| 146 | // [WIP] type(self) needed to quiet lint complaint. |
| 147 | // npm-groovy-lint: def for method parameter type should not be used NoDef |
| 148 | print(" ** $iam: Type of self variable is =" + self.getClass()) |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 149 | print(" ** $iam: Type of body variable is =" + body.getClass()) |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 150 | // if (! self instanceof jenkins_object) { throw } |
| 151 | |
| 152 | if (process(argv)) |
| 153 | { |
| 154 | ranToCompletion = true |
| 155 | } |
| 156 | } |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 157 | catch (Exception err) // groovylint-disable-line CatchException |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 158 | { |
| 159 | println("** ${iam}: EXCEPTION ${err}") |
| 160 | throw err |
| 161 | } |
| 162 | finally |
| 163 | { |
| 164 | println("** ${iam}: LEAVE") |
| 165 | } |
| 166 | |
| 167 | if (!ranToCompletion) |
| 168 | { |
| 169 | throw new Exception("ERROR ${iam}: Detected incomplete script run") |
| 170 | } |
| 171 | |
| 172 | return(true) |
| 173 | } |
| 174 | |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 175 | /* |
| 176 | * ----------------------------------------------------------------------- |
| 177 | [SEE ALSO] |
| 178 | o https://rtyler.github.io/jenkins.io/doc/book/pipeline/shared-libraries/#defining-a-more-structured-dsl |
Joey Armstrong | d548772 | 2024-02-11 09:39:14 -0500 | [diff] [blame] | 179 | o https://blog.mrhaki.com/2009/11/groovy-goodness-passing-closures-to.html |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 180 | * ----------------------------------------------------------------------- |
| 181 | */ |
Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [diff] [blame] | 182 | |
Joey Armstrong | 0c689df | 2023-03-03 16:03:50 -0500 | [diff] [blame] | 183 | // [EOF] |