-
Notifications
You must be signed in to change notification settings - Fork 7
/
jlink.go
477 lines (397 loc) · 13.4 KB
/
jlink.go
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
// 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
//
// https://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.
package main
import (
"bytes"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/mholt/archiver/v3"
"github.com/russross/blackfriday"
)
var (
// The default listening port
PORT = "80"
// A cache directory for base runtimes
RT_CACHE = filepath.FromSlash(os.TempDir() + "/runtime_cache")
// A directory for short-lived files
TMP = os.TempDir()
// Whether Maven Central integration is enabled
MAVEN_CENTRAL = false
// The platform for local runtimes
LOCAL_PLATFORM = determineLocalPlatform()
// The architecture for local runtimes
LOCAL_ARCH = "x64"
// The default path for swagger documentation
SWAGGER_PATH = "/app/swagger-ui"
)
// A client for downloading artifacts and release metadata from api.adoptopenjdk.net
var adoptium = &http.Client{
Timeout: time.Second * 120,
}
// A client for downloading Maven Central artifacts
var mavenCentral = &http.Client{
Timeout: time.Second * 60,
}
// RuntimeRequest represents an incoming request from the JSON endpoint.
type runtimeRequest struct {
// Maven Central artifacts in G:A:V format
Artifacts []string `json:"artifacts"`
// The modules to include in the runtime
Modules []string `json:"modules"`
// The output endian type
Endian string `json:"endian"`
// The Java version
Version string `json:"version"`
// The OS type
Platform string `json:"os"`
// The architecture type
Arch string `json:"arch"`
// The implementation type
Implementation string `json:"implementation"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*.tmpl.html")
// Override environment variables
if port, exists := os.LookupEnv("PORT"); exists {
PORT = port
}
if maven_central, exists := os.LookupEnv("MAVEN_CENTRAL"); exists {
if b, err := strconv.ParseBool(maven_central); err == nil {
MAVEN_CENTRAL = b
} else {
log.Fatal("Invalid value for MAVEN_CENTRAL flag")
}
}
if arch, exists := os.LookupEnv("LOCAL_ARCH"); exists {
LOCAL_ARCH = arch
}
if cache, exists := os.LookupEnv("RT_CACHE"); exists {
RT_CACHE = cache
}
if tmp, exists := os.LookupEnv("TMP"); exists {
TMP = tmp
}
_ = os.MkdirAll(RT_CACHE, os.ModePerm)
_ = os.MkdirAll(TMP, os.ModePerm)
router.GET("/", func(context *gin.Context) {
readmeFile, err := ioutil.ReadFile("./README.md")
if err != nil {
log.Println(err)
}
// readme := blackfriday.MarkdownCommon([]byte(readmeFile))
readme := template.HTML(blackfriday.MarkdownCommon([]byte(readmeFile)))
context.HTML(http.StatusOK, "index.tmpl.html", gin.H{
"markdown": readme,
})
return
})
// An endpoint for API documentation
router.Static("/swagger-ui", SWAGGER_PATH)
// An endpoint for health checks
router.GET("/status", func(context *gin.Context) {
if LOCAL_PLATFORM != "windows" {
out, err := exec.Command("df", "-B1", "--output=avail", RT_CACHE).Output()
if err == nil {
free, _ := strconv.Atoi(strings.Fields(string(out))[1])
context.JSON(http.StatusOK, gin.H{"success": true, "cache_free": free})
return
}
}
context.JSON(http.StatusOK, gin.H{"success": true})
})
// An endpoint for runtime requests
router.GET("/runtime/:arch/:os/:version", func(context *gin.Context) {
var (
arch = context.Param("arch")
endian = context.Query("endian")
impl = context.DefaultQuery("implementation", "hotspot")
platform = context.Param("os")
version = context.Param("version")
modules = strings.Split(context.DefaultQuery("modules", "java.base"), ",")
)
var artifacts []string
if a := context.Query("artifacts"); a != "" {
if MAVEN_CENTRAL {
artifacts = strings.Split(a, ",")
} else {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Maven Central integration is disabled"})
return
}
}
handleRequest(context, platform, arch, version, endian, impl, modules, artifacts)
})
// An endpoint for runtime requests (JSON)
router.POST("/runtime", func(context *gin.Context) {
var req runtimeRequest
err := context.BindJSON(&req)
if err != nil {
return
}
if !MAVEN_CENTRAL && len(req.Artifacts) > 0 {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Maven Central integration is disabled"})
return
}
handleRequest(context, req.Platform, req.Arch, req.Version, req.Endian, req.Implementation, req.Modules, req.Artifacts)
})
// An endpoint for runtime requests containing a module-info.java file
router.POST("/runtime/:arch/:os/:version", func(context *gin.Context) {
bytes, err := context.GetRawData()
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "The request body must be a valid module-info.java file"})
return
}
var (
arch = context.Param("arch")
endian = context.Query("endian")
impl = context.DefaultQuery("implementation", "hotspot")
platform = context.Param("os")
version = context.Param("version")
)
var artifacts []string
if a := context.Query("artifacts"); a != "" {
if MAVEN_CENTRAL {
artifacts = strings.Split(a, ",")
} else {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Maven Central integration is disabled"})
return
}
}
handleRequest(context, platform, arch, version, endian, impl, parseModuleInfo(string(bytes)), artifacts)
})
router.Run(":" + PORT)
}
var (
archCheck = regexp.MustCompile(`^(x64|x32|ppc64|s390x|ppc64le|aarch64|arm)$`)
artifactCheck = regexp.MustCompile(`^[\w\.-]+:[\w\.-]+:[\w\.-]+$`)
moduleCheck = regexp.MustCompile(`^[\w\.]+$`)
platformCheck = regexp.MustCompile(`^(linux|windows|mac|solaris|aix)$`)
versionCheck = regexp.MustCompile(`^[1-9][0-9]*((\.0)*\.[1-9][0-9]*)*(\+[1-9][0-9]*((\.0)*\.[1-9][0-9]*)*)?$`)
)
func handleRequest(context *gin.Context, platform, arch, version, endian, implementation string, modules, artifacts []string) {
// Validate platform type
if !platformCheck.MatchString(platform) {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Valid operating systems: [windows, linux, mac, solaris, aix]"})
return
}
// Validate architecture type
if !archCheck.MatchString(arch) {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Valid architectures: [x64, x32, ppc64, s390x, ppc64le, aarch64, arm]"})
return
}
// Validate artifacts
for _, artifact := range artifacts {
if !artifactCheck.MatchString(artifact) {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Invalid artifact"})
return
}
}
// Validate modules
for _, module := range modules {
if !moduleCheck.MatchString(module) {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Invalid module"})
return
}
}
// Validate endian type
if endian == "" {
// Guess according to supplied architecture
if arch == "ppc64" || arch == "s390x" {
endian = "big"
} else {
endian = "little"
}
}
if endian != "big" && endian != "little" {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Valid endian types: [little, big]"})
return
}
// Validate implementation
if implementation != "hotspot" && implementation != "openj9" {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Valid implementation types: [hotspot, openj9]"})
return
}
// Validate version number
if !versionCheck.MatchString(version) {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Invalid Java version"})
return
}
// Validate major version number
majorVersion, err := getMajorVersion(version)
if err != nil || majorVersion < 9 {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Invalid Java version"})
return
}
// Lookup the target runtime whose modules will be packaged into a new runtime image
target, err := lookupRelease(arch, platform, implementation, version)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to find target runtime"})
return
}
// Lookup a runtime containing a compatible version of jlink for local use
local, err := lookupRelease(LOCAL_ARCH, LOCAL_PLATFORM, implementation, version)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to find local runtime"})
return
}
// Download the local runtime
localRuntimePath, err := downloadRelease(local, version)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to download local runtime"})
log.Println(err)
return
}
// Download the target runtime
targetRuntimePath, err := downloadRelease(target, version)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to download target runtime"})
log.Println(err)
return
}
// Create a directory for Maven Central artifacts
mavenCentral, dir := newTemporaryDirectory("mavenCentral")
defer os.RemoveAll(dir)
// Download any required artifacts
if err := downloadArtifacts(mavenCentral, artifacts); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to download Maven Central artifacts"})
log.Println(err)
return
}
// Run jlink on the target runtime
archive, err := jlink(localRuntimePath, mavenCentral, targetRuntimePath, endian, version, platform, target.Package.Name, modules)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"success": false, "reason": "Failed to generate runtime"})
log.Println(err)
return
}
context.DataFromReader(http.StatusOK, int64(archive.Len()), "application/octet-stream", archive, map[string]string{
"Content-Disposition": "attachment; filename=\"" + target.Package.Name + "\"",
"Accept-Ranges": "bytes",
})
}
// Jlink uses a standard JDK runtime to generate a custom runtime image
// for the given set of modules.
func jlink(jdk, mavenCentral, runtime, endian, version, platform, filename string, modules []string) (*bytes.Buffer, error) {
var modulePath, jlink string
// Add the base module if it's not there
base := false
for _, m := range modules {
if m == "java.base" {
base = true
break
}
}
if !base {
modules = append(modules, "java.base")
}
output, dir := newTemporaryFile("jdk-" + version)
defer os.RemoveAll(dir)
// Build module path according to target platform
switch platform {
case "mac":
_, err := os.Stat(filepath.FromSlash(runtime + "/Contents/Home/jmods"))
if err != nil {
return nil, err
}
modulePath = filepath.FromSlash(runtime + "/Contents/Home/jmods" + string(os.PathListSeparator) + mavenCentral)
case "windows":
_, err := os.Stat(filepath.FromSlash(runtime + "/jmods"))
if err != nil {
return nil, err
}
modulePath = filepath.FromSlash(runtime + "/jmods" + string(os.PathListSeparator) + mavenCentral)
default:
_, err := os.Stat(filepath.FromSlash(runtime + "/jmods"))
if err != nil {
return nil, err
}
modulePath = filepath.FromSlash(runtime + "/jmods" + string(os.PathListSeparator) + mavenCentral)
}
// Build jlink command according to local platform
switch LOCAL_PLATFORM {
case "mac":
jlink = filepath.FromSlash(jdk + "/Contents/Home/bin/jlink")
case "windows":
jlink = filepath.FromSlash(jdk + "/bin/jlink.exe")
default:
jlink = filepath.FromSlash(jdk + "/bin/jlink")
}
if err := os.Chmod(jlink, os.ModePerm); err != nil {
return nil, err
}
cmd := exec.Command(jlink,
// Share string constants
"--compress=1",
// Exclude headers
"--no-header-files",
// Exclude man pages
"--no-man-pages",
// Remove debug information
"--strip-debug",
// The target endian-ness
"--endian", endian,
// The path where modules can be found
"--module-path", modulePath,
// The selected modules
"--add-modules", strings.Join(modules, ","),
// The output directory
"--output", output)
log.Println("JLINK:", cmd.Args)
if err := cmd.Run(); err != nil {
return nil, err
}
archive, dir := newTemporaryFile(filename)
defer os.RemoveAll(dir)
// Archiver can't handle the symlinks in /legal on windows
if LOCAL_PLATFORM == "windows" {
_ = os.RemoveAll(filepath.FromSlash(output + "/legal"))
}
if err := archiver.Archive([]string{output}, archive); err != nil {
return nil, err
}
f, err := os.Open(archive)
if err != nil {
return nil, err
}
defer f.Close()
var buffer bytes.Buffer
buffer.ReadFrom(f)
return &buffer, nil
}
func determineLocalPlatform() string {
var localPlatform string
if platform, exists := os.LookupEnv("LOCAL_PLATFORM"); exists {
localPlatform = platform
} else {
// For a list of possible values, run: go tool dist list
localPlatform = runtime.GOOS
}
// See https://api.adoptopenjdk.net/swagger-ui/#/Binary/get_v3_binary_latest__feature_version___release_type___os___arch___image_type___jvm_impl___heap_size___vendor_
// for a list of accepted platforms by AdoptOpenJDK.
switch localPlatform {
case "darwin":
return "mac"
default:
return localPlatform
}
}