Skip to content

Commit

Permalink
Resolved conflicts and updated documentation
Browse files Browse the repository at this point in the history
Also streamlined the load image functionality. This includes
the code from #624 and fixes #624
  • Loading branch information
rhuss committed Jan 2, 2017
1 parent a07901e commit e07aedb
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 29 deletions.
11 changes: 5 additions & 6 deletions src/main/asciidoc/inc/build/_overview.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ When using this mode, the Dockerfile is created on the fly with all instructions

[[external-dockerfile]]
.External Dockerfile or docker archive
Alternatively an external Dockerfile template or docker archive can be used. This mode is switch on by using one of these three configuration options within
the `<build>` configuration section.
Alternatively an external Dockerfile template or Docker archive can be used. This mode is switch on by using one of these three configuration options within

* *dockerFileDir* specifies a directory containing a `Dockerfile` that will be used to create the image.
* *dockerFile* specifies a specific Dockerfile. The `dockerFileDir` is set to the directory containing the file.
* *dockerArchive* specifies a previously saved image archive to load directly.
* *dockerFileDir* specifies a directory containing a Dockerfile that will be used to create the image. The name of the Dockerfile is `Dockerfile` by default but can be also set with the option `dockerFile` (see below).
* *dockerFile* specifies a specific Dockerfile path. The Docker build context directory is set to `dockerFileDir` if given. If not the directory by default is the directory in which the Dockerfile is stored.
* *dockerArchive* specifies a previously saved image archive to load directly. Such a tar archive can be created with `docker save`. If a `dockerArchive` is provided, no `dockerFile` or `dockerFileDir` must be given.
If `dockerFileDir` is a relative path it is looked up in `${project.basedir}/src/main/docker`. You can make easily an absolute path by prefixing with `${project.basedir}`.
All paths can be either absolute or relative paths (except when both `dockerFileDir` and `dockerFile` are provided in which case `dockerFile` must not be absolute). A relative path is looked up in `${project.basedir}/src/main/docker` by default. You can make it easily an absolute path by using `${project.basedir} in your configuration.

Any additional files located in the `dockerFileDir` directory will also be added to the build context as well as any files specified by an assembly. However, you still need to insert `ADD` or `COPY` directives yourself into the Dockerfile.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void autoPullBaseImage(ServiceHub hub, ImageConfiguration imageConfig)
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();

if (buildConfig.getDockerArchive() != null) {
// No autopull need in archive mode
// No auto pull needed in archive mode
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ void copyArchive(String containerId, File archive, String targetPath)
* Load an image from an archive.
*
* @param image the image to pull.
* @param filepath a URL string of the archive
* @param tarArchive archive file
* @throws DockerAccessException if the image couldn't be loaded.
*/
void loadImage(String image, String filepath) throws DockerAccessException;
void loadImage(String image, File tarArchive) throws DockerAccessException;

/**
* Pull an image from a remote registry and store it locally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ public void removeContainer(String containerId, boolean removeVolumes)
}

@Override
public void loadImage(String image, String filepath) throws DockerAccessException {
public void loadImage(String image, File tarArchive) throws DockerAccessException {
String url = urlBuilder.loadImage();

try {
delegate.post(url, new File(filepath), new BodyAndStatusResponseHandler(), HTTP_OK);
delegate.post(url, tarArchive, new BodyAndStatusResponseHandler(), HTTP_OK);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to load %s", filepath);
throw new DockerAccessException(e, "Unable to load %s", tarArchive);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class BuildImageConfiguration implements Serializable {
private BuildTarArchiveCompression compression = BuildTarArchiveCompression.none;

// Path to Dockerfile to use, initialized lazily ....
private File dockerFileFile;
private File dockerFileFile, dockerArchiveFile;

public BuildImageConfiguration() {}

Expand All @@ -165,8 +165,8 @@ public File getDockerFile() {
return dockerFileFile;
}

public String getDockerArchive() {
return dockerArchive;
public File getDockerArchive() {
return dockerArchiveFile;
}

public String getFrom() {
Expand Down Expand Up @@ -269,6 +269,10 @@ public File getAbsoluteDockerFilePath(MojoParameters mojoParams) {
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerFile().getPath());
}

public File getAbsoluteDockerTarPath(MojoParameters mojoParams) {
return EnvUtil.prepareAbsoluteSourceDirPath(mojoParams, getDockerArchive().getPath());
}

public static class Builder {
private final BuildImageConfiguration config;

Expand Down Expand Up @@ -478,11 +482,23 @@ private void initDockerFileFile(Logger log) {
"Only one of them can be specified.");
}
dockerFileFile = findDockerFileFile(log);

if (dockerArchive != null) {
dockerArchiveFile = new File(dockerArchive);
}
}

private File findDockerFileFile(Logger log) {
if (dockerFile != null) {
return new File(dockerFile);
File dFile = new File(dockerFile);
if (dockerFileDir == null) {
return dFile;
} else {
if (dFile.isAbsolute()) {
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
}
return new File(dockerFileDir, dockerFile);
}
}

if (dockerFileDir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BuildService {

/**
* Build an image
*
*
* @param imageConfig the image configuration
* @param params mojo params for the project
* @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
Expand All @@ -57,7 +57,7 @@ public void buildImage(ImageConfiguration imageConfig, MojoParameters params, bo
long time = System.currentTimeMillis();

if (buildConfig.getDockerArchive() != null) {
docker.loadImage(imageName, buildConfig.getDockerArchive());
docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params));
log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/integration/DockerAccessIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testPullStartStopRemove() throws DockerAccessException, InterruptedE
@Test
public void testLoadImage() throws DockerAccessException {
testDoesNotHave();
dockerClient.loadImage(IMAGE_LATEST, "integration/busybox-image.tar.gz");
dockerClient.loadImage(IMAGE_LATEST, new File("integration/busybox-image.tar.gz"));
assertTrue(hasImage(IMAGE_LATEST));
testRemoveImage(IMAGE_LATEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.io.IOException;
import java.util.Map;

Expand Down Expand Up @@ -140,7 +141,7 @@ private void whenPushImage() {
}
private void whenLoadImage() {
try {
client.loadImage(imageName, archiveFile);
client.loadImage(imageName, new File(archiveFile));
} catch (Exception e) {
thrownException = e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.fabric8.maven.docker.config;
/*
*
*
* Copyright 2016 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -123,7 +123,7 @@ public void dockerArchive() {
config.initAndValidate(logger);

assertFalse(config.isDockerFileMode());
assertEquals("this", config.getDockerArchive());
assertEquals(new File("this"), config.getDockerArchive());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.fabric8.maven.docker.config.*;
import io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest;
import io.fabric8.maven.docker.util.MojoParameters;
import mockit.Expectations;
import mockit.Mocked;
import mockit.integration.junit4.JMockit;
Expand Down Expand Up @@ -167,7 +168,7 @@ public void testDockerArchive() {
ImageConfiguration config = resolveExternalImageConfig(testData);
config.initAndValidate(ConfigHelper.NameFormatter.IDENTITY, null);
assertFalse(config.getBuildConfiguration().isDockerFileMode());
assertEquals("dockerLoad.tar", config.getBuildConfiguration().getDockerArchive());
assertEquals(new File("dockerLoad.tar"), config.getBuildConfiguration().getDockerArchive());
}

@Test(expected = IllegalArgumentException.class)
Expand Down
25 changes: 19 additions & 6 deletions src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.assembly.DockerAssemblyManager;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ConfigHelper;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Tested;
import mockit.Verifications;
import mockit.*;
import mockit.integration.junit4.JMockit;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.util.Collections;

@RunWith(JMockit.class)
Expand All @@ -31,6 +31,9 @@ public class LoadImageTest {
@Injectable
private Logger log;

@Mocked
private MavenProject project;

@Mocked
private MojoParameters params;

Expand All @@ -42,15 +45,23 @@ public class LoadImageTest {

private String dockerArchive;


@Test
public void testLoadImage() throws DockerAccessException, MojoExecutionException {
givenMojoParameters();
givenAnImageConfiguration();
givenDockerArchive("test.tar");
whenBuildImage();
thenImageIsBuilt();
}

private void givenMojoParameters() {
new Expectations() {{
params.getProject();
project.getBasedir(); result = "/maven-project";
params.getSourceDirectory(); result = "src/main/docker";
}};
}

private void givenDockerArchive(String s) {
dockerArchive = s;
}
Expand All @@ -65,15 +76,17 @@ private void givenAnImageConfiguration() {
.alias("build-alias")
.buildConfig(buildConfig)
.build();
imageConfig.initAndValidate(ConfigHelper.NameFormatter.IDENTITY,log);
}

private void whenBuildImage() throws DockerAccessException, MojoExecutionException {
buildService.buildImage(imageConfig, params, false, Collections.<String, String>emptyMap());
}

private void thenImageIsBuilt() throws DockerAccessException {
final File targetFile = new File("/maven-project/src/main/docker/test.tar");
new Verifications() {{
docker.loadImage("build-image", dockerArchive);
docker.loadImage("build-image", withEqual(targetFile));
}};
}

Expand Down

0 comments on commit e07aedb

Please sign in to comment.