Skip to content

Commit a7c2a9a

Browse files
authored
improved inspect patches output format for inspect image (#287)
* improved patches output format for inspect image * added newlines to the end of the test files * per review, removed UID from output and changed attribute name from bug to patch
1 parent 2d60c00 commit a7c2a9a

File tree

11 files changed

+237
-39
lines changed

11 files changed

+237
-39
lines changed

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33

44
package com.oracle.weblogic.imagetool.cli.menu;
55

6-
import java.io.BufferedWriter;
7-
import java.io.OutputStreamWriter;
86
import java.nio.file.Files;
97
import java.nio.file.Path;
108
import java.nio.file.Paths;
11-
import java.util.Map;
129
import java.util.Properties;
13-
import java.util.TreeMap;
1410
import java.util.concurrent.Callable;
1511

16-
import com.github.mustachejava.DefaultMustacheFactory;
17-
import com.github.mustachejava.Mustache;
18-
import com.github.mustachejava.MustacheFactory;
19-
import com.github.mustachejava.util.DecoratedCollection;
2012
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
13+
import com.oracle.weblogic.imagetool.inspect.InspectOutput;
2114
import com.oracle.weblogic.imagetool.util.Utils;
2215
import picocli.CommandLine;
2316

@@ -41,31 +34,9 @@ public CommandResponse call() throws Exception {
4134

4235
Properties baseImageProperties =
4336
Utils.getBaseImageProperties(buildEngine, imageName, scriptToRun, tempDirectory);
44-
MustacheFactory mf = new DefaultMustacheFactory("inspect-responses");
4537

46-
String outputTemplate;
47-
// add additional formats here, the ENUM, and to the resources/inspect-responses folder
48-
switch (outputFormat) {
49-
case JSON:
50-
default:
51-
outputTemplate = "inspect-json.mustache";
52-
}
53-
Mustache mustache = mf.compile(outputTemplate);
38+
System.out.println(new InspectOutput(baseImageProperties));
5439

55-
// sort the output alphabetically for easier readability
56-
TreeMap<String,String> sortedSet = new TreeMap<>();
57-
for (Map.Entry<Object,Object> x: baseImageProperties.entrySet()) {
58-
sortedSet.put(x.getKey().toString(), x.getValue().toString());
59-
}
60-
61-
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out))) {
62-
// create a decorated collection so that the output template can utilize "last" (last collection item)
63-
mustache.execute(out, new Object() {
64-
@SuppressWarnings("unused")
65-
final DecoratedCollection<Map.Entry<String, String>> response =
66-
new DecoratedCollection<>(sortedSet.entrySet());
67-
}).flush();
68-
}
6940
return new CommandResponse(0, "");
7041
}
7142

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2021, Oracle 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.inspect;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Properties;
12+
import java.util.TreeMap;
13+
14+
import com.oracle.weblogic.imagetool.util.Utils;
15+
16+
/**
17+
* Convert image properties to JSON.
18+
* This class should be replaced if/when a full JSON parser is added to the project.
19+
*/
20+
public class InspectOutput {
21+
private final String patchesKey = "oraclePatches";
22+
Map<String,String> attributes;
23+
List<PatchJson> patches;
24+
25+
/**
26+
* Convert image properties to JSON output.
27+
* @param imageProperties Properties from the image.
28+
*/
29+
public InspectOutput(Properties imageProperties) {
30+
Map<String,String> sortedSet = new TreeMap<>();
31+
for (Map.Entry<Object,Object> x: imageProperties.entrySet()) {
32+
sortedSet.put(x.getKey().toString(), x.getValue().toString());
33+
}
34+
if (sortedSet.containsKey(patchesKey)) {
35+
patches = new ArrayList<>();
36+
String patchesValue = sortedSet.get(patchesKey);
37+
if (!Utils.isEmptyString(patchesValue)) {
38+
String[] tokens = patchesValue.split(";");
39+
for (int i = 0; i < tokens.length; i++) {
40+
PatchJson patch = new PatchJson();
41+
patch.bug = tokens[i];
42+
if (i++ < tokens.length) {
43+
patch.uid = tokens[i];
44+
}
45+
if (i++ < tokens.length) {
46+
patch.description = tokens[i].replace("\"", "");
47+
}
48+
patches.add(patch);
49+
}
50+
}
51+
sortedSet.remove(patchesKey);
52+
}
53+
attributes = sortedSet;
54+
}
55+
56+
@Override
57+
public String toString() {
58+
StringBuilder result = new StringBuilder().append("{\n");
59+
if (patches != null) {
60+
result.append(pad(1)).append('\"').append(patchesKey).append('\"').append(" : [\n");
61+
Iterator<PatchJson> patchesIter = patches.iterator();
62+
while (patchesIter.hasNext()) {
63+
PatchJson patch = patchesIter.next();
64+
result.append(pad(2)).append('{').append('\n');
65+
result.append(jsonKeyValuePair(3, "patch", patch.bug)).append(",\n");
66+
result.append(jsonKeyValuePair(3, "description", patch.description)).append('\n');
67+
result.append(pad(2)).append('}');
68+
if (patchesIter.hasNext()) {
69+
result.append(',');
70+
}
71+
result.append('\n');
72+
}
73+
result.append(pad(1)).append("],\n");
74+
}
75+
Iterator<Map.Entry<String,String>> attributeIter = attributes.entrySet().iterator();
76+
while (attributeIter.hasNext()) {
77+
Map.Entry<String,String> entry = attributeIter.next();
78+
result.append(jsonKeyValuePair(1, entry.getKey(), entry.getValue()));
79+
if (attributeIter.hasNext()) {
80+
result.append(',');
81+
}
82+
result.append('\n');
83+
}
84+
result.append(pad(0)).append('}');
85+
return result.toString();
86+
}
87+
88+
private String jsonKeyValuePair(int indent, String key, String value) {
89+
return new String(pad(indent)) + String.format("\"%s\" : \"%s\"", key, value);
90+
}
91+
92+
private char[] pad(int size) {
93+
char[] result = new char[size * 2];
94+
Arrays.fill(result, ' ');
95+
return result;
96+
}
97+
98+
private static class PatchJson {
99+
public String bug;
100+
public String uid;
101+
public String description;
102+
}
103+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ private static HttpRequestRetryHandler retryHandler() {
203203
boolean retriable = !(request instanceof HttpEntityEnclosingRequest);
204204
if (retriable) {
205205
try {
206-
logger.warning("Connect failed, retrying in 10 seconds, attempts={0} ", executionCount);
207-
Thread.sleep(10000);
206+
long waitTime = executionCount < 5 ? 2 : 10;
207+
logger.warning("Connect failed, retrying in {0} seconds, attempts={1} ", waitTime, executionCount);
208+
Thread.sleep(waitTime * 1000);
208209
} catch (InterruptedException e) {
209210
Thread.currentThread().interrupt();
210211
}

imagetool/src/main/resources/inspect-responses/inspect-json.mustache

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

imagetool/src/main/resources/probe-env/inspect-image-long.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ if [ -n "$ORACLE_HOME" ]; then
5252
echo oracleHomeGroup="$(stat -c '%G' "$ORACLE_HOME")"
5353

5454
echo opatchVersion="$($ORACLE_HOME/OPatch/opatch version 2> /dev/null | grep -oE -m 1 '([[:digit:]\.]+)')"
55-
echo oraclePatches="$($ORACLE_HOME/OPatch/opatch lsinventory | awk '{ORS=";"} /^Unique Patch ID/ {print $4} /^Patch description/ {x = substr($0, 21); print x} /^Patch\s*[0-9]+/ {print $2}')"
55+
echo oraclePatches="$($ORACLE_HOME/OPatch/opatch lsinventory | awk '{ORS=";"} /^Unique Patch ID/ {print $4} /^Patch description/ {x = substr($0, 21); print x} /^Patch\s*[0-9]+/ {print $2}' | sed 's/;$//')"
56+
57+
echo oracleInstalledProducts="$(awk -F\" '{ORS=","} /product-family/ { print $2 }' $ORACLE_HOME/inventory/registry.xml | sed 's/,$//')"
5658
fi

imagetool/src/main/resources/probe-env/inspect-image.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ if [ -n "$ORACLE_HOME" ]; then
5050

5151
echo oracleHomeUser="$(stat -c '%U' "$ORACLE_HOME")"
5252
echo oracleHomeGroup="$(stat -c '%G' "$ORACLE_HOME")"
53+
54+
echo oracleInstalledProducts="$(awk -F\" '{ORS=","} /product-family/ { print $2 }' $ORACLE_HOME/inventory/registry.xml | sed 's/,$//')"
5355
fi
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2021, Oracle 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.inspect;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileInputStream;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.StringReader;
12+
import java.util.Properties;
13+
14+
import org.junit.jupiter.api.Tag;
15+
import org.junit.jupiter.api.Test;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNull;
19+
20+
@Tag("unit")
21+
public class InspectTest {
22+
@Test
23+
void testJsonOutput() throws IOException {
24+
testPropertiesToJson("src/test/resources/inspect/image1.properties",
25+
"src/test/resources/inspect/image1.json");
26+
}
27+
28+
@Test
29+
void testEmptyPatches() throws IOException {
30+
testPropertiesToJson("src/test/resources/inspect/image2.properties",
31+
"src/test/resources/inspect/image2.json");
32+
}
33+
34+
void testPropertiesToJson(String propsFile, String jsonFile) throws IOException {
35+
Properties loaded = new Properties();
36+
try (InputStream input = new FileInputStream(propsFile)) {
37+
loaded.load(input);
38+
}
39+
40+
FileReader expected = new FileReader(jsonFile);
41+
StringReader actual = new StringReader(new InspectOutput(loaded).toString());
42+
assertReaders(new BufferedReader(expected), new BufferedReader(actual));
43+
}
44+
45+
private static void assertReaders(BufferedReader expected, BufferedReader actual) throws IOException {
46+
String line;
47+
while ((line = expected.readLine()) != null) {
48+
assertEquals(line, actual.readLine());
49+
}
50+
51+
assertNull(actual.readLine(), "Output had more lines then the expected.");
52+
assertNull(expected.readLine(), "Output had fewer lines then the expected.");
53+
}
54+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"oraclePatches" : [
3+
{
4+
"patch" : "30319071",
5+
"description" : "One-off"
6+
},
7+
{
8+
"patch" : "26355633",
9+
"description" : "One-off"
10+
},
11+
{
12+
"patch" : "26287183",
13+
"description" : "One-off"
14+
},
15+
{
16+
"patch" : "26261906",
17+
"description" : "One-off"
18+
},
19+
{
20+
"patch" : "26051289",
21+
"description" : "One-off"
22+
}
23+
],
24+
"javaHome" : "/u01/jdk",
25+
"javaVersion" : "1.8.0_202",
26+
"opatchVersion" : "13.9.4.2.5",
27+
"oracleHome" : "/u01/oracle",
28+
"oracleHomeGroup" : "oracle",
29+
"oracleHomeUser" : "oracle",
30+
"packageManager" : "YUM",
31+
"wlsVersion" : "12.2.1.3.0"
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2021, Oracle 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+
oraclePatches=30319071;23384603;"One-off";26355633;21447583;"One-off";26287183;21447582;"One-off";26261906;21344506;"One-off";26051289;21455037;"One-off";
5+
oracleHome=/u01/oracle
6+
javaVersion=1.8.0_202
7+
oracleHomeGroup=oracle
8+
oracleHomeUser=oracle
9+
opatchVersion=13.9.4.2.5
10+
packageManager=YUM
11+
wlsVersion=12.2.1.3.0
12+
javaHome=/u01/jdk
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"oraclePatches" : [
3+
],
4+
"domainHome" : "/u01/domains/base_domain",
5+
"javaHome" : "/u01/jdk",
6+
"javaVersion" : "1.8.0_202",
7+
"opatchVersion" : "13.9.4.2.1",
8+
"oracleHome" : "/u01/oracle",
9+
"oracleHomeGroup" : "oracle",
10+
"oracleHomeUser" : "oracle",
11+
"packageManager" : "YUM",
12+
"wlsVersion" : "12.2.1.4.0"
13+
}

0 commit comments

Comments
 (0)