Skip to content

Commit b3abcdc

Browse files
authored
Allow user to override Package Manager type (#181)
1 parent 040251a commit b3abcdc

File tree

13 files changed

+131
-132
lines changed

13 files changed

+131
-132
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.ArrayList;
1414
import java.util.Collections;
1515
import java.util.List;
16+
import java.util.Properties;
1617
import java.util.regex.Matcher;
1718
import java.util.regex.Pattern;
1819

@@ -318,6 +319,51 @@ void installOpatchInstaller(String tmpDir, String opatchBugNumber) throws Except
318319
dockerfileOptions.setOPatchFileName(filename);
319320
}
320321

322+
/**
323+
* Set the docker options for build if fromImage parameter is present.
324+
* @param fromImage image tag
325+
* @param tmpDir temporary directory
326+
* @throws Exception thrown by getBaseImageProperties
327+
*/
328+
public void copyOptionsFromImage(String fromImage, String tmpDir)
329+
throws Exception {
330+
331+
if (fromImage != null && !fromImage.isEmpty()) {
332+
logger.finer("IMG-0002", fromImage);
333+
dockerfileOptions.setBaseImage(fromImage);
334+
335+
Utils.copyResourceAsFile("/probe-env/test-create-env.sh",
336+
tmpDir + File.separator + "test-env.sh", true);
337+
338+
Properties baseImageProperties = Utils.getBaseImageProperties(fromImage, tmpDir);
339+
340+
if (baseImageProperties.getProperty("WLS_VERSION", null) != null) {
341+
throw new IllegalArgumentException(Utils.getMessage("IMG-0038", fromImage,
342+
baseImageProperties.getProperty("ORACLE_HOME")));
343+
}
344+
345+
String existingJavaHome = baseImageProperties.getProperty("JAVA_HOME", null);
346+
if (existingJavaHome != null) {
347+
dockerfileOptions.disableJavaInstall(existingJavaHome);
348+
logger.info("IMG-0000", existingJavaHome);
349+
}
350+
351+
String osProperty = baseImageProperties.getProperty("ID", "ol");
352+
PackageManagerType pkgMgr = PackageManagerType.fromOperatingSystem(osProperty);
353+
logger.fine("fromImage is {0}, using package manager {1}", osProperty, pkgMgr);
354+
if (packageManager != PackageManagerType.OS_DEFAULT && pkgMgr != packageManager) {
355+
logger.info("IMG-0079", pkgMgr, packageManager);
356+
pkgMgr = packageManager;
357+
}
358+
dockerfileOptions.setPackageInstaller(pkgMgr);
359+
} else if (packageManager == PackageManagerType.OS_DEFAULT) {
360+
// Default OS is Oracle Linux, so default package manager is YUM
361+
dockerfileOptions.setPackageInstaller(PackageManagerType.YUM);
362+
} else {
363+
dockerfileOptions.setPackageInstaller(packageManager);
364+
}
365+
}
366+
321367
String getUserId() {
322368
return userId;
323369
}
@@ -475,6 +521,12 @@ String getPassword() {
475521
)
476522
Path verrazzanoModel;
477523

524+
@Option(
525+
names = {"--packageManager"},
526+
description = "Set the Linux package manager to use for installing OS packages. Default: ${DEFAULT-VALUE}"
527+
)
528+
PackageManagerType packageManager = PackageManagerType.OS_DEFAULT;
529+
478530
@SuppressWarnings("unused")
479531
@Unmatched
480532
List<String> unmatchedOptions;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public CommandResponse call() throws Exception {
5454
return new CommandResponse(-1, "Patch ID validation failed");
5555
}
5656

57-
WLSInstallHelper.copyOptionsFromImage(fromImage, dockerfileOptions, tmpDir);
57+
copyOptionsFromImage(fromImage, tmpDir);
5858

