Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -25,6 +26,7 @@
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
import com.oracle.weblogic.imagetool.util.Utils;
import com.oracle.weblogic.imagetool.util.VerrazzanoModel;
import picocli.CommandLine.Option;
import picocli.CommandLine.Unmatched;

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

private List<Object> resolveOptions = null;
private List<Path> resolveFiles = null;

abstract String getInstallerVersion();

private void handleChown() {
Expand Down Expand Up @@ -167,6 +172,23 @@ void init(String buildId) throws Exception {
logger.exiting();
}

List<Path> gatherFiles() {
if (resolveFiles == null) {
resolveFiles = new ArrayList<>();
}
if (verrazzanoModel != null) {
resolveFiles.add(verrazzanoModel);
}
return resolveFiles;
}

List<Object> resolveOptions() {
if (resolveFiles != null && !resolveFiles.isEmpty()) {
resolveOptions = Collections.singletonList(new VerrazzanoModel(imageTag, dockerfileOptions.domain_home()));
}
return resolveOptions;
}

/**
* Returns true if any patches should be applied.
* A PSU is considered a patch.
Expand Down Expand Up @@ -432,6 +454,12 @@ String getPassword() {
)
private boolean skipOpatchUpdate = false;

@Option(
names = {"--vzModel"},
description = "For verrazzano, resolve parameters in the verrazzano model with information from the image tool."
)
Path verrazzanoModel;

@SuppressWarnings("unused")
@Unmatched
List<String> unmatchedOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public CommandResponse call() throws Exception {
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
"Create_Image.mustache", dockerfileOptions, dryRun);

// resolve parameters in the list of mustache templates returned by gatherFiles()
Utils.writeResolvedFiles(gatherFiles(), resolveOptions());

runDockerCommand(dockerfile, cmdBuilder);
} catch (Exception ex) {
logger.fine("**ERROR**", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ public CommandResponse call() throws Exception {
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
"Update_Image.mustache", dockerfileOptions, dryRun);

// resolve parameters in the list of mustache templates returned by gatherFiles()
Utils.writeResolvedFiles(gatherFiles(), resolveOptions());

runDockerCommand(dockerfile, cmdBuilder);
} catch (Exception ex) {
return new CommandResponse(-1, ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,38 @@ public static String writeDockerfile(String destPath, String template, Dockerfil
}
}

/**
* Resolve the parameters in the list of Mustache templates with values passed in command line
* arguments or other values described by the image tool.
* @param paths list of file paths that are mustache templates
* @param options list of option files to resolve the mustache parameters
* @throws IOException if a file in the fileNames is invalid
*/
public static void writeResolvedFiles(List<Path> paths, List<Object> options)
throws IOException {
if (paths != null) {
for (Path path : paths) {
logger.fine("writeResolvedFiles: resolve parameters in file {0}", path);
File directory = path.toFile().getParentFile();
if (directory == null
|| !(Files.exists(path) && Files.isReadable(path) && Files.isWritable(path))) {
throw new IllegalArgumentException(getMessage("IMG-0073", path));
}

MustacheFactory mf = new DefaultMustacheFactory(directory);
Mustache mustache;
try (FileReader fr = new FileReader(path.toFile())) {
mustache = mf.compile(fr, path.getFileName().toString());
}

try (FileWriter fw = new FileWriter(path.toFile())) {
mustache.execute(fw, options).flush();
}
}
}

}

/**
* Executes the given docker command and returns the stdout of the process as properties.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.util;

/**
* Mustache collection of options used to resolve values for the Verrazzano Model
* as provided with the --resolverFiles command line argument.
*/
public class VerrazzanoModel {

private String imageName;
private String domainHome;


/**
* Construct file with the options.
*
* @param imageName name from the image tag argument
* @param domainHome domain home argument or default if no argument
*/
public VerrazzanoModel(String imageName, String domainHome) {
this.imageName = imageName;
this.domainHome = domainHome;
}

/**
* Return the image name value passed in the image tag argument.
*
* @return image tag name
*/
public String imageName() {
return imageName;
}

/**
* Return the domain home passed in the domain home argument or the default if not passed as
* an argument.
*
* @return domain home value
*/
public String domainHome() {
return domainHome;
}
}
1 change: 1 addition & 0 deletions imagetool/src/main/resources/ImageTool.properties
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ IMG-0069=No recommended patches for {0}, version {1}
IMG-0070=Failed to find latest patches for {0}, version {1}
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?
IMG-0072=ORACLE_HOME environment variable is not defined in the base image: {0}
IMG-0073=Invalid file {0} listed for Verrazzano model
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
Expand All @@ -20,11 +21,13 @@
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
import com.oracle.weblogic.imagetool.util.Utils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

@Tag("unit")
Expand Down Expand Up @@ -125,4 +128,48 @@ void handleAdditionalBuildCommands(@TempDir File buildDir) throws Exception {
}
}

