Skip to content

Commit

Permalink
Adds jib:dockercontext to export a Docker context. (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
coollog authored Mar 7, 2018
1 parent ed464a9 commit 57413b4
Show file tree
Hide file tree
Showing 15 changed files with 704 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import com.google.cloud.tools.jib.cache.CachedLayer;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.image.Image;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -60,6 +58,12 @@ public BuildConfiguration getBuildConfiguration() {
public void run()
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
IOException {
List<String> entrypoint =
EntrypointBuilder.makeEntrypoint(
sourceFilesConfiguration,
buildConfiguration.getJvmFlags(),
buildConfiguration.getMainClass());

try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
try (Timer timer2 = timer.subTimer("Initializing cache")) {
ListeningExecutorService listeningExecutorService =
Expand Down Expand Up @@ -145,7 +149,7 @@ public void run()
authenticatePushFuture,
pullBaseImageLayerFuturesFuture,
buildAndCacheApplicationLayerFutures,
getEntrypoint()),
entrypoint),
listeningExecutorService);

timer2.lap("Setting up application layer push");
Expand Down Expand Up @@ -183,29 +187,6 @@ public void run()
}

buildConfiguration.getBuildLogger().info("");
buildConfiguration.getBuildLogger().info("Container entrypoint set to " + getEntrypoint());
}

/**
* Gets the container entrypoint.
*
* <p>The entrypoint is {@code java -cp [classpaths] [main class]}.
*/
@VisibleForTesting
List<String> getEntrypoint() {
List<String> classPaths = new ArrayList<>();
classPaths.add(sourceFilesConfiguration.getDependenciesPathOnImage() + "*");
classPaths.add(sourceFilesConfiguration.getResourcesPathOnImage());
classPaths.add(sourceFilesConfiguration.getClassesPathOnImage());

String classPathsString = String.join(":", classPaths);

List<String> entrypoint = new ArrayList<>(4 + buildConfiguration.getJvmFlags().size());
entrypoint.add("java");
entrypoint.addAll(buildConfiguration.getJvmFlags());
entrypoint.add("-cp");
entrypoint.add(classPathsString);
entrypoint.add(buildConfiguration.getMainClass());
return entrypoint;
buildConfiguration.getBuildLogger().info("Container entrypoint set to " + entrypoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.builder;

import java.util.ArrayList;
import java.util.List;

/** Builds an image entrypoint for the Java application. */
public class EntrypointBuilder {

/**
* Builds the container entrypoint.
*
* <p>The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}.
*/
public static List<String> makeEntrypoint(
SourceFilesConfiguration sourceFilesConfiguration, List<String> jvmFlags, String mainClass) {
List<String> classPaths = new ArrayList<>();
classPaths.add(sourceFilesConfiguration.getDependenciesPathOnImage() + "*");
classPaths.add(sourceFilesConfiguration.getResourcesPathOnImage());
classPaths.add(sourceFilesConfiguration.getClassesPathOnImage());

String classPathsString = String.join(":", classPaths);

List<String> entrypoint = new ArrayList<>(4 + jvmFlags.size());
entrypoint.add("java");
entrypoint.addAll(jvmFlags);
entrypoint.add("-cp");
entrypoint.add(classPathsString);
entrypoint.add(mainClass);
return entrypoint;
}

private EntrypointBuilder() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.filesystem;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

/** Recursively applies a function to each file in a directory. */
public class DirectoryWalker {

private final Path rootDir;

/** Initialize with a root directory to walk. */
public DirectoryWalker(Path rootDir) {
if (!Files.isDirectory(rootDir)) {
throw new IllegalArgumentException("rootDir must be a directory");
}
this.rootDir = rootDir;
}

/**
* Walks {@link #rootDir} and applies {@code pathConsumer} to each file. Note that {@link
* #rootDir} itself is visited as well.
*/
public void walk(PathConsumer pathConsumer) throws IOException {
try (Stream<Path> fileStream = Files.walk(rootDir)) {
fileStream.forEach(
path -> {
try {
pathConsumer.accept(path);

} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});

} catch (UncheckedIOException ex) {
throw ex.getCause();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.filesystem;

import java.io.IOException;
import java.nio.file.Path;

@FunctionalInterface
public interface PathConsumer {

void accept(Path path) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

package com.google.cloud.tools.jib.builder;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

/** Tests for {@link BuildImageSteps}. More comprehensive tests are in the integration tests. */
public class BuildImageStepsTest {
/** Tests for {@link EntrypointBuilder}. */
public class EntrypointBuilderTest {

@Test
public void testGetEntrypoint() {
public void testMakeEntrypoint() {
String expectedDependenciesPath = "/app/libs/";
String expectedResourcesPath = "/app/resources/";
String expectedClassesPath = "/app/classes/";
Expand All @@ -36,7 +35,6 @@ public void testGetEntrypoint() {

SourceFilesConfiguration mockSourceFilesConfiguration =
Mockito.mock(SourceFilesConfiguration.class);
BuildConfiguration mockBuildConfiguration = Mockito.mock(BuildConfiguration.class);

Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage())
.thenReturn(expectedDependenciesPath);
Expand All @@ -45,9 +43,6 @@ public void testGetEntrypoint() {
Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage())
.thenReturn(expectedClassesPath);

Mockito.when(mockBuildConfiguration.getJvmFlags()).thenReturn(expectedJvmFlags);
Mockito.when(mockBuildConfiguration.getMainClass()).thenReturn(expectedMainClass);

Assert.assertEquals(
Arrays.asList(
"java",
Expand All @@ -56,8 +51,7 @@ public void testGetEntrypoint() {
"-cp",
"/app/libs/*:/app/resources/:/app/classes/",
"SomeMainClass"),
new BuildImageSteps(
mockBuildConfiguration, mockSourceFilesConfiguration, Mockito.mock(Path.class))
.getEntrypoint());
EntrypointBuilder.makeEntrypoint(
mockSourceFilesConfiguration, expectedJvmFlags, expectedMainClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.filesystem;

import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;

/** Tests for {@link DirectoryWalker}. */
public class DirectoryWalkerTest {

@Test
public void testWalk() throws URISyntaxException, IOException {
Path testDir = Paths.get(Resources.getResource("layer").toURI());

Set<Path> walkedPaths = new HashSet<>();
PathConsumer addToWalkedPaths = walkedPaths::add;

new DirectoryWalker(testDir).walk(addToWalkedPaths);

Set<Path> expectedPaths =
new HashSet<>(
Arrays.asList(
testDir,
testDir.resolve("a"),
testDir.resolve("a").resolve("b"),
testDir.resolve("a").resolve("b").resolve("bar"),
testDir.resolve("c"),
testDir.resolve("c").resolve("cat"),
testDir.resolve("foo")));
Assert.assertEquals(expectedPaths, walkedPaths);
}
}
Loading

0 comments on commit 57413b4

Please sign in to comment.