blob: 3288560badf6b1ab83324f3ae39c2cbe96ecad3e [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/**
Kirill Mashchenko43001092018-12-25 05:28:53 +040011 * Returns a list of groups which user belongs
12 * @param username String
13 * @return list of groups [String]
Jakub Josefd44b6972018-01-23 17:55:57 +010014 */
Kirill Mashchenko43001092018-12-25 05:28:53 +040015def userGroups(username) {
16 res = []
17 def authorities = Jenkins.instance.securityRealm.loadUserByUsername(username).getAuthorities()
18 authorities.each {
19 res.add(it.toString())
Jakub Josefd44b6972018-01-23 17:55:57 +010020 }
Kirill Mashchenko43001092018-12-25 05:28:53 +040021 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 */
30def 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 */
41def userInGroups(username, groups) {
42 return groups.any{userInGroup(username, it)}
43}
44
45/**
46 * Returns current username from build
47 * @return username String
48 */
49def currentUsername() {
50 username = ''
51 wrap([$class: 'BuildUser']) {
52 username = BUILD_USER
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 */
66def 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 */
76def currentUserInGroup(group) {
77 username = currentUsername()
78 return userInGroup(username, group)
Jakub Josefd44b6972018-01-23 17:55:57 +010079}
80
81/**
Jakub Josef4edd7432017-05-10 17:58:56 +020082 * Get Jenkins job running builds
83 * @param jobName job name
84 * @return list of running builds
85 */
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020086@NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +020087def getJobRunningBuilds(jobName){
Kirill Mashchenko43001092018-12-25 05:28:53 +040088 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 Josef93f08e22017-06-05 19:14:53 +020093}
94
95@NonCPS
96def getRunningBuilds(job){
Kirill Mashchenko43001092018-12-25 05:28:53 +040097 return job.builds.findAll{build -> build.isBuilding()}
Jakub Josef93f08e22017-06-05 19:14:53 +020098}
99
100@NonCPS
101def killStuckBuilds(maxSeconds, job){
Kirill Mashchenko43001092018-12-25 05:28:53 +0400102 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 Josef93f08e22017-06-05 19:14:53 +0200119 }
Kirill Mashchenko43001092018-12-25 05:28:53 +0400120 return result
Jakub Josefd44b6972018-01-23 17:55:57 +0100121}
Richard Felkl838892f2018-06-12 17:58:20 +0200122
123/**
124 * Get Jenkins job object
125 * @param jobName job name
126 * @return job object that matches jobName
127 */
128def getJobByName(jobName){
129 for(item in Hudson.instance.items) {
130 if(item.name == jobName){
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 */
141def 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}