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

Check if file exists when executable path is passed in through jib dockerClient.executable #3744

Merged
merged 3 commits into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -103,30 +103,28 @@ public List<DescriptorDigest> getDiffIds() throws DigestException {
public static final Path DEFAULT_DOCKER_CLIENT = Paths.get("docker");

/**
* Checks if Docker is installed on the user's system and accessible by running the default {@code
* docker} command.
* Checks if Docker is installed on the user's system by running the `docker` command.
*
* @return {@code true} if Docker is installed on the user's system and accessible
*/
public static boolean isDefaultDockerInstalled() {
return isDockerInstalled(DEFAULT_DOCKER_CLIENT);
try {
new ProcessBuilder(DEFAULT_DOCKER_CLIENT.toString()).start();
Copy link
Contributor Author

@mpeddada1 mpeddada1 Aug 25, 2022

Choose a reason for hiding this comment

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

Some background of this change: Checking for existence of the default docker command when it is on PATH has been a little tricky. There can be several places where docker could be installed on a system and this also differs from OS to OS. For checking whether the path passed through jib.dockerClient.executable is installed correctly, however, we no longer run executable itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, that's a good idea.

return true;
} catch (IOException ex) {
return false;
}
}

/**
* Checks if Docker is installed on the user's system and accessible by running the given {@code
* docker} executable.
* Checks if Docker is installed on the user's system and by verifying if the executable path
* provided has the appropriate permissions.
*
* @param dockerExecutable path to the executable to test running
* @return {@code true} if Docker is installed on the user's system and accessible
*/
public static boolean isDockerInstalled(Path dockerExecutable) {
try {
new ProcessBuilder(dockerExecutable.toString()).start();
return true;

} catch (IOException ex) {
return false;
}
return Files.exists(dockerExecutable);
}

/**
Expand Down
Expand Up @@ -24,11 +24,13 @@
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.Resources;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.security.DigestException;
Expand Down Expand Up @@ -74,6 +76,13 @@ public void testIsDockerInstalled_fail() {
Assert.assertFalse(CliDockerClient.isDockerInstalled(Paths.get("path/to/nonexistent/file")));
}

@Test
public void testIsDockerInstalled_pass() throws URISyntaxException {
Assert.assertTrue(
CliDockerClient.isDockerInstalled(
Paths.get(Resources.getResource("core/docker/emptyFile").toURI())));
}

@Test
public void testLoad() throws IOException, InterruptedException {
DockerClient testDockerClient =
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion jib-gradle-plugin/README.md
Expand Up @@ -290,7 +290,7 @@ Property | Type | Default | Description

Property | Type | Default | Description
--- | --- | --- | ---
`executable` | `File` | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon.
`executable` | `File` | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. **Please note**: Users are responsible for ensuring that the Docker path passed in is valid and has the right permissions to be executed.
`environment` | `Map<String, String>` | *None* | Sets environment variables used by the Docker executable.

#### System Properties
Expand Down
2 changes: 1 addition & 1 deletion jib-maven-plugin/README.md
Expand Up @@ -341,7 +341,7 @@ Property | Type | Default | Description

Property | Type | Default | Description
--- | --- | --- | ---
`executable` | string | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon.
`executable` | string | `docker` | Sets the path to the Docker executable that is called to load the image into the Docker daemon. **Please note**: Users are responsible for ensuring that the Docker path passed in is valid and has the right permissions to be executed.
`environment` | map | *None* | Sets environment variables used by the Docker executable.

#### System Properties
Expand Down