/
EC2ResourceModelSource.java
307 lines (282 loc) · 13.1 KB
/
EC2ResourceModelSource.java
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright 2011 DTO Solutions, Inc. (http://dtosolutions.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* EC2ResourceModelSource.java
*
* User: Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
* Created: 9/1/11 4:34 PM
*
*/
package com.dtolabs.rundeck.plugin.resources.ec2;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient;
import com.amazonaws.services.securitytoken.model.*;
import com.dtolabs.rundeck.core.common.*;
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
import com.dtolabs.rundeck.core.resources.ResourceModelSource;
import com.dtolabs.rundeck.core.resources.ResourceModelSourceException;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static com.dtolabs.rundeck.plugin.resources.ec2.EC2ResourceModelSourceFactory.SYNCHRONOUS_LOAD;
/**
* EC2ResourceModelSource produces nodes by querying the AWS EC2 API to list instances.
* <p/>
* The RunDeck node definitions are created from the instances on a mapping system to convert properties of the amazon
* instances to attributes defined on the nodes.
* <p/>
* The EC2 requests are performed asynchronously, so the first request to {@link #getNodes()} will return null, and
* subsequent requests may return the data when it's available.
*
* @author Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
*/
public class EC2ResourceModelSource implements ResourceModelSource {
static Logger logger = Logger.getLogger(EC2ResourceModelSource.class);
private String accessKey;
private String secretKey;
long refreshInterval = 30000;
long lastRefresh = 0;
String filterParams;
String endpoint;
String httpProxyHost;
int httpProxyPort = 80;
String httpProxyUser;
String httpProxyPass;
String mappingParams;
File mappingFile;
boolean useDefaultMapping = true;
boolean runningOnly = true;
boolean queryAsync = true;
Future<INodeSet> futureResult = null;
final Properties mapping = new Properties();
final String assumeRoleArn;
AWSCredentials credentials;
ClientConfiguration clientConfiguration = new ClientConfiguration();;
INodeSet iNodeSet;
static final Properties defaultMapping = new Properties();
InstanceToNodeMapper mapper;
static {
final String mapping = "nodename.selector=tags/Name,instanceId\n"
+ "hostname.selector=publicDnsName,privateIpAddress\n"
+ "sshport.default=22\n"
+ "sshport.selector=tags/ssh_config_Port\n"
+ "description.default=EC2 node instance\n"
+ "osArch.selector=architecture\n"
+ "osFamily.selector=platform\n"
+ "osFamily.default=unix\n"
+ "osName.selector=platform\n"
+ "osName.default=Linux\n"
+ "username.selector=tags/Rundeck-User\n"
+ "username.default=ec2-user\n"
+ "editUrl.default=https://console.aws.amazon.com/ec2/home#Instances:search=${node.instanceId}\n"
+ "privateIpAddress.selector=privateIpAddress\n"
+ "privateDnsName.selector=privateDnsName\n"
+ "tags.selector=tags/Rundeck-Tags\n"
+ "instanceId.selector=instanceId\n"
+ "tag.running.selector=state.name=running\n"
+ "tag.stopped.selector=state.name=stopped\n"
+ "tag.stopping.selector=state.name=stopping\n"
+ "tag.shutting-down.selector=state.name=shutting-down\n"
+ "tag.terminated.selector=state.name=terminated\n"
+ "tag.pending.selector=state.name=pending\n"
+ "state.selector=state.name\n"
+ "tags.default=ec2\n";
try {
final InputStream resourceAsStream = EC2ResourceModelSource.class.getClassLoader().getResourceAsStream(
"defaultMapping.properties");
if (null != resourceAsStream) {
try {
defaultMapping.load(resourceAsStream);
} finally {
resourceAsStream.close();
}
}else{
//fallback in case class loader is misbehaving
final StringReader stringReader = new StringReader(mapping);
try {
defaultMapping.load(stringReader);
} finally {
stringReader.close();
}
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
public EC2ResourceModelSource(final Properties configuration) {
this.accessKey = configuration.getProperty(EC2ResourceModelSourceFactory.ACCESS_KEY);
this.secretKey = configuration.getProperty(EC2ResourceModelSourceFactory.SECRET_KEY);
this.endpoint = configuration.getProperty(EC2ResourceModelSourceFactory.ENDPOINT);
this.httpProxyHost = configuration.getProperty(EC2ResourceModelSourceFactory.HTTP_PROXY_HOST);
int proxyPort = 80;
final String proxyPortStr = configuration.getProperty(EC2ResourceModelSourceFactory.HTTP_PROXY_PORT);
if (null != proxyPortStr && !"".equals(proxyPortStr)) {
try {
proxyPort = Integer.parseInt(proxyPortStr);
} catch (NumberFormatException e) {
logger.warn(EC2ResourceModelSourceFactory.HTTP_PROXY_PORT + " value is not valid: " + proxyPortStr);
}
}
this.httpProxyPort = proxyPort;
this.httpProxyUser = configuration.getProperty(EC2ResourceModelSourceFactory.HTTP_PROXY_USER);
this.httpProxyPass = configuration.getProperty(EC2ResourceModelSourceFactory.HTTP_PROXY_PASS);
this.filterParams = configuration.getProperty(EC2ResourceModelSourceFactory.FILTER_PARAMS);
this.mappingParams = configuration.getProperty(EC2ResourceModelSourceFactory.MAPPING_PARAMS);
final String mappingFilePath = configuration.getProperty(EC2ResourceModelSourceFactory.MAPPING_FILE);
if (null != mappingFilePath) {
mappingFile = new File(mappingFilePath);
}
int refreshSecs = 30;
final String refreshStr = configuration.getProperty(EC2ResourceModelSourceFactory.REFRESH_INTERVAL);
if (null != refreshStr && !"".equals(refreshStr)) {
try {
refreshSecs = Integer.parseInt(refreshStr);
} catch (NumberFormatException e) {
logger.warn(EC2ResourceModelSourceFactory.REFRESH_INTERVAL + " value is not valid: " + refreshStr);
}
}
refreshInterval = refreshSecs * 1000;
if (configuration.containsKey(EC2ResourceModelSourceFactory.USE_DEFAULT_MAPPING)) {
useDefaultMapping = Boolean.parseBoolean(configuration.getProperty(
EC2ResourceModelSourceFactory.USE_DEFAULT_MAPPING));
}
if (configuration.containsKey(EC2ResourceModelSourceFactory.RUNNING_ONLY)) {
runningOnly = Boolean.parseBoolean(configuration.getProperty(
EC2ResourceModelSourceFactory.RUNNING_ONLY));
}
if (null != accessKey && null != secretKey) {
credentials = new BasicAWSCredentials(accessKey.trim(), secretKey.trim());
assumeRoleArn = null;
}else{
assumeRoleArn = configuration.getProperty(EC2ResourceModelSourceFactory.ROLE_ARN);
}
if (null != httpProxyHost && !"".equals(httpProxyHost)) {
clientConfiguration.setProxyHost(httpProxyHost);
clientConfiguration.setProxyPort(httpProxyPort);
clientConfiguration.setProxyUsername(httpProxyUser);
clientConfiguration.setProxyPassword(httpProxyPass);
}
queryAsync = !("true".equals(configuration.getProperty(SYNCHRONOUS_LOAD)) || refreshInterval <= 0);
initialize();
}
private void initialize() {
final ArrayList<String> params = new ArrayList<String>();
if (null != filterParams) {
Collections.addAll(params, filterParams.split(";"));
}
loadMapping();
if (this.credentials == null && assumeRoleArn != null) {
AWSSecurityTokenServiceClient sts_client = new AWSSecurityTokenServiceClient();
// sts_client.setEndpoint("sts-endpoint.amazonaws.com");
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest();
assumeRoleRequest.setRoleArn(assumeRoleArn);
assumeRoleRequest.setRoleSessionName("RundeckEC2ResourceModelSourceSession");
AssumeRoleResult assumeRoleResult = sts_client.assumeRole(assumeRoleRequest);
Credentials assumeCredentials = assumeRoleResult.getCredentials();
credentials = new BasicSessionCredentials(
assumeCredentials.getAccessKeyId(),
assumeCredentials.getSecretAccessKey(),
assumeCredentials.getSessionToken()
);
}
mapper = new InstanceToNodeMapper(this.credentials, mapping, clientConfiguration);
mapper.setFilterParams(params);
mapper.setEndpoint(endpoint);
mapper.setRunningStateOnly(runningOnly);
}
public synchronized INodeSet getNodes() throws ResourceModelSourceException {
checkFuture();
if (!needsRefresh()) {
if (null != iNodeSet) {
logger.info("Returning " + iNodeSet.getNodeNames().size() + " cached nodes from EC2");
}
return iNodeSet;
}
if (lastRefresh > 0 && queryAsync && null == futureResult) {
futureResult = mapper.performQueryAsync();
lastRefresh = System.currentTimeMillis();
} else if (!queryAsync || lastRefresh < 1) {
//always perform synchronous query the first time
iNodeSet = mapper.performQuery();
lastRefresh = System.currentTimeMillis();
}
if (null != iNodeSet) {
logger.info("Read " + iNodeSet.getNodeNames().size() + " nodes from EC2");
}
return iNodeSet;
}
/**
* if any future results are pending, check if they are done and retrieve the results
*/
private void checkFuture() {
if (null != futureResult && futureResult.isDone()) {
try {
iNodeSet = futureResult.get();
} catch (InterruptedException e) {
logger.debug(e);
} catch (ExecutionException e) {
logger.warn("Error performing query: " + e.getMessage(), e);
}
futureResult = null;
}
}
/**
* Returns true if the last refresh time was longer ago than the refresh interval
*/
private boolean needsRefresh() {
return refreshInterval < 0 || (System.currentTimeMillis() - lastRefresh > refreshInterval);
}
private void loadMapping() {
if (useDefaultMapping) {
mapping.putAll(defaultMapping);
}
if (null != mappingFile) {
try {
final FileInputStream fileInputStream = new FileInputStream(mappingFile);
try {
mapping.load(fileInputStream);
} finally {
fileInputStream.close();
}
} catch (IOException e) {
logger.warn(e);
}
}
if (null != mappingParams) {
for (final String s : mappingParams.split(";")) {
if (s.contains("=")) {
final String[] split = s.split("=", 2);
if (2 == split.length) {
mapping.put(split[0], split[1]);
}
}
}
}
if (mapping.size() < 1) {
mapping.putAll(defaultMapping);
}
}
public void validate() throws ConfigurationException {
if (null != accessKey && null == secretKey) {
throw new ConfigurationException("secretKey is required for use with accessKey");
}
}
}