Search in sources :

Example 11 with Image

use of io.fabric8.docker.api.model.Image 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) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails)

Example 12 with Image

use of io.fabric8.docker.api.model.Image in project docker-maven-plugin by fabric8io.

the class WatchService method createBuildWatchTask.

private Runnable createBuildWatchTask(final ImageWatcher watcher, final MojoParameters mojoParameters, final boolean doRestart, final BuildService.BuildContext buildContext) throws MojoExecutionException {
    final ImageConfiguration imageConfig = watcher.getImageConfiguration();
    final AssemblyFiles files = archiveService.getAssemblyFiles(imageConfig, mojoParameters);
    if (files.isEmpty()) {
        log.error("No assembly files for %s. Are you sure you invoked together with the `package` goal?", imageConfig.getDescription());
        throw new MojoExecutionException("No files to watch found for " + imageConfig);
    }
    return new Runnable() {

        @Override
        public void run() {
            List<AssemblyFiles.Entry> entries = files.getUpdatedEntriesAndRefresh();
            if (entries != null && entries.size() > 0) {
                try {
                    log.info("%s: Assembly changed. Rebuild ...", imageConfig.getDescription());
                    if (watcher.getWatchContext().getImageCustomizer() != null) {
                        log.info("%s: Customizing the image ...", imageConfig.getDescription());
                        watcher.getWatchContext().getImageCustomizer().execute(imageConfig);
                    }
                    buildService.buildImage(imageConfig, null, buildContext);
                    String name = imageConfig.getName();
                    watcher.setImageId(queryService.getImageId(name));
                    if (doRestart) {
                        restartContainer(watcher);
                    }
                    callPostGoal(watcher);
                } catch (Exception e) {
                    log.error("%s: Error when rebuilding - %s", imageConfig.getDescription(), e);
                }
            }
        }
    };
}
Also used : AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) WatchImageConfiguration(io.fabric8.maven.docker.config.WatchImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) IOException(java.io.IOException) ExecException(io.fabric8.maven.docker.access.ExecException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 13 with Image

use of io.fabric8.docker.api.model.Image in project docker-maven-plugin by fabric8io.

the class DockerAccessIT method testCreateContainer.

private void testCreateContainer() throws DockerAccessException {
    PortMapping portMapping = new PortMapping(Arrays.asList(new String[] { PORT + ":" + PORT }), new Properties());
    ContainerHostConfig hostConfig = new ContainerHostConfig().portBindings(portMapping);
    ContainerCreateConfig createConfig = new ContainerCreateConfig(IMAGE).command(new Arguments("ping google.com")).hostConfig(hostConfig);
    containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
    assertNotNull(containerId);
    String name = dockerClient.getContainer(containerId).getName();
    assertEquals(CONTAINER_NAME, name);
}
Also used : Arguments(io.fabric8.maven.docker.config.Arguments) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) ContainerHostConfig(io.fabric8.maven.docker.access.ContainerHostConfig)

Example 14 with Image

use of io.fabric8.docker.api.model.Image in project docker-maven-plugin by fabric8io.

the class DockerAccessWinIT method testCreateContainer.

private void testCreateContainer() throws DockerAccessException {
    PortMapping portMapping = new PortMapping(Arrays.asList(new String[] { PORT + ":" + PORT }), new Properties());
    ContainerHostConfig hostConfig = new ContainerHostConfig().portBindings(portMapping);
    ContainerCreateConfig createConfig = new ContainerCreateConfig(IMAGE).command(new Arguments("ping google.com")).hostConfig(hostConfig);
    containerId = dockerClient.createContainer(createConfig, CONTAINER_NAME);
    assertNotNull(containerId);
    String name = dockerClient.getContainer(containerId).getName();
    assertEquals(CONTAINER_NAME, name);
}
Also used : Arguments(io.fabric8.maven.docker.config.Arguments) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) ContainerHostConfig(io.fabric8.maven.docker.access.ContainerHostConfig)

Example 15 with Image

use of io.fabric8.docker.api.model.Image in project docker-maven-plugin by fabric8io.

the class DockerAssemblyConfigurationSourceTest method testOutputDirHasImage.

@Test
public void testOutputDirHasImage() {
    String image = "image";
    MojoParameters params = buildParameters(".", "src/docker", "output/docker");
    DockerAssemblyConfigurationSource source = new DockerAssemblyConfigurationSource(params, new BuildDirs(image, params), assemblyConfig);
    assertTrue(containsDir(image, source.getOutputDirectory()));
    assertTrue(containsDir(image, source.getWorkingDirectory()));
    assertTrue(containsDir(image, source.getTemporaryRootDirectory()));
}
Also used : MojoParameters(io.fabric8.maven.docker.util.MojoParameters) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)35 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)28 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)20 IOException (java.io.IOException)16 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)15 File (java.io.File)13 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)12 ImageStream (io.fabric8.openshift.api.model.ImageStream)10 Arguments (io.fabric8.maven.docker.config.Arguments)8 Service (io.fabric8.kubernetes.api.model.Service)7 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)7 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)6 BuildService (io.fabric8.maven.core.service.BuildService)6 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)6 HashMap (java.util.HashMap)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageName (io.fabric8.maven.docker.util.ImageName)5 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)5