Project

General

Profile

JenkinsGroovy » History » Version 19

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

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 17 Andreas Kohlbecker
The most up to date scripts are found in the jenkins scriptler folders
25
26 18 Andreas Kohlbecker
* http://160.45.63.176/jenkins/scriptler/ git: http://160.45.63.176/jenkins/scriptler.git 
27 17 Andreas Kohlbecker
28
29
----
30
31 1 Andreas Kohlbecker
32 6 Andreas Kohlbecker
### Get all versions of an artifact from a maven repository
33
34 1 Andreas Kohlbecker
~~~
35
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
36 5 Andreas Kohlbecker
metadata.versioning.versions.version.each{
37
	  println "> " + it
38 1 Andreas Kohlbecker
}
39
~~~
40 2 Andreas Kohlbecker
41
doc:
42
43
* http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html
44 3 Andreas Kohlbecker
45 1 Andreas Kohlbecker
* http://www.ibm.com/developerworks/java/library/j-pg05199/
46 6 Andreas Kohlbecker
47
48 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) 
49 6 Andreas Kohlbecker
50
51
~~~
52
def metadata = new XmlSlurper().parse("http://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
53
def list = []
54
metadata.versioning.versions.version.each{
55
  list.add(it)
56
}
57 8 Andreas Kohlbecker
return return list.reverse(true)
58 6 Andreas Kohlbecker
~~~
59 10 Andreas Kohlbecker
60
61 19 Andreas Kohlbecker
### list datasource bean names in datasources.xml
62
63
64
~~~
65
// list datasource bean names in datasources.xml
66
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
67
def instances = []
68
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
69
    instances += it.attribute("id")
70
}
71
return  instances
72
~~~
73
74
75 10 Andreas Kohlbecker
### find last bean in datasources.xml
76
77
78
~~~
79
// find last bean in datasources.xml
80
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
81
def lastBean = null
82
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
83
    lastBean = it
84
}
85
println lastBean
86
~~~
87 11 Andreas Kohlbecker
88
89 12 Andreas Kohlbecker
### monitor cdm server until all instances are started up
90 11 Andreas Kohlbecker
91
92
~~~
93 1 Andreas Kohlbecker
import groovy.json.JsonSlurper
94 12 Andreas Kohlbecker
import groovy.time.TimeCategory
95 11 Andreas Kohlbecker
96 1 Andreas Kohlbecker
// Configuration
97
def cdmserverUrl = "http://test.e-taxonomy.eu/cdmserver/"
98 12 Andreas Kohlbecker
def usr = "xxx"
99
def pwd = "yyy"
100
def timeout = 1 // minutes
101 16 Andreas Kohlbecker
102 12 Andreas Kohlbecker
// init
103
def timeOver
104
use(TimeCategory) {
105
  timeOver = timeout.minutes.from.now
106
}
107 1 Andreas Kohlbecker
108 12 Andreas Kohlbecker
def addr = cdmserverUrl + 'manage/BootloaderService.jsp'
109
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
110 1 Andreas Kohlbecker
def instances = null
111 12 Andreas Kohlbecker
def lastInstance = null
112 1 Andreas Kohlbecker
def allInstancesUp = false
113 15 Andreas Kohlbecker
def message = "Timeout"
114 12 Andreas Kohlbecker
  
115
// monitor the cdmserver until the last instance is up or until the time is over
116
while (new Date() < timeOver) {
117
  def conn = addr.toURL().openConnection()
118
  
119
  conn.setRequestProperty( "Authorization", "Basic ${authString}" )
120
  if( conn.responseCode == 200 ) {
121
    instances = new JsonSlurper().parseText( conn.content.text )
122 13 Andreas Kohlbecker
    lastInstance = instances.pop()
123
    println lastInstance.configuration.instanceName + ": " + lastInstance.status
124
    if(lastInstance.status == "started") {
125 15 Andreas Kohlbecker
      message "OK"
126 13 Andreas Kohlbecker
      allInstancesUp = true
127
      break
128
    }
129 12 Andreas Kohlbecker
  
130 1 Andreas Kohlbecker
  } else {
131 15 Andreas Kohlbecker
    message "Error on requesting for status information${conn.responseCode}: ${conn.responseMessage}"
132 1 Andreas Kohlbecker
  }
133 16 Andreas Kohlbecker
  println new Date()
134
  sleep(10000) // milliseconds
135 12 Andreas Kohlbecker
}
136 15 Andreas Kohlbecker
137
println message
138 13 Andreas Kohlbecker
139
return allInstancesUp
140 1 Andreas Kohlbecker
141
~~~