Skip to content

Commit 9f80cb2

Browse files
authored
Use Mustache for Dockerfile template (#33)
1 parent ef38676 commit 9f80cb2

File tree

13 files changed

+483
-406
lines changed

13 files changed

+483
-406
lines changed

imagetool/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<artifactId>junit</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.github.spullara.mustache.java</groupId>
44+
<artifactId>compiler</artifactId>
45+
</dependency>
4246
</dependencies>
4347

4448
<build>

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
import com.oracle.weblogic.imagetool.api.model.InstallerType;
1111
import com.oracle.weblogic.imagetool.api.model.WLSInstallerType;
1212
import com.oracle.weblogic.imagetool.impl.InstallerFile;
13-
import com.oracle.weblogic.imagetool.util.Constants;
14-
import com.oracle.weblogic.imagetool.util.HttpUtil;
15-
import com.oracle.weblogic.imagetool.util.Utils;
13+
import com.oracle.weblogic.imagetool.util.*;
1614
import picocli.CommandLine.Command;
1715
import picocli.CommandLine.Option;
1816

19-
import java.io.File;
20-
import java.io.FileInputStream;
21-
import java.io.IOException;
17+
import java.io.*;
2218
import java.nio.file.Files;
2319
import java.nio.file.Path;
2420
import java.nio.file.Paths;
@@ -28,7 +24,6 @@
2824
import java.util.LinkedList;
2925
import java.util.List;
3026
import java.util.Properties;
31-
import java.util.logging.FileHandler;
3227
import java.util.logging.Logger;
3328
import java.util.stream.Collectors;
3429

@@ -43,6 +38,7 @@ public class CreateImage extends ImageOperation {
4338
private final Logger logger = Logger.getLogger(CreateImage.class.getName());
4439

4540
public CreateImage() {
41+
super();
4642
}
4743

4844
public CreateImage(boolean isCLIMode) {
@@ -77,8 +73,7 @@ public CommandResponse call() throws Exception {
7773

7874
if (fromImage != null && !fromImage.isEmpty()) {
7975
logger.finer("User specified fromImage " + fromImage);
80-
cmdBuilder.add(Constants.BUILD_ARG);
81-
cmdBuilder.add("BASE_IMAGE=" + fromImage);
76+
dockerfileOptions.setBaseImage(fromImage);
8277

8378
tmpDir2 = Files.createTempDirectory(Paths.get(Utils.getBuildWorkingDir()),
8479
"wlsimgbuilder_temp", PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x")));
@@ -102,12 +97,12 @@ public CommandResponse call() throws Exception {
10297
if (useCache != CachePolicy.ALWAYS) {
10398
String pkgMgr = Utils.getPackageMgrStr(baseImageProperties.getProperty("ID", "ol"));
10499
if (!Utils.isEmptyString(pkgMgr)) {
105-
filterStartTags.add(pkgMgr);
100+
dockerfileOptions.setPackageInstaller(pkgMgr);
106101
}
107102
}
108103
} else {
109104
if (useCache != CachePolicy.ALWAYS) {
110-
filterStartTags.add("_YUM");
105+
dockerfileOptions.setPackageInstaller(Constants.YUM);
111106
}
112107
}
113108

@@ -121,7 +116,7 @@ public CommandResponse call() throws Exception {
121116
copyResponseFilesToDir(tmpDirPath);
122117

123118
// Create Dockerfile
124-
Utils.replacePlaceHolders(tmpDirPath + File.separator + "Dockerfile", "/docker-files/Dockerfile.create", filterStartTags, "/docker-files/Dockerfile.ph");
119+
Utils.writeDockerfile(tmpDirPath + File.separator + "Dockerfile", "Create_Image.mustache", dockerfileOptions);
125120

126121
// add directory to pass the context
127122
cmdBuilder.add(tmpDirPath);
@@ -205,7 +200,7 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
205200
retVal.add("RCU_RUN_FLAG=" + "-run_rcu");
206201
}
207202
}
208-
filterStartTags.add("WDT_");
203+
dockerfileOptions.setWdtEnabled();
209204
Path targetLink = Files.copy(wdtModelPath, Paths.get(tmpDirPath, wdtModelPath.getFileName().toString())
210205
);
211206
retVal.add(Constants.BUILD_ARG);

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.oracle.weblogic.imagetool.impl.meta.CacheStoreFactory;
1414
import com.oracle.weblogic.imagetool.util.ARUUtil;
1515
import com.oracle.weblogic.imagetool.util.Constants;
16+
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
1617
import com.oracle.weblogic.imagetool.util.Utils;
1718

1819
import java.io.File;
@@ -36,16 +37,19 @@
3637
public abstract class ImageOperation implements Callable<CommandResponse> {
3738

3839
private final Logger logger = Logger.getLogger(ImageOperation.class.getName());
39-
final List<String> filterStartTags = new ArrayList<>();
40+
// DockerfileOptions provides switches and values to the customize the Dockerfile template
41+
protected DockerfileOptions dockerfileOptions;
4042
protected CacheStore cacheStore = new CacheStoreFactory().get();
4143
private String nonProxyHosts = null;
4244
boolean isCLIMode;
4345
String password;
4446

4547
ImageOperation() {
48+
dockerfileOptions = new DockerfileOptions();
4649
}
4750

4851
ImageOperation(boolean isCLIMode) {
52+
this();
4953
this.isCLIMode = isCLIMode;
5054
}
5155

@@ -119,12 +123,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
119123
if (!patchLocations.isEmpty()) {
120124
retVal.add(Constants.BUILD_ARG);
121125
retVal.add("PATCHDIR=" + tmpDir.relativize(tmpPatchesDir).toString());
122-
filterStartTags.add("PATCH_");
123-
if (this instanceof CreateImage) {
124-
filterStartTags.add("CREATE_PATCH_");
125-
} else if (this instanceof UpdateImage) {
126-
filterStartTags.add("UPDATE_PATCH_");
127-
}
126+
dockerfileOptions.setPatchingEnabled();
128127
}
129128
logger.finer("Exiting ImageOperation.handlePatchFiles");
130129
return retVal;
@@ -182,12 +181,7 @@ void addOPatch1394ToImage(Path tmpDir, String opatchBugNumber) throws Exception
182181
String filePath =
183182
new PatchFile(useCache, Constants.OPATCH_PATCH_TYPE, Constants.OPATCH_PATCH_TYPE, opatchBugNumber, userId, password).resolve(cacheStore);
184183
Files.copy(Paths.get(filePath), Paths.get(tmpDir.toAbsolutePath().toString(), new File(filePath).getName()));
185-
filterStartTags.add("OPATCH_1394");
186-
if (this instanceof CreateImage) {
187-
filterStartTags.add("CREATE_OPATCH_1394");
188-
} else if (this instanceof UpdateImage) {
189-
filterStartTags.add("UPDATE_OPATCH_1394");
190-
}
184+
dockerfileOptions.setOPatchPatchingEnabled();
191185
}
192186

193187
WLSInstallerType installerType = WLSInstallerType.WLS;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class UpdateImage extends ImageOperation {
4141
private final Logger logger = Logger.getLogger(UpdateImage.class.getName());
4242

4343
public UpdateImage() {
44+
super();
4445
}
4546

4647
public UpdateImage(boolean isCLIMode) {
@@ -67,8 +68,7 @@ public CommandResponse call() throws Exception {
6768
if (fromImage == null || fromImage.isEmpty()) {
6869
return new CommandResponse(-1, "update requires a base image. use --fromImage to specify base image");
6970
} else {
70-
cmdBuilder.add(Constants.BUILD_ARG);
71-
cmdBuilder.add("BASE_IMAGE=" + fromImage);
71+
dockerfileOptions.setBaseImage(fromImage);
7272
}
7373

7474
tmpDir2 = Files.createTempDirectory(Paths.get(Utils.getBuildWorkingDir()), "wlsimgbuilder_temp",
@@ -117,7 +117,7 @@ public CommandResponse call() throws Exception {
117117
if (useCache != CachePolicy.ALWAYS) {
118118
String pkgMgr = Utils.getPackageMgrStr(baseImageProperties.getProperty("ID", "ol"));
119119
if (!Utils.isEmptyString(pkgMgr)) {
120-
filterStartTags.add(pkgMgr);
120+
dockerfileOptions.setPackageInstaller(pkgMgr);
121121
}
122122
}
123123

@@ -162,7 +162,7 @@ public CommandResponse call() throws Exception {
162162
cmdBuilder.addAll(handlePatchFiles(tmpDir, tmpPatchesDir));
163163

164164
// create dockerfile
165-
Utils.replacePlaceHolders(tmpDirPath + File.separator + "Dockerfile", "/docker-files/Dockerfile.update", filterStartTags, "/docker-files/Dockerfile.ph");
165+
Utils.writeDockerfile(tmpDirPath + File.separator + "Dockerfile", "/docker-files/Update_Image.mustache", dockerfileOptions);
166166

167167
// add directory to pass the context
168168
cmdBuilder.add(tmpDirPath);

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2-
*
3-
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
4-
*/
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
54
package com.oracle.weblogic.imagetool.util;
65

76
import java.util.List;
@@ -28,8 +27,6 @@ public final class Constants {
2827
public static final String CACHE_DIR_KEY = "cache.dir";
2928
public static final String DEFAULT_WLS_VERSION = "12.2.1.3.0";
3029
public static final String DEFAULT_JDK_VERSION = "8u202";
31-
public static final String WEBLOGIC_IMAGETOOL = "com/oracle/weblogicx/imagetool";
32-
public static final String METADATA_PREF_KEY = "metadata.file";
3330
public static final String DEFAULT_META_FILE = ".metadata";
3431
public static final String DELETE_ALL_FOR_SURE = "deleteAll4Sure";
3532

@@ -38,12 +35,16 @@ public final class Constants {
3835
).collect(Collectors.toList());
3936
public static final String BUILD_ARG = "--build-arg";
4037
public static final String FILE_CACHE = "FILE";
41-
public static final String PREF_CACHE = "PREF";
42-
public static final String CACHE_STORE_TYPE = "cacheStoreType";
4338
public static final String HTTP = "http";
4439
public static final String HTTPS = "https";
4540
public static final String PATCH_ID_REGEX = "^[pP]?(\\d+)";
4641

42+
//Option flags
43+
public static final String YUM = "_YUM";
44+
public static final String APTGET = "_APT";
45+
public static final String ZYPPER = "_SUSE";
46+
47+
4748
private Constants() {
4849
//restrict access
4950
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.util;
5+
6+
public class DockerfileOptions {
7+
8+
boolean use_pkg_installer_yum = false;
9+
boolean use_pkg_installer_apt_get = false;
10+
boolean use_pkg_installer_apk = false;
11+
boolean use_pkg_installer_zypper = false;
12+
13+
private boolean useWdt = false;
14+
private boolean applyPatches = false;
15+
private boolean updateOpatch = false;
16+
private String username = "oracle";
17+
private String groupname = "oracle";
18+
private String javaHome = "/u01/jdk";
19+
private String baseImageName = "oraclelinux:7-slim";
20+
21+
/**
22+
* Options to be used with the Mustache template.
23+
*/
24+
public DockerfileOptions() {
25+
}
26+
27+
/**
28+
* The userid that should own the JDK and FMW install binaries.
29+
*
30+
* @return the userid
31+
*/
32+
public String userid() {
33+
return username;
34+
}
35+
36+
/**
37+
* The userid that should own the JDK and FMW install binaries.
38+
*/
39+
public void setUserId(String value) {
40+
username = value;
41+
}
42+
43+
/**
44+
* The groupid that should own the JDK and FMW install binaries.
45+
*
46+
* @return the groupid
47+
*/
48+
public String groupid() {
49+
return groupname;
50+
}
51+
52+
/**
53+
* The groupid that should own the JDK and FMW install binaries.
54+
*/
55+
public void setGroupId(String value) {
56+
groupname = value;
57+
}
58+
59+
/**
60+
* The base Docker image that the new image should be based on.
61+
*
62+
* @return the image name
63+
*/
64+
public String baseImage() {
65+
return baseImageName;
66+
}
67+
68+
/**
69+
* The base Docker image that the new image should be based on.
70+
*
71+
* @return this DockerfileOptions object
72+
*/
73+
public DockerfileOptions setBaseImage(String value) {
74+
baseImageName = value;
75+
return this;
76+
}
77+
78+
/**
79+
* Only one package installer should be allowed.
80+
*
81+
* @param option the String constant identifying the installer to use.
82+
* @return this DockerfileOptions object
83+
*/
84+
public DockerfileOptions setPackageInstaller(String option) {
85+
use_pkg_installer_zypper = false;
86+
use_pkg_installer_apt_get = false;
87+
use_pkg_installer_apk = false;
88+
use_pkg_installer_yum = false;
89+
switch (option) {
90+
case Constants.YUM:
91+
use_pkg_installer_yum = true;
92+
break;
93+
case Constants.APTGET:
94+
use_pkg_installer_apt_get = true;
95+
break;
96+
case Constants.ZYPPER:
97+
use_pkg_installer_zypper = true;
98+
break;
99+
default:
100+
}
101+
return this;
102+
}
103+
104+
public String java_home() {
105+
return javaHome;
106+
}
107+
108+
/**
109+
* Set the JAVA_HOME environment variable for the Dockerfile to be written.
110+
*
111+
* @param value the folder where JAVA is or should be installed, aka JAVA_HOME.
112+
*/
113+
public void setJavaHome(String value) {
114+
javaHome = value;
115+
}
116+
117+
/**
118+
* Referenced by Dockerfile template, for enabling patching function.
119+
*
120+
* @return true if patching should be performed.
121+
*/
122+
public boolean isPatchingEnabled() {
123+
return applyPatches;
124+
}
125+
126+
/**
127+
* Toggle patching ON.
128+
*
129+
* @return this DockerfileOptions object
130+
*/
131+
public DockerfileOptions setPatchingEnabled() {
132+
applyPatches = true;
133+
return this;
134+
}
135+
136+
/**
137+
* Referenced by Dockerfile template, for enabling OPatch patching function.
138+
*
139+
* @return true if OPatch patching should be performed.
140+
*/
141+
public boolean isOpatchPatchingEnabled() {
142+
return updateOpatch;
143+
}
144+
145+
/**
146+
* Toggle OPatch patching ON.
147+
*
148+
* @return this DockerfileOptions object
149+
*/
150+
public DockerfileOptions setOPatchPatchingEnabled() {
151+
updateOpatch = true;
152+
return this;
153+
}
154+
155+
/**
156+
* Referenced by Dockerfile template, for enabling WDT function.
157+
*
158+
* @return true if WDT domain create should be performed.
159+
*/
160+
public boolean isWdtEnabled() {
161+
return useWdt;
162+
}
163+
164+
/**
165+
* Toggle WDT domain creation ON.
166+
*
167+
* @return this DockerfileOptions object
168+
*/
169+
public DockerfileOptions setWdtEnabled() {
170+
useWdt = true;
171+
return this;
172+
}
173+
}

0 commit comments

Comments
 (0)