Project

General

Profile

JenkinsGroovy » History » Version 23

Andreas Kohlbecker, 08/30/2013 04:24 PM

1 20 Andreas Kohlbecker
{{>toc}}
2
3
4
5 1 Andreas Kohlbecker
6
# Groovy Scripts for Jenkins
7
8
9
You can evaluate groovy scripts in the Jenkins _Script Console_.
10
11
12
There are several plugins which can be _programmed_ with Groovy:
13
14 4 Andreas Kohlbecker
* [Groovy plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin)
15
16
* [Dynamic Parameter Plug-in](https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in)
17
18 9 Andreas Kohlbecker
* [Groovy Postbuild Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin)
19
20 4 Andreas Kohlbecker
* [Groovy Remote Control](http://groovy.codehaus.org/modules/remote/)
21
22
* [Scriptler plugin](https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin)
23 1 Andreas Kohlbecker
24
25
26
----
27
28 17 Andreas Kohlbecker
The most up to date scripts are found in the jenkins scriptler folders
29
30 18 Andreas Kohlbecker
* http://160.45.63.176/jenkins/scriptler/ git: http://160.45.63.176/jenkins/scriptler.git 
31 17 Andreas Kohlbecker
32
33
----
34
35 1 Andreas Kohlbecker
36 6 Andreas Kohlbecker
### Get all versions of an artifact from a maven repository
37
38 1 Andreas Kohlbecker
~~~
39
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
40 5 Andreas Kohlbecker
metadata.versioning.versions.version.each{
41
	  println "> " + it
42 1 Andreas Kohlbecker
}
43
~~~
44 2 Andreas Kohlbecker
45
doc:
46
47
* http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html
48 3 Andreas Kohlbecker
49 1 Andreas Kohlbecker
* http://www.ibm.com/developerworks/java/library/j-pg05199/
50 6 Andreas Kohlbecker
51
52 7 Andreas Kohlbecker
the same for the **Dynamic Parameter Plug-in** which is very nice in conjunction with the *[Repository Connector Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Repository+Connector+Plugin) 
53 6 Andreas Kohlbecker
54
55
~~~
56
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
57
def list = []
58
metadata.versioning.versions.version.each{
59
  list.add(it)
60
}
61 8 Andreas Kohlbecker
return return list.reverse(true)
62 6 Andreas Kohlbecker
~~~
63 10 Andreas Kohlbecker
64
65 19 Andreas Kohlbecker
### list datasource bean names in datasources.xml
66
67 1 Andreas Kohlbecker
~~~
68 21 Andreas Kohlbecker
/**
69
 * define the instances to excluded from the axix:
70
 */
71
def excludes = ['col', 'vibrant_index']
72
  
73 19 Andreas Kohlbecker
// list datasource bean names in datasources.xml
74
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
75 1 Andreas Kohlbecker
def instances = []
76 21 Andreas Kohlbecker
77 1 Andreas Kohlbecker
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
78 21 Andreas Kohlbecker
    if (! excludes.contains(it.attribute("id"))) {
79
        instances += it.attribute("id")
80
    }
81 19 Andreas Kohlbecker
}
82
return  instances
83
~~~
84
85
86 10 Andreas Kohlbecker
### find last bean in datasources.xml
87
88
89
~~~
90
// find last bean in datasources.xml
91
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
92
def lastBean = null
93
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
94
    lastBean = it
95
}
96
println lastBean
97
~~~
98 11 Andreas Kohlbecker
99
100 12 Andreas Kohlbecker
### monitor cdm server until all instances are started up
101 11 Andreas Kohlbecker
102
103
~~~
104 1 Andreas Kohlbecker
import groovy.json.JsonSlurper
105 12 Andreas Kohlbecker
import groovy.time.TimeCategory
106 11 Andreas Kohlbecker
107 1 Andreas Kohlbecker
// Configuration
108
def cdmserverUrl = "http://test.e-taxonomy.eu/cdmserver/"
109 12 Andreas Kohlbecker
def usr = "xxx"
110
def pwd = "yyy"
111
def timeout = 1 // minutes
112 16 Andreas Kohlbecker
113 12 Andreas Kohlbecker
// init
114
def timeOver
115
use(TimeCategory) {
116
  timeOver = timeout.minutes.from.now
117
}
118 1 Andreas Kohlbecker
119 12 Andreas Kohlbecker
def addr = cdmserverUrl + 'manage/BootloaderService.jsp'
120
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
121 1 Andreas Kohlbecker
def instances = null
122 12 Andreas Kohlbecker
def lastInstance = null
123 1 Andreas Kohlbecker
def allInstancesUp = false
124 15 Andreas Kohlbecker
def message = "Timeout"
125 12 Andreas Kohlbecker
  
126
// monitor the cdmserver until the last instance is up or until the time is over
127
while (new Date() < timeOver) {
128
  def conn = addr.toURL().openConnection()
129
  
130
  conn.setRequestProperty( "Authorization", "Basic ${authString}" )
131
  if( conn.responseCode == 200 ) {
132
    instances = new JsonSlurper().parseText( conn.content.text )
133 13 Andreas Kohlbecker
    lastInstance = instances.pop()
134
    println lastInstance.configuration.instanceName + ": " + lastInstance.status
135
    if(lastInstance.status == "started") {
136 15 Andreas Kohlbecker
      message "OK"
137 13 Andreas Kohlbecker
      allInstancesUp = true
138
      break
139
    }
140 12 Andreas Kohlbecker
  
141 1 Andreas Kohlbecker
  } else {
142 15 Andreas Kohlbecker
    message "Error on requesting for status information${conn.responseCode}: ${conn.responseMessage}"
143 1 Andreas Kohlbecker
  }
144 16 Andreas Kohlbecker
  println new Date()
145
  sleep(10000) // milliseconds
146 12 Andreas Kohlbecker
}
147 15 Andreas Kohlbecker
148
println message
149 13 Andreas Kohlbecker
150
return allInstancesUp
151 1 Andreas Kohlbecker
152
~~~
153 22 Andreas Kohlbecker
154
155
### execute other scriptler script
156
157
158 23 Andreas Kohlbecker
#### by using eval()
159 22 Andreas Kohlbecker
160
~~~
161
// prepare variables for subscript and evaluate it:
162
String subscript = "cdmserver-monitor-instance-status.groovy";
163
println "executing subscript ${subscript}"
164
// variables to be passed to the subscript must not be bound to the current scope, so no def or String here
165
status = (action == "start" ? "started" : "stopped")
166
  
167
def s = ScriptlerConfiguration.getConfiguration().getScriptById(subscript)
168
File scriptSrc = new File(ScriptlerManagment.getScriptDirectory(), s.getScriptPath());
169
~~~
170 1 Andreas Kohlbecker
171 22 Andreas Kohlbecker
172 23 Andreas Kohlbecker
#### by GroovyScript 
173 22 Andreas Kohlbecker
174
see [org.jenkinsci.plugins.scriptler.util.ScriptHelper](https://github.com/jenkinsci/scriptler-plugin/search?q=ScriptHelper&ref=cmdform)
175
176
~~~
177
MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, new StreamTaskListener(sos)));
178
~~~