blob: d092153f3d7a88e8b2489af7517f249263878a5e [file] [log] [blame]
Jakub Josef4edd7432017-05-10 17:58:56 +02001package com.mirantis.mk
Jakub Josef93f08e22017-06-05 19:14:53 +02002import com.cloudbees.groovy.cps.NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +02003
4/**
5 *
6 * Jenkins common functions
7 *
8 */
9
10/**
Jakub Josefd44b6972018-01-23 17:55:57 +010011 * 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 */
15def currentUserInGroup(groupName){
Jakub Josef052e9632018-02-14 14:10:51 +010016 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 */
23def currentUserInGroups(groups){
Jakub Josefd44b6972018-01-23 17:55:57 +010024 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 Josef052e9632018-02-14 14:10:51 +010028 if(groups.contains(authorities[i])){
Jakub Josefd44b6972018-01-23 17:55:57 +010029 hasAccess=true
30 break
31 }
32 }
33 }
34 return hasAccess
35}
36
37/**
Jakub Josef4edd7432017-05-10 17:58:56 +020038 * Get Jenkins job running builds
39 * @param jobName job name
40 * @return list of running builds
41 */
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020042@NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +020043def getJobRunningBuilds(jobName){
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020044 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 Josef93f08e22017-06-05 19:14:53 +020049}
50
51@NonCPS
52def getRunningBuilds(job){
53 return job.builds.findAll{build -> build.isBuilding()}
54}
55
56@NonCPS
57def 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 Josefd44b6972018-01-23 17:55:57 +010077}
Richard Felkl838892f2018-06-12 17:58:20 +020078
79/**
80 * Get Jenkins job object
81 * @param jobName job name
82 * @return job object that matches jobName
83 */
84def 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 */
97def 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}