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 | /** |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 11 | * Tests if current user belongs to given group |
| 12 | * @param groupName name of the group you want to verify user presence |
| 13 | * @return boolean result |
| 14 | */ |
| 15 | def currentUserInGroup(groupName){ |
Jakub Josef | 052e963 | 2018-02-14 14:10:51 +0100 | [diff] [blame] | 16 | return currentUserInGroups([groupName]) |
| 17 | } |
| 18 | /** |
| 19 | * Tests if current user belongs to at least one of given groups |
| 20 | * @param groups list of group names you want to verify user presence |
| 21 | * @return boolean result |
| 22 | */ |
| 23 | def currentUserInGroups(groups){ |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 24 | def hasAccess = false |
| 25 | wrap([$class: 'BuildUser']) { |
| 26 | def authorities = Jenkins.instance.securityRealm.loadUserByUsername(BUILD_USER).getAuthorities() |
| 27 | for(int i=0;i < authorities.size();i++){ |
Jakub Josef | 052e963 | 2018-02-14 14:10:51 +0100 | [diff] [blame] | 28 | if(groups.contains(authorities[i])){ |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 29 | hasAccess=true |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return hasAccess |
| 35 | } |
| 36 | |
| 37 | /** |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 38 | * Get Jenkins job running builds |
| 39 | * @param jobName job name |
| 40 | * @return list of running builds |
| 41 | */ |
Jakub Josef | fbe8c7c | 2017-05-11 13:35:11 +0200 | [diff] [blame] | 42 | @NonCPS |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 43 | def getJobRunningBuilds(jobName){ |
Jakub Josef | fbe8c7c | 2017-05-11 13:35:11 +0200 | [diff] [blame] | 44 | def job = Jenkins.instance.items.find{it -> it.name.equals(jobName)} |
| 45 | if(job){ |
| 46 | return job.builds.findAll{build -> build.isBuilding()} |
| 47 | } |
| 48 | return [] |
Jakub Josef | 93f08e2 | 2017-06-05 19:14:53 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @NonCPS |
| 52 | def getRunningBuilds(job){ |
| 53 | return job.builds.findAll{build -> build.isBuilding()} |
| 54 | } |
| 55 | |
| 56 | @NonCPS |
| 57 | def killStuckBuilds(maxSeconds, job){ |
| 58 | def common = new com.mirantis.mk.Common() |
| 59 | def result = true |
| 60 | def runningBuilds = getRunningBuilds(job) |
| 61 | def jobName = job.name |
| 62 | for(int j=0; j < runningBuilds.size(); j++){ |
| 63 | int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0 |
| 64 | if(durationInSeconds > maxSeconds){ |
| 65 | result = false |
| 66 | def buildId = runningBuilds[j].id |
| 67 | common.infoMsg("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s") |
| 68 | try{ |
| 69 | runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer")); |
| 70 | result = true |
| 71 | }catch(e){ |
| 72 | common.errorMsg("Error occured during aborting build: Exception: ${e}") |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return result |
Jakub Josef | d44b697 | 2018-01-23 17:55:57 +0100 | [diff] [blame] | 77 | } |
Richard Felkl | 838892f | 2018-06-12 17:58:20 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Get Jenkins job object |
| 81 | * @param jobName job name |
| 82 | * @return job object that matches jobName |
| 83 | */ |
| 84 | def getJobByName(jobName){ |
| 85 | for(item in Hudson.instance.items) { |
| 86 | if(item.name == jobName){ |
| 87 | return item |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get Jenkins job parameters |
| 94 | * @param jobName job name |
| 95 | * @return HashMap with parameter names as keys and their values as values |
| 96 | */ |
| 97 | def getJobParameters(jobName){ |
| 98 | def job = getJobByName(jobName) |
| 99 | def prop = job.getProperty(ParametersDefinitionProperty.class) |
| 100 | def params = new java.util.HashMap<String,String>() |
| 101 | if(prop != null) { |
| 102 | for(param in prop.getParameterDefinitions()) { |
| 103 | params.put(param.name, param.defaultValue) |
| 104 | } |
| 105 | } |
| 106 | return params |
| 107 | } |
Denis Egorenko | b5208e4 | 2018-12-14 15:46:36 +0400 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Get list of causes actions for given build |
| 111 | * |
| 112 | * @param build Job build object (like, currentBuild.rawBuild) |
| 113 | * @return list of causes actions for given build |
| 114 | */ |
| 115 | @NonCPS |
| 116 | def getBuildCauseActions(build) { |
| 117 | def causeAction = build.actions.find { it -> it instanceof hudson.model.CauseAction } |
| 118 | if(causeAction) { |
| 119 | return causeAction.causes |
| 120 | } else { |
| 121 | return [] |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get list of builds, triggered by Gerrit with given build |
| 127 | * @param build Job build object (like, currentBuild.rawBuild) |
| 128 | * @return list of builds with names and numbers |
| 129 | */ |
| 130 | @NonCPS |
| 131 | def getGerritBuildContext(build) { |
| 132 | def causes = getBuildCauseActions(build) |
| 133 | if (causes) { |
| 134 | def gerritTriggerCause = causes.find { cause -> |
| 135 | cause instanceof com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause |
| 136 | } |
| 137 | return gerritTriggerCause.context.getOtherBuilds() |
| 138 | } else { |
| 139 | return [] |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Wait for other jobs |
| 145 | * @param config config parameter: |
| 146 | * builds - List of job build objects, which should be checked |
| 147 | * checkBuilds - List of job names or regexps, which should be used to check provided builds list |
| 148 | * regexp - Wheither to use regexp or simple string matching |
| 149 | */ |
| 150 | def waitForOtherBuilds(LinkedHashMap config){ |
| 151 | def common = new com.mirantis.mk.Common() |
| 152 | def builds = config.get('builds') |
| 153 | def checkBuilds = config.get('checkBuilds') |
| 154 | def regexp = config.get('regexp', false) |
| 155 | def waitForBuilds = builds.findAll { build -> |
| 156 | def jobName = build.fullDisplayName.tokenize(' ')[0] |
| 157 | if (regexp) { |
| 158 | checkBuilds.find { jobName ==~ it } |
| 159 | } else { |
| 160 | jobName in checkBuilds |
| 161 | } |
| 162 | } |
| 163 | if (waitForBuilds) { |
| 164 | def waiting = true |
| 165 | common.infoMsg("Waiting for next jobs: ${waitForBuilds}") |
| 166 | while(waiting) { |
| 167 | waiting = false |
| 168 | waitForBuilds.each { job -> |
| 169 | if (job.inProgress) { |
| 170 | waiting = true |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |