Skip to content

Commit c21e10e

Browse files
update Verrazzano model and binding YAML with image metadata (#175)
1 parent 559eaa2 commit c21e10e

File tree

9 files changed

+203
-0
lines changed

9 files changed

+203
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
1212
import java.util.ArrayList;
13+
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.regex.Matcher;
1516
import java.util.regex.Pattern;
@@ -25,6 +26,7 @@
2526
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
2627
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
2728
import com.oracle.weblogic.imagetool.util.Utils;
29+
import com.oracle.weblogic.imagetool.util.VerrazzanoModel;
2830
import picocli.CommandLine.Option;
2931
import picocli.CommandLine.Unmatched;
3032

@@ -36,6 +38,9 @@ public abstract class CommonOptions {
3638
private String tempDirectory = null;
3739
private String nonProxyHosts = null;
3840

41+
private List<Object> resolveOptions = null;
42+
private List<Path> resolveFiles = null;
43+
3944
abstract String getInstallerVersion();
4045

4146
private void handleChown() {
@@ -167,6 +172,23 @@ void init(String buildId) throws Exception {
167172
logger.exiting();
168173
}
169174

175+
List<Path> gatherFiles() {
176+
if (resolveFiles == null) {
177+
resolveFiles = new ArrayList<>();
178+
}
179+
if (verrazzanoModel != null) {
180+
resolveFiles.add(verrazzanoModel);
181+
}
182+
return resolveFiles;
183+
}
184+
185+
List<Object> resolveOptions() {
186+
if (resolveFiles != null && !resolveFiles.isEmpty()) {
187+
resolveOptions = Collections.singletonList(new VerrazzanoModel(imageTag, dockerfileOptions.domain_home()));
188+
}
189+
return resolveOptions;
190+
}
191+
170192
/**
171193
* Returns true if any patches should be applied.
172194
* A PSU is considered a patch.
@@ -432,6 +454,12 @@ String getPassword() {
432454
)
433455
private boolean skipOpatchUpdate = false;
434456

457+
@Option(
458+
names = {"--vzModel"},
459+
description = "For verrazzano, resolve parameters in the verrazzano model with information from the image tool."
460+
)
461+
Path verrazzanoModel;
462+
435463
@SuppressWarnings("unused")
436464
@Unmatched
437465
List<String> unmatchedOptions;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ 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+
98101
runDockerCommand(dockerfile, cmdBuilder);
99102
} catch (Exception ex) {
100103
logger.fine("**ERROR**", ex);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ public CommandResponse call() throws Exception {
152152
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
153153
"Update_Image.mustache", dockerfileOptions, dryRun);
154154

155+
// resolve parameters in the list of mustache templates returned by gatherFiles()
156+
Utils.writeResolvedFiles(gatherFiles(), resolveOptions());
157+
155158
runDockerCommand(dockerfile, cmdBuilder);
156159
} catch (Exception ex) {
157160
return new CommandResponse(-1, ex.getMessage());

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,38 @@ public static String writeDockerfile(String destPath, String template, Dockerfil
210210
}
211211
}
212212

213+
/**
214+
* Resolve the parameters in the list of Mustache templates with values passed in command line
215+
* arguments or other values described by the image tool.
216+
* @param paths list of file paths that are mustache templates
217+
* @param options list of option files to resolve the mustache parameters
218+
* @throws IOException if a file in the fileNames is invalid
219+
*/
220+
public static void writeResolvedFiles(List<Path> paths, List<Object> options)
221+
throws IOException {
222+
if (paths != null) {
223+
for (Path path : paths) {
224+
logger.fine("writeResolvedFiles: resolve parameters in file {0}", path);
225+
File directory = path.toFile().getParentFile();
226+
if (directory == null
227+
|| !(Files.exists(path) && Files.isReadable(path) && Files.isWritable(path))) {
228+
throw new IllegalArgumentException(getMessage("IMG-0073", path));
229+
}
230+
231+
MustacheFactory mf = new DefaultMustacheFactory(directory);
232+
Mustache mustache;
233+
try (FileReader fr = new FileReader(path.toFile())) {
234+
mustache = mf.compile(fr, path.getFileName().toString());
235+
}
236+
237+
try (FileWriter fw = new FileWriter(path.toFile())) {
238+
mustache.execute(fw, options).flush();
239+
}
240+
}
241+
}
242+
243+
}
244+
213245
/**
214246
* Executes the given docker command and returns the stdout of the process as properties.
215247
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
* Mustache collection of options used to resolve values for the Verrazzano Model
8+
* as provided with the --resolverFiles command line argument.
9+
*/
10+
public class VerrazzanoModel {
11+
12+
private String imageName;
13+
private String domainHome;
14+
15+
16+
/**
17+
* Construct file with the options.
18+
*
19+
* @param imageName name from the image tag argument
20+
* @param domainHome domain home argument or default if no argument
21+
*/
22+
public VerrazzanoModel(String imageName, String domainHome) {
23+
this.imageName = imageName;
24+
this.domainHome = domainHome;
25+
}
26+
27+
/**
28+
* Return the image name value passed in the image tag argument.
29+
*
30+
* @return image tag name
31+
*/
32+
public String imageName() {
33+
return imageName;
34+
}
35+
36+
/**
37+
* Return the domain home passed in the domain home argument or the default if not passed as
38+
* an argument.
39+
*
40+
* @return domain home value
41+
*/
42+
public String domainHome() {
43+
return domainHome;
44+
}
45+
}

imagetool/src/main/resources/ImageTool.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ IMG-0069=No recommended patches for {0}, 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

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313
import java.util.ArrayList;
14+
import java.util.Arrays;
1415
import java.util.List;
1516
import java.util.logging.Level;
1617
import java.util.stream.Collectors;
@@ -20,11 +21,13 @@
2021
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
2122
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
2223
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
24+
import com.oracle.weblogic.imagetool.util.Utils;
2325
import org.junit.jupiter.api.Tag;
2426
import org.junit.jupiter.api.Test;
2527
import org.junit.jupiter.api.io.TempDir;
2628

2729
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
2831
import static org.junit.jupiter.api.Assertions.fail;
2932

3033
@Tag("unit")
@@ -125,4 +128,48 @@ void handleAdditionalBuildCommands(@TempDir File buildDir) throws Exception {
125128
}
126129
}
127130

131+
/**
132+
* Test resolving options in a list of input files.
133+
*
134+
* @throws Exception if in error or IOException
135+
*/
136+
@Test
137+
void testResolveOptions() throws Exception {
138+
CreateImage createImage = new CreateImage();
139+
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);
145+
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);
152+
List<Path> resolveFiles =
153+
Arrays.asList(new File("target/test-classes/templates/resolver.yml").toPath(),
154+
new File("target/test-classes/templates/verrazzano.yml").toPath());
155+
resolveFilesField.set(createImage, resolveFiles);
156+
157+
Utils.writeResolvedFiles(resolveFiles, createImage.resolveOptions());
158+
159+
List<String> linesRead =
160+
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");
162+
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);
171+
}
172+
173+
}
174+
}
128175
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
domainHome: {{{domainHome}}}
2+
image: {{{imageName}}}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apiVersion: verrazzano.io/v1beta1
2+
kind: VerrazzanoModel
3+
metadata:
4+
name: dereks-todo-model
5+
namespace: default
6+
spec:
7+
description: "Derek's Todo System"
8+
weblogicDomains:
9+
- name: todo
10+
adminPort: 32701
11+
t3Port: 32702
12+
domainCRValues:
13+
domainUID: todo
14+
domainHome: {{{domainHome}}}
15+
image: {{{imageName}}}
16+
includeServerOutInPodLog: true
17+
replicas: 1
18+
webLogicCredentialsSecret:
19+
name: todo-weblogic-credentials
20+
imagePullSecrets:
21+
- name: ocir
22+
clusters:
23+
- clusterName: cluster-1
24+
serverPod:
25+
env:
26+
- name: JAVA_OPTIONS
27+
value: "-Dweblogic.StdoutDebugEnabled=false"
28+
- name: USER_MEM_ARGS
29+
value: "-Djava.security.egd=file:/dev/./urandom -Xms64m -Xmx256m "
30+
- name: WL_HOME
31+
value: /u01/oracle/wlserver
32+
- name: MW_HOME
33+
value: /u01/oracle
34+
connections:
35+
- ingress:
36+
- name: todo-ingress
37+
match:
38+
- uri:
39+
prefix: "/todo"
40+
- database:
41+
- target: todo-db
42+
datasourceName: todoDb

0 commit comments

Comments
 (0)