Skip to content

Commit

Permalink
✨ Allow CSB application to be built or ran
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarlett authored and avano committed Sep 11, 2023
1 parent 363a322 commit 469ce96
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ private void ensureDirNotPresent() {
}
}

protected boolean shouldRun() {
return true;
}

public abstract void start();

public abstract void stop();
Expand Down Expand Up @@ -78,8 +82,10 @@ public Path getLogPath() {
}

public void waitUntilReady() {
WaitUtils.waitFor(() -> isReady() && isCamelStarted(), this::isFailed, 1000L, "Waiting until the integration " + name + " is running");
started = true;
if (shouldRun()) {
WaitUtils.waitFor(() -> isReady() && isCamelStarted(), this::isFailed, 1000L, "Waiting until the integration " + name + " is running");
started = true;
}
}

private boolean isCamelStarted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ public LocalSpringBootApp(AbstractIntegrationBuilder<?> integrationBuilder) {
String jarName;

final Path projectPath;
final Path existingJarPath = getExistingJar(integrationBuilder);
if (mavenGitApp != null) {
args = ((AbstractMavenGitIntegrationBuilder<?>) integrationBuilder).getJavaProperties().entrySet()
.stream().map(e -> "-D" + e.getKey() + "=" + e.getValue()).collect(Collectors.toList());
jarName = mavenGitApp.getFinalName().map(n -> n + ".jar").orElse(name);
projectPath = mavenGitApp.getProjectLocation();
jarName = existingJarPath != null ? existingJarPath.getFileName().toString()
: mavenGitApp.getFinalName().map(n -> n + ".jar").orElse(name);
projectPath = existingJarPath != null ? existingJarPath.getParent().getParent() : mavenGitApp.getProjectLocation();
} else {
args = integrationBuilder.getProperties() != null ? integrationBuilder.getProperties().entrySet().stream()
.map(e -> "-D" + e.getKey() + "=" + e.getValue()).collect(Collectors.toList()) : Collections.emptyList();
jarName = name + "-1.0.0-SNAPSHOT.jar";
projectPath = TestConfiguration.appLocation().resolve(name);
jarName = existingJarPath != null ? existingJarPath.getFileName().toString() : name + "-1.0.0-SNAPSHOT.jar";
projectPath = existingJarPath != null ? existingJarPath.getParent().getParent() : TestConfiguration.appLocation().resolve(name);
}

Path integrationTarget = projectPath.resolve("target");
Expand All @@ -65,19 +67,21 @@ public LocalSpringBootApp(AbstractIntegrationBuilder<?> integrationBuilder) {

@Override
public void start() {
Path logFile = getLogPath();
ProcessBuilder processBuilder = new ProcessBuilder(getCommand()).redirectOutput(logFile.toFile());

LOG.info("Starting integration {}", name);
try {
appProcess = processBuilder.start();
} catch (IOException e) {
throw new RuntimeException("Unable to start integration process: ", e);
}
WaitUtils.waitFor(() -> logFile.toFile().exists(), "Waiting until the logfile is created");
if (shouldRun()) {
Path logFile = getLogPath();
ProcessBuilder processBuilder = new ProcessBuilder(getCommand()).redirectOutput(logFile.toFile());

LOG.info("Starting integration {}", name);
try {
appProcess = processBuilder.start();
} catch (IOException e) {
throw new RuntimeException("Unable to start integration process: ", e);
}
WaitUtils.waitFor(() -> logFile.toFile().exists(), "Waiting until the logfile is created");

log = new FileLog(logFile);
logStream = new FileLogStream(logFile, LogStream.marker(name));
log = new FileLog(logFile);
logStream = new FileLogStream(logFile, LogStream.marker(name));
}
}

@Override
Expand All @@ -96,12 +100,12 @@ public void stop() {

@Override
public boolean isReady() {
return appProcess.isAlive();
return appProcess != null && appProcess.isAlive();
}

@Override
public boolean isFailed() {
return !appProcess.isAlive();
return appProcess != null && !appProcess.isAlive();
}

private List<String> getCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package software.tnb.product.csb.application;

import software.tnb.common.config.OpenshiftConfiguration;
import software.tnb.common.config.TestConfiguration;
import software.tnb.common.openshift.OpenshiftClient;
import software.tnb.product.deploystrategy.OpenshiftDeployStrategyFactory;
Expand All @@ -16,7 +15,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.internal.readiness.Readiness;
Expand Down Expand Up @@ -67,7 +66,8 @@ public void stop() {
@Override
public boolean isReady() {
try {
final List<Pod> pods = OpenshiftClient.get().getLabeledPods(Map.of(OpenshiftConfiguration.openshiftDeploymentLabel(), finalName));
final List<Pod> pods = OpenshiftClient.get().getPods().stream()
.filter(deploymentStrategy.podSelector()).collect(Collectors.toList());
return !pods.isEmpty() && pods.stream()
.filter(pod -> !pod.isMarkedForDeletion())
.filter(pod -> !"Evicted".equals(pod.getStatus().getReason()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ public abstract class SpringBootApp extends App {
private static final Logger LOG = LoggerFactory.getLogger(SpringBootApp.class);
protected MavenGitRepository mavenGitApp = null;

private boolean shouldRun = true;

@Override
public boolean shouldRun() {
return shouldRun;
}

public SpringBootApp(AbstractIntegrationBuilder<?> integrationBuilder) {
super(integrationBuilder.getIntegrationName());

if (integrationBuilder instanceof AbstractGitIntegrationBuilder<?>
&& ((AbstractGitIntegrationBuilder<?>) integrationBuilder).getRepositoryUrl() != null) {
mavenGitApp = new MavenGitRepository((AbstractMavenGitIntegrationBuilder<?>) integrationBuilder, getName(), getLogPath(Phase.BUILD));
} else {
mavenGitApp = new MavenGitRepository((AbstractMavenGitIntegrationBuilder<?>) integrationBuilder
, getName(), getLogPath(Phase.BUILD)
, ((AbstractMavenGitIntegrationBuilder<?>) integrationBuilder).buildProject());
shouldRun = ((AbstractGitIntegrationBuilder<?>) integrationBuilder).runApplication();
} else if (getExistingJar(integrationBuilder) == null) {
LOG.info("Creating Camel Spring Boot application project for integration {}", name);

Map<String, String> properties = new HashMap<>(13);
Expand Down Expand Up @@ -95,6 +105,11 @@ public SpringBootApp(AbstractIntegrationBuilder<?> integrationBuilder) {
}
}

protected Path getExistingJar(AbstractIntegrationBuilder<?> integrationBuilder) {
return integrationBuilder instanceof SpringBootIntegrationBuilder
? ((SpringBootIntegrationBuilder) integrationBuilder).getExistingJar() : null;
}

private void customizeDependencies(List<Dependency> mavenDependencies) {
File pom = TestConfiguration.appLocation().resolve(name).resolve("pom.xml").toFile();
Model model = Maven.loadPom(pom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -20,6 +21,7 @@
*/
public final class SpringBootIntegrationBuilder extends AbstractMavenGitIntegrationBuilder<SpringBootIntegrationBuilder> {
private final List<Resource> xmlCamelContext = new ArrayList<>();
private Path existingJar;

public SpringBootIntegrationBuilder(String integrationName) {
super(integrationName);
Expand All @@ -45,4 +47,13 @@ public SpringBootIntegrationBuilder fromSpringBootXmlCamelContext(String camelCo
public List<Resource> getXmlCamelContext() {
return xmlCamelContext;
}

public Path getExistingJar() {
return existingJar;
}

public SpringBootIntegrationBuilder useExistingJar(Path existingJar) {
this.existingJar = existingJar;
return buildProject(false); //avoid build if an existing file is used
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MavenGitRepository extends GitRepository {
protected Path projectLocation;
protected Optional<String> finalName;

public MavenGitRepository(AbstractMavenGitIntegrationBuilder<?> gitIntegrationBuilder, String name, Path logFile) {
public MavenGitRepository(AbstractMavenGitIntegrationBuilder<?> gitIntegrationBuilder, String name, Path logFile, boolean buildProject) {
super(gitIntegrationBuilder);

projectLocation = gitIntegrationBuilder.getSubDirectory().map(project -> getPath().resolve(project)).orElse(getPath());
Expand All @@ -40,10 +40,19 @@ public MavenGitRepository(AbstractMavenGitIntegrationBuilder<?> gitIntegrationBu
finalName.ifPresent(fName -> {
final File pom = projectLocation.resolve("pom.xml").toFile();
final Model model = Maven.loadPom(pom);
model.getBuild().setFinalName(finalName.get());
Maven.writePom(pom, model);
if (model.getBuild() != null) {
model.getBuild().setFinalName(finalName.get());
Maven.writePom(pom, model);
}
});

if (buildProject) {
buildProject(gitIntegrationBuilder, name, logFile, mavenBuildProperties);
}
}

private void buildProject(AbstractMavenGitIntegrationBuilder<?> gitIntegrationBuilder, String name, Path logFile,
Map<String, String> mavenBuildProperties) {
BuildRequest.Builder requestBuilder = new BuildRequest.Builder()
.withBaseDirectory(projectLocation)
.withGoals(gitIntegrationBuilder.cleanBeforeBuild() ? "clean" : "", "package")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public abstract class AbstractGitIntegrationBuilder<SELF extends AbstractGitInte
private String branch = "main";

private boolean cleanBeforeBuild = true;
private boolean buildProject = true;
private boolean runApplication = true;

public AbstractGitIntegrationBuilder(String integrationName) {
super(integrationName);
Expand Down Expand Up @@ -38,6 +40,16 @@ public SELF cleanBeforeBuild(boolean cleanBeforeBuild) {
return self();
}

/**
* If the project is just checked out
* @param buildProject boolean, true if the project has been built, false otherwise, default true
* @return self instance
*/
public SELF buildProject(boolean buildProject) {
this.buildProject = buildProject;
return self();
}

public String getRepositoryUrl() {
return repositoryUrl;
}
Expand All @@ -53,4 +65,22 @@ public String getBranch() {
public boolean cleanBeforeBuild() {
return this.cleanBeforeBuild;
}

public boolean buildProject() {
return buildProject;
}

/**
* If the application should run after build
* @param runApplication
* @return self instance
*/
public SELF runApplication(boolean runApplication) {
this.runApplication = runApplication;
return self();
}

public boolean runApplication() {
return runApplication;
}
}

0 comments on commit 469ce96

Please sign in to comment.