Skip to content

Commit

Permalink
Pull alpine image in a retry in Docker tests (elastic#88654)
Browse files Browse the repository at this point in the history
Closes elastic#88651. When using an `alpine` image to perform container
fiddling, first explicitly pull the image and do so in a loop, in an
attempt to make things more robust.
  • Loading branch information
pugnascotia authored Jul 25, 2022
1 parent bbc10e9 commit 2a65683
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ public static Path findInContainer(Path base, String type, String pattern) throw
* @param containerPath The path to mount localPath inside the container.
*/
private static void executePrivilegeEscalatedShellCmd(String shellCmd, Path localPath, Path containerPath) {
final String image = "alpine:3.13";
ensureImageIsPulled(image);

final List<String> args = new ArrayList<>();

args.add("docker run");
Expand All @@ -358,7 +361,7 @@ private static void executePrivilegeEscalatedShellCmd(String shellCmd, Path loca
args.add("--volume \"" + localPath.getParent() + ":" + containerPath.getParent() + "\"");

// Use a lightweight musl libc based small image
args.add("alpine:3.13");
args.add(image);

// And run inline commands via the POSIX shell
args.add("/bin/sh -c \"" + shellCmd + "\"");
Expand All @@ -368,6 +371,33 @@ private static void executePrivilegeEscalatedShellCmd(String shellCmd, Path loca
sh.run(command);
}

private static void ensureImageIsPulled(String image) {
// Don't pull if the image already exists. This does also mean that we never refresh it, but that
// isn't an issue in CI.
if (sh.runIgnoreExitCode("docker image inspect -f '{{ .Id }}' " + image).isSuccess()) {
return;
}

Shell.Result result = null;
int i = 0;
while (true) {
result = sh.runIgnoreExitCode("docker pull " + image);
if (result.isSuccess()) {
return;
}

if (++i == 3) {
throw new RuntimeException("Failed to pull Docker image [" + image + "]: " + result);
}

try {
Thread.sleep(10_000L);
} catch (InterruptedException e) {
// ignore
}
}
}

/**
* Create a directory with specified uid/gid using Docker backed privilege escalation.
* @param localPath The path to the directory to create.
Expand Down

0 comments on commit 2a65683

Please sign in to comment.