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']) { |
| 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 | */ |
| 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 | */ |
| 128 | def 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 | */ |
| 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 | } |