forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedConfigFileProviderConfig.groovy
51 lines (44 loc) · 2.14 KB
/
sharedConfigFileProviderConfig.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
/*** BEGIN META {
"name" : "Push Config File Provider configuration to Client Masters",
"comment" : "Run from the Script Console. This script read the global configuration of the Config File Provider in CJOC and push it to all Client Masters. If a config file with identical ID already exists in the Client Master, it will be overridden.",
"parameters" : [],
"core": "2.73.2.1",
"authors" : [
{ name : "Allan Burdajewicz" }
]
} END META**/
import com.cloudbees.opscenter.server.clusterops.steps.MasterGroovyClusterOpStep
import com.cloudbees.opscenter.server.model.ConnectedMaster
import hudson.model.StreamBuildListener
import jenkins.model.Jenkins
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles
def result = '\n'
// Marshall the GlobalConfigFiles object to XML
def cfpConfigXml = Jenkins.instance.XSTREAM2.toXML(GlobalConfigFiles.get())
// Loop over all online Client Masters
Jenkins.instance.getAllItems(ConnectedMaster.class).eachWithIndex{ it, index ->
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("""
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles
import org.jenkinsci.lib.configprovider.ConfigProvider
GlobalConfigFiles currentStore = GlobalConfigFiles.get()
GlobalConfigFiles globalConfig = Jenkins.instance.XSTREAM2.fromXML('''${cfpConfigXml}''')
globalConfig.configs.each {config ->
if(currentStore.getById(config.id) != null) {
println "Overriding config file '\${config.id}'"
}
println "Saving config file '\${config.id}' of type '\${config.provider.id}'"
ConfigProvider.all().get(config.provider.class).save(config)
}
return
""", listener, "configFileProviderPush.groovy", [:]))
result = result << "Master ${it.name}:\n${stream.toString()}"
stream.toString().eachLine { line, count ->
print line + "\n"
}
}
}