5959
if (dockerfileOptions.installJava()) {
6060
CachedFile jdk = new CachedFile(InstallerType.JDK, jdkVersion);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.menu;
5+
6+
public enum PackageManagerType {
7+
OS_DEFAULT,
8+
NONE,
9+
YUM,
10+
APTGET,
11+
APK,
12+
ZYPPER;
13+
14+
/**
15+
* Returns the package manager that should be used to install software on a given Linux OS.
16+
* Defaults to YUM. Known OS's include: ubuntu, debian, opensuse, centos, ol (Oracle Linux), and rhel.
17+
*
18+
* @param osID identifier for the OS, like ubuntu, debian, rhel, ol, ...
19+
* @return the package manager
20+
*/
21+
public static PackageManagerType fromOperatingSystem(String osID) {
22+
if (osID == null) {
23+
return YUM;
24+
}
25+
26+
osID = osID.replaceAll("[\"]", "");
27+
switch (osID) {
28+
case "ubuntu":
29+
case "debian":
30+
return APTGET;
31+
case "opensuse":
32+
return ZYPPER;
33+
case "centos":
34+
case "ol":
35+
case "rhel":
36+
default:
37+
return YUM;
38+
}
39+
}
40+
}

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

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

122122
if (dockerfileOptions.isRebaseToNew()) {
123123

124-
WLSInstallHelper.copyOptionsFromImage(fromImage, dockerfileOptions, tmpDir);
124+
copyOptionsFromImage(fromImage, tmpDir);
125125

126126
CachedFile jdk = new CachedFile(InstallerType.JDK, jdkVersion);
127127
Path installerPath = jdk.copyFile(cache(), tmpDir);

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public CommandResponse call() throws Exception {
6161

6262
Properties baseImageProperties = Utils.getBaseImageProperties(fromImage, tmpDir);
6363

64-
String pkgMgr = Utils.getPackageMgrStr(baseImageProperties.getProperty("ID", "ol"));
65-
if (!Utils.isEmptyString(pkgMgr)) {
66-
dockerfileOptions.setPackageInstaller(pkgMgr);
67-
}
68-
6964
String oracleHome = baseImageProperties.getProperty("ORACLE_HOME", null);
7065
if (oracleHome == null) {
7166
return new CommandResponse(-1, "IMG-0072", fromImage);

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public final class Constants {
2929
public static final String HTTPS = "https";
3030
public static final String PATCH_ID_REGEX = "^(\\d{8})(?:[_][0-9][0-9]\\.[0-9]\\.[0-9]\\.[0-9]\\.(\\d+))?";
3131
public static final String RIGID_PATCH_ID_REGEX = "^(\\d{8})[_][0-9][0-9]\\.[0-9]\\.[0-9]\\.[0-9]\\.(\\d+)";
32-
//Option flags
33-
public static final String YUM = "_YUM";
34-
public static final String APTGET = "_APT";
35-
public static final String ZYPPER = "_SUSE";
3632

3733

3834
private Constants() {

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.StringJoiner;
1212

13+
import com.oracle.weblogic.imagetool.cli.menu.PackageManagerType;
1314
import com.oracle.weblogic.imagetool.installer.MiddlewareInstall;
1415
import com.oracle.weblogic.imagetool.installer.MiddlewareInstallPackage;
1516
import com.oracle.weblogic.imagetool.wdt.WdtOperation;
@@ -25,10 +26,6 @@ public class DockerfileOptions {
2526
private static final String DEFAULT_ORAINV_DIR = "/u01/oracle/oraInventory";
2627

2728
final String buildId;
28-
boolean useYum = false;
29-
boolean useAptGet = false;
30-
boolean useApk = false;
31-
boolean useZypper = false;
3229

3330
private boolean useWdt;
3431
private boolean applyPatches;
@@ -50,6 +47,7 @@ public class DockerfileOptions {
5047
private String opatchFileName;
5148
private String sourceImage;
5249
private String targetImage;
50+
private PackageManagerType pkgMgr;
5351

5452
// WDT values
5553
private String wdtHome;
@@ -227,31 +225,36 @@ public void setRebaseToNew(boolean rebaseToNew) {
227225
}
228226

229227
/**
230-
* Only one package installer should be allowed.
228+
* Set the Linux package Manager type to use during the build.
231229
*
232-
* @param option the String constant identifying the installer to use.
230+
* @param option the Linux package Manager type to use during the build.
233231
* @return this DockerfileOptions object
234232
*/
235-
public DockerfileOptions setPackageInstaller(String option) {
236-
useZypper = false;
237-
useAptGet = false;
238-
useApk = false;
239-
useYum = false;
240-
switch (option) {
241-
case Constants.YUM:
242-
useYum = true;
243-
break;
244-
case Constants.APTGET:
245-
useAptGet = true;
246-
break;
247-
case Constants.ZYPPER:
248-
useZypper = true;
249-
break;
250-
default:
251-
}
233+
public DockerfileOptions setPackageInstaller(PackageManagerType option) {
234+
pkgMgr = option;
252235
return this;
253236
}
254237

238+
@SuppressWarnings("unused")
239+
public boolean useYum() {
240+
return pkgMgr == PackageManagerType.YUM;
241+
}
242+
243+
@SuppressWarnings("unused")
244+
public boolean useAptGet() {
245+
return pkgMgr == PackageManagerType.APTGET;
246+
}
247+
248+
@SuppressWarnings("unused")
249+
public boolean useZypper() {
250+
return pkgMgr == PackageManagerType.ZYPPER;
251+
}
252+
253+
@SuppressWarnings("unused")
254+
public boolean useApk() {
255+
return pkgMgr == PackageManagerType.APK;
256+
}
257+
255258
@SuppressWarnings("unused")
256259
public String java_home() {
257260
return javaHome;

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -407,36 +407,6 @@ public static boolean isEmptyString(String s) {
407407
return (s == null) || s.isEmpty();
408408
}
409409

410-
/**
411-
* Returns the package manager that should be used to install software on a given Linux OS.
412-
* Defaults to YUM. Known OS's include: ubuntu, debian, opensuse, centos, ol, and rhel.
413-
*
414-
* @param osID identifier for the OS, like ubuntu, debian, rhel, ol, ...
415-
* @return the package manager
416-
*/
417-
public static String getPackageMgrStr(String osID) {
418-
String retVal = Constants.YUM;
419-
if (osID != null) {
420-
osID = osID.replaceAll("[\"]", "");
421-
switch (osID) {
422-
case "ubuntu":
423-
case "debian":
424-
retVal = Constants.APTGET;
425-
break;
426-
case "opensuse":
427-
retVal = Constants.ZYPPER;
428-
break;
429-
case "centos":
430-
case "ol":
431-
case "rhel":
432-
default:
433-
retVal = Constants.YUM;
434-
break;
435-
}
436-
}
437-
return retVal;
438-
}
439-
440410
/**
441411
* Reads the docker image environment variables into Java Properties.
442412
*

imagetool/src/main/resources/ImageTool.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ IMG-0075=Added patch entry [[cyan: {0}]]={1}
7777
IMG-0076=Cache key {0} already exists, remove it first
7878
IMG-0077=Skipping duplicate patch {0}. Patch file already exists in the build context folder. Did you accidentally list the patch twice?
7979
IMG-0078=[[cyan: --type]] is [[brightred: DEPRECATED]] and will be removed in an upcoming release.
80+
IMG-0079=OS Package Manager override, changed from {0} to {1}

0 commit comments

Comments
 (0)