forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listEC2InstancesOnAccount.groovy
32 lines (30 loc) · 1.51 KB
/
listEC2InstancesOnAccount.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
Author: kuisathaverat
Description: List all EC2 instances owner by the Account configured on the EC2 Cloud configurationn, it helps to diagnose EC2 Plugin issues.
see also https://github.com/jenkinsci/jenkins-scripts/pull/98 for list only EC2 templates instances.
**/
import com.amazonaws.services.ec2.model.InstanceStateName
Jenkins.instance.clouds.findAll{it -> it instanceof hudson.plugins.ec2.AmazonEC2Cloud}
.each{ c ->
println c.getCloudName() + ' - ' + c.getRegion() + ' - CAP:' + c.instanceCap
int running = 0
int terminated = 0
int shuttingdown = 0
c.connect()?.describeInstances()?.getReservations().each{ r->
r?.getInstances().each{ i ->
InstanceStateName stateName = InstanceStateName.fromValue(i.getState().getName());
if (stateName != InstanceStateName.Terminated && stateName != InstanceStateName.ShuttingDown) {
running++
} else if (stateName == InstanceStateName.Terminated) {
terminated++
} else if (stateName == InstanceStateName.ShuttingDown) {
shuttingdown++
}
println "\t\tExisting instance found: " + i.getInstanceId() + " AMI: " + i.getImageId() + ' - State:' + stateName + ' - Description' + i.getTags()
}
}
println "\tTotal Intances Running:" + running
println "\tTotal Intances Terminated:" + terminated
println "\tTotal Intances ShuttingDown:" + shuttingdown
}
return