Skip to content

Instantly share code, notes, and snippets.

@fedevegili
Created June 22, 2024 14:45
Show Gist options
  • Save fedevegili/f1a8bdcf2bc8e2f694dced7fa71c87aa to your computer and use it in GitHub Desktop.
Save fedevegili/f1a8bdcf2bc8e2f694dced7fa71c87aa to your computer and use it in GitHub Desktop.
ec2-fleet-plugin change scale overnight
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(), // oldId
oldCloud.getAwsCredentialsId(), // awsCredentialsId
"", // credentialsId
oldCloud.getRegion(), // region
oldCloud.getEndpoint(), // endpoint
oldCloud.getFleet(), // fleet
oldCloud.getLabelString(), // labelString
oldCloud.getFsRoot(), // fs root
oldCloud.getComputerConnector(), // computerConnector
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
0,
oldCloud.getNumExecutors(), // numExecutors
oldCloud.isAddNodeOnlyIfRunning(), // addNodeOnlyIfRunning
oldCloud.isRestrictUsage(), // restrictUsage allow execute only jobs with proper label
"-1", // maxTotalUses
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, veryBusyStartHour, veryBusyEndHour, veryBusyMinSize, veryBusyMaxSize, 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 >= veryBusyStartHour && hour <= veryBusyEndHour) {
println "Applying VERY BUSY settings"
changeFleetWorkers(name, veryBusyMinSize, veryBusyMaxSize, busyIdleTime)
} else 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
pipeline {
agent {
label 'jenkins-worker-master'
}
triggers {
cron(env.BRANCH_NAME == 'master' ? '10 * * * *' : '')
}
options {
timeout(time: 60, unit: 'SECONDS', activity: false)
buildDiscarder(logRotator(numToKeepStr: '30'))
}
stages {
stage('run') {
steps {
script {
ec2fleet = load "scale-workers/ec2fleet.groovy"
busyStartHour = 8
busyEndHour = 21
veryBusyStartHour = 13
veryBusyEndHour = 18
ec2fleet.scaleByTime(
"jenkins-fleet-light", //name
busyStartHour, // busyStartHour
busyEndHour, // busyEndHour
1, // busyMinSize
3, // busyMaxSize
5, // busyIdleTime
veryBusyStartHour,
veryBusyEndHour,
1, // veryBusyMinSize
3, // veryBusyMaxSize
0, // lazyMinSize
2, // lazyMaxSize
30 // lazyIdleTime
)
ec2fleet.scaleByTime(
"jenkins-fleet-heavy", //name
busyStartHour, // busyStartHour
busyEndHour, // busyEndHour
2, // busyMinSize
20, // busyMaxSize
15, // busyIdleTime
veryBusyStartHour,
veryBusyEndHour,
3, // veryBusyMinSize
20, // veryBusyMaxSize
0, // lazyMinSize
6, // lazyMaxSize
5 // lazyIdleTime
)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment