Joey Armstrong | db892b5 | 2023-03-03 10:44:06 -0500 | [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 | // % npm-groovy-lint vars/iam.groovy |
| 18 | // ----------------------------------------------------------------------- |
| 19 | |
| 20 | // ----------------------------------------------------------------------- |
| 21 | // ----------------------------------------------------------------------- |
| 22 | String getIam(String func) |
| 23 | { |
| 24 | // Cannot rely on a stack trace due to jenkins manipulation |
| 25 | // Report who and where caller came from. |
| 26 | String src = [ |
| 27 | 'repo:ci-management', |
| 28 | 'vars', |
| 29 | 'iam', |
| 30 | ].join('/') |
| 31 | |
| 32 | String iam = [src, func].join('::') |
| 33 | return(iam) |
| 34 | } |
| 35 | |
| 36 | // ----------------------------------------------------------------------- |
| 37 | // Intent: Display future enhancement list. |
| 38 | // ----------------------------------------------------------------------- |
| 39 | void todo() |
| 40 | { |
| 41 | String iam = getIam('todo') |
| 42 | |
| 43 | println(""" |
| 44 | [TODO: $iam] |
| 45 | o Pass jenkins parameters so todo() function can be conditionally called. |
| 46 | o Add call parameters to: |
| 47 | - Specify {ENTER,LEAVE} strings for logging. |
| 48 | - Set methods for caller to specify an alternate getIam() path. |
| 49 | """) |
| 50 | |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // ----------------------------------------------------------------------- |
| 55 | // Intent: Placeholder in case future enhancements are needed |
| 56 | // ----------------------------------------------------------------------- |
| 57 | Boolean process(Map config) |
| 58 | { |
| 59 | String iam = getIam('process') |
| 60 | |
| 61 | Boolean leave = false |
| 62 | |
| 63 | // Identify caller with a banner for logging |
| 64 | if (config.containsKey('enter')) { |
| 65 | println("** ${iam}: ENTER") |
| 66 | } |
| 67 | else if (config.containsKey('leave')) { |
| 68 | leave = true |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | println("** ${iam}: HELLO") |
| 73 | } |
| 74 | |
| 75 | // Display future enhancement list |
| 76 | if (config.containsKey('todo')) { |
| 77 | todo() |
| 78 | } |
| 79 | |
| 80 | // Maintain a sane logging enclosure block |
| 81 | if (leave) |
| 82 | { |
| 83 | println("** ${iam}: LEAVE") |
| 84 | } |
| 85 | |
| 86 | return(true) |
| 87 | } |
| 88 | |
| 89 | // ----------------------------------------------------------------------- |
| 90 | // Intent: Debug method for jenkins jobs identifying caller for logging. |
| 91 | // ----------------------------------------------------------------------- |
| 92 | // Given: |
| 93 | // self jenkins environment pointer 'this' |
| 94 | // config groovy closure used to pass key/value pairs into argv |
| 95 | // enter Display "** {iam} ENTER" |
| 96 | // leave Display "** {iam} LEAVE" |
| 97 | // todo Display future enhancement list |
| 98 | // ----------------------------------------------------------------------- |
| 99 | // Usage: |
| 100 | // o called from a jenkins {pipeline,stage,script} block. |
| 101 | // o iam(this) |
| 102 | // { |
| 103 | // foo = bar // paramter foo is... |
| 104 | // tans = fans |
| 105 | // } |
| 106 | // ----------------------------------------------------------------------- |
| 107 | Boolean call(def self, Map config) |
| 108 | { |
| 109 | String iam = getIam('main') |
| 110 | |
| 111 | argv = argv ?: [:] // {ternary,elvis} operator |
| 112 | println("** ${iam}: argv=${argv}") |
| 113 | |
| 114 | Boolean ranToCompletion = false |
| 115 | try |
| 116 | { |
| 117 | // [WIP] type(self) needed to quiet lint complaint. |
| 118 | // npm-groovy-lint: def for method parameter type should not be used NoDef |
| 119 | print(" ** $iam: Type of self variable is =" + self.getClass()) |
| 120 | // if (! self instanceof jenkins_object) { throw } |
| 121 | |
| 122 | if (process(argv)) |
| 123 | { |
| 124 | ranToCompletion = true |
| 125 | } |
| 126 | } |
| 127 | /* groovylint-disable*/ /* yuck! */ |
| 128 | catch (Exception err) |
| 129 | /* groovylint-enable */ |
| 130 | { |
| 131 | println("** ${iam}: EXCEPTION ${err}") |
| 132 | throw err |
| 133 | } |
| 134 | finally |
| 135 | { |
| 136 | println("** ${iam}: LEAVE") |
| 137 | } |
| 138 | |
| 139 | if (!ranToCompletion) |
| 140 | { |
| 141 | throw new Exception("ERROR ${iam}: Detected incomplete script run") |
| 142 | } |
| 143 | |
| 144 | return(true) |
| 145 | } |
| 146 | |
| 147 | // [EOF] |
| 148 | |