Repository navigation
Expand file tree
/
Copy pathAddVersionsToComponents.groovy
More file actions
215 lines (189 loc) · 8 KB
/
Copy pathAddVersionsToComponents.groovy
File metadata and controls
215 lines (189 loc) · 8 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//This script is licensed under:
//Apache License
//Version 2.0, January 2004
//http://www.apache.org/licenses/
//Tested with IBM UrbanCode Deploy 6.2.6.1
//Command line usage:
//groovy -cp udclient.jar AddVerionsToComponents.groovy https://hostname:8443 username password ApplicationName numberOfVersions
//where ApplicationName is the name of the application.
//The script will add numberOfVersions Versions to each Component referenced by this application
//Example:
//groovy -cp udclient.jar AddVerionsToComponents.groovy https://localhost:8443 admin admin MyapplicationName 5
import org.apache.commons.lang.StringUtils
import org.apache.http.HttpHeaders
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.methods.HttpPut
import org.apache.http.entity.StringEntity
import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder;
import groovy.json.JsonSlurper
class Credentials{
String serverURL;
String username;
String password;
}
class Parameters{
String applicationName;
int numberOfVersions;
}
//sets the values of the Credentials class which contains the server url and the username/password
void initializeParameters(Credentials credentials, Parameters parameters){
//if the user provided the input parameters, we're running on the command line
if (args.size() == 5){
credentials.serverURL = args[0]
credentials.username = args[1]
credentials.password = args[2]
parameters.applicationName = args[3]
parameters.numberOfVersions = Integer.parseInt(args[4])
}
else{
println "Command Line usage: AddVersionsToComponents ServerURL username password ApplicationName numberOfVersions"
System.exit(1)
}
}
//Initializes an HttpClient that accepts all certificates
//depends on import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder
//included in HttpComponents-Util.jar, which is partly included in udclient.jar
HttpClient initializeClient(String username,String password){
HttpClientBuilder builder = new HttpClientBuilder();
builder.setPreemptiveAuthentication(true);
builder.setUsername(username);
builder.setPassword(password);
//Accept all certificates
builder.setTrustAllCerts(true);
//use proxy if defined
if (!StringUtils.isEmpty(System.getenv("PROXY_HOST")) &&
StringUtils.isNumeric(System.getenv("PROXY_PORT")))
{
builder.setProxyHost(System.getenv("PROXY_HOST"));
builder.setProxyPort(Integer.valueOf(System.getenv("PROXY_PORT")));
}
if (!StringUtils.isEmpty(System.getenv("PROXY_USERNAME")) &&
!StringUtils.isEmpty(System.getenv("PROXY_PASSWORD")))
{
builder.setProxyUsername(System.getenv("PROXY_USERNAME"));
builder.setProxyPassword(System.getenv("PROXY_PASSWORD"));
}
return builder.buildClient();
}
//Perform a given HttpRequest, assume answer is <= 299 , parse the outcome as JSON. Also release the connection for the Request.
Object performGetRequest(HttpClient client, String requestURL){
HttpRequest request = new HttpGet(requestURL);
//Execute the REST GET call
HttpResponse response = client.execute(request);
//Check that the call was successful
int statusCode = response.getStatusLine().getStatusCode();
if ( statusCode > 299 ) {
println "ERROR : HttpGet to: "+requestURL+ " returned: " +statusCode;
return null;
}
else{
//Convert the InputStream returned by response.getEntity().getContent() to a String
BufferedReader reader=new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
StringBuilder builder=new StringBuilder();
for(String line=null;(line=reader.readLine())!=null;){
builder.append(line).append("\n");
}
//Parse the returned JSON
//http://groovy-lang.org/json.html
JsonSlurper slurper = new JsonSlurper();
objects=slurper.parseText(builder.toString());
//Ensure to release the connection
request.releaseConnection();
return objects;
}
}
//returns a JSON Array that contains a JSON Object for each Component
Object getComponents(HttpClient client, String serverURL){
//prepare the GET call to the REST endpoint /cli/component
components=performGetRequest(client,serverURL+"/cli/component" )
return components;
}
//returns a JSON Array that contains a JSON Object for each Component
Object getComponent(HttpClient client, String serverURL, String componentId){
//prepare the GET call to the REST endpoint /cli/component/info
component=performGetRequest(client,serverURL+"/cli/component/info?component="+componentId )
return component;
}
//returns a JSON Array that contains a JSON Object for each version of a given Component
Object getVersions(HttpClient client, String serverURL, String componentID){
//prepare the GET call to the REST endpoint /cli/component/versions
versions=performGetRequest(client,serverURL+"/cli/component/versions?component="+componentID )
return versions;
}
Object getComponentsForApplication(client, serverURL, String applicationName){
componentsForApplication = new ArrayList();
components = getComponents(client, serverURL)
components.each{
componentId = it.id
componentName = it.name
component = getComponent(client, serverURL, componentId)
applications = component.applications
applications.each {
if(it.name.equals(applicationName)) {
componentsForApplication.add(componentId)
println "Found Component name: "+componentName
}
}
}
return componentsForApplication
}
void addVersionsToComponent(HttpClient client, String serverURL, String componentID, ArrayList versionNames) {
versionNames.each {
performPostRequest(client,serverURL+"/cli/version/createVersion?component="+componentID+"&name="+it)
}
}
//Perform a given HttpRequest, assume answer is <= 299. Also release the connection for the Request.
Object performPostRequest(HttpClient client, String requestURL){
HttpRequest postRequest = new HttpPost(requestURL);
//Set the ACCEPT Header to indicate that the client expects to receive a json formatted response
postRequest.setHeader(HttpHeaders.ACCEPT, "application/json");
//Execute the REST POST call
HttpResponse response = client.execute(postRequest);
//Check that the call was successful
int statusCode = response.getStatusLine().getStatusCode();
if ( statusCode > 299 ) {
println "ERROR : HttpPost to: "+requestURL+ " returned: " +statusCode;
}
else {
//Convert the InputStream returned by response.getEntity().getContent() to a String
BufferedReader reader=new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
StringBuilder builder=new StringBuilder();
for(String line=null;(line=reader.readLine())!=null;){
builder.append(line).append("\n");
}
//Parse the returned JSON
//http://groovy-lang.org/json.html
JsonSlurper slurper = new JsonSlurper();
objects=slurper.parseText(builder.toString());
//Ensure to release the connection
postRequest.releaseConnection();
return objects;
}
}
//generate version names which have an initial integer prefix followed by a unique id
ArrayList generateVersionNames(int numberOfVersions) {
ArrayList names = new ArrayList()
for(int i =0; i < numberOfVersions; i++)
{
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
names.add(randomUUIDString);
}
return names
}
//Main script contents
Credentials credentials = new Credentials();
Parameters parameters = new Parameters();
initializeParameters(credentials, parameters);
HttpClient client = initializeClient(credentials.username,credentials.password);
//Loop over components and find if they belong to the wanted application
componentIds = getComponentsForApplication(client,credentials.serverURL, parameters.applicationName)
componentIds.each {
componentId = it
versionNames = generateVersionNames(parameters.numberOfVersions)
addVersionsToComponent(client, credentials.serverURL, componentId, versionNames)
}