Skip to content

Commit

Permalink
JS-370 Add Node.js amd64 alpine into the multi plugin (#4916)
Browse files Browse the repository at this point in the history
Co-authored-by: Tibor Blenessy <saberduck@users.noreply.github.com>
  • Loading branch information
vdiez and saberduck authored Nov 25, 2024
1 parent c66caba commit d78c434
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ plugin_qa_no_node_task:
SONARJS_ARTIFACT: multi
MVN_TEST: '-Dtest=!EslintCustomRulesTest,!SonarJsIntegrationTest --projects !org.sonarsource.javascript:eslint-custom-rules-plugin'

plugin_qa_no_node_alpine_task:
<<: *PLUGIN_QA_BODY
eks_container:
dockerfile: .cirrus/alpine.Dockerfile
env:
SQ_VERSION: LATEST_RELEASE
SONARJS_ARTIFACT: linux-x64-musl
MVN_TEST: '-Dtest=!EslintCustomRulesTest,!SonarJsIntegrationTest --projects !org.sonarsource.javascript:eslint-custom-rules-plugin'

plugin_qa_sq_dev_task:
<<: *PLUGIN_QA_BODY
eks_container:
Expand Down
38 changes: 38 additions & 0 deletions .cirrus/alpine.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ARG CIRRUS_AWS_ACCOUNT=275878209202
FROM ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:j17-latest AS base

FROM eclipse-temurin:17-alpine

ENV USER=sonarsource
ENV GROUPNAME=$USER
ENV UID=1000
ENV GID=1000

# copy maven scripts from base image
COPY --from=base /usr/local/bin /usr/local/bin

# Maven
COPY --from=base /usr/local/share/maven /usr/local/share/maven
RUN ln -sf /usr/local/share/maven/bin/mvn /usr/local/bin/mvn
ENV MAVEN_CONFIG="/home/sonarsource/.m2"

RUN addgroup \
--gid "$GID" \
"$GROUPNAME" \
&& adduser \
--disabled-password \
--home /home/sonarsource \
--ingroup "$GROUPNAME" \
--uid "$UID" \
$USER


RUN mkdir -p /home/sonarsource/.m2
RUN chown -R sonarsource:sonarsource /home/sonarsource/.m2
# copy maven settings from base image
COPY --from=base --chown=sonarsource:sonarsource /home/sonarsource/.m2/* /home/sonarsource/.m2/

RUN apk add bash --no-cache

USER sonarsource
WORKDIR /home/sonarsource
7 changes: 7 additions & 0 deletions its/plugin/sonarlint-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@
<type>sonar-plugin</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-javascript-plugin</artifactId>
<classifier>linux-x64-musl</classifier>
<type>sonar-plugin</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-javascript-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.sonar.javascript.it.plugin.sonarlint.tests;

import static com.sonar.javascript.it.plugin.sonarlint.tests.TestUtils.usingEmbeddedNode;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

Expand Down Expand Up @@ -117,10 +118,6 @@ void should_raise_issues() throws IOException {
}
}

private static boolean usingEmbeddedNode() {
return TestUtils.JAVASCRIPT_PLUGIN_LOCATION.toString().contains("multi");
}

@Test
void should_start_node_server_once() throws Exception {
analyze(FILE_PATH, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;
import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;

public class TestUtils {

static final Path JAVASCRIPT_PLUGIN_LOCATION = artifact();

static final List<String> platformStrings = List.of("darwin-arm64", "darwin-x64", "linux-arm64", "linux-x64", "linux-x64-musl", "win-x64", "multi");
/**
* This is used to test artifact with and without embedded runtime during plugin QA integration tests
*
Expand All @@ -55,9 +57,16 @@ private static Path artifact() {
}

private static Pattern pluginFilenameMatcher() {
return "multi".equals(System.getenv("SONARJS_ARTIFACT"))
? Pattern.compile("sonar-javascript-plugin-.*-multi\\.jar")
: Pattern.compile("sonar-javascript-plugin-[0-9.]*(?:-SNAPSHOT)?\\.jar");
if (System.getenv("SONARJS_ARTIFACT") != null) {
return Pattern.compile(String.format("sonar-javascript-plugin-.*-%s\\.jar", System.getenv("SONARJS_ARTIFACT")));
}

return Pattern.compile("sonar-javascript-plugin-[0-9.]*(?:-SNAPSHOT)?\\.jar");
}


static boolean usingEmbeddedNode() {
return platformStrings.stream().anyMatch(TestUtils.JAVASCRIPT_PLUGIN_LOCATION.toString()::contains);
}

public static Path projectDir(String projectName) {
Expand Down
7 changes: 7 additions & 0 deletions its/plugin/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@
<type>sonar-plugin</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-javascript-plugin</artifactId>
<classifier>linux-x64-musl</classifier>
<type>sonar-plugin</type>
<overWrite>true</overWrite>
</artifactItem>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-javascript-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.sonar.orchestrator.junit5.OrchestratorExtension;
import com.sonar.orchestrator.locator.FileLocation;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
Expand Down Expand Up @@ -74,11 +76,18 @@ private static String classifier() {
var os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
var arch = System.getProperty("os.arch");
if (os.contains("linux") && arch.contains("64")) {
if (isAlpine()) {
return "-linux-x64-musl";
}
return "-linux-x64";
} else if (os.contains("windows")) {
return "-win-x64";
} else {
return "-multi";
}
}

private static boolean isAlpine() {
return Files.exists(Path.of("/etc/alpine-release"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ private static URLLocation artifact() {
}

private static Pattern pluginFilenameMatcher() {
return "multi".equals(System.getenv("SONARJS_ARTIFACT"))
? Pattern.compile("sonar-javascript-plugin-.*-multi\\.jar")
: Pattern.compile("sonar-javascript-plugin-[0-9.]*(?:-SNAPSHOT)?\\.jar");
if (System.getenv("SONARJS_ARTIFACT") != null) {
return Pattern.compile(String.format("sonar-javascript-plugin-.*-%s\\.jar", System.getenv("SONARJS_ARTIFACT")));
}

return Pattern.compile("sonar-javascript-plugin-[0-9.]*(?:-SNAPSHOT)?\\.jar");
}

public static final OrchestratorExtension ORCHESTRATOR = OrchestratorExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,21 @@ enum Platform {
WIN_X64,
LINUX_ARM64,
LINUX_X64,
LINUX_X64_MUSL,
DARWIN_ARM64,
DARWIN_X64,
UNSUPPORTED;

private String pathInJar() {
switch (this) {
case WIN_X64:
return "/win-x64/";
case LINUX_ARM64:
return "/linux-arm64/";
case LINUX_X64:
return "/linux-x64/";
case DARWIN_ARM64:
return "/darwin-arm64/";
case DARWIN_X64:
return "/darwin-x64/";
default:
return "";
}
return switch (this) {
case WIN_X64 -> "/win-x64/";
case LINUX_ARM64 -> "/linux-arm64/";
case LINUX_X64 -> "/linux-x64/";
case LINUX_X64_MUSL -> "/linux-x64-musl/";
case DARWIN_ARM64 -> "/darwin-arm64/";
case DARWIN_X64 -> "/darwin-x64/";
default -> "";
};
}

/**
Expand Down Expand Up @@ -120,9 +116,8 @@ static Platform detect(Environment env) {
return WIN_X64;
} else if (lowerCaseOsName.contains("linux") && isARM64(env) ) {
return LINUX_ARM64;
} else if (lowerCaseOsName.contains("linux") && isX64(env) && !env.isAlpine()) {
// alpine linux is using musl libc, which is not compatible with linux-x64
return LINUX_X64;
} else if (lowerCaseOsName.contains("linux") && isX64(env)) {
return env.isAlpine() ? LINUX_X64_MUSL : LINUX_X64;
} else if (lowerCaseOsName.contains("mac os") && isARM64(env)) {
return DARWIN_ARM64;
} else if (lowerCaseOsName.contains("mac os") && isX64(env)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.DARWIN_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_ARM64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_X64;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.LINUX_X64_MUSL;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.UNSUPPORTED;
import static org.sonar.plugins.javascript.bridge.EmbeddedNode.Platform.WIN_X64;

Expand Down Expand Up @@ -124,14 +125,18 @@ void should_detect_platform_for_linux_arm64_environment() {

@Test
void should_detect_platform_for_linux_x64_environment() {
var linux = mock(Environment.class);
when(linux.getOsName()).thenReturn("linux");
when(linux.getOsArch()).thenReturn("amd64");
var platform = Platform.detect(linux);
var platform = Platform.detect(createLinuxX64Environment());
assertThat(platform).isEqualTo(LINUX_X64);
assertThat(platform.archivePathInJar()).isEqualTo("/linux-x64/node.xz");
}

@Test
void should_detect_platform_for_linux_x64_musl_environment() {
var platform = Platform.detect(createLinuxX64MuslEnvironment());
assertThat(platform).isEqualTo(LINUX_X64_MUSL);
assertThat(platform.archivePathInJar()).isEqualTo("/linux-x64-musl/node.xz");
}

@Test
void should_return_unsupported_for_unknown_environment() {
var platform = Platform.detect(createUnsupportedEnvironment());
Expand Down Expand Up @@ -205,6 +210,22 @@ private Environment createLinuxArm64Environment() {
return mockEnvironment;
}

private Environment createLinuxX64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("linux");
when(mockEnvironment.getOsArch()).thenReturn("amd64");
when(mockEnvironment.isAlpine()).thenReturn(false);
return mockEnvironment;
}

private Environment createLinuxX64MuslEnvironment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("linux");
when(mockEnvironment.getOsArch()).thenReturn("amd64");
when(mockEnvironment.isAlpine()).thenReturn(true);
return mockEnvironment;
}

private Environment createMacOSArm64Environment() {
Environment mockEnvironment = mock(Environment.class);
when(mockEnvironment.getOsName()).thenReturn("mac os x");
Expand Down
39 changes: 38 additions & 1 deletion sonar-plugin/sonar-javascript-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@
</configuration>
</execution>

<execution>
<id>linux-x64-musl</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>linux-x64-musl</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<!-- This is used to get plugin version at runtime using getPackage().getImplementationVersion() -->
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/node.xz</resource>
<file>${project.build.directory}/node/linux-x64-musl/node.xz</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/version.txt</resource>
<file>${project.build.directory}/node/linux-x64-musl/version.txt</file>
</transformer>
</transformers>
</configuration>
</execution>

<execution>
<id>darwin-arm64</id>
<phase>package</phase>
Expand Down Expand Up @@ -316,6 +344,14 @@
<resource>linux-x64/version.txt</resource>
<file>${project.build.directory}/node/linux-x64/version.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/node.xz</resource>
<file>${project.build.directory}/node/linux-x64-musl/node.xz</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>linux-x64-musl/version.txt</resource>
<file>${project.build.directory}/node/linux-x64-musl/version.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>darwin-arm64/node.xz</resource>
<file>${project.build.directory}/node/darwin-arm64/node.xz</file>
Expand Down Expand Up @@ -363,7 +399,7 @@
<rules>
<requireFilesSize>
<minsize>70000000</minsize>
<maxsize>90000000</maxsize>
<maxsize>110000000</maxsize>
<files>
<file>${project.build.directory}/${project.build.finalName}-multi.jar</file>
</files>
Expand All @@ -373,6 +409,7 @@
<maxsize>55000000</maxsize>
<files>
<file>${project.build.directory}/${project.build.finalName}-linux-x64.jar</file>
<file>${project.build.directory}/${project.build.finalName}-linux-x64-musl.jar</file>
<file>${project.build.directory}/${project.build.finalName}-win-x64.jar</file>
<file>${project.build.directory}/${project.build.finalName}-darwin-arm64.jar</file>
</files>
Expand Down
10 changes: 10 additions & 0 deletions tools/fetch-node/node-distros.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export const NODE_VERSION = 'v22.11.0';
const NODE_ORG_URL = `https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}`;
const NODE_ARTIFACTORY_URL = `https://repox.jfrog.io/artifactory/nodejs-dist/${NODE_VERSION}/node-${NODE_VERSION}`;

// alpine builds are not officially supported. this is the URL the official docker alpine uses
// https://github.com/nodejs/docker-node/blob/15cd6b44e0284c459de7763b45e3b16972c0716e/22/alpine3.20/Dockerfile#L23
const UNOFFICIAL_NODE_ORG_URL = `https://unofficial-builds.nodejs.org/download/release/${NODE_VERSION}/node-${NODE_VERSION}`;
/**
* Node.js runtimes distributions
*
Expand Down Expand Up @@ -47,6 +50,13 @@ export const DISTROS = [
sha: '4f862bab52039835efbe613b532238b6e4dde98d139a34e6923193e073438b13',
binPath: 'bin/node',
},
{
id: 'linux-x64-musl',
url: `${UNOFFICIAL_NODE_ORG_URL}-linux-x64-musl.tar.gz`,
artifactoryUrl: `${NODE_ARTIFACTORY_URL}-linux-x64-musl.tar.gz`,
sha: 'c9b4eba63f6569547e3a3423b446613a5a56dffb571b10f556bac2ae45fdc1fb',
binPath: 'bin/node',
},
];

export const VERSION_FILENAME = 'version.txt';
1 change: 1 addition & 0 deletions tools/fetch-node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<argument>${project.basedir}/downloads/runtimes/darwin-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-arm64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-x64/node</argument>
<argument>${project.basedir}/downloads/runtimes/linux-x64-musl/node</argument>
<argument>${project.basedir}/downloads/runtimes/win-x64/node.exe</argument>
</arguments>
</configuration>
Expand Down

0 comments on commit d78c434

Please sign in to comment.