Skip to content

Commit

Permalink
Check if file exists when executable path is passed in through jib do…
Browse files Browse the repository at this point in the history
…ckerClient.executable (#3744)

* Check if file exists when executable path is passed in through jib.dockerClient.executable
  • Loading branch information
mpeddada1 committed Aug 25, 2022
1 parent df6c248 commit 67fa40b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
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();
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

0 comments on commit 67fa40b

Please sign in to comment.