Repository navigation
Expand file tree
/
Copy pathListComponentsWithSize.groovy
More file actions
178 lines (167 loc) · 7.11 KB
/
Copy pathListComponentsWithSize.groovy
File metadata and controls
178 lines (167 loc) · 7.11 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
//This script is licensed under:
//Apache License
//Version 2.0, January 2004
//http://www.apache.org/licenses/
//Tested with IBM UrbanCode Deploy 6.2.1.0
//Command line usage:
//groovy -cp udclient.jar ListComponentsWithSize.groovy https://hostname:8443 username password
//Usage from a UrbanCode Deploy process does not require any input parameters, but you can execute the following from a Shell step in order to have the udclient classes on the classpath:
//${p:agent/GROOVY_HOME}/bin/groovy -cp ${p:agent/AGENT_HOME}/opt/udclient/udclient.jar groovyScript.groovy
//Example output
/**
Components:
Name: TestDP
cleanupCountToKeep: 0
cleanupDaysToKeep: 0
Version Name = v2
Version Size = 634880
Archived = false
Active = true
Version Name = v1
Version Size = 626688
Archived = false
Active = true
Total Size: 1261568
**/
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;
import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder;
import groovy.json.JsonSlurper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.System;
import java.net.URLEncoder;
import org.apache.commons.lang.StringUtils;
class Credentials{
String serverURL;
String username;
String password;
}
//sets the values of the Credentials class which contains the server url and the username/password
void initializeParameters(Credentials credentials){
//if the user provided the input parameters, we're running on the command line
if (args.size()==3){
credentials.serverURL = args[0];
credentials.username = args[1];
credentials.password = args[2];
}
//otherwise we assume that we are running from a UrbanCode Deploy process
//we obtain the Server URL from a system property:
//https://www.ibm.com/support/knowledgecenter/SS4GSP_6.2.2/com.ibm.udeploy.doc/topics/propertiesreference.html
//we obtain the Token from the environment variable DS_AUTH_TOKEN
else{
try{
credentials.serverURL="${p:server.url}";
credentials.username="PasswordIsAuthToken";
credentials.password=System.getenv("DS_AUTH_TOKEN");
}//If we do not have the above variables, we might be running from the command line, but the user did not pass the expected parameters
catch (Exception ex){
println "Command Line usage: ListAllApplications ServerURL username password\nDo not provide any parameters if running from a UrbanCode Deploy process"
org.codehaus.groovy.runtime.StackTraceUtils.sanitize(ex).printStackTrace();
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();
}
//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 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;
}
//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;
}
}
//Main script contents
Credentials credentials = new Credentials();
initializeParameters(credentials);
HttpClient client = initializeClient(credentials.username,credentials.password);
def components = getComponents(client,credentials.serverURL);
println "Components:\n"
//Use a groovy closure with the implicit parameter "it"
//http://groovy-lang.org/closures.html
//http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Collection.html#each%28groovy.lang.Closure%29
if (components){
components.each{
println "Name: "+it.name;
println "cleanupCountToKeep: "+it.cleanupCountToKeep;
println "cleanupDaysToKeep: "+it.cleanupDaysToKeep;
def versions = getVersions(client,credentials.serverURL,it.id);
long totalSize = 0L;
if(versions){
versions.each{
println " Version Name = "+it.name;
println " Version Size = "+it.sizeOnDisk;
println " Archived = "+it.archived;
println " Active = "+it.active;
totalSize += it.sizeOnDisk;
}
}
println "Total Size: " + totalSize;
println ""
}
}