Skip to content

Commit

Permalink
[git] add consul-populate-server
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogDevelopper committed Sep 30, 2024
1 parent f21f690 commit 46b4715
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.frogdevelopment.consul.populate.git;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.nio.file.Path;

import org.eclipse.jgit.lib.Repository;

@RequiredArgsConstructor
public class GitHolder {

@Getter
private final Path repositoryDirectory;

private Repository repository;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import jakarta.inject.Singleton;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
Expand All @@ -19,7 +20,7 @@
import com.frogdevelopment.consul.populate.files.YamlFilesImporter;

import io.micronaut.context.BeanContext;
import io.micronaut.context.annotation.Context;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.StringUtils;
Expand All @@ -29,33 +30,20 @@
@Requires(property = "consul.git")
public class GitImportFactory {

@Context
Repository repository(final GitProperties gitProperties) throws Exception {
final var credentialsProvider = new UsernamePasswordCredentialsProvider(gitProperties.getToken(), "");
CredentialsProvider.setDefault(credentialsProvider);

@Singleton
GitHolder gitHolder(final GitProperties gitProperties) throws IOException, URISyntaxException {
final var tmpDirectory = Files.createTempDirectory(Paths.get("/tmp"), "consul-populate-");
tmpDirectory.toFile().deleteOnExit();

final var urIish = new URIish(gitProperties.getUri());
final var repositoryDirectory = tmpDirectory.resolve(urIish.getHumanishName());

log.info("Cloning repository in {}", repositoryDirectory);

try (final var git = Git.cloneRepository()
.setURI(gitProperties.getUri())
.setDirectory(repositoryDirectory.toFile())
.setGitDir(repositoryDirectory.resolve(".git").toFile())
.setBranch(gitProperties.getBranch())
.call()) {
return git.getRepository();
}
return new GitHolder(repositoryDirectory);
}

@Singleton
DataImporter dataImporter(final Repository repository, final GitProperties gitProperties, final BeanContext beanContext) {
final var gitRepositoryDirectory = repository.getDirectory().toPath();
final var repositoryDirectory = gitRepositoryDirectory.getParent();
DataImporter dataImporter(final GitHolder gitHolder, final GitProperties gitProperties, final BeanContext beanContext) {
final var repositoryDirectory = gitHolder.getRepositoryDirectory();

final var fileProperties = gitProperties.getFileProperties();
final var rootPath = StringUtils.isEmpty(fileProperties.getRootPath())
Expand All @@ -72,4 +60,20 @@ DataImporter dataImporter(final Repository repository, final GitProperties gitPr
};
}

@Bean(preDestroy = "close")
Git git(final GitHolder gitHolder, final GitProperties gitProperties) throws Exception {
final var credentialsProvider = new UsernamePasswordCredentialsProvider(gitProperties.getToken(), "");
CredentialsProvider.setDefault(credentialsProvider);

final var repositoryDirectory = gitHolder.getRepositoryDirectory();
log.debug("Cloning repository in {}", repositoryDirectory);

return Git.cloneRepository()
.setURI(gitProperties.getUri())
.setDirectory(repositoryDirectory.toFile())
.setGitDir(repositoryDirectory.resolve(".git").toFile())
.setBranch(gitProperties.getBranch())
.call();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class GitProperties {
private String branch;

@NotNull
Duration initialDelay = Duration.ofSeconds(5);
private Duration initialDelay = Duration.ofSeconds(5);

@NotNull
Duration delay = Duration.ofMinutes(5);
private Duration delay = Duration.ofMinutes(5);

@ConfigurationBuilder(configurationPrefix = "files")
private ImportFileProperties fileProperties = new ImportFileProperties();
Expand Down
1 change: 1 addition & 0 deletions consul-populate-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
annotationProcessor(mn.lombok)

implementation(projects.consulPopulateCore)
implementation("commons-io:commons-io:2.17.0")

compileOnly(mn.lombok)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,62 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;

import com.frogdevelopment.consul.populate.PopulateService;
import com.frogdevelopment.consul.populate.git.GitHolder;
import com.frogdevelopment.consul.populate.git.GitProperties;

import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.runtime.event.annotation.EventListener;
import io.micronaut.runtime.server.event.ServerShutdownEvent;
import io.micronaut.runtime.server.event.ServerStartupEvent;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.TaskScheduler;

@Slf4j
@Singleton
@RequiredArgsConstructor
public class ServiceReadyListener implements ApplicationEventListener<ServerStartupEvent> {
public class ServerListener{

private final PopulateService populateService;
private final GitProperties gitProperties;
@Named(TaskExecutors.SCHEDULED)
private final TaskScheduler taskScheduler;
private final Repository repository;
private final GitHolder gitHolder;
private final Git git;

@Override
public void onApplicationEvent(final ServerStartupEvent event) {
@EventListener
public void onServerStartupEvent(final ServerStartupEvent event) {
log.info("Populating Consul");
populateService.populate();

log.info("Listening for index changes");
repository.getListenerList().addIndexChangedListener(event1 -> populateService.populate());
git.getRepository().getListenerList().addIndexChangedListener(indexEvent -> populateService.populate());

log.info("Scheduling pull command with initial-delay={} and delay={}", gitProperties.getInitialDelay(), gitProperties.getDelay());
taskScheduler.scheduleWithFixedDelay(gitProperties.getInitialDelay(), gitProperties.getDelay(), () -> {
try {
log.debug("Pull repository");
final var result = new Git(repository).pull().call();
final var result = git.pull().call();
log.debug("Repository updated: {}", result.isSuccessful());
} catch (final Exception e) {
log.error("Scheduled task encountered an error. Please check logs", e);
}
});
}

@EventListener
public void onServerShutdownEvent(final ServerShutdownEvent event) {
try {
log.info("Deleting Git Repository...");
FileUtils.deleteDirectory(gitHolder.getRepositoryDirectory().toFile());
} catch (final IOException e) {
log.error("Failed to delete repository directory", e);
}
}
}
2 changes: 1 addition & 1 deletion consul-populate-server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</encoder>
</appender>

<!-- <logger name="com.frogdevelopment.micronaut.gateway" level="DEBUG"/>-->
<logger name="com.frogdevelopment" level="DEBUG"/>

<root level="INFO">
<appender-ref ref="STDOUT"/>
Expand Down

0 comments on commit 46b4715

Please sign in to comment.