Skip to content

Commit 95af9ce

Browse files
committed
Merge branch 'inspect-platform-option' into 'main'
Added --platform option to the CLI for inspect See merge request weblogic-cloud/weblogic-image-tool!504
2 parents c83c398 + 19a066a commit 95af9ce

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptions.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private Properties getBaseImageProperties() throws IOException, InterruptedExcep
210210
logger.finer(props);
211211
}
212212
} else {
213-
props = Utils.getBaseImageProperties(buildEngine, fromImage,
213+
props = Utils.getBaseImageProperties(buildEngine, fromImage, buildPlatform,
214214
"/probe-env/inspect-image.sh", buildDir());
215215
}
216216
return props;
@@ -332,6 +332,10 @@ public String buildId() {
332332
return buildId;
333333
}
334334

335+
public String buildPlatform() {
336+
return buildPlatform;
337+
}
338+
335339
/**
336340
* Given the provided --buildPlatform, derive the architecture from the provided string.
337341
* Docker/Podman refer to the target architecture as the build platform.

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/InspectImage.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ public CommandResponse call() throws Exception {
3535
}
3636

3737
Properties baseImageProperties =
38-
Utils.getBaseImageProperties(buildEngine, imageName, scriptToRun, tempDirectory);
38+
Utils.getBaseImageProperties(buildEngine, imageName, imagePlatform, scriptToRun, tempDirectory);
3939

4040
System.out.println(new InspectOutput(baseImageProperties));
4141

4242
return CommandResponse.success(null);
4343
}
4444

45+
@SuppressWarnings("unused")
4546
@Option(
4647
names = {"--image", "-i"},
4748
required = true,
@@ -58,18 +59,28 @@ public CommandResponse call() throws Exception {
5859
)
5960
String buildEngine = Constants.BUILDER_DEFAULT;
6061

62+
@SuppressWarnings("unused")
6163
@Option(
62-
names = {"--patches"},
64+
names = {"--patches", "-p"},
6365
description = "Include OPatch information in the output, including a list of patches applied.",
6466
defaultValue = "false"
6567
)
6668
private boolean listPatches;
6769

70+
@SuppressWarnings("unused")
6871
@Option(
69-
names = {"--format"},
72+
names = {"--format", "-f"},
7073
paramLabel = "FORMAT",
7174
description = "Output format. Supported values: ${COMPLETION-CANDIDATES} Default: ${DEFAULT-VALUE}",
7275
defaultValue = "JSON"
7376
)
7477
private OutputFormat outputFormat;
78+
79+
@SuppressWarnings("unused")
80+
@Option(
81+
names = {"--platform"},
82+
paramLabel = "<image platform>",
83+
description = "Specify the platform for selecting the image. Example: linux/amd64 or linux/arm64"
84+
)
85+
private String imagePlatform;
7586
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/RebaseImage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public CommandResponse call() throws Exception {
4545
dockerfileOptions.setSourceImage(sourceImage);
4646

4747
logger.info("IMG-0091", sourceImage);
48-
Properties sourceImageProperties = Utils.getBaseImageProperties(buildEngine, sourceImage,
48+
Properties sourceImageProperties = Utils.getBaseImageProperties(buildEngine, sourceImage, buildPlatform(),
4949
"/probe-env/inspect-image.sh", buildDir());
5050

5151
String oldOracleHome = sourceImageProperties.getProperty("oracleHome", null);
@@ -62,7 +62,7 @@ public CommandResponse call() throws Exception {
6262
dockerfileOptions.setRebaseToTarget(true);
6363

6464
Properties targetImageProperties = Utils.getBaseImageProperties(buildEngine, targetImage,
65-
"/probe-env/inspect-image.sh", buildDir());
65+
buildPlatform(), "/probe-env/inspect-image.sh", buildDir());
6666
newOracleHome = targetImageProperties.getProperty("oracleHome", null);
6767
newJavaHome = targetImageProperties.getProperty("javaHome", null);
6868
useFileOwnerFromTarget(targetImageProperties);

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public CommandResponse call() throws Exception {
5353

5454
dockerfileOptions.setBaseImage(fromImage()).setWdtBase(fromImage());
5555

56-
Properties baseImageProperties = Utils.getBaseImageProperties(buildEngine, fromImage(),
56+
Properties baseImageProperties = Utils.getBaseImageProperties(buildEngine, fromImage(), buildPlatform(),
5757
"/probe-env/inspect-image-long.sh", buildDir());
5858

5959
dockerfileOptions.setJavaHome(baseImageProperties.getProperty("javaHome", null));

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.Paths;
2020
import java.nio.file.StandardCopyOption;
2121
import java.text.MessageFormat;
22+
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.Base64;
2425
import java.util.Collection;
@@ -422,20 +423,24 @@ public static boolean isEmptyString(String s) {
422423
/**
423424
* Reads the docker image environment variables into Java Properties.
424425
*
425-
* @param builder the binary to create the container (like docker)
426-
* @param dockerImage the name of the Docker image to read from
427-
* @param script the script resource (path to the script in the JAR)
428-
* @param contextDir the image build context folder
426+
* @param builder the binary to create the container (like docker)
427+
* @param dockerImage the name of the Docker image to be run
428+
* @param imagePlatform the platform of the Docker image to be run
429+
* @param script the script resource (path to the script in the JAR)
430+
* @param contextDir the image build context folder
429431
* @return The key/value pairs representing the ENV of the Docker image
430432
* @throws IOException when the Docker command fails
431433
* @throws InterruptedException when the Docker command is interrupted
432434
*/
433-
public static Properties getBaseImageProperties(String builder, String dockerImage, String script,
435+
public static Properties getBaseImageProperties(String builder,
436+
String dockerImage,
437+
String imagePlatform,
438+
String script,
434439
String contextDir) throws IOException, InterruptedException {
435-
logger.entering(builder, dockerImage, script, contextDir);
440+
logger.entering(builder, dockerImage, imagePlatform, script, contextDir);
436441
final String scriptToRun = "test-env.sh";
437442
Utils.copyResourceAsFile(script, contextDir + File.separator + scriptToRun);
438-
List<String> imageEnvCmd = Utils.getDockerRunCmd(builder,
443+
List<String> imageEnvCmd = Utils.getDockerRunCmd(builder, imagePlatform,
439444
contextDir + File.separator + scriptToRun, dockerImage);
440445
logger.info("IMG-0097", dockerImage);
441446
Properties result = Utils.runDockerCommand(imageEnvCmd);
@@ -447,12 +452,13 @@ public static Properties getBaseImageProperties(String builder, String dockerIma
447452
* Constructs a docker command to run a script in the container with a volume mount.
448453
*
449454
* @param builder docker/podman executable
455+
* @param imagePlatform the platform for selecting the image (multi-arch images)
450456
* @param scriptToRun the local script to encode and run
451457
* @param dockerImage docker image tag
452458
* @return command
453459
*/
454-
private static List<String> getDockerRunCmd(String builder, String scriptToRun, String dockerImage)
455-
throws IOException {
460+
private static List<String> getDockerRunCmd(String builder, String imagePlatform, String scriptToRun,
461+
String dockerImage) throws IOException {
456462

457463
// We are removing the volume mount option, -v won't work in remote docker daemon and also
458464
// problematic if the mounted volume source is on a nfs volume as we have no idea what the docker volume
@@ -463,11 +469,21 @@ private static List<String> getDockerRunCmd(String builder, String scriptToRun,
463469

464470
byte[] fileBytes = Files.readAllBytes(Paths.get(scriptToRun));
465471
String encodedFile = Base64.getEncoder().encodeToString(fileBytes);
466-
String oneCommand = String.format("echo %s | base64 -d | /bin/sh", encodedFile);
467-
logger.finest("running command in image [" + oneCommand + "]");
468-
return Stream.of(
469-
builder, "run", "--rm",
470-
dockerImage, "/bin/sh", "-c", oneCommand).collect(Collectors.toList());
472+
String shellCommand = String.format("echo %s | base64 -d | /bin/sh", encodedFile);
473+
logger.finest("running command in image [" + shellCommand + "]");
474+
List<String> command = new ArrayList<>(10);
475+
command.add(builder);
476+
command.add("run");
477+
if (imagePlatform != null) {
478+
command.add("--platform");
479+
command.add(imagePlatform);
480+
}
481+
command.add("--rm");
482+
command.add(dockerImage);
483+
command.add("/bin/sh");
484+
command.add("-c");
485+
command.add(shellCommand);
486+
return command;
471487
}
472488

473489
/**

0 commit comments

Comments
 (0)