From 53c043975a4bfb7650a1794f50f7f64576d6907f Mon Sep 17 00:00:00 2001 From: ruhan Date: Mon, 23 Dec 2024 15:07:38 +0800 Subject: [PATCH 1/3] Add UploadLogCommand --- .gitignore | 1 + pom.xml | 5 ++ .../jboss/pnc/konfluxtooling/EntryPoint.java | 2 + .../logging/UploadLogCommand.java | 89 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java diff --git a/.gitignore b/.gitignore index 1e7d2d1..61c2a58 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ release.properties *.ipr *.iml *.iws +.cache/ # Plugin directory /.quarkus/cli/plugins/ diff --git a/pom.xml b/pom.xml index 419acc2..c2bb460 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,11 @@ commons-io 2.16.1 + + org.jboss.pnc + bifrost-upload-client + 3.0.0 + org.projectlombok diff --git a/src/main/java/org/jboss/pnc/konfluxtooling/EntryPoint.java b/src/main/java/org/jboss/pnc/konfluxtooling/EntryPoint.java index 42e837d..6fcf8de 100644 --- a/src/main/java/org/jboss/pnc/konfluxtooling/EntryPoint.java +++ b/src/main/java/org/jboss/pnc/konfluxtooling/EntryPoint.java @@ -2,6 +2,7 @@ import org.jboss.pnc.konfluxtooling.deploy.CopyArtifactsCommand; import org.jboss.pnc.konfluxtooling.deploy.DeployCommand; +import org.jboss.pnc.konfluxtooling.logging.UploadLogCommand; import org.jboss.pnc.konfluxtooling.notification.NotifyCommand; import org.jboss.pnc.konfluxtooling.prebuild.Preprocessor; @@ -12,6 +13,7 @@ @CommandLine.Command(mixinStandardHelpOptions = true, subcommands = { CopyArtifactsCommand.class, DeployCommand.class, + UploadLogCommand.class, NotifyCommand.class, Preprocessor.class }) diff --git a/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java new file mode 100644 index 0000000..bcb72de --- /dev/null +++ b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java @@ -0,0 +1,89 @@ +package org.jboss.pnc.konfluxtooling.logging; + +import java.io.File; +import java.math.BigInteger; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.time.OffsetDateTime; + +import org.jboss.pnc.bifrost.upload.BifrostLogUploader; +import org.jboss.pnc.bifrost.upload.LogMetadata; +import org.jboss.pnc.bifrost.upload.TagOption; + +import io.quarkus.logging.Log; +import picocli.CommandLine; + +@CommandLine.Command(name = "upload-log") +public class UploadLogCommand implements Runnable { + + private static final int DEFAULT_MAX_RETRIES = 4; + + private static final int DEFAULT_DELAY_SECONDS = 60; + + @CommandLine.Option(names = "--file", required = true) + String logFile; + + @CommandLine.Option(names = "--bifrost-url", required = true) + String bifrostURL; + + @CommandLine.Option(names = "--max-retries") + int maxRetries = DEFAULT_MAX_RETRIES; + + @CommandLine.Option(names = "--delay-seconds") + int delaySeconds = DEFAULT_DELAY_SECONDS; + + @CommandLine.Option(names = "--process-context", required = true) + String processContext; + + @CommandLine.Option(names = "--process-context-variant", required = true) + String processContextVariant; + + @CommandLine.Option(names = "--tmp", required = true) + String tmp; + + @CommandLine.Option(names = "--request-context", required = true) + String requestContext; + + public void run() { + try { + var logFilePath = Path.of(logFile); + var file = logFilePath.toFile(); + if (!file.exists()) { + throw new RuntimeException(String.format( + "No log file found at %s. Has the build been correctly done?", logFilePath)); + } + var md5 = getMD5(logFilePath); + uploadLogsToBifrost(file, md5); + } catch (Exception e) { + Log.error("Upload log failed", e); + throw new RuntimeException(e); + } + } + + private String getMD5(Path logFilePath) throws Exception { + byte[] data = Files.readAllBytes(logFilePath); + byte[] hash = MessageDigest.getInstance("MD5").digest(data); + return new BigInteger(1, hash).toString(16); + } + + private void uploadLogsToBifrost(File logFile, String md5) { + BifrostLogUploader logUploader = new BifrostLogUploader(URI.create(bifrostURL), + maxRetries, + delaySeconds, + () -> System.getProperty("ACCESS_TOKEN")); + + LogMetadata logMetadata = LogMetadata.builder() + .tag(TagOption.BUILD_LOG) + .endTime(OffsetDateTime.now()) + .loggerName("org.jboss.pnc._userlog_.build-agent") + .processContext(processContext) + .processContextVariant(processContextVariant) + .tmp(tmp) + .requestContext(requestContext) + .build(); + + logUploader.uploadFile(logFile, logMetadata, md5); + } +} From 46723449e291922a0bf00353991d1c05f37eeb01 Mon Sep 17 00:00:00 2001 From: ruhan Date: Wed, 25 Dec 2024 12:22:54 +0800 Subject: [PATCH 2/3] Add description for UploadLogCommand options --- .../logging/UploadLogCommand.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java index bcb72de..f54c4bb 100644 --- a/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java +++ b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java @@ -15,6 +15,8 @@ import io.quarkus.logging.Log; import picocli.CommandLine; +import static org.apache.commons.lang3.StringUtils.isBlank; + @CommandLine.Command(name = "upload-log") public class UploadLogCommand implements Runnable { @@ -25,29 +27,38 @@ public class UploadLogCommand implements Runnable { @CommandLine.Option(names = "--file", required = true) String logFile; - @CommandLine.Option(names = "--bifrost-url", required = true) + @CommandLine.Option(names = "--bifrost-url") String bifrostURL; @CommandLine.Option(names = "--max-retries") int maxRetries = DEFAULT_MAX_RETRIES; - @CommandLine.Option(names = "--delay-seconds") + @CommandLine.Option(names = "--delay-seconds", + description = "in case of retries this is the delay in seconds before next retry") int delaySeconds = DEFAULT_DELAY_SECONDS; - @CommandLine.Option(names = "--process-context", required = true) + @CommandLine.Option(names = "--process-context", + description = "id of an long running operation (in this case the build-id is used)") String processContext; - @CommandLine.Option(names = "--process-context-variant", required = true) + @CommandLine.Option(names = "--process-context-variant", + description = "in case there are subtasks or retries of individual steps this field can be used to add another ID") String processContextVariant; - @CommandLine.Option(names = "--tmp", required = true) - String tmp; + @CommandLine.Option(names = "--tmp", + description = "temp build or not, used for a log clean-up") + String tmp = "false"; - @CommandLine.Option(names = "--request-context", required = true) + @CommandLine.Option(names = "--request-context", + description = "an id of the initial (http) request that triggered this and potentially other processes") String requestContext; public void run() { try { + if (isBlank(bifrostURL)) { + Log.info("No bifrost url specified and no log upload is performed"); + return; + } var logFilePath = Path.of(logFile); var file = logFilePath.toFile(); if (!file.exists()) { From b430981cfdc27f79d0d0b686810b88258440efc1 Mon Sep 17 00:00:00 2001 From: ruhan Date: Fri, 27 Dec 2024 10:12:57 +0800 Subject: [PATCH 3/3] Inject accessToken as ConfigProperty --- .../jboss/pnc/konfluxtooling/logging/UploadLogCommand.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java index f54c4bb..897d69d 100644 --- a/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java +++ b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java @@ -7,7 +7,9 @@ import java.nio.file.Path; import java.security.MessageDigest; import java.time.OffsetDateTime; +import java.util.Optional; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.jboss.pnc.bifrost.upload.BifrostLogUploader; import org.jboss.pnc.bifrost.upload.LogMetadata; import org.jboss.pnc.bifrost.upload.TagOption; @@ -24,6 +26,9 @@ public class UploadLogCommand implements Runnable { private static final int DEFAULT_DELAY_SECONDS = 60; + @ConfigProperty(name = "access.token") + Optional accessToken; + @CommandLine.Option(names = "--file", required = true) String logFile; @@ -83,7 +88,7 @@ private void uploadLogsToBifrost(File logFile, String md5) { BifrostLogUploader logUploader = new BifrostLogUploader(URI.create(bifrostURL), maxRetries, delaySeconds, - () -> System.getProperty("ACCESS_TOKEN")); + () -> accessToken.orElse("")); LogMetadata logMetadata = LogMetadata.builder() .tag(TagOption.BUILD_LOG)