Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
Jakub Josef | 93f08e2 | 2017-06-05 19:14:53 +0200 | [diff] [blame] | 2 | import com.cloudbees.groovy.cps.NonCPS |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 3 | |
| 4 | /** |
| 5 | * |
| 6 | * Jenkins common functions |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /** |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 11 | * Returns a list of groups which user belongs |
| 12 | * @param username String |
| 13 | * @return list of groups [String] |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 14 | */ |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 15 | def userGroups(username) { |
| 16 | res = [] |
| 17 | def authorities = Jenkins.instance.securityRealm.loadUserByUsername(username).getAuthorities() |
| 18 | authorities.each { |
| 19 | res.add(it.toString()) |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 20 | } |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 21 | return res |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Check if user belongs to group |
| 26 | * @param username String |
| 27 | * @param group String |
| 28 | * @return boolean result |
| 29 | */ |
| 30 | def userInGroup(username, group) { |
| 31 | def authorities = userGroups(username) |
| 32 | return authorities.any{it==group} |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check if user belongs to at least one of given groups |
| 37 | * @param username String |
| 38 | * @param groups [String] |
| 39 | * @return boolean result |
| 40 | */ |
| 41 | def userInGroups(username, groups) { |
| 42 | return groups.any{userInGroup(username, it)} |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns current username from build |
| 47 | * @return username String |
| 48 | */ |
| 49 | def currentUsername() { |
| 50 | username = '' |
| 51 | wrap([$class: 'BuildUser']) { |
Maxim Rasskazov | 2b7c3be | 2019-06-21 14:50:02 +0400 | [diff] [blame^] | 52 | username = env.BUILD_USER_ID ?: 'jenkins' |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 53 | } |
| 54 | if (username) { |
| 55 | return username |
| 56 | } else { |
| 57 | throw new Exception('cant get current username') |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Check if current user belongs to at least one of given groups |
| 63 | * @param groups [String] |
| 64 | * @return boolean result |
| 65 | */ |
| 66 | def currentUserInGroups(groups) { |
| 67 | username = currentUsername() |
| 68 | return userInGroups(username, groups) |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if current user belongs to group |
| 73 | * @param group String |
| 74 | * @return boolean result |
| 75 | */ |
| 76 | def currentUserInGroup(group) { |
| 77 | username = currentUsername() |
| 78 | return userInGroup(username, group) |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 82 | * Get Jenkins job running builds |
| 83 | * @param jobName job name |
| 84 | * @return list of running builds |
| 85 | */ |
Jakub Josef | fbe8c7c | 2017-05-11 13:35:11 +0200 | [diff] [blame] | 86 | @NonCPS |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 87 | def getJobRunningBuilds(jobName){ |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 88 | def job = Jenkins.instance.items.find{it -> it.name.equals(jobName)} |
| 89 | if(job){ |
| 90 | return job.builds.findAll{build -> build.isBuilding()} |
| 91 | } |
| 92 | return [] |
Jakub Josef | 93f08e2 | 2017-06-05 19:14:53 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @NonCPS |
| 96 | def getRunningBuilds(job){ |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 97 | return job.builds.findAll{build -> build.isBuilding()} |
Jakub Josef | 93f08e2 | 2017-06-05 19:14:53 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @NonCPS |
| 101 | def killStuckBuilds(maxSeconds, job){ |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 102 | def common = new com.mirantis.mk.Common() |
| 103 | def result = true |
| 104 | def runningBuilds = getRunningBuilds(job) |
| 105 | def jobName = job.name |
| 106 | for(int j=0; j < runningBuilds.size(); j++){ |
| 107 | int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0 |
| 108 | if(durationInSeconds > maxSeconds){ |
| 109 | result = false |
| 110 | def buildId = runningBuilds[j].id |
| 111 | common.infoMsg("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s") |
| 112 | try{ |
| 113 | runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer")); |
| 114 | result = true |
| 115 | }catch(e){ |
| 116 | common.errorMsg("Error occured during aborting build: Exception: ${e}") |
| 117 | } |
| 118 | } |
Jakub Josef | 93f08e2 | 2017-06-05 19:14:53 +0200 | [diff] [blame] | 119 | } |
Kirill Mashchenko | 4300109 | 2018-12-25 05:28:53 +0400 | [diff] [blame] | 120 | return result |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 121 | } |
Richard Felkl | 838892f | 2018-06-12 17:58:20 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Get Jenkins job object |
| 125 | * @param jobName job name |
| 126 | * @return job object that matches jobName |
| 127 | */ |
Denis Egorenko | b1a40b6 | 2019-01-11 18:04:57 +0400 | [diff] [blame] | 128 | def getJobByName(jobName, regexp=false){ |
Richard Felkl | 838892f | 2018-06-12 17:58:20 +0200 | [diff] [blame] | 129 | for(item in Hudson.instance.items) { |
Denis Egorenko | b1a40b6 | 2019-01-11 18:04:57 +0400 | [diff] [blame] | 130 | if (regexp && item.name ==~ jobName || item.name == jobName) { |
Richard Felkl | 838892f | 2018-06-12 17:58:20 +0200 | [diff] [blame] | 131 | return item |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get Jenkins job parameters |
| 138 | * @param jobName job name |
| 139 | * @return HashMap with parameter names as keys and their values as values |
| 140 | */ |
| 141 | def getJobParameters(jobName){ |
| 142 | def job = getJobByName(jobName) |
| 143 | def prop = job.getProperty(ParametersDefinitionProperty.class) |
| 144 | def params = new java.util.HashMap<String,String>() |
| 145 | if(prop != null) { |
| 146 | for(param in prop.getParameterDefinitions()) { |
| 147 | params.put(param.name, param.defaultValue) |
| 148 | } |
| 149 | } |
| 150 | return params |
| 151 | } |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Get list of causes actions for given build |
| 155 | * |
| 156 | * @param build Job build object (like, currentBuild.rawBuild) |
| 157 | * @return list of causes actions for given build |
| 158 | */ |
| 159 | @NonCPS |
| 160 | def getBuildCauseActions(build) { |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 161 | for(action in build.actions) { |
| 162 | if (action instanceof hudson.model.CauseAction) { |
| 163 | return action.causes |
| 164 | } |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 165 | } |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 166 | return [] |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get list of builds, triggered by Gerrit with given build |
| 171 | * @param build Job build object (like, currentBuild.rawBuild) |
| 172 | * @return list of builds with names and numbers |
| 173 | */ |
| 174 | @NonCPS |
| 175 | def getGerritBuildContext(build) { |
| 176 | def causes = getBuildCauseActions(build) |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 177 | for(cause in causes) { |
| 178 | if (cause instanceof com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause) { |
| 179 | return cause.context.getOtherBuilds() |
| 180 | } |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 181 | } |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 182 | return [] |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Wait for other jobs |
| 187 | * @param config config parameter: |
| 188 | * builds - List of job build objects, which should be checked |
| 189 | * checkBuilds - List of job names or regexps, which should be used to check provided builds list |
| 190 | * regexp - Wheither to use regexp or simple string matching |
| 191 | */ |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 192 | @NonCPS |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 193 | def waitForOtherBuilds(LinkedHashMap config){ |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 194 | def context = config.get('context', 'gerrit') |
| 195 | def builds = [] |
| 196 | if (context == 'gerrit') { |
| 197 | builds = getGerritBuildContext(currentBuild.rawBuild) |
| 198 | } else if (context == 'custom') { |
| 199 | builds = config.get('builds') |
| 200 | } |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 201 | def checkBuilds = config.get('checkBuilds') |
| 202 | def regexp = config.get('regexp', false) |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 203 | |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 204 | def waitForBuilds = builds.findAll { build -> |
| 205 | def jobName = build.fullDisplayName.tokenize(' ')[0] |
| 206 | if (regexp) { |
| 207 | checkBuilds.find { jobName ==~ it } |
| 208 | } else { |
| 209 | jobName in checkBuilds |
| 210 | } |
| 211 | } |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 212 | |
| 213 | def buildsMap = [] |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 214 | if (waitForBuilds) { |
| 215 | def waiting = true |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 216 | print "\u001B[36mWaiting for next jobs: ${waitForBuilds}\u001B[0m" |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 217 | while(waiting) { |
| 218 | waiting = false |
| 219 | waitForBuilds.each { job -> |
| 220 | if (job.inProgress) { |
| 221 | waiting = true |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 222 | } else { |
| 223 | buildInfo = [ |
| 224 | 'jobName': job.fullDisplayName.tokenize(' ')[0], |
| 225 | 'jobNumber': job.number, |
| 226 | ] |
| 227 | buildsMap.add(buildInfo) |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
Denis Egorenko | 270c530 | 2019-01-10 20:53:01 +0400 | [diff] [blame] | 232 | return buildsMap |
Denis Egorenko | 46ff138 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 233 | } |