forked from lsegal/jenkins-codebuilder-plugin
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCodeBuilderCloud.java
More file actions
487 lines (426 loc) · 15.3 KB
/
CodeBuilderCloud.java
File metadata and controls
487 lines (426 loc) · 15.3 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package dev.lsegal.jenkins.codebuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.SdkClientException;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.services.codebuild.AWSCodeBuild;
import com.amazonaws.services.codebuild.AWSCodeBuildClientBuilder;
import com.amazonaws.services.codebuild.model.ListProjectsRequest;
import com.amazonaws.services.codebuild.model.ListProjectsResult;
import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsHelper;
import com.cloudbees.jenkins.plugins.awscredentials.AmazonWebServicesCredentials;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import hudson.Extension;
import hudson.ProxyConfiguration;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.labels.LabelAtom;
import hudson.slaves.Cloud;
import hudson.slaves.NodeProvisioner;
import hudson.slaves.NodeProvisioner.PlannedNode;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
/**
* This is the root class that contains all configuration state about the
* CodeBuilder cloud agents. Jenkins calls
* {@link CodeBuilderCloud#provision(Label label, int excessWorkload)} on this
* class to create new nodes.
*
* @author Loren Segal
*/
public class CodeBuilderCloud extends Cloud {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeBuilderCloud.class);
private static final String DEFAULT_JNLP_IMAGE = "lsegal/jnlp-docker-agent:alpine";
private static final int DEFAULT_AGENT_TIMEOUT = 120;
private static final String DEFAULT_COMPUTE_TYPE = "BUILD_GENERAL1_SMALL";
private static final boolean DEFAULT_TERMINATE_AGENT = true;
static {
clearAllNodes();
}
@Nonnull
private final String projectName;
@Nonnull
private final String credentialsId;
@Nonnull
private final String region;
private String label;
private String computeType;
private String jenkinsUrl;
private String jnlpImage;
private int agentTimeout;
private boolean terminateAgent;
private transient AWSCodeBuild client;
/**
* Constructor for CodeBuilderCloud.
*
* @param name the name of the cloud or null if you want it
* auto-generated.
* @param projectName the name of the AWS CodeBuild project to build from.
* @param credentialsId the credentials ID to use or null/empty if pulled from
* environment.
* @param region the AWS region to use.
* @throws InterruptedException if any.
*/
@DataBoundConstructor
public CodeBuilderCloud(String name, @Nonnull String projectName, @Nullable String credentialsId,
@Nonnull String region) throws InterruptedException {
super(StringUtils.isNotBlank(name) ? name : "codebuilder_" + jenkins().clouds.size());
this.projectName = projectName;
this.credentialsId = StringUtils.defaultIfBlank(credentialsId, "");
if (StringUtils.isBlank(region)) {
this.region = getDefaultRegion();
} else {
this.region = region;
}
LOGGER.info("[CodeBuilder]: Initializing Cloud: {}", this);
}
/**
* The active Jenkins instance.
*
* @return a {@link jenkins.model.Jenkins} object.
*/
@Nonnull
protected static Jenkins jenkins() {
return Jenkins.getActiveInstance();
}
/**
* Clear all CodeBuilder nodes on boot-up because these cannot be permanent.
*/
private static void clearAllNodes() {
List<Node> nodes = jenkins().getNodes();
if (nodes.size() == 0) {
return;
}
LOGGER.info("[CodeBuilder]: Clearing all previous CodeBuilder nodes...");
for (final Node n : nodes) {
if (n instanceof CodeBuilderAgent) {
try {
((CodeBuilderAgent) n).terminate();
} catch (InterruptedException | IOException e) {
LOGGER.error("[CodeBuilder]: Failed to terminate agent '{}'", n.getDisplayName(), e);
}
}
}
}
/** {@inheritDoc} */
@Override
public String toString() {
return String.format("%s<%s>", name, projectName);
}
/**
* Getter for the field <code>projectName</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getProjectName() {
return projectName;
}
/**
* Getter for the field <code>region</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getRegion() {
return region;
}
/**
* Getter for the field <code>label</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getLabel() {
return StringUtils.defaultIfBlank(label, "");
}
/**
* Setter for the field <code>label</code>.
*
* @param label a {@link String} object.
*/
@DataBoundSetter
public void setLabel(String label) {
this.label = label;
}
/**
* Getter for the field <code>jenkinsUrl</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getJenkinsUrl() {
if (StringUtils.isNotBlank(jenkinsUrl)) {
return jenkinsUrl;
} else {
JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
if (config != null) {
return StringUtils.defaultIfBlank(config.getUrl(), "unknown");
}
}
return "unknown";
}
/**
* Setter for the field <code>jenkinsUrl</code>.
*
* @param jenkinsUrl a {@link String} object.
*/
@DataBoundSetter
public void setJenkinsUrl(String jenkinsUrl) {
JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
if (config != null && StringUtils.equals(config.getUrl(), jenkinsUrl)) {
return;
}
this.jenkinsUrl = jenkinsUrl;
}
/**
* Getter for the field <code>jnlpImage</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getJnlpImage() {
return StringUtils.isBlank(jnlpImage) ? DEFAULT_JNLP_IMAGE : jnlpImage;
}
/**
* Setter for the field <code>jnlpImage</code>.
*
* @param jnlpImage a {@link String} object.
*/
@DataBoundSetter
public void setJnlpImage(String jnlpImage) {
this.jnlpImage = jnlpImage;
}
/**
* Getter for the field <code>agentTimeout</code>.
*
* @return a int.
*/
@Nonnull
public int getAgentTimeout() {
return agentTimeout == 0 ? DEFAULT_AGENT_TIMEOUT : agentTimeout;
}
/**
* Setter for the field <code>agentTimeout</code>.
*
* @param agentTimeout a int.
*/
@DataBoundSetter
public void setAgentTimeout(int agentTimeout) {
this.agentTimeout = agentTimeout;
}
/**
* Getter for the field <code>computeType</code>.
*
* @return a {@link String} object.
*/
@Nonnull
public String getComputeType() {
return StringUtils.isBlank(computeType) ? DEFAULT_COMPUTE_TYPE : computeType;
}
/**
* Setter for the field <code>computeType</code>.
*
* @param computeType a {@link String} object.
*/
@DataBoundSetter
public void setComputeType(String computeType) {
this.computeType = computeType;
}
@CheckForNull
private static AmazonWebServicesCredentials getCredentials(@Nullable String credentialsId) {
return AWSCredentialsHelper.getCredentials(credentialsId, jenkins());
}
private static AWSCodeBuild buildClient(String credentialsId, String region) {
ProxyConfiguration proxy = jenkins().proxy;
ClientConfiguration clientConfiguration = new ClientConfiguration();
if (proxy != null) {
clientConfiguration.setProxyHost(proxy.name);
clientConfiguration.setProxyPort(proxy.port);
clientConfiguration.setProxyUsername(proxy.getUserName());
clientConfiguration.setProxyPassword(proxy.getPassword());
}
AWSCodeBuildClientBuilder builder = AWSCodeBuildClientBuilder.standard()
.withClientConfiguration(clientConfiguration).withRegion(region);
AmazonWebServicesCredentials credentials = getCredentials(credentialsId);
if (credentials != null) {
String awsAccessKeyId = credentials.getCredentials().getAWSAccessKeyId();
String obfuscatedAccessKeyId = StringUtils.left(awsAccessKeyId, 4)
+ StringUtils.repeat("*", awsAccessKeyId.length() - 8) + StringUtils.right(awsAccessKeyId, 4);
LOGGER.debug("[CodeBuilder]: Using credentials: {}", obfuscatedAccessKeyId);
builder.withCredentials(credentials);
}
LOGGER.debug("[CodeBuilder]: Selected Region: {}", region);
return builder.build();
}
/**
* Getter for the field <code>client</code>.
*
* @return a {@link com.amazonaws.services.codebuild.AWSCodeBuild} object.
*/
public synchronized AWSCodeBuild getClient() {
if (this.client == null) {
this.client = CodeBuilderCloud.buildClient(credentialsId, region);
}
return this.client;
}
private transient long lastProvisionTime = 0;
/** {@inheritDoc} */
@Override
public synchronized Collection<PlannedNode> provision(Label label, int excessWorkload) {
List<NodeProvisioner.PlannedNode> list = new ArrayList<NodeProvisioner.PlannedNode>();
String labelName = label == null ? getLabel() : label.getDisplayName();
LOGGER.info("[CodeBuilder]: Excess workload sent by Jenkins: {} for label: {}", excessWorkload, labelName);
// guard against non-matching labels
if (label != null && !label.matches(Arrays.asList(new LabelAtom(getLabel())))) {
return list;
}
// guard against double-provisioning with a 500ms cooldown clock
long timeDiff = System.currentTimeMillis() - lastProvisionTime;
if (timeDiff < 500) {
LOGGER.info("[CodeBuilder]: Provision of {} skipped, still on cooldown ({}ms of 500ms)", excessWorkload,
timeDiff);
return list;
}
long stillProvisioning = numStillProvisioning();
long numToLaunch = Math.max(excessWorkload - stillProvisioning, 0);
LOGGER.info("[CodeBuilder]: Provisioning {} nodes for label '{}' ({} already provisioning)", numToLaunch, labelName,
stillProvisioning);
for (int i = 0; i < numToLaunch; i++) {
final String suffix = RandomStringUtils.randomAlphabetic(4);
final String displayName = String.format("%s.cb-%s", projectName, suffix);
final CodeBuilderCloud cloud = this;
final Future<Node> nodeResolver = Computer.threadPoolForRemoting.submit(() -> {
CodeBuilderLauncher launcher = new CodeBuilderLauncher(cloud);
CodeBuilderAgent agent = new CodeBuilderAgent(cloud, displayName, launcher);
jenkins().addNode(agent);
return agent;
});
list.add(new NodeProvisioner.PlannedNode(displayName, nodeResolver, 1));
}
lastProvisionTime = System.currentTimeMillis();
return list;
}
/**
* Find the number of {@link CodeBuilderAgent} instances still connecting to
* Jenkins host.
*/
private long numStillProvisioning() {
return jenkins().getNodes().stream().filter(CodeBuilderAgent.class::isInstance).map(CodeBuilderAgent.class::cast)
.filter(a -> !a.getLauncher().isLaunchSupported()).count();
}
/** {@inheritDoc} */
@Override
public boolean canProvision(Label label) {
LOGGER.info("[CodeBuilder]: Check if can provision node for label '{}'", label);
boolean canProvision = label == null ? true : label.matches(Arrays.asList(new LabelAtom(getLabel())));
if (canProvision) {
LOGGER.info("[CodeBuilder]: Label '{}' matches current label '{}'. Node will be provisioned...", label, getLabel());
}
else {
LOGGER.info("[CodeBuilder]: Label '{}' DOESN'T MATCH current label '{}'. Node WON'T provisioned...", label, getLabel());
}
return canProvision;
}
private static String getDefaultRegion() {
try {
return new DefaultAwsRegionProviderChain().getRegion();
} catch (SdkClientException exc) {
return null;
}
}
public boolean isTerminateAgent() {
return terminateAgent;
}
public void setTerminateAgent(boolean terminateAgent) {
this.terminateAgent = terminateAgent;
}
@Extension
public static class DescriptorImpl extends Descriptor<Cloud> {
@Override
public String getDisplayName() {
return Messages.displayName();
}
public String getDefaultJnlpImage() {
return DEFAULT_JNLP_IMAGE;
}
public int getDefaultAgentTimeout() {
return DEFAULT_AGENT_TIMEOUT;
}
public String getDefaultComputeType() {
return DEFAULT_COMPUTE_TYPE;
}
public boolean getDefaultTerminateAgent() {
return DEFAULT_TERMINATE_AGENT;
}
public ListBoxModel doFillCredentialsIdItems() {
return AWSCredentialsHelper.doFillCredentialsIdItems(jenkins());
}
public ListBoxModel doFillRegionItems() {
final ListBoxModel options = new ListBoxModel();
String defaultRegion = getDefaultRegion();
if (StringUtils.isNotBlank(defaultRegion)) {
options.add(defaultRegion);
}
for (Region r : RegionUtils.getRegionsForService(AWSCodeBuild.ENDPOINT_PREFIX)) {
if (StringUtils.equals(r.getName(), defaultRegion)) {
continue;
}
options.add(r.getName());
}
return options;
}
public ListBoxModel doFillProjectNameItems(@QueryParameter String credentialsId, @QueryParameter String region) {
if (StringUtils.isBlank(region)) {
region = getDefaultRegion();
if (StringUtils.isBlank(region)) {
return new ListBoxModel();
}
}
try {
final List<String> projects = new ArrayList<String>();
String lastToken = null;
do {
ListProjectsResult result = CodeBuilderCloud.buildClient(credentialsId, region)
.listProjects(new ListProjectsRequest().withNextToken(lastToken));
projects.addAll(result.getProjects());
lastToken = result.getNextToken();
} while (lastToken != null);
Collections.sort(projects);
final ListBoxModel options = new ListBoxModel();
for (final String arn : projects) {
options.add(arn);
}
return options;
} catch (RuntimeException e) {
// missing credentials will throw an "AmazonClientException: Unable to load AWS
// credentials from any provider in the chain"
LOGGER.error("[CodeBuilder]: Exception listing projects (region={})", region, e);
return new ListBoxModel();
}
}
public String getDefaultRegion() {
return CodeBuilderCloud.getDefaultRegion();
}
}
}