Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring working directory #1266

Merged
merged 10 commits into from
Nov 27, 2018
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Added

- Image ID is now written to `build/jib-image.id` ([#1204](https://github.com/GoogleContainerTools/jib/issues/1204))
- `jib.container.entrypoint = 'INHERIT'` allows inheriting `ENTRYPOINT` and `CMD` from the base image. While inheriting `ENTRYPOINT`, you can also override `CMD` using `jib.container.args`.
- `container.workingDirectory` configuration parameter to set the working directory ([#1225](https://github.com/GoogleContainerTools/jib/issues/1225))

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -146,7 +147,11 @@ public void buildDocker()

} catch (AppRootInvalidException ex) {
throw new GradleException(
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed - should we include cause ex in these?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looked over AppRootInvalidException and it seems that in the constructor, message and invalidAppRoot are always set to the invalid appRoot.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to include cause ex.

It is actually intended that the message is set to appRoot (like FileNotFoundException, whose message is simply the missing file path).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, is the message parameter necessary then? I couldn't find a call to the constructor that wasn't appRoot repeated twice (new AppRootInvalidException(appRoot, appRoot, ex)).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really necessary in our codebase, but I think it makes sense that, conceptually, the general exception message is set to appRoot rather than leaving it undefined. If anyone is doing a very general exception handling of displaying a message, it would tell the invalid value.

} catch (WorkingDirectoryInvalidException ex) {
throw new GradleException(
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.IOException;
Expand Down Expand Up @@ -126,7 +127,11 @@ public void buildImage()

} catch (AppRootInvalidException ex) {
throw new GradleException(
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue());
} catch (WorkingDirectoryInvalidException ex) {
throw new GradleException(
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -143,7 +144,11 @@ public void buildTar()

} catch (AppRootInvalidException ex) {
throw new GradleException(
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue());
} catch (WorkingDirectoryInvalidException ex) {
throw new GradleException(
"container.workingDirectory is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());
}
}

Expand Down
1 change: 1 addition & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Added

- Image ID is now written to `target/jib-image.id` ([#1204](https://github.com/GoogleContainerTools/jib/issues/1204))
- `<container><entrypoint>INHERIT</entrypoint></container>` allows inheriting `ENTRYPOINT` and `CMD` from the base image. While inheriting `ENTRYPOINT`, you can also override `CMD` using `<container><args>`.
- `<container><workingDirectory>` configuration parameter to set the working directory ([#1225](https://github.com/GoogleContainerTools/jib/issues/1225))

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -112,7 +113,11 @@ public void execute() throws MojoExecutionException {

} catch (AppRootInvalidException ex) {
throw new MojoExecutionException(
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue());
} catch (WorkingDirectoryInvalidException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());

} catch (InvalidImageReferenceException
| IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import java.io.IOException;
Expand Down Expand Up @@ -128,7 +129,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {

} catch (AppRootInvalidException ex) {
throw new MojoExecutionException(
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue());
} catch (WorkingDirectoryInvalidException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());

} catch (InvalidImageReferenceException
| IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.cloud.tools.jib.plugins.common.MainClassInferenceException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.cloud.tools.jib.plugins.common.RawConfiguration;
import com.google.cloud.tools.jib.plugins.common.WorkingDirectoryInvalidException;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -106,7 +107,11 @@ public void execute() throws MojoExecutionException {

} catch (AppRootInvalidException ex) {
throw new MojoExecutionException(
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidAppRoot());
"<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue());
} catch (WorkingDirectoryInvalidException ex) {
throw new MojoExecutionException(
"<container><workingDirectory> is not an absolute Unix-style path: "
+ ex.getInvalidPathValue());

} catch (InvalidImageReferenceException
| IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.tools.jib.plugins.common;

/**
* Indicates that the container.appRoot config value is invalid (i.e., the path is not in the
* Indicates that the {@code container.appRoot} config value is invalid. (The path is not in the
* absolute unix-path style.
*/
public class AppRootInvalidException extends Exception {
Expand All @@ -29,7 +29,7 @@ public AppRootInvalidException(String message, String invalidAppRoot, Throwable
this.invalidAppRoot = invalidAppRoot;
}

public String getInvalidAppRoot() {
public String getInvalidPathValue() {
return invalidAppRoot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static PluginConfigurationProcessor processCommonConfigurationForDockerDa
@Nullable Map<String, String> dockerEnvironment,
HelpfulSuggestions helpfulSuggestions)
throws InvalidImageReferenceException, MainClassInferenceException, AppRootInvalidException,
InferredAuthRetrievalException, IOException {
InferredAuthRetrievalException, IOException, WorkingDirectoryInvalidException {
ImageReference targetImageReference =
getGeneratedTargetDockerTag(rawConfiguration, projectProperties, helpfulSuggestions);
DockerDaemonImage targetImage = DockerDaemonImage.named(targetImageReference);
Expand All @@ -153,7 +153,7 @@ public static PluginConfigurationProcessor processCommonConfigurationForTarImage
Path tarImagePath,
HelpfulSuggestions helpfulSuggestions)
throws InvalidImageReferenceException, MainClassInferenceException, AppRootInvalidException,
InferredAuthRetrievalException, IOException {
InferredAuthRetrievalException, IOException, WorkingDirectoryInvalidException {
ImageReference targetImageReference =
getGeneratedTargetDockerTag(rawConfiguration, projectProperties, helpfulSuggestions);
TarImage targetImage = TarImage.named(targetImageReference).saveTo(tarImagePath);
Expand All @@ -166,7 +166,8 @@ public static PluginConfigurationProcessor processCommonConfigurationForTarImage
public static PluginConfigurationProcessor processCommonConfigurationForRegistryImage(
RawConfiguration rawConfiguration, ProjectProperties projectProperties)
throws InferredAuthRetrievalException, InvalidImageReferenceException,
MainClassInferenceException, AppRootInvalidException, IOException {
MainClassInferenceException, AppRootInvalidException, IOException,
WorkingDirectoryInvalidException {
Preconditions.checkArgument(rawConfiguration.getToImage().isPresent());

ImageReference targetImageReference = ImageReference.parse(rawConfiguration.getToImage().get());
Expand Down Expand Up @@ -208,7 +209,7 @@ static PluginConfigurationProcessor processCommonConfiguration(
ImageReference targetImageReference,
boolean isTargetImageCredentialPresent)
throws InvalidImageReferenceException, MainClassInferenceException, AppRootInvalidException,
InferredAuthRetrievalException, IOException {
InferredAuthRetrievalException, IOException, WorkingDirectoryInvalidException {
JibSystemProperties.checkHttpTimeoutProperty();

ImageReference baseImageReference =
Expand Down Expand Up @@ -248,11 +249,8 @@ static PluginConfigurationProcessor processCommonConfiguration(
.setExposedPorts(ExposedPortsParser.parse(rawConfiguration.getPorts()))
.setLabels(rawConfiguration.getLabels())
.setUser(rawConfiguration.getUser().orElse(null));
rawConfiguration
.getWorkingDirectory()
.ifPresent(
workingDirectory ->
jibContainerBuilder.setWorkingDirectory(AbsoluteUnixPath.get(workingDirectory)));
getWorkingDirectoryChecked(rawConfiguration)
.ifPresent(workingDirectory -> jibContainerBuilder.setWorkingDirectory(workingDirectory));
if (rawConfiguration.getUseCurrentTimestamp()) {
eventDispatcher.dispatch(
LogEvent.warn(
Expand Down Expand Up @@ -300,6 +298,21 @@ static AbsoluteUnixPath getAppRootChecked(
}
}

@VisibleForTesting
static Optional<AbsoluteUnixPath> getWorkingDirectoryChecked(RawConfiguration rawConfiguration)
throws WorkingDirectoryInvalidException {
if (!rawConfiguration.getWorkingDirectory().isPresent()) {
return Optional.empty();
}

String path = rawConfiguration.getWorkingDirectory().get();
try {
return Optional.of(AbsoluteUnixPath.get(path));
} catch (IllegalArgumentException ex) {
throw new WorkingDirectoryInvalidException(path, path, ex);
}
}

// TODO: find a way to reduce the number of arguments.
private static boolean configureCredentialRetrievers(
EventDispatcher eventDispatcher,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2018 Google LLC.
*
* 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.plugins.common;

/**
* Indicates that the {@code container.workingDirectory} config value is invalid. (The path is not
* in the absolute unix-path style).
*/
public class WorkingDirectoryInvalidException extends Exception {

private final String invalidPath;

public WorkingDirectoryInvalidException(String message, String invalidPath, Throwable ex) {
super(message, ex);
this.invalidPath = invalidPath;
}

public String getInvalidPathValue() {
return invalidPath;
}
}
Loading