Skip to content

Commit

Permalink
Add UploadLogCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhan1 committed Dec 23, 2024
1 parent aba7f93 commit 53c0439
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ release.properties
*.ipr
*.iml
*.iws
.cache/

# Plugin directory
/.quarkus/cli/plugins/
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>org.jboss.pnc</groupId>
<artifactId>bifrost-upload-client</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jboss/pnc/konfluxtooling/EntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,6 +13,7 @@
@CommandLine.Command(mixinStandardHelpOptions = true, subcommands = {
CopyArtifactsCommand.class,
DeployCommand.class,
UploadLogCommand.class,
NotifyCommand.class,
Preprocessor.class
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 53c0439

Please sign in to comment.