forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
print-masters-plugins.groovy
130 lines (112 loc) · 3.47 KB
/
print-masters-plugins.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*** BEGIN META {
"name" : "Print all installed plugins for every Client Master",
"comment" : "This script prints all the installed plugins and their version for every online Client Master. It must be launched as a Groovy script from the CJOC server. There is one parameter "csv" that defines whether the output should be in csv or should be formatted",
"parameters" : [],
"core": "2.73.2.1",
"authors" : [
{ name : "Jean-Philippe Briend" }
{ name : "Isaac Cohen" }
]
} END META**/
import com.cloudbees.opscenter.server.model.*
import com.cloudbees.opscenter.server.clusterops.steps.*
// CSV parameter defines whether the output should be a CSV or formatted
def csv=0
def retour = '\n'
def matrix = [:]
def headers = ['Plugins']
// Loop over all online Client Masters
Jenkins.instance.getAllItems(ConnectedMaster.class).eachWithIndex{ it, index ->
headers.add(it.name)
if (it.channel) {
def stream = new ByteArrayOutputStream();
def listener = new StreamBuildListener(stream);
// Execute remote Groovy script in the Client Master
// Result of the execution must be a String
it.channel.call(new MasterGroovyClusterOpStep.Script("""
result = ''
for (plugin in Jenkins.instance.pluginManager.plugins) {
result = result + /"\${plugin.displayName}"/ + ',' + /"\${plugin.version}"\n/
}
return result
""", listener, "host-script.groovy", [:]))
retour = retour << "Master ${it.name}:\n${stream.toString().minus('Result: ')}"
stream.toString().eachLine { line, count ->
if (line?.trim()) {
matcher = ( line =~ /"(.*)"*","(.*)"/ )
if (matcher[0] && matcher[0].size() == 3) {
if (!matrix[matcher[0][1]]) {
matrix[matcher[0][1]] = []
}
matrix[matcher[0][1]][index] = matcher[0][2]
}
}
}
}
}
if (csv==0) {
printFormatted(headers, matrix)
} else {
printCSV(headers, matrix)
}
def printCSV(headers, matrix) {
// Print the headers
headers.eachWithIndex{ it, index ->
print "\"${it}\""
def delimiter = (index+1 == headers.size()) ? "\n" : ","
print delimiter
}
//Print the plugin versions
matrix.each{ k, v ->
print "\"${k}\","
v.eachWithIndex{ it, index ->
def value = (it) ? it : ""
print "\"${value}\""
def delimiter = (index+1 == v.size()) ? "\n" : ","
print delimiter
}
}
}
def printFormatted(headers, matrix) {
// Determine sizes to add padding
int keylength = 0
def valuelength = []
// Start with header sizes
headers.eachWithIndex { it, index ->
if (index == 0) {
keylength = headers[0].size()
} else {
valuelength[index-1] = headers[index-1].size()
}
}
// Determine the size of the Key + values
matrix.each{ k, v ->
keylength = (k.size() > keylength) ? k.size() : keylength
v.eachWithIndex{ it, index ->
(!valuelength[index]) ? 0 : valuelength[index]
if (it) {
valuelength[index] = (it.size() > valuelength[index]) ? it.size() : valuelength[index]
} else {
//any null values should be set
v[index] = ""
}
}
}
// Print the headers
headers.eachWithIndex{ it, index ->
def header = (index == 0) ? it.padRight(keylength) : it.padRight(valuelength[index-1])
print header
print " | "
}
print "\n"
//Print the plugin versions
matrix.each{ k, v ->
print k.padRight(keylength) + " | "
v.eachWithIndex{ it, index ->
print it.padRight(valuelength[index])
print " | "
}
print "\n"
}
}
return 0