Skip to content

Commit 73e8143

Browse files
gosurya-oracleddsharpe
authored andcommitted
Usecache
1 parent bc842a7 commit 73e8143

File tree

11 files changed

+228
-116
lines changed

11 files changed

+228
-116
lines changed

src/main/java/com/oracle/weblogicx/imagebuilder/api/FileResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface FileResolver {
88

99
/**
1010
* Given a cache store, check whether the installer / patch file exists on disk.
11-
* patch files will be downloaded as required.
11+
* files will be downloaded if cachePolicy allows it.
1212
*
1313
* @param cacheStore store that keeps track of required artifacts
1414
* @return location of file on disk if found

src/main/java/com/oracle/weblogicx/imagebuilder/api/model/AbstractFile.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
public abstract class AbstractFile implements FileResolver {
1414

1515
protected String key;
16+
protected CachePolicy cachePolicy;
17+
protected String userId;
18+
protected String password;
1619

17-
public AbstractFile(String key) {
20+
public AbstractFile(String key, CachePolicy cachePolicy, String userId, String password) {
1821
this.key = key;
22+
this.cachePolicy = cachePolicy;
23+
this.userId = userId;
24+
this.password = password;
1925
}
2026

2127
protected boolean isFileOnDisk(String filePath) {

src/main/java/com/oracle/weblogicx/imagebuilder/cli/menu/CreateImage.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
package com.oracle.weblogicx.imagebuilder.cli.menu;
44

5-
import com.oracle.weblogicx.imagebuilder.api.model.CachePolicy;
65
import com.oracle.weblogicx.imagebuilder.api.model.CommandResponse;
76
import com.oracle.weblogicx.imagebuilder.api.model.InstallerType;
87
import com.oracle.weblogicx.imagebuilder.api.model.WLSInstallerType;
@@ -29,6 +28,7 @@
2928
import java.util.stream.Collectors;
3029

3130
import static com.oracle.weblogicx.imagebuilder.api.model.CachePolicy.ALWAYS;
31+
import static com.oracle.weblogicx.imagebuilder.util.Constants.BUILD_ARG;
3232
import static com.oracle.weblogicx.imagebuilder.util.Constants.DEFAULT_JDK_VERSION;
3333
import static com.oracle.weblogicx.imagebuilder.util.Constants.DEFAULT_WLS_VERSION;
3434

@@ -79,7 +79,7 @@ public CommandResponse call() throws Exception {
7979
cmdBuilder.addAll(handleInstallerFiles(tmpDir));
8080

8181
if (fromImage != null && !fromImage.isEmpty()) {
82-
cmdBuilder.add(Constants.BUILD_ARG);
82+
cmdBuilder.add(BUILD_ARG);
8383
cmdBuilder.add("BASE_IMAGE=" + fromImage);
8484

8585
tmpDir2 = Files.createTempDirectory(Paths.get(System.getProperty("user.home")), null);
@@ -124,8 +124,7 @@ public CommandResponse call() throws Exception {
124124
cmdBuilder.add(tmpDirPath);
125125

126126
logger.info("docker cmd = " + String.join(" ", cmdBuilder));
127-
Utils.runDockerCommand(cmdBuilder, dockerLog);
128-
127+
Utils.runDockerCommand(isCLIMode, cmdBuilder, dockerLog);
129128
} catch (Exception ex) {
130129
return new CommandResponse(-1, ex.getMessage());
131130
} finally {
@@ -148,7 +147,7 @@ public CommandResponse call() throws Exception {
148147
* @return list of strings
149148
* @throws Exception in case of error
150149
*/
151-
List<String> handleInstallerFiles(Path tmpDir) throws Exception {
150+
private List<String> handleInstallerFiles(Path tmpDir) throws Exception {
152151
List<String> retVal = new LinkedList<>();
153152
String tmpDirPath = tmpDir.toAbsolutePath().toString();
154153
List<InstallerFile> requiredInstallers = gatherRequiredInstallers();
@@ -188,18 +187,18 @@ private List<String> handleWDTArgsIfRequired(Path tmpDir) throws IOException {
188187
if (Files.isRegularFile(wdtModelPath)) {
189188
filterStartTags.add("WDT_");
190189
Path targetLink = Files.createLink(Paths.get(tmpDirPath, wdtModelPath.getFileName().toString()), wdtModelPath);
191-
retVal.add(Constants.BUILD_ARG);
190+
retVal.add(BUILD_ARG);
192191
retVal.add("WDT_MODEL=" + tmpDir.relativize(targetLink).toString());
193192

194193
if (wdtArchivePath != null && Files.isRegularFile(wdtArchivePath)) {
195194
targetLink = Files.createLink(Paths.get(tmpDirPath, wdtArchivePath.getFileName().toString()), wdtArchivePath);
196-
retVal.add(Constants.BUILD_ARG);
195+
retVal.add(BUILD_ARG);
197196
retVal.add("WDT_ARCHIVE=" + tmpDir.relativize(targetLink).toString());
198197
}
199198

200199
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
201200
targetLink = Files.createLink(Paths.get(tmpDirPath, wdtVariablesPath.getFileName().toString()), wdtVariablesPath);
202-
retVal.add(Constants.BUILD_ARG);
201+
retVal.add(BUILD_ARG);
203202
retVal.add("WDT_VARIABLE=" + tmpDir.relativize(targetLink).toString());
204203
retVal.addAll(getWDTRequiredBuildArgs(wdtVariablesPath));
205204
}
@@ -232,7 +231,7 @@ private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
232231
Constants.REQD_WDT_BUILD_ARGS.contains(((String) x).toUpperCase())
233232
).collect(Collectors.toList());
234233
matchingKeys.forEach(x -> {
235-
retVal.add(Constants.BUILD_ARG);
234+
retVal.add(BUILD_ARG);
236235
retVal.add(((String) x).toUpperCase() + "=" + variableProps.getProperty((String) x));
237236
});
238237
return retVal;
@@ -247,15 +246,14 @@ private List<String> getWDTRequiredBuildArgs(Path wdtVariablesPath) throws IOExc
247246
*/
248247
private List<InstallerFile> gatherRequiredInstallers() throws Exception {
249248
List<InstallerFile> retVal = new LinkedList<>();
250-
retVal.add(new InstallerFile(InstallerType.fromValue(installerType.toString()), installerVersion,
251-
(useCache != ALWAYS), userId, password));
252-
retVal.add(new InstallerFile(InstallerType.JDK, jdkVersion, (useCache != ALWAYS), userId, password));
253249
if (wdtModelPath != null && Files.isRegularFile(wdtModelPath)) {
254-
InstallerFile wdtInstaller = new InstallerFile(InstallerType.WDT, wdtVersion, (useCache != ALWAYS),
255-
null, null);
250+
InstallerFile wdtInstaller = new InstallerFile(useCache, InstallerType.WDT, wdtVersion, null, null);
256251
retVal.add(wdtInstaller);
257252
addWDTURL(wdtInstaller.getKey() + "_url");
258253
}
254+
retVal.add(new InstallerFile(useCache, InstallerType.fromValue(installerType.toString()), installerVersion,
255+
userId, password));
256+
retVal.add(new InstallerFile(useCache, InstallerType.JDK, jdkVersion, userId, password));
259257
return retVal;
260258
}
261259

@@ -266,7 +264,10 @@ private List<InstallerFile> gatherRequiredInstallers() throws Exception {
266264
* @throws Exception in case of error
267265
*/
268266
private void addWDTURL(String wdtURLKey) throws Exception {
269-
if ("latest".equalsIgnoreCase(wdtVersion) || cacheStore.getValueFromCache(wdtURLKey) == null) {
267+
if (cacheStore.getValueFromCache(wdtURLKey) == null) {
268+
if (useCache == ALWAYS) {
269+
throw new Exception("CachePolicy prohibits download. Add the required wdt installer to cache");
270+
}
270271
List<String> wdtTags = HttpUtil.getWDTTags();
271272
String tagToMatch = "latest".equalsIgnoreCase(wdtVersion) ? wdtTags.get(0) : "weblogic-deploy-tooling-" + wdtVersion;
272273
if (wdtTags.contains(tagToMatch)) {
@@ -314,18 +315,6 @@ private void copyResponseFilesToDir(String dirPath) throws IOException {
314315
)
315316
private String jdkVersion;
316317

317-
@Option(
318-
names = {"--useCache"},
319-
paramLabel = "<Cache Policy>",
320-
defaultValue = "always",
321-
description = "Whether to use local cache or download installers.\n" +
322-
"first - try to use cache and download artifacts if required\n" +
323-
"always - default. use cache always and never download artifacts\n" +
324-
"never - never use cache and always download artifacts",
325-
hidden = true
326-
)
327-
private CachePolicy useCache;
328-
329318
@Option(
330319
names = {"--fromImage"},
331320
description = "Docker image to use as base image."

src/main/java/com/oracle/weblogicx/imagebuilder/cli/menu/ImageOperation.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.oracle.weblogicx.imagebuilder.api.FileResolver;
66
import com.oracle.weblogicx.imagebuilder.api.meta.CacheStore;
7+
import com.oracle.weblogicx.imagebuilder.api.model.CachePolicy;
78
import com.oracle.weblogicx.imagebuilder.api.model.CommandResponse;
89
import com.oracle.weblogicx.imagebuilder.api.model.WLSInstallerType;
910
import com.oracle.weblogicx.imagebuilder.impl.PatchFile;
@@ -88,14 +89,14 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
8889
String toPatchesPath = tmpPatchesDir.toAbsolutePath().toString();
8990

9091
if (latestPSU) {
91-
FileResolver psuResolver = new PatchFile(installerType.toString(), installerVersion, null, userId, password);
92+
FileResolver psuResolver = new PatchFile(useCache, installerType.toString(), installerVersion, null, userId, password);
9293
patchLocations.add(psuResolver.resolve(cacheStore));
9394
}
9495
if (patches != null && !patches.isEmpty()) {
9596
for (String patchId : patches) {
9697
if (patchId.matches(PATCH_ID_REGEX)) {
9798
patchId = patchId.substring(1);
98-
patchLocations.add(new PatchFile(installerType.toString(), installerVersion, patchId, userId,
99+
patchLocations.add(new PatchFile(useCache, installerType.toString(), installerVersion, patchId, userId,
99100
password).resolve(cacheStore));
100101
} else {
101102
logger.severe("Ignoring invalid patch id format: " + patchId);
@@ -126,7 +127,7 @@ List<String> handlePatchFiles(Path tmpDir, Path tmpPatchesDir) throws Exception
126127
List<String> getInitialBuildCmd() {
127128

128129
List<String> cmdBuilder = Stream.of("docker", "build",
129-
"--squash", "--force-rm", "--rm=true", "--no-cache", "--network=host").collect(Collectors.toList());
130+
"--squash", "--force-rm", "--rm=true", "--no-cache").collect(Collectors.toList());
130131

131132
cmdBuilder.add("--tag");
132133
cmdBuilder.add(imageTag);
@@ -160,7 +161,7 @@ void handleProxyUrls() throws IOException {
160161
}
161162

162163
void addOPatch1394ToImage(Path tmpDir) throws Exception {
163-
String filePath = new PatchFile("opatch", "13.9.4.0.0", "28186730", userId, password).resolve(cacheStore);
164+
String filePath = new PatchFile(useCache, "opatch", "13.9.4.0.0", "28186730", userId, password).resolve(cacheStore);
164165
Files.createLink(Paths.get(tmpDir.toAbsolutePath().toString(), new File(filePath).getName()),
165166
Paths.get(filePath));
166167
filterStartTags.add("OPATCH_1394");
@@ -197,6 +198,17 @@ FileHandler setupLogger(boolean isCLIMode) {
197198

198199
String installerVersion = Constants.DEFAULT_WLS_VERSION;
199200

201+
@Option(
202+
names = {"--useCache"},
203+
paramLabel = "<Cache Policy>",
204+
defaultValue = "first",
205+
description = "Whether to use local cache or download installers.\n" +
206+
"first - default. try to use cache and download artifacts if required\n" +
207+
"always - use cache always and never download artifacts\n" +
208+
"never - never use cache and always download artifacts"
209+
)
210+
CachePolicy useCache;
211+
200212
@Option(
201213
names = {"--latestPSU"},
202214
description = "Whether to apply patches from latest PSU."

src/main/java/com/oracle/weblogicx/imagebuilder/cli/menu/UpdateImage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public CommandResponse call() throws Exception {
131131
cmdBuilder.add(tmpDirPath);
132132

133133
logger.info("docker cmd = " + String.join(" ", cmdBuilder));
134-
Utils.runDockerCommand(cmdBuilder, dockerLog);
134+
Utils.runDockerCommand(isCLIMode, cmdBuilder, dockerLog);
135135

136136
} catch (Exception ex) {
137137
return new CommandResponse(-1, ex.getMessage());

src/main/java/com/oracle/weblogicx/imagebuilder/impl/InstallerFile.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import com.oracle.weblogicx.imagebuilder.api.meta.CacheStore;
66
import com.oracle.weblogicx.imagebuilder.api.model.AbstractFile;
7+
import com.oracle.weblogicx.imagebuilder.api.model.CachePolicy;
78
import com.oracle.weblogicx.imagebuilder.api.model.InstallerType;
89
import com.oracle.weblogicx.imagebuilder.util.HttpUtil;
910

1011
import java.io.File;
12+
import java.io.IOException;
1113
import java.net.URL;
1214
import java.util.Collections;
1315
import java.util.List;
@@ -20,25 +22,11 @@
2022
*/
2123
public class InstallerFile extends AbstractFile {
2224

23-
private boolean tryToDownload;
24-
private String userId;
25-
private String password;
26-
private InstallerType type = null;
25+
private InstallerType type;
2726
private final Logger logger = Logger.getLogger(InstallerFile.class.getName());
2827

29-
private InstallerFile(String key, boolean tryToDownload) {
30-
super(key);
31-
this.tryToDownload = tryToDownload;
32-
}
33-
34-
public InstallerFile(String key, boolean tryToDownload, String userId, String password) {
35-
this(key, tryToDownload);
36-
this.userId = userId;
37-
this.password = password;
38-
}
39-
40-
public InstallerFile(InstallerType type, String version, boolean tryToDownload, String userId, String password) {
41-
this(type.toString() + CACHE_KEY_SEPARATOR + version, tryToDownload, userId, password);
28+
public InstallerFile(CachePolicy cachePolicy, InstallerType type, String version, String userId, String password) {
29+
super(type.toString() + CACHE_KEY_SEPARATOR + version, cachePolicy, userId, password);
4230
this.type = type;
4331
}
4432

@@ -49,31 +37,41 @@ public InstallerFile(InstallerType type, String version, boolean tryToDownload,
4937
public String resolve(CacheStore cacheStore) throws Exception {
5038
// check entry exists in cache
5139
String filePath = cacheStore.getValueFromCache(key);
52-
// check if the file exists on disk
53-
if (!isFileOnDisk(filePath)) {
54-
if (tryToDownload) {
55-
String urlPath = cacheStore.getValueFromCache(key + "_url");
56-
if (urlPath != null) {
57-
String fileName = new URL(urlPath).getPath();
58-
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
59-
String targetFilePath = cacheStore.getCacheDir() + File.separator + key +
60-
File.separator + fileName;
61-
new File(targetFilePath).getParentFile().mkdirs();
62-
logger.info("Downloading from " + urlPath + " to " + targetFilePath);
63-
HttpUtil.downloadFile(urlPath, targetFilePath, userId, password);
64-
cacheStore.addToCache(key, targetFilePath);
65-
return targetFilePath;
66-
} else {
67-
throw new Exception("Cannot find download link for entry " + key + "_url in cache");
40+
switch (cachePolicy) {
41+
case ALWAYS:
42+
if (!isFileOnDisk(filePath)) {
43+
throw new Exception("CachePolicy prohibits download. Please add cache entry for key: " + key);
6844
}
69-
} else {
70-
//not allowed to download
71-
throw new Exception("CachePolicy prohibits download. Please add cache entry for key: " + key);
72-
}
45+
break;
46+
case FIRST:
47+
if (!isFileOnDisk(filePath)) {
48+
filePath = downloadInstaller(cacheStore);
49+
}
50+
break;
51+
case NEVER:
52+
filePath = downloadInstaller(cacheStore);
53+
break;
7354
}
7455
return filePath;
7556
}
7657

58+
private String downloadInstaller(CacheStore cacheStore) throws IOException {
59+
String urlPath = cacheStore.getValueFromCache(key + "_url");
60+
if (urlPath != null) {
61+
String fileName = new URL(urlPath).getPath();
62+
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
63+
String targetFilePath = cacheStore.getCacheDir() + File.separator + key +
64+
File.separator + fileName;
65+
new File(targetFilePath).getParentFile().mkdirs();
66+
logger.info("Downloading from " + urlPath + " to " + targetFilePath);
67+
HttpUtil.downloadFile(urlPath, targetFilePath, userId, password);
68+
cacheStore.addToCache(key, targetFilePath);
69+
return targetFilePath;
70+
} else {
71+
throw new IOException("Cannot find download link for entry " + key + "_url in cache");
72+
}
73+
}
74+
7775
/**
7876
* Constructs the build-arg required to pass to the docker build
7977
*

0 commit comments

Comments
 (0)