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 | } |