-
-
Save nom3ad/7c6abac59e43d13aa4a697a985c5a639 to your computer and use it in GitHub Desktop.
Idle hours for jenkins ec2 fleet plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazon.jenkins.ec2fleet.EC2FleetCloud | |
def changeFleetWorkers(name, minSize, maxSize, idleMins) { | |
Jenkins jenkins = Jenkins.getInstance() | |
println "Changing worker ${name} to minSize: ${minSize}, maxSize: ${maxSize}, idleMins: ${idleMins}" | |
oldCloud = jenkins.getCloud(name) | |
oldMinSize = oldCloud.getMinSize() | |
oldMaxSize = oldCloud.getMaxSize() | |
oldIdleMins = oldCloud.getIdleMinutes() | |
println "Old settings: oldMinSize: ${oldMinSize}, oldMaxSize: ${oldMaxSize}, oldIdleMins: ${oldIdleMins}" | |
if (oldMinSize == minSize && oldMaxSize == maxSize && oldIdleMins == idleMins) { | |
println "Exitting because all settings are equal" | |
} | |
// find detailed information about parameters on plugin config page or | |
// https://github.com/jenkinsci/ec2-fleet-plugin/blob/master/src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetCloud.java | |
EC2FleetCloud ec2FleetCloud = new EC2FleetCloud( | |
name, // fleetCloudName | |
oldCloud.getOldId(), | |
oldCloud.getAwsCredentialsId(), | |
"", | |
oldCloud.getRegion(), | |
oldCloud.getEndpoint(), | |
oldCloud.getFleet(), | |
oldCloud.getLabelString(), // labels | |
oldCloud.getFsRoot(), // fs root | |
oldCloud.getComputerConnector(), | |
oldCloud.isPrivateIpUsed(), // if need to use privateIpUsed | |
oldCloud.isAlwaysReconnect(), // if need alwaysReconnect | |
idleMins, // if need to allow downscale set > 0 in min | |
minSize, // minSize | |
maxSize, // maxSize | |
oldCloud.getNumExecutors(), // numExecutors | |
oldCloud.isAddNodeOnlyIfRunning(), // addNodeOnlyIfRunning | |
oldCloud.isRestrictUsage(), // restrictUsage allow execute only jobs with proper label | |
oldCloud.isDisableTaskResubmit(), //disableTaskResubmit | |
oldCloud.getInitOnlineTimeoutSec(), //initOnlineTimeoutSec | |
oldCloud.getInitOnlineCheckIntervalSec(), //initOnlineCheckIntervalSec | |
oldCloud.isScaleExecutorsByWeight(), //scaleExecutorsByWeight | |
oldCloud.getCloudStatusIntervalSec(), //cloudStatusIntervalSec | |
oldCloud.isNoDelayProvision() //noDelayProvision | |
) | |
jenkins.clouds.remove(oldCloud) | |
// add cloud configuration to Jenkins | |
jenkins.clouds.add(ec2FleetCloud) | |
// save current Jenkins state to disk | |
jenkins.save() | |
println "Done" | |
} | |
def scaleByTime(name, busyStartHour, busyEndHour, busyMinSize, busyMaxSize, busyIdleTime, lazyMinSize, lazyMaxSize, lazyIdleTime) { | |
Calendar date = Calendar.getInstance(); | |
def hour = date.get(Calendar.HOUR_OF_DAY); | |
def dayOfWeek = date.get(Calendar.DAY_OF_WEEK); | |
def isWeekend = false | |
println "Processing ${name}" | |
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY) { | |
isWeekend = true; | |
} | |
if (!isWeekend && hour >= busyStartHour && hour <= busyEndHour) { | |
println "Applying busy settings" | |
changeFleetWorkers(name, busyMinSize, busyMaxSize, busyIdleTime) | |
} else { | |
println "Applying lazy settings" | |
changeFleetWorkers(name, lazyMinSize, lazyMaxSize, lazyIdleTime) | |
} | |
} | |
return this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent { | |
label 'jenkins-worker-light' | |
} | |
triggers { | |
cron(env.BRANCH_NAME == 'master' ? '*/15 * * * *' : '') | |
} | |
options { | |
timeout(time: 60, unit: 'SECONDS', activity: false) | |
} | |
stages { | |
stage('run') { | |
steps { | |
script { | |
ec2fleet = load "scale-workers/ec2fleet.groovy" | |
busyStartHour = 9 | |
busyEndHour = 19 | |
ec2fleet.scaleByTime( | |
"jenkins-fleet-light", //name | |
busyStartHour, // busyStartHour | |
busyEndHour, // busyEndHour | |
1, // busyMinSize | |
1, // busyMaxSize | |
60, // busyIdleTine | |
0, // lazyMinSize | |
1, // lazyMaxSize | |
30 // lazyIdleTine | |
) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment