Project

General

Profile

JenkinsGroovy » History » Version 20

Andreas Kohlbecker, 08/21/2013 09:40 AM

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
68
~~~
69
// list datasource bean names in datasources.xml
70
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
71
def instances = []
72
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
73
    instances += it.attribute("id")
74
}
75
return  instances
76
~~~
77
78
79 10 Andreas Kohlbecker
### find last bean in datasources.xml
80
81
82
~~~
83
// find last bean in datasources.xml
84
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
85
def lastBean = null
86
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
87
    lastBean = it
88
}
89
println lastBean
90
~~~
91 11 Andreas Kohlbecker
92
93 12 Andreas Kohlbecker
### monitor cdm server until all instances are started up
94 11 Andreas Kohlbecker
95
96
~~~
97 1 Andreas Kohlbecker
import groovy.json.JsonSlurper
98 12 Andreas Kohlbecker
import groovy.time.TimeCategory
99 11 Andreas Kohlbecker
100 1 Andreas Kohlbecker
// Configuration
101
def cdmserverUrl = "http://test.e-taxonomy.eu/cdmserver/"
102 12 Andreas Kohlbecker
def usr = "xxx"
103
def pwd = "yyy"
104
def timeout = 1 // minutes
105 16 Andreas Kohlbecker
106 12 Andreas Kohlbecker
// init
107
def timeOver
108
use(TimeCategory) {
109
  timeOver = timeout.minutes.from.now
110
}
111 1 Andreas Kohlbecker
112 12 Andreas Kohlbecker
def addr = cdmserverUrl + 'manage/BootloaderService.jsp'
113
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
114 1 Andreas Kohlbecker
def instances = null
115 12 Andreas Kohlbecker
def lastInstance = null
116 1 Andreas Kohlbecker
def allInstancesUp = false
117 15 Andreas Kohlbecker
def message = "Timeout"
118 12 Andreas Kohlbecker
  
119
// monitor the cdmserver until the last instance is up or until the time is over
120
while (new Date() < timeOver) {
121
  def conn = addr.toURL().openConnection()
122
  
123
  conn.setRequestProperty( "Authorization", "Basic ${authString}" )
124
  if( conn.responseCode == 200 ) {
125
    instances = new JsonSlurper().parseText( conn.content.text )
126 13 Andreas Kohlbecker
    lastInstance = instances.pop()
127
    println lastInstance.configuration.instanceName + ": " + lastInstance.status
128
    if(lastInstance.status == "started") {
129 15 Andreas Kohlbecker
      message "OK"
130 13 Andreas Kohlbecker
      allInstancesUp = true
131
      break
132
    }
133 12 Andreas Kohlbecker
  
134 1 Andreas Kohlbecker
  } else {
135 15 Andreas Kohlbecker
    message "Error on requesting for status information${conn.responseCode}: ${conn.responseMessage}"
136 1 Andreas Kohlbecker
  }
137 16 Andreas Kohlbecker
  println new Date()
138
  sleep(10000) // milliseconds
139 12 Andreas Kohlbecker
}
140 15 Andreas Kohlbecker
141
println message
142 13 Andreas Kohlbecker
143
return allInstancesUp
144 1 Andreas Kohlbecker
145
~~~