This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSlaveConfiguration.java
More file actions
425 lines (355 loc) · 20.3 KB
/
SlaveConfiguration.java
File metadata and controls
425 lines (355 loc) · 20.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
/*
* ElasticBox Confidential
* Copyright (c) 2014 All Right Reserved, ElasticBox Inc.
*
* NOTICE: All information contained herein is, and remains the property
* of ElasticBox. The intellectual and technical concepts contained herein are
* proprietary and may be covered by U.S. and Foreign Patents, patents in process,
* and are protected by trade secret or copyright law. Dissemination of this
* information or reproduction of this material is strictly forbidden unless prior
* written permission is obtained from ElasticBox.
*/
package com.elasticbox.jenkins;
import com.elasticbox.Client;
import com.elasticbox.Constants;
import com.elasticbox.jenkins.model.box.AbstractBox;
import com.elasticbox.jenkins.model.box.policy.PolicyBox;
import com.elasticbox.jenkins.model.services.deployment.DeployBoxOrderServiceImpl;
import com.elasticbox.jenkins.model.services.deployment.DeploymentType;
import com.elasticbox.jenkins.model.services.deployment.execution.order.DeployBoxOrderResult;
import com.elasticbox.jenkins.model.services.error.ServiceException;
import com.elasticbox.jenkins.model.workspace.AbstractWorkspace;
import com.elasticbox.jenkins.util.ClientCache;
import hudson.Extension;
import hudson.RelativePath;
import hudson.model.Node;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
public class SlaveConfiguration extends AbstractSlaveConfiguration {
private static final Logger LOGGER = Logger.getLogger(SlaveConfiguration.class.getName());
public static final String SLAVE_CONFIGURATIONS = "slaveConfigurations";
@DataBoundConstructor
public SlaveConfiguration(String id, String workspace, String box, String boxVersion, String profile,
String claims, String provider, String location, int minInstances, int maxInstances, String tags,
String variables, String labels,String description, String remoteFs, Node.Mode mode, int retentionTime,
String maxBuildsText, int executors, int launchTimeout, String boxDeploymentType) {
super(id, workspace, box, boxVersion, profile, claims, provider, location, minInstances, maxInstances,
tags, variables, labels, description, remoteFs, mode, retentionTime,
StringUtils.isBlank(maxBuildsText) ? 0 : Integer.parseInt(maxBuildsText), executors, launchTimeout,
boxDeploymentType);
}
@Extension
public static final class DescriptorImpl extends AbstractSlaveConfigurationDescriptor {
@Override
public String getDisplayName() {
return "Slave Configuration";
}
public void validateSlaveConfiguration(SlaveConfiguration slaveConfig, ElasticBoxCloud newCloud)
throws FormException {
String slaveConfigText = slaveConfig.getDescription() != null
? MessageFormat.format("slave configuration ''{0}''", slaveConfig.getDescription())
: "a slave configuration";
if (StringUtils.isBlank(slaveConfig.getWorkspace())) {
throw new FormException(
MessageFormat.format(
"No Workspace is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
if (StringUtils.isBlank(slaveConfig.getBox())) {
throw new FormException(
MessageFormat.format(
"No Box is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
if (StringUtils.isBlank(slaveConfig.getBoxVersion())) {
throw new FormException(
MessageFormat.format(
"No Version is selected for the selected box in {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
if (slaveConfig.getProfile() != null) {
if (StringUtils.isBlank(slaveConfig.getProfile())) {
throw new FormException(
MessageFormat.format(
"No Deployment Policy is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
} else if (slaveConfig.getClaims() != null) {
if (StringUtils.isBlank(slaveConfig.getClaims())) {
throw new FormException(
MessageFormat.format(
"Claims must be specified to select a Deployment Policy for {0} "
+ "of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
} else if (slaveConfig.getProvider() != null) {
if (StringUtils.isBlank(slaveConfig.getProvider())) {
throw new FormException(
MessageFormat.format(
"No Provider is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
if (StringUtils.isBlank(slaveConfig.getLocation())) {
throw new FormException(
MessageFormat.format(
"No Region is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
} else {
throw new FormException(
MessageFormat.format(
"No deployment option is selected for {0} of ElasticBox cloud ''{1}''.",
slaveConfigText,
newCloud.getDisplayName()),
SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
if (slaveConfig.getExecutors() < 1) {
slaveConfig.setExecutors(1);
}
if (StringUtils.isBlank(slaveConfig.getId())) {
slaveConfig.setId(UUID.randomUUID().toString());
}
FormValidation result = ((SlaveConfiguration.DescriptorImpl)Jenkins.get()
.getDescriptorOrDie(SlaveConfiguration.class))
.doCheckBoxVersion(slaveConfig.getBoxVersion(),
newCloud.name,
newCloud.getEndpointUrl(),
newCloud.getToken(),
slaveConfig.getWorkspace(),
slaveConfig.getBox());
if (result.kind == FormValidation.Kind.ERROR) {
throw new FormException(result.getMessage(), SlaveConfiguration.SLAVE_CONFIGURATIONS);
}
}
private Client retrieveClientWithCredentials(String name, String endpointUrl, String credentialsId) {
if (StringUtils.isBlank(endpointUrl) || StringUtils.isBlank(credentialsId)) {
return null;
}
String token = ElasticBoxCloud.retrieveTokenFromCredentials(endpointUrl, credentialsId);
Client client = (name != null && name.length() > 0)
? ClientCache.getClient(name)
: new Client(endpointUrl, token, ClientCache.getJenkinsHttpProxyCfg());
if ((client == null) || ( !client.getEndpointUrl().equals(endpointUrl) )) {
client = new Client(endpointUrl, token, ClientCache.getJenkinsHttpProxyCfg());
}
return client;
}
public ListBoxModel doFillWorkspaceItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId) {
final ListBoxModel workspaceOptions = getEmptyListBoxModel(Constants.CHOOSE_WORKSPACE_MESSAGE, "");
if (anyOfThemIsBlank(endpointUrl, credentialsId)) {
return workspaceOptions;
}
try {
final DeployBoxOrderResult<List<AbstractWorkspace>> result =
new DeployBoxOrderServiceImpl(retrieveClientWithCredentials(name, endpointUrl, credentialsId))
.getWorkspaces();
final List<AbstractWorkspace> workspaces = result.getResult();
for (AbstractWorkspace workspace : workspaces) {
workspaceOptions.add(workspace.getName(), workspace.getId());
}
} catch (ServiceException e) {
LOGGER.warning("Invalid parameters: endpointUrl=" + endpointUrl + ", credentials=" + credentialsId);
}
return workspaceOptions;
}
public ListBoxModel doFillBoxItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace) {
ListBoxModel boxes = getEmptyListBoxModel(Constants.CHOOSE_BOX_MESSAGE, "");
if (anyOfThemIsBlank(credentialsId, workspace)) {
return boxes;
}
try {
final DeployBoxOrderResult<List<AbstractBox>> result =
new DeployBoxOrderServiceImpl(retrieveClientWithCredentials(name, endpointUrl, credentialsId))
.updateableBoxes(workspace);
for (AbstractBox box : result.getResult()) {
boxes.add(box.getName(), box.getId());
}
} catch (ServiceException e) {
LOGGER.warning("Invalid parameters: endpointUrl=" + endpointUrl + ", credentials=" + credentialsId);
}
return boxes;
}
public ListBoxModel doFillBoxDeploymentTypeItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box) {
ListBoxModel boxDeploymentType = getEmptyListBoxModel(Constants.CHOOSE_DEPLOYMENT_TYPE_MESSAGE, "");
if (anyOfThemIsBlank(endpointUrl, credentialsId, workspace, box)) {
return boxDeploymentType;
}
try {
final DeploymentType deploymentType
= new DeployBoxOrderServiceImpl(retrieveClientWithCredentials(name, endpointUrl, credentialsId))
.deploymentType(box);
final String id = deploymentType.getValue();
boxDeploymentType.add(new ListBoxModel.Option(id,id,true));
} catch (ServiceException e) {
LOGGER.warning("Invalid parameters: endpointUrl=" + endpointUrl + ", credentials=" + credentialsId);
}
return boxDeploymentType;
}
public ListBoxModel doFillBoxVersionItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box) {
if (anyOfThemIsBlank(endpointUrl, workspace, box, credentialsId)) {
return getEmptyListBoxModel();
}
return DescriptorHelper.getBoxVersions(retrieveClientWithCredentials(name, endpointUrl, credentialsId),
workspace, box);
}
public ListBoxModel doFillProfileItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box) {
if (anyOfThemIsBlank(endpointUrl, workspace, box, credentialsId)) {
return getEmptyListBoxModel();
}
List<PolicyBox> policyBoxList = null;
try {
DeployBoxOrderResult<List<PolicyBox>> result = new DeployBoxOrderServiceImpl(
retrieveClientWithCredentials(name, endpointUrl, credentialsId) )
.deploymentPolicies(workspace, box);
policyBoxList = result.getResult();
} catch (ServiceException e) {
LOGGER.severe("Invalid parameters: endpointUrl=" + endpointUrl + ", credentials=" + credentialsId);
}
final ListBoxModel profiles;
if (policyBoxList == null) {
profiles = getEmptyListBoxModel("--Unable to retrieve policies--", "");
} else {
if (policyBoxList.size() == 0) {
profiles = getEmptyListBoxModel("--No compatible policy box found--", "");
} else if (policyBoxList.size() == 1) {
profiles = new ListBoxModel();
} else {
profiles = getEmptyListBoxModel("--Please choose policy box--", "");
}
for (PolicyBox policyBox : policyBoxList) {
profiles.add(policyBox.getName(), policyBox.getId());
}
}
if (profiles.size() == 1 && !"".equals(profiles.get(0).name)) {
profiles.get(0).selected = true;
}
return profiles;
}
public ListBoxModel doFillProviderItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace) {
ListBoxModel providers = getEmptyListBoxModel(Constants.CHOOSE_PROVIDER_MESSAGE, "");
if (anyOfThemIsBlank(endpointUrl, credentialsId, workspace)) {
return providers;
}
return DescriptorHelper.getCloudFormationProviders(
retrieveClientWithCredentials(name, endpointUrl, credentialsId), workspace);
}
public ListBoxModel doFillLocationItems(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String provider) {
ListBoxModel locations = getEmptyListBoxModel(Constants.CHOOSE_REGION_MESSAGE, "");
if (anyOfThemIsBlank(endpointUrl, credentialsId, provider)) {
return locations;
}
return DescriptorHelper.getCloudFormationLocations(
retrieveClientWithCredentials(name, endpointUrl, credentialsId), provider);
}
public FormValidation doCheckWorkspace(@QueryParameter String workspace) {
if (StringUtils.isBlank(workspace)) {
return FormValidation.error("Workspace is required");
}
return FormValidation.ok();
}
public FormValidation doCheckBox(@QueryParameter String box) {
if (StringUtils.isBlank(box)) {
return FormValidation.error("Box to deploy is required");
}
return FormValidation.ok();
}
public FormValidation doCheckBoxVersion(@QueryParameter String value,
@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box) {
if (StringUtils.isBlank(workspace)) {
return FormValidation.error("Workspace is required");
}
if (StringUtils.isBlank(box)) {
return FormValidation.error("Box is required");
}
if (StringUtils.isBlank(credentialsId)) {
return FormValidation.error("Credentials are required");
}
return checkBoxVersion(value, box, workspace,
retrieveClientWithCredentials(name, endpointUrl, credentialsId));
}
public DescriptorHelper.JsonArrayResponse doGetBoxStack(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box,
@QueryParameter String boxVersion) {
return DescriptorHelper.getBoxStack(retrieveClientWithCredentials(name, endpointUrl, credentialsId),
workspace, box, StringUtils.isBlank(boxVersion) ? box : boxVersion);
}
public DescriptorHelper.JsonArrayResponse doGetInstances(@RelativePath("..") @QueryParameter String name,
@RelativePath("..") @QueryParameter String endpointUrl,
@RelativePath("..") @QueryParameter String credentialsId,
@QueryParameter String workspace,
@QueryParameter String box,
@QueryParameter String boxVersion) {
return DescriptorHelper.getInstancesAsJsonArrayResponse(
retrieveClientWithCredentials(name, endpointUrl, credentialsId),
workspace, StringUtils.isBlank(boxVersion) ? box : boxVersion);
}
private boolean anyOfThemIsBlank(String... inputParameters) {
for (String inputParameter : inputParameters) {
if (StringUtils.isBlank(inputParameter)) {
return true;
}
}
return false;
}
private ListBoxModel getEmptyListBoxModel() {
return getEmptyListBoxModel("","");
}
private ListBoxModel getEmptyListBoxModel(final String emptyName, final String emptyValue) {
final List<ListBoxModel.Option> options = Arrays.asList(new ListBoxModel.Option(emptyName, emptyValue));
return new ListBoxModel(options);
}
}
}