Skip to content

Commit 0304911

Browse files
authored
Support domainHomeSourceType in generated WDT files (#233)
* convert VerrazzanoModels to resourceTemplates to make more generic and support VZ and WKO targets from WDT * remove resourceTemplate/verrazzanoModels from rebase, and prevent resource file update on dryRun
1 parent 9a58b82 commit 0304911

File tree

12 files changed

+174
-108
lines changed

12 files changed

+174
-108
lines changed

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313
import java.util.ArrayList;
14-
import java.util.Collections;
1514
import java.util.List;
1615
import java.util.Properties;
1716
import java.util.regex.Matcher;
@@ -34,7 +33,6 @@
3433
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
3534
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
3635
import com.oracle.weblogic.imagetool.util.Utils;
37-
import com.oracle.weblogic.imagetool.util.VerrazzanoModel;
3836
import picocli.CommandLine.Option;
3937
import picocli.CommandLine.Unmatched;
4038

@@ -48,9 +46,6 @@ public abstract class CommonOptions {
4846
private String tempDirectory = null;
4947
private String nonProxyHosts = null;
5048

51-
private List<Object> resolveOptions = null;
52-
private List<Path> resolveFiles = null;
53-
5449
abstract String getInstallerVersion();
5550

5651
private void handleChown() {
@@ -182,23 +177,6 @@ void init(String buildId) throws Exception {
182177
logger.exiting();
183178
}
184179

185-
List<Path> gatherFiles() {
186-
if (resolveFiles == null) {
187-
resolveFiles = new ArrayList<>();
188-
}
189-
if (verrazzanoModel != null) {
190-
resolveFiles.add(verrazzanoModel);
191-
}
192-
return resolveFiles;
193-
}
194-
195-
List<Object> resolveOptions() {
196-
if (resolveFiles != null && !resolveFiles.isEmpty()) {
197-
resolveOptions = Collections.singletonList(new VerrazzanoModel(imageTag, dockerfileOptions.domain_home()));
198-
}
199-
return resolveOptions;
200-
}
201-
202180
/**
203181
* Returns true if any patches should be applied.
204182
* A PSU is considered a patch.
@@ -566,12 +544,6 @@ String getPassword() {
566544
)
567545
private boolean skipOpatchUpdate = false;
568546

569-
@Option(
570-
names = {"--vzModel"},
571-
description = "For verrazzano, resolve parameters in the verrazzano model with information from the image tool."
572-
)
573-
Path verrazzanoModel;
574-
575547
@Option(
576548
names = {"--packageManager"},
577549
description = "Set the Linux package manager to use for installing OS packages. Default: ${DEFAULT-VALUE}"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public CommandResponse call() throws Exception {
9595
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
9696
"Create_Image.mustache", dockerfileOptions, dryRun);
9797

98-
// resolve parameters in the list of mustache templates returned by gatherFiles()
99-
Utils.writeResolvedFiles(gatherFiles(), resolveOptions());
100-
10198
runDockerCommand(dockerfile, cmdBuilder);
99+
if (!dryRun) {
100+
wdtOptions.handleResourceTemplates(imageTag);
101+
}
102102
} catch (Exception ex) {
103103
logger.fine("**ERROR**", ex);
104104
return new CommandResponse(-1, ex.getMessage());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public CommandResponse call() throws Exception {
163163
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
164164
"Update_Image.mustache", dockerfileOptions, dryRun);
165165

166-
// resolve parameters in the list of mustache templates returned by gatherFiles()
167-
Utils.writeResolvedFiles(gatherFiles(), resolveOptions());
168-
169166
runDockerCommand(dockerfile, cmdBuilder);
167+
if (!dryRun) {
168+
wdtOptions.handleResourceTemplates(imageTag);
169+
}
170170
} catch (Exception ex) {
171171
return new CommandResponse(-1, ex.getMessage());
172172
} finally {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
1818
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
1919
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
20+
import com.oracle.weblogic.imagetool.util.DomainHomeSourceType;
21+
import com.oracle.weblogic.imagetool.util.ResourceTemplateOptions;
2022
import com.oracle.weblogic.imagetool.util.Utils;
2123
import picocli.CommandLine.Option;
2224

@@ -115,6 +117,28 @@ private List<String> addWdtFilesAsList(Path fileArg, String type, String tmpDir)
115117
return fileList;
116118
}
117119

120+
/**
121+
* Resolve variables in the provided list of resource template files.
122+
* See --resourceTemplates. For example, WDT -target vz is used to generate a custom resource.
123+
* In the generated file(s), WDT will not know what the image name is and will leave a placeholder.
124+
* This function provides the values for variables that are known during image build.
125+
*/
126+
public void handleResourceTemplates(String imageName) throws IOException {
127+
DomainHomeSourceType domainType = DomainHomeSourceType.PV;
128+
if (modelOnly()) {
129+
domainType = DomainHomeSourceType.MODEL;
130+
} else if (isUsingWdt()) {
131+
domainType = DomainHomeSourceType.IMAGE;
132+
}
133+
134+
ResourceTemplateOptions options = new ResourceTemplateOptions()
135+
.domainHome(wdtDomainHome)
136+
.imageName(imageName)
137+
.domainHomeSourceType(domainType);
138+
139+
// resolve parameters in the list of mustache templates returned by gatherFiles()
140+
Utils.writeResolvedFiles(resourceTemplates, options);
141+
}
118142

119143
@Option(
120144
names = {"--wdtModel"},
@@ -213,4 +237,11 @@ private List<String> addWdtFilesAsList(Path fileArg, String type, String tmpDir)
213237
description = "path to file the passphrase to decrypt the WDT model"
214238
)
215239
private Path encryptionKeyFile;
240+
241+
@Option(
242+
names = {"--resourceTemplates"},
243+
split = ",",
244+
description = "Resolve variables in the resource template(s) with information from the image tool build."
245+
)
246+
List<Path> resourceTemplates;
216247
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.util;
5+
6+
public enum DomainHomeSourceType {
7+
MODEL("FromModel"),
8+
IMAGE("Image"),
9+
PV("PersistentVolume");
10+
11+
private String value;
12+
13+
/**
14+
* Create an Enum value for the domain home source type.
15+
* @param value value the value to use for substitution in the mustache template.
16+
*/
17+
DomainHomeSourceType(String value) {
18+
this.value = value;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return value;
24+
}
25+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.util;
5+
6+
/**
7+
* Provide backing bean for Mustache templates provided in --resourceTemplates.
8+
*/
9+
public class ResourceTemplateOptions {
10+
private String imageName;
11+
private String domainHome;
12+
private DomainHomeSourceType domainHomeSourceType = DomainHomeSourceType.PV;
13+
14+
15+
/**
16+
* Return the image name value passed in the image --tag argument.
17+
*
18+
* @return image tag name
19+
*/
20+
public String imageName() {
21+
return imageName;
22+
}
23+
24+
/**
25+
* Set the image name value.
26+
*
27+
* @param value image name from --tag argument.
28+
* @return builder/this
29+
*/
30+
public ResourceTemplateOptions imageName(String value) {
31+
imageName = value;
32+
return this;
33+
}
34+
35+
/**
36+
* Return the domain home passed in the --wdtDomainHome argument or the default if not provided.
37+
*
38+
* @return domain home value
39+
*/
40+
public String domainHome() {
41+
return domainHome;
42+
}
43+
44+
/**
45+
* Set the domain home.
46+
*
47+
* @param value the domain home from the --wdtDomainHome argument
48+
* @return builder/this
49+
*/
50+
public ResourceTemplateOptions domainHome(String value) {
51+
domainHome = value;
52+
return this;
53+
}
54+
55+
/**
56+
* Return the domain home type based on WDT inputs provided.
57+
* If --wdtModelOnly, then type is FromModel.
58+
* If not model only but --wdtModel is provided, then Image.
59+
* Else, PersistentVolume (domain in PV).
60+
*
61+
* @return domain home source type
62+
*/
63+
public String domainHomeSourceType() {
64+
return domainHomeSourceType.toString();
65+
}
66+
67+
/**
68+
* Set the domain home source type (FromModel, Image, or PersistentVolume).
69+
*
70+
* @param value FromModel, Image, or PersistentVolume
71+
* @return builder/this
72+
*/
73+
public ResourceTemplateOptions domainHomeSourceType(DomainHomeSourceType value) {
74+
domainHomeSourceType = value;
75+
return this;
76+
}
77+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static String writeDockerfile(String destPath, String template, Dockerfil
219219
* @param options list of option files to resolve the mustache parameters
220220
* @throws IOException if a file in the fileNames is invalid
221221
*/
222-
public static void writeResolvedFiles(List<Path> paths, List<Object> options)
222+
public static void writeResolvedFiles(List<Path> paths, ResourceTemplateOptions options)
223223
throws IOException {
224224
if (paths != null) {
225225
for (Path path : paths) {

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

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

imagetool/src/main/resources/ImageTool.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ IMG-0069=[[yellow: {0} - there are no recommended patches for version {1}]]
7171
IMG-0070=Failed to find latest patches for {0}, version {1}
7272
IMG-0071=WDT Operation is set to UPDATE, but the DOMAIN_HOME environment variable was not defined in the base image, {0}. Did you mean to use "--wdtOperation CREATE" to create a new domain?
7373
IMG-0072=ORACLE_HOME environment variable is not defined in the base image: {0}
74-
IMG-0073=Invalid file {0} listed for Verrazzano model
74+
IMG-0073=Invalid file {0} listed for --resourceTemplates
7575
IMG-0074=OPatch will not be updated, fromImage has version {0}, available version is {1}
7676
IMG-0075=Added patch entry [[cyan: {0}]]={1}
7777
IMG-0076=Cache key {0} already exists, remove it first

imagetool/src/test/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptionsTest.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
2222
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
2323
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
24-
import com.oracle.weblogic.imagetool.util.Utils;
2524
import org.junit.jupiter.api.Tag;
2625
import org.junit.jupiter.api.Test;
2726
import org.junit.jupiter.api.io.TempDir;
2827

2928
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertTrue;
3129
import static org.junit.jupiter.api.Assertions.fail;
3230

3331
@Tag("unit")
@@ -135,41 +133,47 @@ void handleAdditionalBuildCommands(@TempDir File buildDir) throws Exception {
135133
*/
136134
@Test
137135
void testResolveOptions() throws Exception {
138-
CreateImage createImage = new CreateImage();
136+
final String IMG_NAME = "mydomain:latest";
137+
final String DOMAIN_HOME = "/u01/domains/xxxx";
138+
final String DOMAIN_SOURCE = "PersistentVolume";
139139

140-
// accessing private fields normally set by the command line
141-
Field optionsField = CommonOptions.class.getDeclaredField("dockerfileOptions");
142-
optionsField.setAccessible(true);
143-
DockerfileOptions dockerfile = new DockerfileOptions("testbuildid");
144-
optionsField.set(createImage, dockerfile);
140+
WdtOptions wdtOptions = new WdtOptions();
145141

146-
Field imageTagField = CommonOptions.class.getDeclaredField("imageTag");
147-
imageTagField.setAccessible(true);
148-
imageTagField.set(createImage, "mydomain:latest");
149-
150-
Field resolveFilesField = CommonOptions.class.getDeclaredField("resolveFiles");
151-
resolveFilesField.setAccessible(true);
142+
// accessing private fields normally set by the command line
143+
Field resourceTemplatesField = WdtOptions.class.getDeclaredField("resourceTemplates");
144+
resourceTemplatesField.setAccessible(true);
152145
List<Path> resolveFiles =
153146
Arrays.asList(new File("target/test-classes/templates/resolver.yml").toPath(),
154147
new File("target/test-classes/templates/verrazzano.yml").toPath());
155-
resolveFilesField.set(createImage, resolveFiles);
148+
resourceTemplatesField.set(wdtOptions, resolveFiles);
149+
150+
Field imageTagField = WdtOptions.class.getDeclaredField("wdtDomainHome");
151+
imageTagField.setAccessible(true);
152+
imageTagField.set(wdtOptions, DOMAIN_HOME);
156153

157-
Utils.writeResolvedFiles(resolveFiles, createImage.resolveOptions());
154+
wdtOptions.handleResourceTemplates(IMG_NAME);
158155

159156
List<String> linesRead =
160157
Files.readAllLines(new File("target/test-classes/templates/resolver.yml").toPath());
161-
assertEquals(2, linesRead.size(), "Number of lines read from the file was unexpected");
158+
assertEquals(3, linesRead.size(), "Number of lines read from the file was unexpected");
162159
for (String line : linesRead) {
163-
line = line.trim();
164-
if (line.startsWith("domainHome")) {
165-
assertTrue(line.endsWith("/u01/domains/base_domain"), "Invalid domain home value " + line);
166-
} else if (line.startsWith("image")) {
167-
assertTrue(line.endsWith("mydomain:latest"),
168-
"Invalid tag name " + line);
169-
} else {
170-
fail("Unexpected line read " + line);
160+
String[] linePart = line.split(" ");
161+
if (linePart.length != 2) {
162+
fail("Resolving template file, resolver.yml, failed to resolve line: " + line);
163+
}
164+
switch (linePart[0]) {
165+
case "domainHome:":
166+
assertEquals(DOMAIN_HOME, linePart[1], "Invalid domain home value: " + line);
167+
break;
168+
case "image:":
169+
assertEquals(IMG_NAME, linePart[1], "Invalid image name value: " + line);
170+
break;
171+
case "domainHomeSourceType:":
172+
assertEquals(DOMAIN_SOURCE, linePart[1], "Invalid domain home source type: " + line);
173+
break;
174+
default:
175+
fail("Unexpected line in resolver.yml: " + line);
171176
}
172-
173177
}
174178
}
175179
}

0 commit comments

Comments
 (0)