Skip to content

Commit 065c424

Browse files
gs: still working on it
Former-commit-id: c22476e5a6ad035633301a4116f0a686df89359f
1 parent 19793f2 commit 065c424

File tree

16 files changed

+375
-32
lines changed

16 files changed

+375
-32
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<maven.compiler.target>1.8</maven.compiler.target>
1414
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
1515
<shade-plugin-version>3.2.1</shade-plugin-version>
16-
<JAVA8_HOME></JAVA8_HOME>
1716
</properties>
1817

1918
<dependencies>
@@ -32,6 +31,11 @@
3231
<artifactId>picocli</artifactId>
3332
<version>3.8.0</version>
3433
</dependency>
34+
<dependency>
35+
<groupId>com.google.dagger</groupId>
36+
<artifactId>dagger</artifactId>
37+
<version>2.19</version>
38+
</dependency>
3539
</dependencies>
3640

3741
<build>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.api;
2+
3+
import com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver;
4+
5+
public interface FileResolver {
6+
7+
String resolve(MetaDataResolver metaDataResolver) throws Exception;
8+
9+
}

src/main/java/com/oracle/weblogicx/imagebuilder/builder/api/meta/MetaDataResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
package com.oracle.weblogicx.imagebuilder.builder.api.meta;
44

55
import java.util.Map;
6-
import java.util.Optional;
76

