Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to stop docker containers during VM shutdown #1492

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/io/fabric8/maven/docker/StopMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.regex.Matcher;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -71,6 +72,41 @@ public class StopMojo extends AbstractDockerMojo {
@Parameter(property = "docker.stopNamePattern")
private String stopNamePattern;

/**
* If true, the containers are not stopped right away, but when the build is finished (success or failed).
*/
@Parameter(property = "docker.executeStopOnVMShutdown", defaultValue = "false")
private boolean executeStopOnVMShutdown;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would appreciate if you could add documentation regarding this field in docker:stop configuration[0]. Source for this doc section is located here[1]

[0] http://dmp.fabric8.io/#docker:stop
[1] https://github.com/fabric8io/docker-maven-plugin/blob/master/src/main/asciidoc/inc/_docker-stop.adoc


@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All goals in this plugin override executeInternal method which contains goal related functionality. I'm not sure why you're overriding execute method. Is this possible to move this stop on VM shutdown logic to executeInternal method instead?

if (this.executeStopOnVMShutdown) {
this.executeStopOnVMShutdown = false;
if (!invokedTogetherWithDockerStart()) {
throw new MojoExecutionException("docker:stop with executeStopOnVMShutdown is only possible if" +
" the docker containers started within the same maven session.");
}

try {
// HttpDelete class is not used during start mojo, so we need to load it and initialize it. It is not
// possible to load classes in the shutdown hook as
Class.forName("org.apache.http.client.methods.HttpDelete", true, this.getClass().getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonar reports this as a security hotspot: https://sonarcloud.io/project/security_hotspots?id=fabric8io_docker-maven-plugin&pullRequest=1492&hotspots=AXv_23_LXkHjdTy7bc6z

Do you think we can avoid this by just logging some message instead of printing whole stacktrace?

}

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
super.execute();
} catch (MojoExecutionException | MojoFailureException e) {
e.printStackTrace();
}
}));
} else {
super.execute();
}
}

@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, IOException, ExecException {
QueryService queryService = hub.getQueryService();
Expand Down