Skip to content

Commit 2da5a21

Browse files
authored
Added OS values to inspect feature (#298)
1 parent a917bff commit 2da5a21

File tree

9 files changed

+149
-38
lines changed

9 files changed

+149
-38
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/inspect/InspectOutput.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,63 @@
33

44
package com.oracle.weblogic.imagetool.inspect;
55

6-
import java.util.ArrayList;
6+
import java.util.AbstractMap;
77
import java.util.Arrays;
88
import java.util.Iterator;
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Properties;
1212
import java.util.TreeMap;
13-
14-
import com.oracle.weblogic.imagetool.util.Utils;
13+
import java.util.stream.Collectors;
1514

1615
/**
1716
* Convert image properties to JSON.
1817
* This class should be replaced if/when a full JSON parser is added to the project.
1918
*/
2019
public class InspectOutput {
21-
private final String patchesKey = "oraclePatches";
20+
private static final String patchesKey = "oraclePatches";
2221
Map<String,String> attributes;
23-
List<PatchJson> patches;
22+
List<InventoryPatch> patches;
23+
OperatingSystemProperties os;
2424

2525
/**
2626
* Convert image properties to JSON output.
2727
* @param imageProperties Properties from the image.
2828
*/
2929
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);
30+
// convert Properties to TreeMap (to sort attributes alphabetically)
31+
Map<String,String> sorted = imageProperties.entrySet().stream()
32+
.map(InspectOutput::convertToStringEntry)
33+
.filter(e -> !e.getKey().equals(patchesKey)) // do not store patches entry as a normal attribute
34+
.filter(e -> !e.getKey().startsWith("__OS__")) // do not store OS entries as a normal attribute
35+
.collect(Collectors.toMap(
36+
Map.Entry::getKey, Map.Entry::getValue,
37+
(v1, v2) -> v1, // discard duplicates, but there shouldn't be any dupes
38+
TreeMap::new)); // use a sorted map
39+
40+
if (imageProperties.containsKey(patchesKey)) {
41+
patches = InventoryPatch.parseInventoryPatches(imageProperties.get(patchesKey).toString());
5242
}
53-
attributes = sortedSet;
43+
44+
attributes = sorted;
45+
os = OperatingSystemProperties.getOperatingSystemProperties(imageProperties);
46+
}
47+
48+
private static Map.Entry<String,String> convertToStringEntry(Map.Entry<Object,Object> entry) {
49+
return new AbstractMap.SimpleEntry<>(entry.getKey().toString(), entry.getValue().toString());
5450
}
5551

