Skip to content

Commit 57413b4

Browse files
authored
Adds jib:dockercontext to export a Docker context. (#121)
1 parent ed464a9 commit 57413b4

File tree

15 files changed

+704
-112
lines changed

15 files changed

+704
-112
lines changed

jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildImageSteps.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import com.google.cloud.tools.jib.cache.CachedLayer;
2424
import com.google.cloud.tools.jib.http.Authorization;
2525
import com.google.cloud.tools.jib.image.Image;
26-
import com.google.common.annotations.VisibleForTesting;
2726
import com.google.common.util.concurrent.Futures;
2827
import com.google.common.util.concurrent.ListenableFuture;
2928
import com.google.common.util.concurrent.ListeningExecutorService;
3029
import com.google.common.util.concurrent.MoreExecutors;
3130
import java.io.IOException;
3231
import java.nio.file.Path;
33-
import java.util.ArrayList;
3432
import java.util.List;
3533
import java.util.concurrent.ExecutionException;
3634
import java.util.concurrent.Executors;
@@ -60,6 +58,12 @@ public BuildConfiguration getBuildConfiguration() {
6058
public void run()
6159
throws InterruptedException, ExecutionException, CacheMetadataCorruptedException,
6260
IOException {
61+
List<String> entrypoint =
62+
EntrypointBuilder.makeEntrypoint(
63+
sourceFilesConfiguration,
64+
buildConfiguration.getJvmFlags(),
65+
buildConfiguration.getMainClass());
66+
6367
try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
6468
try (Timer timer2 = timer.subTimer("Initializing cache")) {
6569
ListeningExecutorService listeningExecutorService =
@@ -145,7 +149,7 @@ public void run()
145149
authenticatePushFuture,
146150
pullBaseImageLayerFuturesFuture,
147151
buildAndCacheApplicationLayerFutures,
148-
getEntrypoint()),
152+
entrypoint),
149153
listeningExecutorService);
150154

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

185189
buildConfiguration.getBuildLogger().info("");
186-
buildConfiguration.getBuildLogger().info("Container entrypoint set to " + getEntrypoint());
187-
}
188-
189-
/**
190-
* Gets the container entrypoint.
191-
*
192-
* <p>The entrypoint is {@code java -cp [classpaths] [main class]}.
193-
*/
194-
@VisibleForTesting
195-
List<String> getEntrypoint() {
196-
List<String> classPaths = new ArrayList<>();
197-
classPaths.add(sourceFilesConfiguration.getDependenciesPathOnImage() + "*");
198-
classPaths.add(sourceFilesConfiguration.getResourcesPathOnImage());
199-
classPaths.add(sourceFilesConfiguration.getClassesPathOnImage());
200-
201-
String classPathsString = String.join(":", classPaths);
202-
203-
List<String> entrypoint = new ArrayList<>(4 + buildConfiguration.getJvmFlags().size());
204-
entrypoint.add("java");
205-
entrypoint.addAll(buildConfiguration.getJvmFlags());
206-
entrypoint.add("-cp");
207-
entrypoint.add(classPathsString);
208-
entrypoint.add(buildConfiguration.getMainClass());
209-
return entrypoint;
190+
buildConfiguration.getBuildLogger().info("Container entrypoint set to " + entrypoint);
210191
}
211192
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.builder;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
/** Builds an image entrypoint for the Java application. */
23+
public class EntrypointBuilder {
24+
25+
/**
26+
* Builds the container entrypoint.
27+
*
28+
* <p>The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}.
29+
*/
30+
public static List<String> makeEntrypoint(
31+
SourceFilesConfiguration sourceFilesConfiguration, List<String> jvmFlags, String mainClass) {
32+
List<String> classPaths = new ArrayList<>();
33+
classPaths.add(sourceFilesConfiguration.getDependenciesPathOnImage() + "*");
34+
classPaths.add(sourceFilesConfiguration.getResourcesPathOnImage());
35+
classPaths.add(sourceFilesConfiguration.getClassesPathOnImage());
36+
37+
String classPathsString = String.join(":", classPaths);
38+
39+
List<String> entrypoint = new ArrayList<>(4 + jvmFlags.size());
40+
entrypoint.add("java");
41+
entrypoint.addAll(jvmFlags);
42+
entrypoint.add("-cp");
43+
entrypoint.add(classPathsString);
44+
entrypoint.add(mainClass);
45+
return entrypoint;
46+
}
47+
48+
private EntrypointBuilder() {}
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.filesystem;
18+
19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.stream.Stream;
24+
25+
/** Recursively applies a function to each file in a directory. */
26+
public class DirectoryWalker {
27+
28+
private final Path rootDir;
29+
30+
/** Initialize with a root directory to walk. */
31+
public DirectoryWalker(Path rootDir) {
32+
if (!Files.isDirectory(rootDir)) {
33+
throw new IllegalArgumentException("rootDir must be a directory");
34+
}
35+
this.rootDir = rootDir;
36+
}
37+
38+
/**
39+
* Walks {@link #rootDir} and applies {@code pathConsumer} to each file. Note that {@link
40+
* #rootDir} itself is visited as well.
41+
*/
42+
public void walk(PathConsumer pathConsumer) throws IOException {
43+
try (Stream<Path> fileStream = Files.walk(rootDir)) {
44+
fileStream.forEach(
45+
path -> {
46+
try {
47+
pathConsumer.accept(path);
48+
49+
} catch (IOException ex) {
50+
throw new UncheckedIOException(ex);
51+
}
52+
});
53+
54+
} catch (UncheckedIOException ex) {
55+
throw ex.getCause();
56+
}
57+
}
58+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.filesystem;
18+
19+
import java.io.IOException;
20+
import java.nio.file.Path;
21+
22+
@FunctionalInterface
23+
public interface PathConsumer {
24+
25+
void accept(Path path) throws IOException;
26+
}

jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildImageStepsTest.java renamed to jib-core/src/test/java/com/google/cloud/tools/jib/builder/EntrypointBuilderTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616

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

19-
import java.nio.file.Path;
2019
import java.util.Arrays;
2120
import java.util.List;
2221
import org.junit.Assert;
2322
import org.junit.Test;
2423
import org.mockito.Mockito;
2524

26-
/** Tests for {@link BuildImageSteps}. More comprehensive tests are in the integration tests. */
27-
public class BuildImageStepsTest {
25+
/** Tests for {@link EntrypointBuilder}. */
26+
public class EntrypointBuilderTest {
2827

2928
@Test
30-
public void testGetEntrypoint() {
29+
public void testMakeEntrypoint() {
3130
String expectedDependenciesPath = "/app/libs/";
3231
String expectedResourcesPath = "/app/resources/";
3332
String expectedClassesPath = "/app/classes/";
@@ -36,7 +35,6 @@ public void testGetEntrypoint() {
3635

3736
SourceFilesConfiguration mockSourceFilesConfiguration =
3837
Mockito.mock(SourceFilesConfiguration.class);
39-
BuildConfiguration mockBuildConfiguration = Mockito.mock(BuildConfiguration.class);
4038

4139
Mockito.when(mockSourceFilesConfiguration.getDependenciesPathOnImage())
4240
.thenReturn(expectedDependenciesPath);
@@ -45,9 +43,6 @@ public void testGetEntrypoint() {
4543
Mockito.when(mockSourceFilesConfiguration.getClassesPathOnImage())
4644
.thenReturn(expectedClassesPath);
4745

48-
Mockito.when(mockBuildConfiguration.getJvmFlags()).thenReturn(expectedJvmFlags);
49-
Mockito.when(mockBuildConfiguration.getMainClass()).thenReturn(expectedMainClass);
50-
5146
Assert.assertEquals(
5247
Arrays.asList(
5348
"java",
@@ -56,8 +51,7 @@ public void testGetEntrypoint() {
5651
"-cp",
5752
"/app/libs/*:/app/resources/:/app/classes/",
5853
"SomeMainClass"),
59-
new BuildImageSteps(
60-
mockBuildConfiguration, mockSourceFilesConfiguration, Mockito.mock(Path.class))
61-
.getEntrypoint());
54+
EntrypointBuilder.makeEntrypoint(
55+
mockSourceFilesConfiguration, expectedJvmFlags, expectedMainClass));
6256
}
6357
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.filesystem;
18+
19+
import com.google.common.io.Resources;
20+
import java.io.IOException;
21+
import java.net.URISyntaxException;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
24+
import java.util.Arrays;
25+
import java.util.HashSet;
26+
import java.util.Set;
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
30+
/** Tests for {@link DirectoryWalker}. */
31+
public class DirectoryWalkerTest {
32+
33+
@Test
34+
public void testWalk() throws URISyntaxException, IOException {
35+
Path testDir = Paths.get(Resources.getResource("layer").toURI());
36+
37+
Set<Path> walkedPaths = new HashSet<>();
38+
PathConsumer addToWalkedPaths = walkedPaths::add;
39+
40+
new DirectoryWalker(testDir).walk(addToWalkedPaths);
41+
42+
Set<Path> expectedPaths =
43+
new HashSet<>(
44+
Arrays.asList(
45+
testDir,
46+
testDir.resolve("a"),
47+
testDir.resolve("a").resolve("b"),
48+
testDir.resolve("a").resolve("b").resolve("bar"),
49+
testDir.resolve("c"),
50+
testDir.resolve("c").resolve("cat"),
51+
testDir.resolve("foo")));
52+
Assert.assertEquals(expectedPaths, walkedPaths);
53+
}
54+
}

0 commit comments

Comments
 (0)