-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathJCloudsSlave.java
More file actions
507 lines (443 loc) · 20.7 KB
/
JCloudsSlave.java
File metadata and controls
507 lines (443 loc) · 20.7 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package jenkins.plugins.openstack.compute;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.collect.ImmutableList;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.node_monitors.DiskSpaceMonitorDescriptor;
import hudson.slaves.AbstractCloudComputer;
import hudson.slaves.AbstractCloudSlave;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.OfflineCause;
import hudson.slaves.RetentionStrategy;
import jenkins.model.Jenkins;
import jenkins.plugins.openstack.compute.internal.DestroyMachine;
import jenkins.plugins.openstack.compute.internal.Openstack;
import jenkins.plugins.openstack.compute.slaveopts.LauncherFactory;
import org.jenkinsci.plugins.cloudstats.CloudStatistics;
import org.jenkinsci.plugins.cloudstats.PhaseExecutionAttachment;
import org.jenkinsci.plugins.cloudstats.ProvisioningActivity;
import org.jenkinsci.plugins.cloudstats.TrackedItem;
import org.jenkinsci.plugins.resourcedisposer.AsyncResourceDisposer;
import org.jenkinsci.plugins.resourcedisposer.Disposable;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.openstack4j.model.compute.Address;
import org.openstack4j.model.compute.Addresses;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.Server;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Jenkins Slave node.
*/
public class JCloudsSlave extends AbstractCloudSlave implements TrackedItem {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(JCloudsSlave.class.getName());
/** metadata fields that aren't worth showing to the user. */
private static final List<String> HIDDEN_METADATA_VALUES = Arrays.asList(
Openstack.FINGERPRINT_KEY_FINGERPRINT,
Openstack.FINGERPRINT_KEY_URL,
JCloudsSlaveTemplate.OPENSTACK_CLOUD_NAME_KEY,
JCloudsSlaveTemplate.OPENSTACK_TEMPLATE_NAME_KEY,
ServerScope.METADATA_KEY
);
private final @Nonnull String cloudName;
// Full/effective options
private /*final*/ @Nonnull SlaveOptions options;
private final @Nonnull ProvisioningActivity.Id provisioningId;
private transient @Nonnull Cache<String, Map<String, String>> cache;
private /*final*/ @Nonnull String nodeId;
private final long created = System.currentTimeMillis();
// Backward compatibility
private transient @Deprecated int overrideRetentionTime;
private transient @Deprecated String jvmOptions;
private transient @Deprecated String credentialsId;
private transient @Deprecated String slaveType; // converted to string for easier conversion
private transient @Deprecated Server metadata;
public JCloudsSlave(
@Nonnull ProvisioningActivity.Id id, @Nonnull Server metadata, @Nonnull String labelString, @Nonnull SlaveOptions slaveOptions
) throws IOException, Descriptor.FormException {
super(Objects.requireNonNull(metadata.getName()), slaveOptions.getFsRoot(), null /*needs to be set later via setter*/);
this.cloudName = id.getCloudName(); // TODO deprecate field
this.provisioningId = id;
this.options = slaveOptions;
this.nodeId = metadata.getId();
this.cache = makeCache();
setNumExecutors(slaveOptions.getNumExecutors());
setMode(Mode.NORMAL);
setLabelString(labelString);
setRetentionStrategy(new JCloudsRetentionStrategy());
setNodeProperties(mkNodeProperties(Openstack.getAccessIpAddress(metadata), slaveOptions.getNodeProperties()));
setLauncher(new JCloudsLauncher(getLauncherFactory().createLauncher(this)));
}
private static @Nonnull List<NodeProperty<? extends Node>> mkNodeProperties(@CheckForNull String vmIpAddressOrNull,
@CheckForNull final List<? extends NodeProperty<?>> templateNPsOrNull) {
final String vmIpAddressOrEmpty = Util.fixNull(vmIpAddressOrNull);
final EnvironmentVariablesNodeProperty.Entry ipAddressEnvVar = new EnvironmentVariablesNodeProperty.Entry(
"OPENSTACK_PUBLIC_IP", vmIpAddressOrEmpty);
final List<? extends NodeProperty<?>> templateNPsOrEmpty = Util.fixNull(templateNPsOrNull);
final List<NodeProperty<? extends Node>> result = mergeNodeProperties(templateNPsOrEmpty, ipAddressEnvVar);
return result;
}
private static @Nonnull List<NodeProperty<? extends Node>> mergeNodeProperties(
@Nonnull List<? extends NodeProperty<?>> templateNPs,
@Nonnull EnvironmentVariablesNodeProperty.Entry... extraEnvVarEntries) {
final EnvironmentVariablesNodeProperty envVarsNP = new EnvironmentVariablesNodeProperty(extraEnvVarEntries);
final ImmutableList.Builder<NodeProperty<? extends Node>> b = ImmutableList.builder();
boolean envVarsAdded = false;
for (final NodeProperty<?> templateNP : templateNPs) {
if (envVarsAdded == false && templateNP instanceof EnvironmentVariablesNodeProperty) {
final EnvironmentVariablesNodeProperty templateEnvVarNP = (EnvironmentVariablesNodeProperty) templateNP;
final EnvVars templateEnvVars = templateEnvVarNP.getEnvVars();
final EnvVars extraEnvVars = envVarsNP.getEnvVars();
extraEnvVars.putAll(templateEnvVars);
b.add(envVarsNP);
envVarsAdded = true;
} else {
final NodeProperty<?> copyOfTemplateNP = makeCopy(templateNP);
b.add(copyOfTemplateNP);
}
}
if (!envVarsAdded && extraEnvVarEntries.length != 0) {
b.add(envVarsNP);
}
final List<NodeProperty<? extends Node>> nodePropertiesList = b.build();
return nodePropertiesList;
}
@SuppressWarnings("unchecked")
private static <T> T makeCopy(final T original) {
final String xml = Jenkins.XSTREAM.toXML(original);
final Object copy = Jenkins.XSTREAM.fromXML(xml);
return (T) copy;
}
// In 2.0, "nodeId" was removed and replaced by "metadata". Then metadata was deprecated in favour of "nodeId" again.
// The configurations stored are expected to have at least one of them.
@SuppressWarnings({"unused", "deprecation"})
@SuppressFBWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "The fields are non-null after readResolve"})
protected Object readResolve() {
super.readResolve();
cache = makeCache();
if (options == null) {
// Node options are not of override of anything so we need to ensure this fill all mandatory fields
// We base the outdated config on current plugin defaults to increase the chance it will work.
SlaveOptions.Builder builder = JCloudsCloud.DescriptorImpl.getDefaultOptions().getBuilder()
.jvmOptions(Util.fixEmpty(jvmOptions))
;
LauncherFactory lf = "SSH".equals(slaveType)
? new LauncherFactory.SSH(credentialsId)
: LauncherFactory.JNLP.JNLP
;
builder.launcherFactory(lf);
if (overrideRetentionTime > 0) {
builder = builder.retentionTime(overrideRetentionTime);
}
options = builder.build();
jvmOptions = null;
credentialsId = null;
slaveType = null;
}
if (metadata != null && (nodeId == null || !nodeId.equals(metadata.getId()))) {
nodeId = metadata.getId();
metadata = null;
}
nodeId = nodeId.replaceFirst(".*/", ""); // Remove region prefix
return this;
}
/** Creates a cache where data will be kept for a short duration before being discarded. */
private static @Nonnull <K, V> Cache<K, V> makeCache() {
return Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
}
/**
* Gets most of the Server settings that were provided to Openstack
* when the slave was created by the plugin.
* Not all settings are interesting and any that are empty/null are omitted.
*
* @return A Map of option name to value. This will not be null or empty.
*/
@Restricted(DoNotUse.class) // Jelly
public @Nonnull Map<String, String> getOpenstackSlaveData() {
final Map<String, String> result = new LinkedHashMap<>();
final SlaveOptions slaveOptions = getSlaveOptions();
putIfNotNullOrEmpty(result, "Network(s)", slaveOptions.getNetworkId());
putIfNotNullOrEmpty(result, "Custom Metadata", slaveOptions.getCustomMetaData());
putIfNotNullOrEmpty(result, "Floating Ip Pool", slaveOptions.getFloatingIpPool());
putIfNotNullOrEmpty(result, "Security Groups", slaveOptions.getSecurityGroups());
putIfNotNullOrEmpty(result, "Start Timeout (ms)", slaveOptions.getStartTimeout());
final Object launcherFactory = slaveOptions.getLauncherFactory();
putIfNotNullOrEmpty(result, "Launcher Factory",
launcherFactory == null ? null : launcherFactory.getClass().getSimpleName());
putIfNotNullOrEmpty(result, "JVM Options", slaveOptions.getJvmOptions());
return result;
}
/**
* Get settings from OpenStack about the Server for this slave.
*
* @return A Map of fieldName to value. This will not be null or empty.
*/
@Restricted(DoNotUse.class) // Jelly
public @Nonnull Map<String, String> getLiveOpenstackServerDetails() {
return getCachableData("liveData", (String unused) -> readLiveOpenstackServerDetails());
}
private @Nonnull Map<String, String> readLiveOpenstackServerDetails() {
final Map<String, String> result = new LinkedHashMap<>();
final Server s = readOpenstackServer();
if (s == null) {
return result;
}
final Addresses addresses = s.getAddresses();
if (addresses != null) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, List<? extends Address>> e: addresses.getAddresses().entrySet()) {
String networkName = e.getKey();
for (Address address : e.getValue()) {
if (sb.length() != 0) {
sb.append(", ");
}
sb.append(address.getAddr()).append(" (").append(address.getType()).append(" in ").append(networkName).append(")");
}
}
putIfNotNullOrEmpty(result, "Addresses", sb.toString());
}
putIfNotNullOrEmpty(result, "Availability Zone", s.getAvailabilityZone());
putIfNotNullOrEmpty(result, "Config Drive", s.getConfigDrive());
final Flavor flavor = s.getFlavor();
if (flavor != null) {
putIfNotNullOrEmpty(result, "Flavor", Openstack.getFlavorInfo(flavor));
}
putIfNotNullOrEmpty(result, "Host", s.getHost());
putIfNotNullOrEmpty(result, "Instance Name", s.getInstanceName());
putIfNotNullOrEmpty(result, "Key Name", s.getKeyName());
List<String> volumes = s.getOsExtendedVolumesAttached();
if (volumes != null && !volumes.isEmpty()) {
putIfNotNullOrEmpty(result, "osExtendedVolumesAttached", volumes);
}
putIfNotNullOrEmpty(result, "Power State", s.getPowerState());
putIfNotNullOrEmpty(result, "Status", s.getStatus());
putIfNotNullOrEmpty(result, "Fault", s.getFault());
putIfNotNullOrEmpty(result, "Created", s.getCreated());
putIfNotNullOrEmpty(result, "Launched At", s.getLaunchedAt());
putIfNotNullOrEmpty(result, "Updated", s.getUpdated());
putIfNotNullOrEmpty(result, "Terminated At", s.getTerminatedAt());
final Map<String, String> metaDataOrNull = s.getMetadata();
if (metaDataOrNull != null) {
for (Map.Entry<String, String> e : metaDataOrNull.entrySet()) {
final String key = e.getKey();
if (HIDDEN_METADATA_VALUES.contains(key)) {
continue;
}
putIfNotNullOrEmpty(result, "metadata." + key, e.getValue());
}
}
return result;
}
private Server readOpenstackServer() {
try {
return getOpenstack(cloudName).getServerById(nodeId);
} catch (NoSuchElementException ex) {
// just return empty
} catch (Exception ex) {
LOGGER.log(Level.WARNING,
"Unable to read details of server '" + nodeId + "' from cloud '" + cloudName + "'.", ex);
}
return null;
}
private static void putIfNotNullOrEmpty(
@Nonnull final Map<String, String> mapToBeAddedTo,
@Nonnull final String fieldName,
@CheckForNull final Object fieldValue
) {
if (fieldValue != null) {
final String valueString = Util.fixEmptyAndTrim(fieldValue.toString());
if (valueString != null) {
mapToBeAddedTo.put(fieldName, valueString);
}
}
}
/** Gets something from the cache, loading it into the cache if necessary. */
private @Nonnull Map<String, String> getCachableData(@Nonnull final String key, @Nonnull final Function<String, Map<String, String>> dataloader) {
try {
return Objects.requireNonNull(cache.get(key, dataloader));
} catch (RuntimeException e) { // Propagated from cacheMissFunction
final Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw e;
}
}
/**
* Get public IP address of the server.
*
* @throws NoSuchElementException The server does not exist anymore. Plugin should not get slave to this state ever
* but there is no way to prevent external machine deletion.
*/
public @CheckForNull String getPublicAddress() throws NoSuchElementException {
return Openstack.getAccessIpAddress(getOpenstack(cloudName).getServerById(nodeId));
}
/**
* Get effective options used to configure this slave.
*/
public @Nonnull SlaveOptions getSlaveOptions() {
options.getClass();
return options;
}
public @Nonnull LauncherFactory getLauncherFactory() {
LauncherFactory lf = options.getLauncherFactory();
return lf == null ? LauncherFactory.JNLP.JNLP : lf;
}
@Override
@SuppressWarnings("unchecked") // Parent signature is type-unsafe, let's handle it here instead of in all clients
public RetentionStrategy<JCloudsComputer> getRetentionStrategy() {
return ((RetentionStrategy<JCloudsComputer>) super.getRetentionStrategy());
}
// Exposed for testing and Jelly
@Restricted(NoExternalUse.class)
public @Nonnull String getServerId() {
return nodeId;
}
@Override
public AbstractCloudComputer<JCloudsSlave> createComputer() {
LOGGER.info("Creating a new computer for " + getNodeName());
return new JCloudsComputer(this);
}
@Override
public @Nonnull ProvisioningActivity.Id getId() {
return this.provisioningId;
}
public long getCreatedTime() {
return created;
}
/**
* @return True if the agent should have been up by now, and it is not. Note it could have been up momentarily before.
*/
public boolean isLaunchTimedOut() {
JCloudsComputer computer = getComputer();
if (computer != null && computer.isOnline()) return false;
long existsFor = System.currentTimeMillis() - created;
return existsFor > getSlaveOptions().getStartTimeout();
}
@Override public JCloudsComputer getComputer() {
return (JCloudsComputer) super.getComputer();
}
@Extension
public static final class JCloudsSlaveDescriptor extends SlaveDescriptor {
@Override
public String getDisplayName() {
return "JClouds Slave";
}
@Override
public boolean isInstantiable() {
return false;
}
}
@Override
protected void _terminate(TaskListener listener) {
CloudStatistics cloudStatistics = CloudStatistics.get();
ProvisioningActivity activity = cloudStatistics.getActivityFor(this);
if (activity != null) {
activity.enterIfNotAlready(ProvisioningActivity.Phase.COMPLETED);
// Attach what is likely a reason for the termination
OfflineCause offlineCause = getFatalOfflineCause();
if (offlineCause != null) {
PhaseExecutionAttachment attachment = new PhaseExecutionAttachment(ProvisioningActivity.Status.WARN, offlineCause.toString());
cloudStatistics.attach(activity, ProvisioningActivity.Phase.COMPLETED, attachment);
}
}
getLauncherFactory().onNodeTerminated();
// Wrap deletion disposables into statistics tracking disposables
AsyncResourceDisposer.get().dispose(
new RecordDisposal(
new DestroyMachine(cloudName, nodeId),
provisioningId
)
);
}
/**
* Get computer {@link OfflineCause} provided it is severe enough the computer should be discarded.
*
* @return value if should be discarded, null if online or offline with non-fatal cause.
*/
/*package*/ @CheckForNull OfflineCause getFatalOfflineCause() {
JCloudsComputer computer = getComputer();
// Computer might be gone yet, so use the offline cause attached to node when that happens
OfflineCause oc = computer != null
? computer.getOfflineCause()
: getTemporaryOfflineCause()
;
if (isLaunchTimedOut() && (oc instanceof OfflineCause.LaunchFailed)) return oc;
return oc instanceof DiskSpaceMonitorDescriptor.DiskSpace || oc instanceof OfflineCause.ChannelTermination
? oc
: null
;
}
private static Openstack getOpenstack(String cloudName) {
return JCloudsCloud.getByName(cloudName).getOpenstack();
}
/**
* Second layer disposable to track removal of Jenkins slave.
*
* @see DestroyMachine
*/
private final static class RecordDisposal implements Disposable {
private static final long serialVersionUID = -3623764445481732365L;
private final @Nonnull Disposable inner;
private final @Nonnull ProvisioningActivity.Id provisioningId;
private RecordDisposal(@Nonnull Disposable inner, @Nonnull ProvisioningActivity.Id provisioningId) {
this.inner = inner;
this.provisioningId = provisioningId;
}
@Override
public @Nonnull State dispose() throws Throwable {
try {
return inner.dispose();
} catch (Throwable ex) {
CloudStatistics statistics = CloudStatistics.get();
ProvisioningActivity activity = statistics.getPotentiallyCompletedActivityFor(provisioningId);
if (activity != null) {
PhaseExecutionAttachment.ExceptionAttachment attachment = new PhaseExecutionAttachment.ExceptionAttachment(
ProvisioningActivity.Status.WARN, ex
);
statistics.attach(activity, ProvisioningActivity.Phase.COMPLETED, attachment);
}
// Log the problem once and then stop tracking it
if (ex instanceof DestroyMachine.CloudGoneException) return State.PURGED;
throw ex;
}
}
@Override
public @Nonnull String getDisplayName() {
return inner.getDisplayName();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RecordDisposal that = (RecordDisposal) o;
if (!inner.equals(that.inner)) return false;
return provisioningId.equals(that.provisioningId);
}
@Override
public int hashCode() {
return Objects.hash(inner.hashCode(), provisioningId.hashCode());
}
}
}