Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions openaev-api/src/main/java/io/openaev/rest/executor/ExecutorApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.openaev.aop.RBAC;
import io.openaev.database.model.Action;
import io.openaev.database.model.Executor;
import io.openaev.database.model.ResourceType;
import io.openaev.database.model.Token;
import io.openaev.database.model.*;
import io.openaev.database.repository.ExecutorRepository;
import io.openaev.database.repository.TokenRepository;
import io.openaev.rest.exception.ElementNotFoundException;
Expand All @@ -33,6 +30,7 @@
import java.time.Instant;
import java.util.Optional;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
Expand All @@ -44,6 +42,11 @@
@RestController
public class ExecutorApi extends RestBehavior {

public enum INSTALLATION_MODE {
SERVICE,
SESSION
}

@Value("${info.app.version:unknown}")
String version;

Expand Down Expand Up @@ -246,28 +249,34 @@ public Executor registerExecutor(
"Target platform for the agent package (e.g., windows, linux, mac). Case insensitive.",
required = true)
@PathVariable
String platform,
Endpoint.PLATFORM_TYPE platformEnum,
@Parameter(
description =
"Target architecture for the agent package (e.g., x86, x64, arm). Case insensitive.",
required = true)
@PathVariable
String architecture,
Endpoint.PLATFORM_ARCH architectureEnum,
@Parameter(
description = "Installation Mode: session, user or system service",
required = true)
@PathVariable
String installationMode)
INSTALLATION_MODE installationMode)
throws IOException {
platform = Optional.ofNullable(platform).map(String::toLowerCase).orElse("");
architecture = Optional.ofNullable(architecture).map(String::toLowerCase).orElse("");

if (!AVAILABLE_PLATFORMS.contains(platform)) {
throw new IllegalArgumentException("Platform invalid : " + platform);
if (!AVAILABLE_PLATFORMS.contains(platformEnum)) {
throw new IllegalArgumentException("Platform invalid : " + platformEnum.name());
}
if (!AVAILABLE_ARCHITECTURES.contains(architecture)) {
throw new IllegalArgumentException("Architecture invalid : " + architecture);
if (!AVAILABLE_ARCHITECTURES.contains(architectureEnum)) {
throw new IllegalArgumentException("Architecture invalid : " + architectureEnum.name());
}
String platform =
Optional.ofNullable(platformEnum)
.map(platformType -> platformType.name().toLowerCase())
.orElse("");
String architecture =
Optional.ofNullable(architectureEnum)
.map(platformArch -> platformArch.name().toLowerCase())
.orElse("");

byte[] file = null;
String filename = null;
Expand All @@ -278,7 +287,7 @@ public Executor registerExecutor(

filename = "openaev-agent-installer-";
if (installationMode != null && !installationMode.equals(SERVICE)) {
filename = filename.concat(installationMode).concat("-");
filename = filename.concat(installationMode.name().toLowerCase()).concat("-");
}

if (executorOpenaevBinariesOrigin.equals("local")) { // if we want the local binaries
Expand Down Expand Up @@ -328,7 +337,7 @@ public Executor registerExecutor(
"Target platform for the agent installation (e.g., windows, linux, mac). Case insensitive.",
required = true)
@PathVariable
String platform,
String platformAsString,
@Parameter(
description = "Unique token associated with the agent installation.",
required = true)
Expand All @@ -343,8 +352,7 @@ public Executor registerExecutor(
String installationDir,
@Parameter(description = "Service name") @RequestParam(required = false) String serviceName)
throws IOException {
platform = Optional.ofNullable(platform).map(String::toLowerCase).orElse("");

Endpoint.PLATFORM_TYPE platform = Endpoint.PLATFORM_TYPE.valueOf(StringUtils.capitalize(platformAsString.toLowerCase()));
if (!AVAILABLE_PLATFORMS.contains(platform)) {
throw new IllegalArgumentException("Platform invalid : " + platform);
}
Expand Down
23 changes: 13 additions & 10 deletions openaev-api/src/main/java/io/openaev/service/EndpointService.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public Endpoint register(final EndpointRegisterInput input) throws IOException {
AssetAgentJob assetAgentJob = new AssetAgentJob();
assetAgentJob.setCommand(
generateUpgradeCommand(
endpoint.getPlatform().name(),
endpoint.getPlatform(),
input.getInstallationMode(),
input.getInstallationDirectory(),
input.getServiceName()));
Expand Down Expand Up @@ -622,21 +622,21 @@ private AgentRegisterInput toAgentEndpoint(EndpointRegisterInput input) {
}

public String getFileOrDownloadFromJfrog(
String platform,
Endpoint.PLATFORM_TYPE platform,
String file,
String adminToken,
String installationDir,
String serviceNameOrPrefix)
throws IOException {
String extension =
switch (platform.toLowerCase()) {
switch (platform.name().toLowerCase()) {
case "windows" -> "ps1";
case "linux", "macos" -> "sh";
default -> throw new UnsupportedOperationException("");
};
InputStream in = null;
String filename;
String resourcePath = "/openaev-agent/" + platform.toLowerCase() + "/";
String resourcePath = "/openaev-agent/" + platform.name().toLowerCase() + "/";

if (executorOpenaevBinariesOrigin.equals("local")) { // if we want the local binaries
filename = file + "-" + version + "." + extension;
Expand Down Expand Up @@ -667,11 +667,11 @@ public String getFileOrDownloadFromJfrog(
}

public String generateServiceNameOrPrefix(
String platform, String installationMode, String serviceNameOrPrefix) {
Endpoint.PLATFORM_TYPE platform, String installationMode, String serviceNameOrPrefix) {
if (serviceNameOrPrefix != null && !serviceNameOrPrefix.equals("")) {
return serviceNameOrPrefix;
}
if (platform.equalsIgnoreCase(Endpoint.PLATFORM_TYPE.Windows.name())) {
if (platform.equals(Endpoint.PLATFORM_TYPE.Windows)) {
if (installationMode != null && installationMode.equals(SERVICE)) {
return OPENAEV_SERVICE_NAME_WINDOWS_SERVICE;
}
Expand All @@ -697,11 +697,11 @@ public String generateServiceNameOrPrefix(
}

public String generateInstallationDir(
String platform, String installationMode, String installationDir) {
Endpoint.PLATFORM_TYPE platform, String installationMode, String installationDir) {
if (installationDir != null && !installationDir.equals("")) {
return installationDir;
}
if (platform.equalsIgnoreCase(Endpoint.PLATFORM_TYPE.Windows.name())) {
if (platform.equals(Endpoint.PLATFORM_TYPE.Windows)) {
if (installationMode != null && installationMode.equals(SERVICE)) {
return OPENAEV_INSTALL_DIR_WINDOWS_SERVICE;
}
Expand All @@ -727,7 +727,7 @@ public String generateInstallationDir(
}

public String generateInstallCommand(
String platform,
Endpoint.PLATFORM_TYPE platform,
String token,
String installationMode,
String installationDir,
Expand All @@ -748,7 +748,10 @@ public String generateInstallCommand(
}

public String generateUpgradeCommand(
String platform, String installationMode, String installationDir, String serviceNameOrPrefix)
Endpoint.PLATFORM_TYPE platform,
String installationMode,
String installationDir,
String serviceNameOrPrefix)
throws IOException {
String upgradeName = OPENAEV_AGENT_UPGRADE;
if (installationMode != null && !installationMode.equals(SERVICE)) {
Expand Down
14 changes: 6 additions & 8 deletions openaev-api/src/main/java/io/openaev/utils/AgentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ public class AgentUtils {

private AgentUtils() {}

public static final List<String> AVAILABLE_PLATFORMS =
public static final List<Endpoint.PLATFORM_TYPE> AVAILABLE_PLATFORMS =
List.of(
Endpoint.PLATFORM_TYPE.Linux.name().toLowerCase(),
Endpoint.PLATFORM_TYPE.Windows.name().toLowerCase(),
Endpoint.PLATFORM_TYPE.MacOS.name().toLowerCase());
Endpoint.PLATFORM_TYPE.Linux,
Endpoint.PLATFORM_TYPE.Windows,
Endpoint.PLATFORM_TYPE.MacOS);

public static final List<String> AVAILABLE_ARCHITECTURES =
List.of(
Endpoint.PLATFORM_ARCH.x86_64.name().toLowerCase(),
Endpoint.PLATFORM_ARCH.arm64.name().toLowerCase());
public static final List<Endpoint.PLATFORM_ARCH> AVAILABLE_ARCHITECTURES =
List.of(Endpoint.PLATFORM_ARCH.x86_64, Endpoint.PLATFORM_ARCH.arm64);

public static List<Agent> getActiveAgents(Asset asset, Inject inject) {
return ((Endpoint) Hibernate.unproxy(asset))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void given_validEndpointInput_should_upsertEndpointSuccessfully() throws Excepti

Mockito.doReturn("command")
.when(endpointService)
.generateUpgradeCommand(String.valueOf(Endpoint.PLATFORM_TYPE.Windows), null, null, null);
.generateUpgradeCommand(Endpoint.PLATFORM_TYPE.Windows, null, null, null);

// --EXECUTE--
String response =
Expand Down Expand Up @@ -176,7 +176,7 @@ void given_validInputForNonExistingEndpoint_should_createAndUpsertSuccessfully()

Mockito.doReturn("command")
.when(endpointService)
.generateUpgradeCommand(String.valueOf(Endpoint.PLATFORM_TYPE.Windows), null, null, null);
.generateUpgradeCommand(Endpoint.PLATFORM_TYPE.Windows, null, null, null);

// --EXECUTE--
String response =
Expand Down