-
Notifications
You must be signed in to change notification settings - Fork 12
/
AdoptOpenJDKInstaller.java
435 lines (391 loc) · 17.8 KB
/
AdoptOpenJDKInstaller.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
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
package io.jenkins.plugins.adoptopenjdk;
/*
* #%L
* Eclipse Temurin installer Plugin
* %%
* Copyright (C) 2016 - 2019 Mads Mohr Christensen
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.DownloadService;
import hudson.model.JDK;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.tools.ToolInstallation;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolInstallerDescriptor;
import hudson.tools.ZipExtractionInstaller;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.apache.commons.io.input.CountingInputStream;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* Install OpenJDK from <a href="https://adoptium.net">Adoptium</a>
* Based on <a href="https://github.com/jenkinsci/jdk-tool-plugin">Oracle Java SE Development Kit Installer</a>
*/
public class AdoptOpenJDKInstaller extends ToolInstaller {
private static boolean DISABLE_CACHE = Boolean.getBoolean(AdoptOpenJDKInstaller.class.getName() + ".cache.disable");
/**
* Eclipse Temurin release id
*/
public final String id;
@DataBoundConstructor
public AdoptOpenJDKInstaller(String id) {
super(null);
this.id = id;
}
@NonNull
private static AdoptOpenJDKFamilyList getAdoptOpenJDKFamilyList() throws IOException {
AdoptOpenJDKList list = AdoptOpenJDKList.all().get(AdoptOpenJDKList.class);
if (list == null) {
throw new IOException(Messages.AdoptOpenJDKInstaller_getAdoptOpenJDKFamilyList_NoDownloadable());
}
return list.toList();
}
@Override
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "TODO needs triage")
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log)
throws IOException, InterruptedException {
FilePath expected = preferredLocation(tool, node);
try {
// already installed?
FilePath marker = expected.child(".installedByJenkins");
if (marker.exists() && marker.readToString().equals(id)) {
return expected;
}
expected.deleteRecursive();
expected.mkdirs();
AdoptOpenJDKFamilyList jdkFamilyList = getAdoptOpenJDKFamilyList();
if (jdkFamilyList.isEmpty()) {
throw new IOException(Messages.AdoptOpenJDKInstaller_performInstallation_emptyJdkFamilyList());
}
AdoptOpenJDKRelease release = jdkFamilyList.getRelease(id);
if (release == null) {
throw new IOException(Messages.AdoptOpenJDKInstaller_performInstallation_releaseNotFound(id));
}
Platform p = Platform.of(node);
CPU c = CPU.of(node);
AdoptOpenJDKFile binary = release.getBinary(p, c);
if (binary == null) {
throw new IOException(
Messages.AdoptOpenJDKInstaller_performInstallation_binaryNotFound(id, p.name(), c.name()));
}
File cache = getLocalCacheFile(p, c);
if (!DISABLE_CACHE && cache.exists()) {
try (InputStream in = cache.toURI().toURL().openStream()) {
CountingInputStream cis = new CountingInputStream(in);
try {
log.getLogger()
.println(Messages.AdoptOpenJDKInstaller_performInstallation_fromCache(
cache, expected, node.getDisplayName()));
// the zip contains already the directory so we unzip to parent directory
FilePath parent = expected.getParent();
if (parent != null) {
parent.unzipFrom(cis);
} else {
throw new NullPointerException("Parent directory of " + expected + " is null");
}
} catch (IOException e) {
throw new IOException(
Messages.AdoptOpenJDKInstaller_performInstallation_failedToUnpack(
cache.toURI().toURL(), cis.getByteCount()),
e);
}
}
} else {
String url = binary.binary_link;
ZipExtractionInstaller zipExtractionInstaller = new ZipExtractionInstaller(null, url, null);
FilePath installation = zipExtractionInstaller.performInstallation(tool, node, log);
installation.child(".timestamp").delete(); // we don't use the timestamp
FilePath base = findPullUpDirectory(installation, p);
if (base != null && base != expected) {
base.moveAllChildrenTo(expected);
}
marker.write(id, null);
if (!DISABLE_CACHE) {
// update the local cache on master
// download to a temporary file and rename it in to handle concurrency and failure correctly,
Path tmp = new File(cache.getPath() + ".tmp").toPath();
try {
Path tmpParent = tmp.getParent();
if (tmpParent != null) {
Files.createDirectories(tmpParent);
}
try (OutputStream out = Files.newOutputStream(tmp)) {
expected.zip(out);
}
Files.move(tmp, cache.toPath(), StandardCopyOption.REPLACE_EXISTING);
} finally {
Files.deleteIfExists(tmp);
}
}
}
} catch (DetectionFailedException e) {
log.getLogger().println(Messages.AdoptOpenJDKInstaller_performInstallation_JdkSkipped(e.getMessage()));
}
return expected;
}
private File getLocalCacheFile(Platform platform, CPU cpu) {
// we force .zip file
return new File(Jenkins.get().getRootDir(), "caches/adoptopenjdk/" + platform + "/" + cpu + "/" + id + ".zip");
}
/**
* Often an archive contains an extra top-level directory that's unnecessary when extracted on the disk
* into the expected location. If your installation sources provide that kind of archives, override
* this method to find the real root location.
*
* <p>
* The caller will "pull up" the discovered real root by throw away the intermediate directory,
* so that the user-configured "tool home" directory contains the right files.
*
* <p>
* The default implementation applies some heuristics to auto-determine if the pull up is necessary.
* This should work for typical archive files.
*
* @param root The directory that contains the extracted archive. This directory contains nothing but the
* extracted archive. For example, if the user installed
* <a href="https://archive.apache.org/dist/ant/binaries/jakarta-ant-1.1.zip">jakarta-ant-1.1.zip</a>, this directory would contain
* a single directory "jakarta-ant".
* @param platform The platform for which to find pull up directory for.
* @return Return the real top directory inside {@code root} that contains the meat. In the above example,
* {@code root.child("jakarta-ant")} should be returned. If there's no directory to pull up, return null.
* @throws IOException Signals that an I/O exception of some sort has occurred.
* @throws InterruptedException Thrown when a thread is interrupted.
*/
protected FilePath findPullUpDirectory(FilePath root, Platform platform) throws IOException, InterruptedException {
// if the directory just contains one directory and that alone, assume that's the pull up subject
// otherwise leave it as is.
List<FilePath> children = root.listDirectories();
if (children.size() != 1) return null;
// Since the MacOS tar.gz file uses a different layout we need to handle this platform differently
// https://blog.adoptopenjdk.net/2018/10/macos-binary-changes
if (platform == Platform.MACOS) {
FilePath contents = children.get(0).child("Contents/Home");
if (contents.exists() && contents.isDirectory()) return contents;
}
return children.get(0);
}
/**
* Supported platform
*/
public enum Platform {
LINUX("linux"),
WINDOWS("windows"),
MACOS("mac"),
SOLARIS("solaris"),
AIX("aix");
private final String id;
Platform(String id) {
this.id = id;
}
public static Platform of(Node n) throws IOException, InterruptedException, DetectionFailedException {
VirtualChannel channel = n.getChannel();
if (channel == null) {
throw new IOException(Messages.AdoptOpenJDKInstaller_Platform_nullChannel(n.getDisplayName()));
}
return channel.call(new Platform.GetCurrentPlatform());
}
public static Platform current() throws DetectionFailedException {
String arch = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
if (arch.contains("linux")) return LINUX;
if (arch.contains("windows")) return WINDOWS;
if (arch.contains("sun") || arch.contains("solaris")) return SOLARIS;
if (arch.contains("mac")) return MACOS;
if (arch.contains("aix")) return AIX;
throw new DetectionFailedException(Messages.AdoptOpenJDKInstaller_Platform_unknownPlatform(arch));
}
public String getId() {
return id;
}
static class GetCurrentPlatform extends MasterToSlaveCallable<Platform, DetectionFailedException> {
private static final long serialVersionUID = 1L;
public Platform call() throws DetectionFailedException {
return current();
}
}
}
/**
* Supported CPU architecture
*/
public enum CPU {
i386,
amd64,
Sparc,
s390x,
ppc64,
arm;
public static CPU of(Node n) throws IOException, InterruptedException, DetectionFailedException {
VirtualChannel channel = n.getChannel();
if (channel == null) {
throw new IOException(Messages.AdoptOpenJDKInstaller_CPU_nullChannel(n.getDisplayName()));
}
return channel.call(new CPU.GetCurrentCPU());
}
public static CPU current() throws DetectionFailedException {
String arch = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
if (arch.contains("sparc")) return Sparc;
if (arch.contains("amd64") || arch.contains("86_64")) return amd64;
if (arch.contains("86")) return i386;
if (arch.contains("s390x")) return s390x;
if (arch.contains("ppc64")) return ppc64;
if (arch.contains("arm") || arch.contains("aarch64")) return arm;
throw new DetectionFailedException(Messages.AdoptOpenJDKInstaller_CPU_unknownCpu(arch));
}
static class GetCurrentCPU extends MasterToSlaveCallable<CPU, DetectionFailedException> {
private static final long serialVersionUID = 1L;
public CPU call() throws DetectionFailedException {
return current();
}
}
}
@Extension
@Symbol("adoptOpenJdkInstaller")
public static class DescriptorImpl extends ToolInstallerDescriptor<AdoptOpenJDKInstaller> {
@NonNull
@Override
public String getDisplayName() {
return Messages.AdoptOpenJDKInstaller_DescriptorImpl_DisplayName();
}
@Override
public boolean isApplicable(Class<? extends ToolInstallation> toolType) {
return toolType == JDK.class;
}
public List<AdoptOpenJDKFamily> getInstallableJDKs() throws IOException {
return Arrays.asList(getAdoptOpenJDKFamilyList().data);
}
}
@Extension
@Symbol("adoptOpenJdk")
public static final class AdoptOpenJDKList extends DownloadService.Downloadable {
public AdoptOpenJDKList() {
super(AdoptOpenJDKInstaller.class);
}
public AdoptOpenJDKFamilyList toList() throws IOException {
JSONObject d = getData();
if (d == null) return new AdoptOpenJDKFamilyList();
return (AdoptOpenJDKFamilyList) JSONObject.toBean(d, AdoptOpenJDKFamilyList.class);
}
}
private static final class DetectionFailedException extends Exception {
private DetectionFailedException(String message) {
super(message);
}
}
@SuppressFBWarnings(
value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "NP_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD"},
justification = "Field initialized during deserialization from JSON object")
public static final class AdoptOpenJDKFamilyList {
public AdoptOpenJDKFamily[] data = new AdoptOpenJDKFamily[0];
public int version;
public boolean isEmpty() {
for (AdoptOpenJDKFamily f : data) {
if (f.releases.length > 0) {
return false;
}
}
return true;
}
public AdoptOpenJDKRelease getRelease(String productCode) {
for (AdoptOpenJDKFamily f : data) {
for (AdoptOpenJDKRelease r : f.releases) {
if (r.matchesId(productCode)) {
return r;
}
}
}
return null;
}
}
@SuppressFBWarnings(
value = {"UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD"},
justification = "Field initialized during deserialization from JSON object")
public static final class AdoptOpenJDKFamily {
public String name;
public AdoptOpenJDKRelease[] releases;
}
@SuppressFBWarnings(
value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "NP_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD"},
justification = "Field initialized during deserialization from JSON object")
public static final class AdoptOpenJDKRelease {
public AdoptOpenJDKFile[] binaries;
public String release_name;
public String openjdk_impl;
public boolean matchesId(String rhs) {
return rhs != null && rhs.equals(release_name);
}
public AdoptOpenJDKFile getBinary(Platform platform, CPU cpu) throws IOException {
for (AdoptOpenJDKFile f : binaries) {
if (!platform.getId().equals(f.os) || !openjdk_impl.equals(f.openjdk_impl)) {
continue;
}
switch (cpu) {
case i386:
if (f.architecture.equals("x32")) return f;
break;
case amd64:
if (f.architecture.equals("x64")) return f;
break;
case Sparc:
if (f.architecture.equals("sparcv9")) return f;
break;
case ppc64:
if (f.architecture.equals("ppc64") || f.architecture.equals("ppc64le")) return f;
break;
case s390x:
if (f.architecture.equals("s390x")) return f;
break;
case arm:
if (f.architecture.equals("arm") || f.architecture.equals("aarch64")) return f;
break;
default:
throw new IOException(
Messages.AdoptOpenJDKInstaller_AdoptOpenJDKRelease_usupportedCpu(cpu.name()));
}
}
return null;
}
}
@SuppressFBWarnings(
value = "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD",
justification = "Field initialized during deserialization from JSON object")
public static final class AdoptOpenJDKFile {
public String architecture;
public String os;
public String openjdk_impl;
public String binary_link;
}
}