-
Notifications
You must be signed in to change notification settings - Fork 645
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All goals in this plugin override |
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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