Project

General

Profile

JenkinsGroovy » History » Version 15

Andreas Kohlbecker, 08/15/2013 07:52 PM

1 1 Andreas Kohlbecker
2
# Groovy Scripts for Jenkins
3
4
5
You can evaluate groovy scripts in the Jenkins _Script Console_.
6
7
8
There are several plugins which can be _programmed_ with Groovy:
9
10 4 Andreas Kohlbecker
* [Groovy plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin)
11
12
* [Dynamic Parameter Plug-in](https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in)
13
14 9 Andreas Kohlbecker
* [Groovy Postbuild Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin)
15
16 4 Andreas Kohlbecker
* [Groovy Remote Control](http://groovy.codehaus.org/modules/remote/)
17
18
* [Scriptler plugin](https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin)
19 1 Andreas Kohlbecker
20
21
22
----
23
24
25 6 Andreas Kohlbecker
### Get all versions of an artifact from a maven repository
26
27 1 Andreas Kohlbecker
~~~
28
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
29 5 Andreas Kohlbecker
metadata.versioning.versions.version.each{
30
	  println "> " + it
31 1 Andreas Kohlbecker
}
32
~~~
33 2 Andreas Kohlbecker
34
doc:
35
36
* http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html
37 3 Andreas Kohlbecker
38 1 Andreas Kohlbecker
* http://www.ibm.com/developerworks/java/library/j-pg05199/
39 6 Andreas Kohlbecker
40
41 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) 
42 6 Andreas Kohlbecker
43
44
~~~
45
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
46
def list = []
47
metadata.versioning.versions.version.each{
48
  list.add(it)
49
}
50 8 Andreas Kohlbecker
return return list.reverse(true)
51 6 Andreas Kohlbecker
~~~
52 10 Andreas Kohlbecker
53
54
### find last bean in datasources.xml
55
56
57
~~~
58
// find last bean in datasources.xml
59
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
60
def lastBean = null
61
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
62
    lastBean = it
63
}
64
println lastBean
65
~~~
66 11 Andreas Kohlbecker
67
68 12 Andreas Kohlbecker
### monitor cdm server until all instances are started up
69 11 Andreas Kohlbecker
70
71
~~~
72 1 Andreas Kohlbecker
import groovy.json.JsonSlurper
73 12 Andreas Kohlbecker
import groovy.time.TimeCategory
74 11 Andreas Kohlbecker
75 1 Andreas Kohlbecker
// Configuration
76
def cdmserverUrl = "http://test.e-taxonomy.eu/cdmserver/"
77 12 Andreas Kohlbecker
def usr = "xxx"
78
def pwd = "yyy"
79
def timeout = 1 // minutes
80
// init
81
def timeOver
82
use(TimeCategory) {
83
  timeOver = timeout.minutes.from.now
84
}
85 1 Andreas Kohlbecker
86 12 Andreas Kohlbecker
def addr = cdmserverUrl + 'manage/BootloaderService.jsp'
87
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
88 1 Andreas Kohlbecker
def instances = null
89 12 Andreas Kohlbecker
def lastInstance = null
90 1 Andreas Kohlbecker
def allInstancesUp = false
91 15 Andreas Kohlbecker
def message = "Timeout"
92 12 Andreas Kohlbecker
  
93
// monitor the cdmserver until the last instance is up or until the time is over
94
while (new Date() < timeOver) {
95
  def conn = addr.toURL().openConnection()
96
  
97
  conn.setRequestProperty( "Authorization", "Basic ${authString}" )
98
  if( conn.responseCode == 200 ) {
99
    instances = new JsonSlurper().parseText( conn.content.text )
100 13 Andreas Kohlbecker
    lastInstance = instances.pop()
101
    println lastInstance.configuration.instanceName + ": " + lastInstance.status
102
    if(lastInstance.status == "started") {
103 15 Andreas Kohlbecker
      message "OK"
104 13 Andreas Kohlbecker
      allInstancesUp = true
105
      break
106
    }
107 12 Andreas Kohlbecker
  
108 1 Andreas Kohlbecker
  } else {
109 15 Andreas Kohlbecker
    message "Error on requesting for status information${conn.responseCode}: ${conn.responseMessage}"
110 13 Andreas Kohlbecker
  }
111 15 Andreas Kohlbecker
  wait(10000) // milliseconds
112 12 Andreas Kohlbecker
}
113 15 Andreas Kohlbecker
114
println message
115 13 Andreas Kohlbecker
116
return allInstancesUp
117 1 Andreas Kohlbecker
118
~~~