blob: 75f204dcb9d759d7b92a778640ec775cbd6a6f61 [file] [log] [blame]
Joey Armstrongdb892b52023-03-03 10:44:06 -05001#!/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// -----------------------------------------------------------------------
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050022String getIam(Map argv, String func)
Joey Armstrongdb892b52023-03-03 10:44:06 -050023{
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050024 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 Armstrongdb892b52023-03-03 10:44:06 -050031
32 String iam = [src, func].join('::')
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050033 if (argv.containsKey('version'))
34 {
35 iam += sprintf("[%s]", argv.version)
36 }
Joey Armstrongdb892b52023-03-03 10:44:06 -050037 return(iam)
38}
39
40// -----------------------------------------------------------------------
41// Intent: Display future enhancement list.
42// -----------------------------------------------------------------------
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050043void todo(Map argv)
Joey Armstrongdb892b52023-03-03 10:44:06 -050044{
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050045 String iam = getIam(argv, 'todo')
Joey Armstrongdb892b52023-03-03 10:44:06 -050046
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 Armstrong1fa8cb82023-03-03 14:05:51 -050061Boolean process(Map argv)
Joey Armstrongdb892b52023-03-03 10:44:06 -050062{
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050063 String iam = getIam(argv, 'process')
Joey Armstrongdb892b52023-03-03 10:44:06 -050064
65 Boolean leave = false
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 Armstrong1fa8cb82023-03-03 14:05:51 -050072 leave = true
Joey Armstrongdb892b52023-03-03 10:44:06 -050073 }
74 else
75 {
76 println("** ${iam}: HELLO")
77 }
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050078
Joey Armstrongdb892b52023-03-03 10:44:06 -050079 // Display future enhancement list
80 if (config.containsKey('todo')) {
81 todo()
82 }
83
84 // Maintain a sane logging enclosure block
85 if (leave)
86 {
87 println("** ${iam}: LEAVE")
88 }
Joey Armstrong1fa8cb82023-03-03 14:05:51 -050089
Joey Armstrongdb892b52023-03-03 10:44:06 -050090 return(true)
91}
92
93// -----------------------------------------------------------------------
94// Intent: Debug method for jenkins jobs identifying caller for logging.
95// -----------------------------------------------------------------------
96// Given:
97// self jenkins environment pointer 'this'
98// config groovy closure used to pass key/value pairs into argv
99// enter Display "** {iam} ENTER"
100// leave Display "** {iam} LEAVE"
101// todo Display future enhancement list
Joey Armstrong1fa8cb82023-03-03 14:05:51 -0500102// label path/to/src[ver:1.0]
103// version specify version and label separately
Joey Armstrongdb892b52023-03-03 10:44:06 -0500104// -----------------------------------------------------------------------
105// Usage:
106// o called from a jenkins {pipeline,stage,script} block.
107// o iam(this)
108// {
109// foo = bar // paramter foo is...
110// tans = fans
111// }
112// -----------------------------------------------------------------------
Joey Armstrong1fa8cb82023-03-03 14:05:51 -0500113Boolean call(def self, Map argv)
Joey Armstrongdb892b52023-03-03 10:44:06 -0500114{
Joey Armstrongdb892b52023-03-03 10:44:06 -0500115 argv = argv ?: [:] // {ternary,elvis} operator
Joey Armstrong1fa8cb82023-03-03 14:05:51 -0500116 String iam = getIam(argv, 'main')
117
Joey Armstrongdb892b52023-03-03 10:44:06 -0500118 println("** ${iam}: argv=${argv}")
119
120 Boolean ranToCompletion = false
121 try
122 {
123 // [WIP] type(self) needed to quiet lint complaint.
124 // npm-groovy-lint: def for method parameter type should not be used NoDef
125 print(" ** $iam: Type of self variable is =" + self.getClass())
126 // if (! self instanceof jenkins_object) { throw }
127
128 if (process(argv))
129 {
130 ranToCompletion = true
131 }
132 }
133 /* groovylint-disable*/ /* yuck! */
134 catch (Exception err)
135 /* groovylint-enable */
136 {
137 println("** ${iam}: EXCEPTION ${err}")
138 throw err
139 }
140 finally
141 {
142 println("** ${iam}: LEAVE")
143 }
144
145 if (!ranToCompletion)
146 {
147 throw new Exception("ERROR ${iam}: Detected incomplete script run")
148 }
149
150 return(true)
151}
152
153// [EOF]
154