Skip to content

Commit 5f7efe4

Browse files
jshum2479ddsharpe
authored andcommitted
Handle multiple WDT models(#46)
1 parent eb02a09 commit 5f7efe4

File tree

8 files changed

+277
-99
lines changed

8 files changed

+277
-99
lines changed

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

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public CommandResponse call() throws Exception {
131131
} catch (Exception ex) {
132132
return new CommandResponse(-1, ex.getMessage());
133133
} finally {
134-
Utils.deleteFilesRecursively(tmpDir);
134+
if (cleanup) {
135+
Utils.deleteFilesRecursively(tmpDir);
136+
}
135137
}
136138
Instant endTime = Instant.now();
137139
logger.finer("Exiting CreateImage call ");
@@ -217,52 +219,51 @@ private List<String> handleWdtArgsIfRequired(String tmpDir) throws IOException {
217219
logger.finer("Entering CreateImage.handleWdtArgsIfRequired: " + tmpDir);
218220
List<String> retVal = new LinkedList<>();
219221
if (wdtModelPath != null) {
220-
if (Files.isRegularFile(wdtModelPath)) {
221-
if (wdtDomainType != DomainType.WLS) {
222-
if (installerType != WLSInstallerType.FMW) {
223-
throw new IOException("FMW installer is required for JRF domain");
224-
}
225-
retVal.add(Constants.BUILD_ARG);
226-
retVal.add("DOMAIN_TYPE=" + wdtDomainType);
227-
if (runRcu) {
228-
retVal.add(Constants.BUILD_ARG);
229-
retVal.add("RCU_RUN_FLAG=" + "-run_rcu");
230-
}
222+
String[] modelFiles = wdtModelPath.toString().split(",");
223+
List<String> modelList = new ArrayList<>();
224+
225+
for (String modelFile : modelFiles) {
226+
Path modelFilePath = Paths.get(modelFile);
227+
if (Files.isRegularFile(modelFilePath)) {
228+
String modelFilename = modelFilePath.getFileName().toString();
229+
Files.copy(modelFilePath, Paths.get(tmpDir, modelFilename));
230+
modelList.add(modelFilename);
231+
} else {
232+
throw new IOException("WDT model file " + modelFile + " not found");
231233
}
232-
dockerfileOptions.setWdtEnabled();
233-
String modelFilename = wdtModelPath.getFileName().toString();
234-
Files.copy(wdtModelPath, Paths.get(tmpDir, modelFilename));
235-
retVal.add(Constants.BUILD_ARG);
236-
retVal.add("WDT_MODEL=" + modelFilename);
234+
}
235+
dockerfileOptions.setWdtModels(modelList);
237236

238-
if (wdtArchivePath != null && Files.isRegularFile(wdtArchivePath)) {
239-
String archiveFilename = wdtArchivePath.getFileName().toString();
240-
Files.copy(wdtArchivePath, Paths.get(tmpDir, archiveFilename));
241-
retVal.add(Constants.BUILD_ARG);
242-
retVal.add("WDT_ARCHIVE=" + archiveFilename);
237+
if (wdtDomainType != DomainType.WLS) {
238+
if (installerType != WLSInstallerType.FMW) {
239+
throw new IOException("FMW installer is required for JRF domain");
243240
}
244-
245-
if (wdtDomainHome != null) {
241+
retVal.add(Constants.BUILD_ARG);
242+
retVal.add("DOMAIN_TYPE=" + wdtDomainType);
243+
if (runRcu) {
246244
retVal.add(Constants.BUILD_ARG);
247-
retVal.add("DOMAIN_HOME=" + wdtDomainHome);
245+
retVal.add("RCU_RUN_FLAG=" + "-run_rcu");
248246
}
247+
}
248+
dockerfileOptions.setWdtEnabled();
249249

250+
if (wdtArchivePath != null && Files.isRegularFile(wdtArchivePath)) {
251+
String wdtArchiveFilename = wdtArchivePath.getFileName().toString();
252+
Files.copy(wdtArchivePath, Paths.get(tmpDir, wdtArchiveFilename));
253+
retVal.add(Constants.BUILD_ARG);
254+
retVal.add("WDT_ARCHIVE=" + wdtArchiveFilename);
255+
}
256+
if (wdtDomainHome != null) {
257+
retVal.add(Constants.BUILD_ARG);
258+
retVal.add("DOMAIN_HOME=" + wdtDomainHome);
259+
}
250260

251-
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
252-
String variableFileName = wdtVariablesPath.getFileName().toString();
253-
Files.copy(wdtVariablesPath, Paths.get(tmpDir, variableFileName));
254-
retVal.add(Constants.BUILD_ARG);
255-
retVal.add("WDT_VARIABLE=" + variableFileName);
256-
retVal.addAll(getWdtRequiredBuildArgs(wdtVariablesPath));
257-
}
258-
259-
Path tmpScriptsDir = Files.createDirectory(Paths.get(tmpDir, "scripts"));
260-
String toScriptsPath = tmpScriptsDir.toAbsolutePath().toString();
261-
Utils.copyResourceAsFile("/container-scripts/startAdminServer.sh", toScriptsPath, true);
262-
Utils.copyResourceAsFile("/container-scripts/startManagedServer.sh", toScriptsPath, true);
263-
Utils.copyResourceAsFile("/container-scripts/waitForAdminServer.sh", toScriptsPath, true);
264-
} else {
265-
throw new IOException("WDT model file " + wdtModelPath + " not found");
261+
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
262+
String wdtVariableFilename = wdtVariablesPath.getFileName().toString();
263+
Files.copy(wdtVariablesPath, Paths.get(tmpDir, wdtVariableFilename));
264+
retVal.add(Constants.BUILD_ARG);
265+
retVal.add("WDT_VARIABLE=" + wdtVariableFilename);
266+
retVal.addAll(getWdtRequiredBuildArgs(wdtVariablesPath));
266267
}
267268
}
268269
logger.finer("Exiting CreateImage.handleWdtArgsIfRequired: ");
@@ -303,7 +304,7 @@ private List<String> getWdtRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
303304
private List<InstallerFile> gatherRequiredInstallers() throws Exception {
304305
logger.finer("Entering CreateImage.gatherRequiredInstallers: ");
305306
List<InstallerFile> retVal = new LinkedList<>();
306-
if (wdtModelPath != null && Files.isRegularFile(wdtModelPath)) {
307+
if (wdtModelPath != null) {
307308
InstallerFile wdtInstaller = new InstallerFile(useCache, InstallerType.WDT, wdtVersion, null, null);
308309
retVal.add(wdtInstaller);
309310
addWdtUrl(wdtInstaller.getKey());

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ private void handleChown() {
319319
)
320320
Path dockerLog;
321321

322+
323+
@Option(
324+
names = {"--cleanup"},
325+
description = "Cleanup temporary files. Default: ${DEFAULT-VALUE}.",
326+
defaultValue = "true",
327+
hidden = true
328+
)
329+
boolean cleanup;
330+
322331
@Option(
323332
names = {"--chown"},
324333
split = ":",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ public CommandResponse call() throws Exception {
179179
} catch (Exception ex) {
180180
return new CommandResponse(-1, ex.getMessage());
181181
} finally {
182-
Utils.deleteFilesRecursively(tmpDir);
182+
if (cleanup) {
183+
Utils.deleteFilesRecursively(tmpDir);
184+
}
183185
}
184186
Instant endTime = Instant.now();
185187
logger.finer("Exiting UpdateImage.call ");
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.logging;
5+
6+
import java.io.PrintWriter;
7+
import java.io.StringWriter;
8+
import java.text.MessageFormat;
9+
import java.util.Date;
10+
import java.util.logging.Formatter;
11+
import java.util.logging.LogRecord;
12+
import java.util.regex.Pattern;
13+
14+
/**
15+
* This class is a simple log formatting class designed to include the ResourceBundle's MessageKey in the log.
16+
*/
17+
public class FileFormatter extends Formatter {
18+
private static final String CATALOG_KEY_PATTERN_STRING = "^[A-Z]{3,10}?-[0-9]{3,5}?$";
19+
private static final Pattern CATALOG_KEY_PATTERN = Pattern.compile(CATALOG_KEY_PATTERN_STRING);
20+
21+
private static final String DATE_FORMAT_STRING = "####<{0,date} {0,time}>";
22+
// private static final String DATE_FORMAT_STRING = "####<{0,date,yyyy.MM.dd} {0,time,HH:mm:ss}>";
23+
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
24+
25+
private Object[] args;
26+
private MessageFormat formatter;
27+
private Date date;
28+
29+
/**
30+
* The constructor.
31+
*/
32+
public FileFormatter() {
33+
this.date = new Date();
34+
this.args = new Object[1];
35+
this.formatter = new MessageFormat(DATE_FORMAT_STRING);
36+
}
37+
38+
/**
39+
* Formats the log record.
40+
*
41+
* @param record the log record
42+
* @return the formatted log record
43+
*/
44+
@Override
45+
public synchronized String format(LogRecord record) {
46+
StringBuilder sb = new StringBuilder();
47+
48+
date.setTime(record.getMillis());
49+
args[0] = date;
50+
51+
StringBuffer text = new StringBuffer();
52+
formatter.format(args, text, null);
53+
sb.append(text);
54+
55+
// Level
56+
sb.append(" <");
57+
sb.append(record.getLevel().getLocalizedName());
58+
sb.append(">");
59+
60+
// Class name
61+
sb.append(" <");
62+
String source = record.getSourceClassName();
63+
if (source != null) {
64+
sb.append(source.substring(source.lastIndexOf('.') + 1));
65+
} else {
66+
sb.append(record.getLoggerName());
67+
}
68+
sb.append(">");
69+
70+
// Method name
71+
sb.append(" <");
72+
if (record.getSourceMethodName() != null) {
73+
sb.append(record.getSourceMethodName());
74+
}
75+
sb.append(">");
76+
77+
String messageKey = record.getMessage();
78+
String message = formatMessage(record);
79+
80+
if (messageKey != null) {
81+
sb.append(" <");
82+
if (CATALOG_KEY_PATTERN.matcher(messageKey).matches()) {
83+
sb.append(messageKey);
84+
}
85+
sb.append(">");
86+
}
87+
sb.append(" <");
88+
sb.append(message);
89+
if (record.getThrown() != null) {
90+
try {
91+
StringWriter sw = new StringWriter();
92+
PrintWriter pw = new PrintWriter(sw);
93+
record.getThrown().printStackTrace(pw);
94+
pw.close();
95+
sb.append(LINE_SEPARATOR);
96+
sb.append(sw.toString());
97+
sb.append(LINE_SEPARATOR);
98+
} catch (Exception ex) {
99+
//ignore
100+
}
101+
}
102+
sb.append(">");
103+
sb.append(LINE_SEPARATOR);
104+
return sb.toString();
105+
}
106+
}

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

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,48 @@
33

44
package com.oracle.weblogic.imagetool.util;
55

6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.StringJoiner;
10+
611
public class DockerfileOptions {
712

813
boolean useYum = false;
914
boolean useAptGet = false;
1015
boolean useApk = false;
1116
boolean useZypper = false;
1217

13-
private boolean useWdt = false;
14-
private boolean applyPatches = false;
15-
private boolean updateOpatch = false;
16-
private boolean skipJavaInstall = false;
17-
private String username = "oracle";
18-
private String groupname = "oracle";
19-
private String javaHome = "/u01/jdk";
20-
private String baseImageName = "oraclelinux:7-slim";
21-
private String tempDirectory = "/tmp/delme";
18+
private boolean useWdt;
19+
private boolean applyPatches;
20+
private boolean updateOpatch;
21+
private boolean skipJavaInstall;
22+
23+
private String username;
24+
private String groupname;
25+
private String javaHome;
26+
private String tempDirectory;
27+
private String baseImageName;
28+
private ArrayList<String> wdtModelList;
2229

2330
/**
2431
* Options to be used with the Mustache template.
2532
*/
2633
public DockerfileOptions() {
34+
wdtModelList = new ArrayList<>();
35+
36+
useWdt = false;
37+
applyPatches = false;
38+
updateOpatch = false;
39+
skipJavaInstall = false;
40+
41+
username = "oracle";
42+
groupname = "oracle";
43+
44+
javaHome = "/u01/jdk";
45+
tempDirectory = "/tmp/delme";
46+
47+
baseImageName = "oraclelinux:7-slim";
2748
}
2849

2950
/**
@@ -182,6 +203,48 @@ public boolean isWdtEnabled() {
182203
return useWdt;
183204
}
184205

206+
/**
207+
* If WDT is enabled, and the model is not in the archive, the model file argument must be set.
208+
* @param value a model filename, or comma-separated model filenames.
209+
*/
210+
public void setWdtModels(List<String> value) {
211+
if (value != null) {
212+
wdtModelList.addAll(value);
213+
}
214+
}
215+
216+
/**
217+
* Referenced by Dockerfile template, a simple list of model filenames.
218+
*
219+
* @return a list of Strings with the model filenames.
220+
*/
221+
public List<String> wdtModels() {
222+
return wdtModelList;
223+
}
224+
225+
/**
226+
* Referenced by Dockerfile template, provides the WDT argument for 1..n model files.
227+
*
228+
* @return model_file argument for WDT command.
229+
*/
230+
public String wdtModelFileArgument() {
231+
StringJoiner result = new StringJoiner(",","-model_file ","");
232+
result.setEmptyValue("");
233+
for (String model : wdtModelList) {
234+
result.add(tempDirectory + "/" + model);
235+
}
236+
return result.toString();
237+
}
238+
239+
/**
240+
* Referenced by Dockerfile template, provides temporary location to write/copy files in the Docker image.
241+
*
242+
* @return the path to the temporary directory.
243+
*/
244+
public String tmpDir() {
245+
return tempDirectory;
246+
}
247+
185248
/**
186249
* Toggle WDT domain creation ON.
187250
*

0 commit comments

Comments
 (0)