Skip to content

Commit 6d383e8

Browse files
authored
Do not reset on server restart and allow configuring reuse (#145)
* Revert reuse * Allow configuring reuse * Do not reset on server restart
1 parent ddddde5 commit 6d383e8

File tree

6 files changed

+97
-75
lines changed

6 files changed

+97
-75
lines changed

deployment/src/main/java/io/quarkus/jgit/deployment/GiteaContainer.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.testcontainers.containers.wait.strategy.Wait.forListeningPorts;
44

55
import java.io.IOException;
6-
import java.util.Base64;
76

87
import org.jboss.logging.Logger;
98
import org.testcontainers.containers.GenericContainer;
@@ -27,8 +26,10 @@ class GiteaContainer extends GenericContainer<GiteaContainer> {
2726
withEnv("GITEA__security__INSTALL_LOCK", "true");
2827
withEnv("GITEA__server__DISABLE_SSH", "true");
2928
withExposedPorts(HTTP_PORT);
29+
withReuse(devServiceConfig.reuse());
3030
waitingFor(forListeningPorts(HTTP_PORT));
31-
withReuse(true);
31+
// Needed for podman (see https://github.com/testcontainers/testcontainers-java/issues/7310)
32+
withStartupAttempts(2);
3233
devServiceConfig.httpPort().ifPresent(port -> addFixedExposedPort(port, HTTP_PORT));
3334
if (devServiceConfig.showLogs()) {
3435
withLogConsumer(new JBossLoggingConsumer(log));
@@ -69,34 +70,6 @@ private void createAdminUser() throws IOException, InterruptedException {
6970
}
7071
}
7172

72-
private void createRepository() throws IOException, InterruptedException {
73-
String[] cmd = {
74-
"/usr/bin/curl",
75-
"-X",
76-
"POST",
77-
"http://localhost:3000/api/v1/user/repos",
78-
"-H",
79-
"'Accept: application/json'",
80-
"-H",
81-
"'Authorization: Basic " + getBasicAuth() + "'",
82-
"-H",
83-
"'Content-Type: application/json'",
84-
"-d",
85-
"'{\"auto_init\":true,\"default_branch\":\"main\",\"name\":\"hello-world\",\"private\":false,\"readme\":\"Default\"}'"
86-
};
87-
log.info(String.join(" ", cmd));
88-
ExecResult execResult = execInContainer(cmd);
89-
log.info(execResult.getStdout());
90-
if (execResult.getExitCode() != 0) {
91-
throw new RuntimeException("Failed to create repository: " + execResult.getStderr());
92-
}
93-
}
94-
95-
private String getBasicAuth() {
96-
String auth = devServiceConfig.adminUsername() + ":" + devServiceConfig.adminPassword();
97-
return Base64.getEncoder().encodeToString(auth.getBytes());
98-
}
99-
10073
public String getHttpUrl() {
10174
return "http://" + getHost() + ":" + getMappedPort(HTTP_PORT);
10275
}

deployment/src/main/java/io/quarkus/jgit/deployment/JGitBuildTimeConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ interface DevService {
4646
@WithDefault("quarkus")
4747
String adminPassword();
4848

49-
// /**
50-
// * Create Hello World repository?
51-
// */
52-
// @WithDefault("true")
53-
// boolean createRepository();
49+
/**
50+
* Should the container be reused?
51+
*/
52+
@WithDefault("false")
53+
boolean reuse();
5454
}
5555
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.quarkus.jgit.deployment;
2+
3+
import java.util.Map;
4+
import java.util.function.BooleanSupplier;
5+
6+
import org.jboss.logging.Logger;
7+
8+
import io.quarkus.deployment.IsNormal;
9+
import io.quarkus.deployment.annotations.BuildStep;
10+
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem;
11+
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
12+
import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService;
13+
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig;
14+
import io.quarkus.devservices.common.ContainerShutdownCloseable;
15+
16+
public class JGitDevServicesProcessor {
17+
18+
private static final Logger log = Logger.getLogger(JGitDevServicesProcessor.class);
19+
static volatile RunningDevService devService;
20+
21+
@BuildStep(onlyIfNot = IsNormal.class, onlyIf = { GlobalDevServicesConfig.Enabled.class, DevServicesEnabled.class })
22+
DevServicesResultBuildItem createContainer(JGitBuildTimeConfig config,
23+
CuratedApplicationShutdownBuildItem closeBuildItem) {
24+
if (devService != null) {
25+
// only produce DevServicesResultBuildItem when the dev service first starts.
26+
return null;
27+
}
28+
var gitServer = new GiteaContainer(config.devservices());
29+
gitServer.start();
30+
String httpUrl = gitServer.getHttpUrl();
31+
log.infof("Gitea HTTP URL: %s", httpUrl);
32+
Map<String, String> configOverrides = Map.of("quarkus.jgit.devservices.http-url", httpUrl);
33+
34+
ContainerShutdownCloseable closeable = new ContainerShutdownCloseable(gitServer, JGitProcessor.FEATURE);
35+
closeBuildItem.addCloseTask(closeable::close, true);
36+
devService = new RunningDevService(JGitProcessor.FEATURE, gitServer.getContainerId(), closeable, configOverrides);
37+
return devService.toBuildItem();
38+
}
39+
40+
public static class DevServicesEnabled implements BooleanSupplier {
41+
42+
final JGitBuildTimeConfig config;
43+
44+
public DevServicesEnabled(JGitBuildTimeConfig config) {
45+
this.config = config;
46+
}
47+
48+
@Override
49+
public boolean getAsBoolean() {
50+
return config.devservices().enabled();
51+
}
52+
}
53+
54+
}
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
package io.quarkus.jgit.deployment;
22

3-
import java.util.Map;
4-
import java.util.function.BooleanSupplier;
5-
6-
import org.jboss.logging.Logger;
7-
8-
import io.quarkus.deployment.IsNormal;
93
import io.quarkus.deployment.annotations.BuildProducer;
104
import io.quarkus.deployment.annotations.BuildStep;
11-
import io.quarkus.deployment.builditem.DevServicesResultBuildItem;
125
import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
136
import io.quarkus.deployment.builditem.FeatureBuildItem;
147
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
158
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
169
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
17-
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig;
1810

1911
class JGitProcessor {
2012

21-
private static final String FEATURE = "jgit";
22-
23-
private static final Logger log = Logger.getLogger(JGitProcessor.class);
13+
static final String FEATURE = "jgit";
2414

2515
@BuildStep
2616
FeatureBuildItem feature() {
@@ -65,33 +55,4 @@ void runtimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> p
6555
NativeImageResourceBundleBuildItem includeResourceBundle() {
6656
return new NativeImageResourceBundleBuildItem("org.eclipse.jgit.internal.JGitText");
6757
}
68-
69-
@SuppressWarnings("resource")
70-
@BuildStep(onlyIfNot = IsNormal.class, onlyIf = { GlobalDevServicesConfig.Enabled.class, DevServicesEnabled.class })
71-
DevServicesResultBuildItem createContainer(JGitBuildTimeConfig config) {
72-
var gitServer = new GiteaContainer(config.devservices());
73-
gitServer.start();
74-
String httpUrl = gitServer.getHttpUrl();
75-
log.infof("Gitea HTTP URL: %s", httpUrl);
76-
Map<String, String> configOverrides = Map.of(
77-
"quarkus.jgit.devservices.http-url", httpUrl);
78-
79-
return new DevServicesResultBuildItem.RunningDevService(FEATURE, gitServer.getContainerId(),
80-
gitServer::close, configOverrides).toBuildItem();
81-
}
82-
83-
public static class DevServicesEnabled implements BooleanSupplier {
84-
85-
final JGitBuildTimeConfig config;
86-
87-
public DevServicesEnabled(JGitBuildTimeConfig config) {
88-
this.config = config;
89-
}
90-
91-
@Override
92-
public boolean getAsBoolean() {
93-
return config.devservices().enabled();
94-
}
95-
}
96-
9758
}

docs/modules/ROOT/pages/includes/quarkus-jgit.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ endif::add-copy-button-to-env-var[]
9393
|string
9494
|`quarkus`
9595

96+
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-reuse]] [.property-path]##`quarkus.jgit.devservices.reuse`##
97+
98+
[.description]
99+
--
100+
Should the container be reused?
101+
102+
103+
ifdef::add-copy-button-to-env-var[]
104+
Environment variable: env_var_with_copy_button:+++QUARKUS_JGIT_DEVSERVICES_REUSE+++[]
105+
endif::add-copy-button-to-env-var[]
106+
ifndef::add-copy-button-to-env-var[]
107+
Environment variable: `+++QUARKUS_JGIT_DEVSERVICES_REUSE+++`
108+
endif::add-copy-button-to-env-var[]
109+
--
110+
|boolean
111+
|`false`
112+
96113
a| [[quarkus-jgit_quarkus-jgit-devservices-http-url]] [.property-path]##`quarkus.jgit.devservices.http-url`##
97114

98115
[.description]

docs/modules/ROOT/pages/includes/quarkus-jgit_quarkus.jgit.adoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ endif::add-copy-button-to-env-var[]
9393
|string
9494
|`quarkus`
9595

96+
a|icon:lock[title=Fixed at build time] [[quarkus-jgit_quarkus-jgit-devservices-reuse]] [.property-path]##`quarkus.jgit.devservices.reuse`##
97+
98+
[.description]
99+
--
100+
Should the container be reused?
101+
102+
103+
ifdef::add-copy-button-to-env-var[]
104+
Environment variable: env_var_with_copy_button:+++QUARKUS_JGIT_DEVSERVICES_REUSE+++[]
105+
endif::add-copy-button-to-env-var[]
106+
ifndef::add-copy-button-to-env-var[]
107+
Environment variable: `+++QUARKUS_JGIT_DEVSERVICES_REUSE+++`
108+
endif::add-copy-button-to-env-var[]
109+
--
110+
|boolean
111+
|`false`
112+
96113
a| [[quarkus-jgit_quarkus-jgit-devservices-http-url]] [.property-path]##`quarkus.jgit.devservices.http-url`##
97114

98115
[.description]

0 commit comments

Comments
 (0)