forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-shared-clouds-json.groovy
43 lines (40 loc) · 1.17 KB
/
get-shared-clouds-json.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
33
34
35
36
37
38
39
40
41
42
43
/*** BEGIN META {
"name" : "Get Shared Clouds status JSON",
"comment" : "Run this script to save the output as variable to process in the client
"parameters" : [],
"core": "2.440.1.4",
"authors" : [
{ name : "Carlos Rodriguez Lopez" }
]
} END META**/
import groovy.json.JsonOutput
def shareCloud = []
Jenkins.instance.allItems.grep {
it.class.name == 'com.cloudbees.opscenter.server.model.SharedCloud'
}.each {
def jnlpCloud = it?.cloud
def connected = jnlpCloud?.countConnectedSlaves() ?: 0
def available = jnlpCloud?.countAvailableSlaves() ?: 0
def inuse = connected - available
def inuse_ratio = 0
if (inuse != 0 || connected != 0) {
inuse_ratio = (connected - available) / connected
}
def availablenodes = []
jnlpCloud?.channelStates.each { c ->
if (c.available) {
availablenodes.add(c?.name)
}
}
def shareCloudItem = [
name: "${it?.name}",
connected: "$connected",
available: "$available",
inuse_ratio: "$inuse_ratio",
free_vms: availablenodes
]
shareCloud << shareCloudItem
}
def jsonShareCloud = JsonOutput.toJson(shareCloud)
println jsonShareCloud
return this