87
/**
98
* This is the interface that helps keep track of application metadata like
109
* which patches have been downloaded and their location on disk.
1110
*/
1211
public interface MetaDataResolver {
1312

13+
String CACHE_KEY_SEPARATOR = "_";
14+
1415
/**
1516
* Cache dir used by this application. cache dir is where the application downloads
1617
* artifacts to.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.api.model;
2+
3+
import com.oracle.weblogicx.imagebuilder.builder.api.FileResolver;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
8+
public abstract class AbstractFile implements FileResolver {
9+
10+
protected String key;
11+
12+
public AbstractFile(String key) {
13+
this.key = key;
14+
}
15+
16+
protected boolean isFileOnDisk(String filePath) {
17+
return filePath != null && Files.isRegularFile(Paths.get(filePath));
18+
}
19+
}

src/main/java/com/oracle/weblogicx/imagebuilder/builder/cli/CLIDriver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.oracle.weblogicx.imagebuilder.builder.cli;
22

33
import com.oracle.weblogicx.imagebuilder.builder.api.model.CommandResponse;
4-
import com.oracle.weblogicx.imagebuilder.builder.cli.build.BuildWLSImage;
4+
import com.oracle.weblogicx.imagebuilder.builder.cli.build.CreateImage;
5+
import com.oracle.weblogicx.imagebuilder.builder.cli.build.UpdateImage;
56
import com.oracle.weblogicx.imagebuilder.builder.cli.cache.CacheCLI;
67
import picocli.CommandLine;
78
import picocli.CommandLine.Command;
@@ -15,15 +16,16 @@
1516
import static com.oracle.weblogicx.imagebuilder.builder.util.ARUConstants.CLI_OPTION;
1617

1718
@Command(
18-
name = "builder",
19+
name = "imagebuilder",
1920
mixinStandardHelpOptions = true,
2021
description = "%nImageBuilder is a tool to help build docker images of WebLogic with selected " +
2122
"patches and/or psu(s) applied.%n",
2223
version = "1.0",
2324
sortOptions = false,
2425
subcommands = {
2526
CacheCLI.class,
26-
BuildWLSImage.class,
27+
CreateImage.class,
28+
UpdateImage.class,
2729
HelpCommand.class
2830
},
2931
requiredOptionMarker = '*',
@@ -45,7 +47,7 @@ public static void main(String[] args) {
4547
} else {
4648
List<String> argsList = Stream.of(args).collect(Collectors.toList());
4749
argsList.add(CLI_OPTION);
48-
// CommandLine commandLine = new CommandLine(new BuildWLSImage())
50+
// CommandLine commandLine = new CommandLine(new CreateImage())
4951
// .setCaseInsensitiveEnumValuesAllowed(true)
5052
// .setUsageHelpWidth(120);
5153
//

src/main/java/com/oracle/weblogicx/imagebuilder/builder/cli/cache/AddEntry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public class AddEntry implements Callable<CommandResponse> {
3333

3434
@Option(
3535
names = {"--path"},
36-
description = "Value for the cache entry. A file path on the local disk",
36+
description = "Value for the cache entry",
3737
required = true
3838
)
39-
private Path location;
39+
private String location;
4040

4141
@Unmatched
4242
List<String> unmatcheOptions;
@@ -46,15 +46,15 @@ public class AddEntry implements Callable<CommandResponse> {
4646

4747
@Override
4848
public CommandResponse call() throws Exception {
49-
if (key != null && !key.isEmpty() && location != null && Files.exists(location)) {
49+
if (key != null && !key.isEmpty() && location != null && !location.isEmpty()) {
5050
String oldValue = META_RESOLVER.getValueFromCache(key);
5151
String msg;
5252
if (oldValue != null) {
5353
msg = String.format("Replaced old value %s with new value %s for key %s", oldValue, location, key);
5454
} else {
5555
msg = String.format("Added entry %s=%s", key, location);
5656
}
57-
if (META_RESOLVER.addToCache(key, location.toAbsolutePath().toString())) {
57+
if (META_RESOLVER.addToCache(key, location)) {
5858
return new CommandResponse(0, msg);
5959
} else {
6060
return new CommandResponse(-1, "Command Failed");

src/main/java/com/oracle/weblogicx/imagebuilder/builder/cli/cache/AddInstallerEntry.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.oracle.weblogicx.imagebuilder.builder.api.model.CommandResponse;
44
import com.oracle.weblogicx.imagebuilder.builder.api.model.InstallerType;
5-
import picocli.CommandLine;
65
import picocli.CommandLine.Command;
76
import picocli.CommandLine.Option;
87
import picocli.CommandLine.Unmatched;
@@ -13,7 +12,7 @@
1312
import java.util.concurrent.Callable;
1413

1514
import static com.oracle.weblogicx.imagebuilder.builder.impl.meta.FileMetaDataResolver.META_RESOLVER;
16-
import static com.oracle.weblogicx.imagebuilder.builder.util.ARUConstants.CACHE_KEY_SEPARATOR;
15+
import static com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver.CACHE_KEY_SEPARATOR;
1716
import static com.oracle.weblogicx.imagebuilder.builder.util.ARUConstants.DEFAULT_WLS_VERSION;
1817

1918
@Command(

src/main/java/com/oracle/weblogicx/imagebuilder/builder/cli/cache/AddPatchEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.concurrent.Callable;
1919

2020
import static com.oracle.weblogicx.imagebuilder.builder.impl.meta.FileMetaDataResolver.META_RESOLVER;
21-
import static com.oracle.weblogicx.imagebuilder.builder.util.ARUConstants.CACHE_KEY_SEPARATOR;
21+
import static com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver.CACHE_KEY_SEPARATOR;
2222
import static com.oracle.weblogicx.imagebuilder.builder.util.ARUConstants.DEFAULT_WLS_VERSION;
2323

2424
@Command(
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.impl;
2+
3+
import com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver;
4+
import com.oracle.weblogicx.imagebuilder.builder.api.model.AbstractFile;
5+
import com.oracle.weblogicx.imagebuilder.builder.api.model.InstallerType;
6+
import com.oracle.weblogicx.imagebuilder.builder.util.HttpUtil;
7+
8+
import java.io.File;
9+
import java.net.URL;
10+
import java.util.Objects;
11+
12+
import static com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver.CACHE_KEY_SEPARATOR;
13+
14+
public class InstallerFile extends AbstractFile {
15+
16+
private boolean tryToDownload;
17+
private String userId;
18+
private String password;
19+
20+
public InstallerFile(String key, boolean tryToDownload) {
21+
super(key);
22+
this.tryToDownload = tryToDownload;
23+
}
24+
25+
public InstallerFile(String key, boolean tryToDownload, String userId, String password) {
26+
this(key, tryToDownload);
27+
Objects.requireNonNull(userId, "userId cannot be null");
28+
Objects.requireNonNull(password, "password cannot be null");
29+
this.userId = userId;
30+
this.password = password;
31+
}
32+
33+
public InstallerFile(InstallerType type, String version, boolean tryToDownload, String userId, String password) {
34+
this(type.toString() + CACHE_KEY_SEPARATOR + version, tryToDownload, userId, password);
35+
}
36+
37+
@Override
38+
public String resolve(MetaDataResolver metaDataResolver) throws Exception {
39+
// check entry exists in cache
40+
String filePath = metaDataResolver.getValueFromCache(key);
41+
// check if the file exists on disk
42+
if (!isFileOnDisk(filePath)) {
43+
if (tryToDownload) {
44+
String urlPath = metaDataResolver.getValueFromCache(key + "_url");
45+
if (urlPath != null) {
46+
String fileName = new URL(urlPath).getPath();
47+
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
48+
String targetFilePath = metaDataResolver.getCacheDir() + File.separator + fileName;
49+
System.out.println("1. Downloading from " + urlPath + " to " + targetFilePath);
50+
HttpUtil.downloadFile(urlPath, targetFilePath, userId, password);
51+
metaDataResolver.addToCache(key, targetFilePath);
52+
return targetFilePath;
53+
} else {
54+
throw new Exception("Cannot find download link for entry " + key + "_url in cache");
55+
}
56+
} else {
57+
//not allowed to download
58+
throw new Exception("CachePolicy prohibits download. Please add cache entry for key: " + key);
59+
}
60+
}
61+
return filePath;
62+
}
63+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.oracle.weblogicx.imagebuilder.builder.impl;
2+
3+
import com.oracle.weblogicx.imagebuilder.builder.api.meta.MetaDataResolver;
4+
import com.oracle.weblogicx.imagebuilder.builder.api.model.AbstractFile;
5+
import com.oracle.weblogicx.imagebuilder.builder.api.model.WLSInstallerType;
6+
import com.oracle.weblogicx.imagebuilder.builder.util.ARUUtil;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Objects;
13+
14+
public class PatchFile extends AbstractFile {
15+
16+
private String userId;
17+
private String password;
18+
private String patchId;
19+
private WLSInstallerType type;
20+
private String version;
21+
22+
public PatchFile(String key) {
23+
super(key);
24+
}
25+
26+
private PatchFile(String key, String userId, String password) {
27+
this(key);
28+
this.userId = userId;
29+
this.password = password;
30+
}
31+
32+
public PatchFile(WLSInstallerType type, String version, String patchId, String userId, String password) {
33+
super(null);
34+
Objects.requireNonNull(userId, "userId cannot be null");
35+
Objects.requireNonNull(password, "password cannot be null");
36+
this.type = type;
37+
this.version = version;
38+
this.patchId = patchId;
39+
this.userId = userId;
40+
this.password = password;
41+
}
42+
43+
@Override
44+
public String resolve(MetaDataResolver metaDataResolver) throws Exception {
45+
if (patchId == null || patchId.isEmpty()) {
46+
//this implies get latest psu
47+
this.key = ARUUtil.getLatestPSUFor(type.toString(), version, userId, password);
48+
} else {
49+
List<String> patches = ARUUtil.getPatchesFor(type.toString(), version, Collections.singletonList(patchId),
50+
userId, password);
51+
if (patches != null && !patches.isEmpty()) {
52+
this.key = patches.get(0);
53+
}
54+
}
55+
56+
if (this.key == null || this.key.isEmpty()) {
57+
if (this.patchId == null || this.patchId.isEmpty()) {
58+
throw new Exception(String.format("Failed to find latest psu for product type %s, version %s",
59+
this.type, this.version));
60+
} else {
61+
throw new Exception(String.format("Failed to find patch %s for product type %s, version %s",
62+
this.patchId, this.type, this.version));
63+
}
64+
} else {
65+
String filePath = metaDataResolver.getValueFromCache(this.key);
66+
if (filePath == null || !Files.isRegularFile(Paths.get(filePath))) {
67+
throw new Exception(String.format(
68+
"File doesn't exist at location %s for product type %s, version %s, patch %s", filePath,
69+
this.type, this.version, this.patchId));
70+
}
71+
return filePath;
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)