Skip to content

Commit

Permalink
Merge pull request #11 from ruhan1/main-logging
Browse files Browse the repository at this point in the history
Add UploadLogCommand
  • Loading branch information
matejonnet authored Jan 23, 2025
2 parents b60505e + 03395e8 commit caa8a0f
Show file tree
Hide file tree
Showing 4 changed files with 108 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 @@ -108,6 +108,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.PreprocessorCommand;

Expand All @@ -12,6 +13,7 @@
@CommandLine.Command(mixinStandardHelpOptions = true, subcommands = {
CopyArtifactsCommand.class,
DeployCommand.class,
UploadLogCommand.class,
NotifyCommand.class,
PreprocessorCommand.class
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.jboss.pnc.konfluxtooling.logging;

import static org.apache.commons.lang3.StringUtils.isBlank;

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 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;

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;

@ConfigProperty(name = "access.token")
Optional<String> accessToken;

@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,
() -> accessToken.orElse(""));

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 caa8a0f

Please sign in to comment.