Skip to content

Commit 1bf4bab

Browse files
caaladortaefi
andauthored
fix: Support Alpine by detecting OS using /etc/alpine-release (#14300)
* fix: Support Alpine by detecting OS using /etc/alpine-release musl is Experimental so using the experimental download root Fixes #13837 * test: Add tests for Platform and unofficial node download URL Co-authored-by: taefi <taefi.soroosh@gmail.com>
1 parent 769d5ab commit 1bf4bab

3 files changed

Lines changed: 150 additions & 2 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/installer/NodeInstaller.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class NodeInstaller {
4848

4949
public static final String DEFAULT_NODEJS_DOWNLOAD_ROOT = "https://nodejs.org/dist/";
5050

51+
public static final String UNOFFICIAL_NODEJS_DOWNLOAD_ROOT = "https://unofficial-builds.nodejs.org/download/release/";
52+
5153
private static final String NODE_WINDOWS = INSTALL_PATH.replaceAll("/",
5254
"\\\\") + "\\node.exe";
5355
private static final String NODE_DEFAULT = INSTALL_PATH + "/node";
@@ -197,7 +199,7 @@ public void install() throws InstallationException {
197199
synchronized (lock) {
198200
// If no download root defined use default root
199201
if (nodeDownloadRoot == null) {
200-
nodeDownloadRoot = URI.create(DEFAULT_NODEJS_DOWNLOAD_ROOT);
202+
nodeDownloadRoot = URI.create(platform.getNodeDownloadRoot());
201203
}
202204

203205
if (nodeIsAlreadyInstalled()) {

‎flow-server/src/main/java/com/vaadin/flow/server/frontend/installer/Platform.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
*/
1616
package com.vaadin.flow.server.frontend.installer;
1717

18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
1822
import com.vaadin.flow.server.frontend.FrontendVersion;
1923

24+
import static com.vaadin.flow.server.frontend.installer.NodeInstaller.DEFAULT_NODEJS_DOWNLOAD_ROOT;
25+
import static com.vaadin.flow.server.frontend.installer.NodeInstaller.UNOFFICIAL_NODEJS_DOWNLOAD_ROOT;
26+
2027
/**
2128
* Platform contains information about system architecture and OS.
2229
* <p>
@@ -109,11 +116,14 @@ public String getCodename() {
109116
}
110117
}
111118

119+
private final String nodeDownloadRoot;
112120
private final OS os;
113121
private final Architecture architecture;
122+
private final String classifier;
114123

115124
// Node.js supports Apple silicon from v16.0.0
116125
private static final int NODE_VERSION_THRESHOLD_MAC_ARM64 = 16;
126+
public static final String ALPINE_RELEASE_FILE_PATH = "/etc/alpine-release";
117127

118128
/**
119129
* Construct a new Platform.
@@ -124,8 +134,15 @@ public String getCodename() {
124134
* platform Architecture
125135
*/
126136
public Platform(OS os, Architecture architecture) {
137+
this(DEFAULT_NODEJS_DOWNLOAD_ROOT, os, architecture, null);
138+
}
139+
140+
public Platform(String nodeDownloadRoot, OS os, Architecture architecture,
141+
String classifier) {
142+
this.nodeDownloadRoot = nodeDownloadRoot;
127143
this.os = os;
128144
this.architecture = architecture;
145+
this.classifier = classifier;
129146
}
130147

131148
/**
@@ -136,6 +153,22 @@ public Platform(OS os, Architecture architecture) {
136153
public static Platform guess() {
137154
OS os = OS.guess();
138155
Architecture architecture = Architecture.guess();
156+
// The default libc is glibc, but Alpine uses musl. When not default,
157+
// the nodejs download
158+
// (and path within it) needs a classifier in the suffix (ex. -musl).
159+
// We know Alpine is in use if the release file exists, and this is the
160+
// simplest check.
161+
Path alpineReleaseFilePath = Paths.get(ALPINE_RELEASE_FILE_PATH);
162+
if (os == OS.LINUX && Files.exists(alpineReleaseFilePath)) {
163+
return new Platform(
164+
// Currently, musl is Experimental. The download root can be
165+
// overridden with config
166+
// if this changes and there's not been an update to this
167+
// project, yet.
168+
// See
169+
// https://github.com/nodejs/node/blob/master/BUILDING.md#platform-list
170+
UNOFFICIAL_NODEJS_DOWNLOAD_ROOT, os, architecture, "musl");
171+
}
139172
return new Platform(os, architecture);
140173
}
141174

@@ -210,7 +243,18 @@ public OS getOs() {
210243
* @return platform node classifier
211244
*/
212245
public String getNodeClassifier(FrontendVersion nodeVersion) {
213-
return getCodename() + "-" + resolveArchitecture(nodeVersion).getName();
246+
String result = getCodename() + "-"
247+
+ resolveArchitecture(nodeVersion).getName();
248+
return classifier != null ? result + "-" + classifier : result;
249+
}
250+
251+
/**
252+
* Gets the platform dependent download root.
253+
*
254+
* @return platform download root
255+
*/
256+
public String getNodeDownloadRoot() {
257+
return nodeDownloadRoot;
214258
}
215259

216260
private Architecture resolveArchitecture(FrontendVersion nodeVersion) {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2000-2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.server.frontend.installer;
17+
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
22+
import com.vaadin.flow.server.frontend.FrontendVersion;
23+
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.mockito.MockedStatic;
27+
import org.mockito.Mockito;
28+
29+
import static com.vaadin.flow.server.frontend.installer.NodeInstaller.DEFAULT_NODEJS_DOWNLOAD_ROOT;
30+
import static com.vaadin.flow.server.frontend.installer.NodeInstaller.UNOFFICIAL_NODEJS_DOWNLOAD_ROOT;
31+
import static com.vaadin.flow.server.frontend.installer.Platform.ALPINE_RELEASE_FILE_PATH;
32+
33+
public class PlatformTest {
34+
35+
@Test
36+
public void testGuess_whenOsIsLinuxAndAlpineReleaseFileExists_unofficialNodeDownloadPathReturned() {
37+
try (MockedStatic<Platform.OS> os = Mockito
38+
.mockStatic(Platform.OS.class);
39+
MockedStatic<Paths> paths = Mockito.mockStatic(Paths.class);
40+
MockedStatic<Files> files = Mockito.mockStatic(Files.class)) {
41+
42+
os.when(Platform.OS::guess).thenReturn(Platform.OS.LINUX);
43+
Path alpineReleaseFilePath = Paths.get(ALPINE_RELEASE_FILE_PATH);
44+
paths.when(() -> Paths.get(ALPINE_RELEASE_FILE_PATH))
45+
.thenReturn(alpineReleaseFilePath);
46+
files.when(() -> Files.exists(alpineReleaseFilePath))
47+
.thenReturn(true);
48+
49+
Platform platform = Platform.guess();
50+
Assert.assertEquals(UNOFFICIAL_NODEJS_DOWNLOAD_ROOT,
51+
platform.getNodeDownloadRoot());
52+
53+
FrontendVersion frontendVersion = Mockito
54+
.mock(FrontendVersion.class);
55+
Assert.assertTrue(platform.getNodeClassifier(frontendVersion)
56+
.contains("-musl"));
57+
}
58+
}
59+
60+
@Test
61+
public void testGuess_whenOsIsLinuxAndAlpineReleaseFileDoesNotExist_officialNodeDownloadPathReturned() {
62+
try (MockedStatic<Platform.OS> os = Mockito
63+
.mockStatic(Platform.OS.class)) {
64+
65+
os.when(Platform.OS::guess).thenReturn(Platform.OS.LINUX);
66+
67+
Platform platform = Platform.guess();
68+
Assert.assertEquals(DEFAULT_NODEJS_DOWNLOAD_ROOT,
69+
platform.getNodeDownloadRoot());
70+
71+
FrontendVersion frontendVersion = Mockito
72+
.mock(FrontendVersion.class);
73+
Assert.assertFalse(platform.getNodeClassifier(frontendVersion)
74+
.contains("-musl"));
75+
}
76+
}
77+
78+
@Test
79+
public void testGuess_whenOsIsAnythingOtherThanLinuxAlpineRelease_officialNodeDownloadPathReturned() {
80+
try (MockedStatic<Platform.OS> os = Mockito
81+
.mockStatic(Platform.OS.class)) {
82+
83+
os.when(Platform.OS::guess).thenReturn(Platform.OS.WINDOWS);
84+
85+
Platform platform = Platform.guess();
86+
Assert.assertEquals(DEFAULT_NODEJS_DOWNLOAD_ROOT,
87+
platform.getNodeDownloadRoot());
88+
89+
os.when(Platform.OS::guess).thenReturn(Platform.OS.MAC);
90+
91+
platform = Platform.guess();
92+
Assert.assertEquals(DEFAULT_NODEJS_DOWNLOAD_ROOT,
93+
platform.getNodeDownloadRoot());
94+
95+
os.when(Platform.OS::guess).thenReturn(Platform.OS.SUN_OS);
96+
97+
platform = Platform.guess();
98+
Assert.assertEquals(DEFAULT_NODEJS_DOWNLOAD_ROOT,
99+
platform.getNodeDownloadRoot());
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)