Search in sources :

Example 1 with ExecException

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);
    }
}
Also used : DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ExecException(io.fabric8.maven.docker.access.ExecException) StopMode(io.fabric8.maven.docker.config.StopMode)

Example 2 with ExecException

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;
}
Also used : ExecDetails(io.fabric8.maven.docker.model.ExecDetails) ExecException(io.fabric8.maven.docker.access.ExecException) Arguments(io.fabric8.maven.docker.config.Arguments) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails)

Example 3 with ExecException

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());
            }
        }
    }
}
Also used : WaitConfiguration(io.fabric8.maven.docker.config.WaitConfiguration) ExecException(io.fabric8.maven.docker.access.ExecException)

Example 4 with ExecException

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();
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) GavLabel(io.fabric8.maven.docker.util.GavLabel) QueryService(io.fabric8.maven.docker.service.QueryService) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher)

Example 5 with ExecException

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);
            }
        }
    }
}
Also used : DockerAccess(io.fabric8.maven.docker.access.DockerAccess) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecException(io.fabric8.maven.docker.access.ExecException) IOException(java.io.IOException) AnsiLogger(io.fabric8.maven.docker.util.AnsiLogger) File(java.io.File) DockerAccessFactory(io.fabric8.maven.docker.service.DockerAccessFactory)

Aggregations

ExecException (io.fabric8.maven.docker.access.ExecException)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)3 Network (io.fabric8.maven.docker.model.Network)3 QueryService (io.fabric8.maven.docker.service.QueryService)3 RunService (io.fabric8.maven.docker.service.RunService)3 File (java.io.File)3 IOException (java.io.IOException)3 PortMapping (io.fabric8.maven.docker.access.PortMapping)2 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)2 Container (io.fabric8.maven.docker.model.Container)2 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)2 ContainerShutdownDescriptor (io.fabric8.maven.docker.service.ContainerTracker.ContainerShutdownDescriptor)2 GavLabel (io.fabric8.maven.docker.util.GavLabel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DockerAccess (io.fabric8.maven.docker.access.DockerAccess)1 AssemblyFiles (io.fabric8.maven.docker.assembly.AssemblyFiles)1