/**
* Test resolving options in a list of input files.
*
* @throws Exception if in error or IOException
*/
@Test
void testResolveOptions() throws Exception {
CreateImage createImage = new CreateImage();

// accessing private fields normally set by the command line
Field optionsField = CommonOptions.class.getDeclaredField("dockerfileOptions");
optionsField.setAccessible(true);
DockerfileOptions dockerfile = new DockerfileOptions("testbuildid");
optionsField.set(createImage, dockerfile);

Field imageTagField = CommonOptions.class.getDeclaredField("imageTag");
imageTagField.setAccessible(true);
imageTagField.set(createImage, "mydomain:latest");

Field resolveFilesField = CommonOptions.class.getDeclaredField("resolveFiles");
resolveFilesField.setAccessible(true);
List<Path> resolveFiles =
Arrays.asList(new File("target/test-classes/templates/resolver.yml").toPath(),
new File("target/test-classes/templates/verrazzano.yml").toPath());
resolveFilesField.set(createImage, resolveFiles);

Utils.writeResolvedFiles(resolveFiles, createImage.resolveOptions());

List<String> linesRead =
Files.readAllLines(new File("target/test-classes/templates/resolver.yml").toPath());
assertEquals(2, linesRead.size(), "Number of lines read from the file was unexpected");
for (String line : linesRead) {
line = line.trim();
if (line.startsWith("domainHome")) {
assertTrue(line.endsWith("/u01/domains/base_domain"), "Invalid domain home value " + line);
} else if (line.startsWith("image")) {
assertTrue(line.endsWith("mydomain:latest"),
"Invalid tag name " + line);
} else {
fail("Unexpected line read " + line);
}

}
}
}
2 changes: 2 additions & 0 deletions imagetool/src/test/resources/templates/resolver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
domainHome: {{{domainHome}}}
image: {{{imageName}}}
42 changes: 42 additions & 0 deletions imagetool/src/test/resources/templates/verrazzano.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: verrazzano.io/v1beta1
kind: VerrazzanoModel
metadata:
name: dereks-todo-model
namespace: default
spec:
description: "Derek's Todo System"
weblogicDomains:
- name: todo
adminPort: 32701
t3Port: 32702
domainCRValues:
domainUID: todo
domainHome: {{{domainHome}}}
image: {{{imageName}}}
includeServerOutInPodLog: true
replicas: 1
webLogicCredentialsSecret:
name: todo-weblogic-credentials
imagePullSecrets:
- name: ocir
clusters:
- clusterName: cluster-1
serverPod:
env:
- name: JAVA_OPTIONS
value: "-Dweblogic.StdoutDebugEnabled=false"
- name: USER_MEM_ARGS
value: "-Djava.security.egd=file:/dev/./urandom -Xms64m -Xmx256m "
- name: WL_HOME
value: /u01/oracle/wlserver
- name: MW_HOME
value: /u01/oracle
connections:
- ingress:
- name: todo-ingress
match:
- uri:
prefix: "/todo"
- database:
- target: todo-db
datasourceName: todoDb