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..f54c4bb --- /dev/null +++ b/src/main/java/org/jboss/pnc/konfluxtooling/logging/UploadLogCommand.java @@ -0,0 +1,100 @@ +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; + +import static org.apache.commons.lang3.StringUtils.isBlank; + +@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") + String bifrostURL; + + @CommandLine.Option(names = "--max-retries") + int maxRetries = DEFAULT_MAX_RETRIES; + + @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", + description = "id of an long running operation (in this case the build-id is used)") + String processContext; + + @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", + description = "temp build or not, used for a log clean-up") + String tmp = "false"; + + @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()) { + 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); + } +}