use of io.fabric8.maven.docker.access.ExecException in project docker-maven-plugin by fabric8io.
the class RunService method shutdown.
private void shutdown(ContainerTracker.ContainerShutdownDescriptor descriptor, boolean keepContainer, boolean removeVolumes) throws DockerAccessException, ExecException {
String containerId = descriptor.getContainerId();
StopMode stopMode = descriptor.getStopMode();
if (descriptor.getPreStop() != null) {
try {
execInContainer(containerId, descriptor.getPreStop(), descriptor.getImageConfiguration());
} catch (DockerAccessException e) {
log.error("%s", e.getMessage());
} catch (ExecException e) {
if (descriptor.isBreakOnError()) {
throw e;
} else {
log.warn("Cannot run preStop: %s", e.getMessage());
}
}
}
if (stopMode.equals(StopMode.graceful)) {
int killGracePeriod = adjustGracePeriod(descriptor.getKillGracePeriod());
log.debug("shutdown will wait max of %d seconds before removing container", killGracePeriod);
long waited;
if (killGracePeriod == 0) {
docker.stopContainer(containerId, 0);
waited = 0;
} else {
waited = shutdownAndWait(containerId, killGracePeriod);
}
log.info("%s: Stop%s container %s after %s ms", descriptor.getDescription(), (keepContainer ? "" : " and removed"), containerId.substring(0, 12), waited);
} else if (stopMode.equals(StopMode.kill)) {
docker.killContainer(containerId);
log.info("%s: Killed%s container %s.", descriptor.getDescription(), (keepContainer ? "" : " and removed"), containerId.subSequence(0, 12));
}
if (!keepContainer) {
removeContainer(descriptor, removeVolumes, containerId);
}
}
use of io.fabric8.maven.docker.access.ExecException in project docker-maven-plugin by fabric8io.
the class RunService method execInContainer.
/**
* Create and start a Exec container with the given image configuration.
* @param containerId container id to run exec command against
* @param command command to execute
* @param imageConfiguration configuration of the container's image
* @return the exec container id
*
* @throws DockerAccessException if access to the docker backend fails
*/
public String execInContainer(String containerId, String command, ImageConfiguration imageConfiguration) throws DockerAccessException, ExecException {
Arguments arguments = new Arguments();
arguments.setExec(Arrays.asList(EnvUtil.splitOnSpaceWithEscape(command)));
String execContainerId = docker.createExecContainer(containerId, arguments);
docker.startExecContainer(execContainerId, logConfig.createSpec(containerId, imageConfiguration));
ExecDetails execContainer = docker.getExecContainer(execContainerId);
Integer exitCode = execContainer.getExitCode();
if (exitCode != null && exitCode != 0) {
ContainerDetails container = docker.getContainer(containerId);
throw new ExecException(execContainer, container);
}
return execContainerId;
}
use of io.fabric8.maven.docker.access.ExecException in project docker-maven-plugin by fabric8io.
the class StartContainerExecutor method waitAndPostExec.
private void waitAndPostExec(String containerId, Properties projProperties) throws IOException, ExecException {
// Wait if requested
hub.getWaitService().wait(imageConfig, projProperties, containerId);
WaitConfiguration waitConfig = imageConfig.getRunConfiguration().getWaitConfiguration();
if (waitConfig != null && waitConfig.getExec() != null && waitConfig.getExec().getPostStart() != null) {
try {
hub.getRunService().execInContainer(containerId, waitConfig.getExec().getPostStart(), imageConfig);
} catch (ExecException exp) {
if (waitConfig.getExec().isBreakOnError()) {
throw exp;
} else {
log.warn("Cannot run postStart: %s", exp.getMessage());
}
}
}
}
use of io.fabric8.maven.docker.access.ExecException in project docker-maven-plugin by fabric8io.
the class StopMojo method executeInternal.
@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, IOException, ExecException {
QueryService queryService = hub.getQueryService();
RunService runService = hub.getRunService();
GavLabel gavLabel = getGavLabel();
if (!keepRunning) {
if (invokedTogetherWithDockerStart()) {
runService.stopStartedContainers(keepContainer, removeVolumes, autoCreateCustomNetworks, gavLabel);
} else {
stopContainers(queryService, runService, gavLabel);
}
}
// Switch off all logging
LogDispatcher dispatcher = getLogDispatcher(hub);
dispatcher.untrackAllContainerLogs();
}
use of io.fabric8.maven.docker.access.ExecException in project docker-maven-plugin by fabric8io.
the class AbstractDockerMojo method execute.
/**
* Entry point for this plugin. It will set up the helper class and then calls
* {@link #executeInternal(ServiceHub)}
* which must be implemented by subclass.
*
* @throws MojoExecutionException
* @throws MojoFailureException
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!skip) {
boolean ansiRestore = Ansi.isEnabled();
File output = null;
if (outputFile != null) {
output = new File(outputFile);
if (output.exists()) {
output.delete();
}
}
log = new AnsiLogger(getLog(), useColorForLogging(), verbose, !settings.getInteractiveMode(), getLogPrefix(), output);
try {
authConfigFactory.setLog(log);
imageConfigResolver.setLog(log);
LogOutputSpecFactory logSpecFactory = new LogOutputSpecFactory(useColor, logStdout, logDate);
ConfigHelper.validateExternalPropertyActivation(project, getAllImages());
DockerAccess access = null;
try {
// The 'real' images configuration to use (configured images + externally resolved images)
this.minimalApiVersion = initImageConfiguration(getBuildTimestamp());
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
access = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
ServiceHub serviceHub = serviceHubFactory.createServiceHub(project, session, access, log, logSpecFactory);
executeInternal(serviceHub);
} catch (IOException | ExecException exp) {
logException(exp);
throw new MojoExecutionException(log.errorMessage(exp.getMessage()), exp);
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
if (access != null) {
access.shutdown();
}
}
} finally {
Ansi.setEnabled(ansiRestore);
try {
log.close();
} catch (IOException exp) {
logException(exp);
}
}
}
}
Aggregations