5652
@Override
5753
public String toString() {
5854
StringBuilder result = new StringBuilder().append("{\n");
5955
if (patches != null) {
6056
result.append(pad(1)).append('\"').append(patchesKey).append('\"').append(" : [\n");
61-
Iterator<PatchJson> patchesIter = patches.iterator();
57+
Iterator<InventoryPatch> patchesIter = patches.iterator();
6258
while (patchesIter.hasNext()) {
63-
PatchJson patch = patchesIter.next();
59+
InventoryPatch patch = patchesIter.next();
6460
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');
61+
result.append(jsonKeyValuePair(3, "patch", patch.bug())).append(",\n");
62+
result.append(jsonKeyValuePair(3, "description", patch.description())).append('\n');
6763
result.append(pad(2)).append('}');
6864
if (patchesIter.hasNext()) {
6965
result.append(',');
@@ -72,6 +68,14 @@ public String toString() {
7268
}
7369
result.append(pad(1)).append("],\n");
7470
}
71+
if (os != null) {
72+
result.append(pad(1)).append('\"').append("os").append('\"').append(" : {\n");
73+
result.append(jsonKeyValuePair(2, "id", os.id())).append(",\n");
74+
result.append(jsonKeyValuePair(2, "name", os.name())).append(",\n");
75+
result.append(jsonKeyValuePair(2, "version", os.version())).append("\n");
76+
result.append(pad(1)).append("},");
77+
result.append('\n');
78+
}
7579
Iterator<Map.Entry<String,String>> attributeIter = attributes.entrySet().iterator();
7680
while (attributeIter.hasNext()) {
7781
Map.Entry<String,String> entry = attributeIter.next();
@@ -94,10 +98,4 @@ private char[] pad(int size) {
9498
Arrays.fill(result, ' ');
9599
return result;
96100
}
97-
98-
private static class PatchJson {
99-
public String bug;
100-
public String uid;
101-
public String description;
102-
}
103101
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.List;
8+
9+
import com.oracle.weblogic.imagetool.util.Utils;
10+
11+
public class InventoryPatch {
12+
private String bug;
13+
private String uid;
14+
private String description;
15+
16+
public String bug() {
17+
return bug;
18+
}
19+
20+
public String uid() {
21+
return uid;
22+
}
23+
24+
public String description() {
25+
return description;
26+
}
27+
28+
/**
29+
* Parse the provided string into patch objects, and return the list of patches.
30+
* The fields should be separated by a semi-colon with three fields per patch.
31+
* @param patchesString patch data from the inspected image
32+
* @return a list of patch objects
33+
*/
34+
public static List<InventoryPatch> parseInventoryPatches(String patchesString) {
35+
List<InventoryPatch> patches = new ArrayList<>();
36+
if (!Utils.isEmptyString(patchesString)) {
37+
String[] tokens = patchesString.split(";");
38+
for (int i = 0; i < tokens.length; i++) {
39+
InventoryPatch patch = new InventoryPatch();
40+
patch.bug = tokens[i];
41+
if (i++ < tokens.length) {
42+
patch.uid = tokens[i];
43+
}
44+
if (i++ < tokens.length) {
45+
patch.description = tokens[i].replace("\"", "");
46+
}
47+
patches.add(patch);
48+
}
49+
}
50+
return patches;
51+
}
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.Properties;
7+
8+
public class OperatingSystemProperties {
9+
private String id;
10+
private String version;
11+
private String name;
12+
13+
public String id() {
14+
return id;
15+
}
16+
17+
public String version() {
18+
return version;
19+
}
20+
21+
public String name() {
22+
return name;
23+
}
24+
25+
/**
26+
* Using the properties obtained from the image, extract the OS properties prefixed with __OS__.
27+
* @param imageProperties properties returned from the image inspection
28+
* @return the OS property values as an object
29+
*/
30+
public static OperatingSystemProperties getOperatingSystemProperties(Properties imageProperties) {
31+
OperatingSystemProperties result = new OperatingSystemProperties();
32+
result.id = imageProperties.getProperty("__OS__ID").replace("\"", "");
33+
result.version = imageProperties.getProperty("__OS__VERSION").replace("\"", "");
34+
result.name = imageProperties.getProperty("__OS__NAME").replace("\"", "");
35+
return result;
36+
}
37+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ if [ -n "$ORACLE_HOME" ]; then
6060

6161
echo oracleInstalledProducts="$(awk -F\" '{ORS=","} /product-family/ { print $2 }' $ORACLE_HOME/inventory/registry.xml | sed 's/,$//')"
6262
fi
63+
64+
if [ -f "/etc/os-release" ]; then
65+
grep '=' /etc/os-release | sed 's/^/__OS__/'
66+
fi

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ if [ -n "$ORACLE_HOME" ]; then
5757

5858
echo oracleInstalledProducts="$(awk -F\" '{ORS=","} /product-family/ { print $2 }' $ORACLE_HOME/inventory/registry.xml | sed 's/,$//')"
5959
fi
60+
61+
if [ -f "/etc/os-release" ]; then
62+
grep '=' /etc/os-release | sed 's/^/__OS__/'
63+
fi

imagetool/src/test/resources/inspect/image1.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"description" : "One-off"
2222
}
2323
],
24+
"os" : {
25+
"id" : "ol",
26+
"name" : "Oracle Linux Server",
27+
"version" : "7.9"
28+
},
2429
"javaHome" : "/u01/jdk",
2530
"javaVersion" : "1.8.0_202",
2631
"opatchVersion" : "13.9.4.2.5",

imagetool/src/test/resources/inspect/image1.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ opatchVersion=13.9.4.2.5
1010
packageManager=YUM
1111
wlsVersion=12.2.1.3.0
1212
javaHome=/u01/jdk
13+
__OS__NAME="Oracle Linux Server"
14+
__OS__VERSION="7.9"
15+
__OS__ID="ol"

imagetool/src/test/resources/inspect/image2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"oraclePatches" : [
33
],
4+
"os" : {
5+
"id" : "ol",
6+
"name" : "Oracle Linux Server",
7+
"version" : "7.9"
8+
},
49
"domainHome" : "/u01/domains/base_domain",
510
"javaHome" : "/u01/jdk",
611
"javaVersion" : "1.8.0_202",

imagetool/src/test/resources/inspect/image2.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ opatchVersion=13.9.4.2.1
1111
packageManager=YUM
1212
wlsVersion=12.2.1.4.0
1313
javaHome=/u01/jdk
14+
__OS__NAME="Oracle Linux Server"
15+
__OS__VERSION="7.9"
16+
__OS__ID="ol"

0 commit comments

Comments
 (0)