Adding Linux Distribution Version Software Facet

This commit is contained in:
Luca Frosini 2022-03-10 18:14:25 +01:00
parent 39c35b5611
commit 3751ae9796
4 changed files with 180 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -100,7 +101,7 @@ public class HostingNodeManager {
this.containerContext = containerContext;
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}
public HostingNode getHostingNode() {
return hostingNode;
}
@ -264,6 +265,38 @@ public class HostingNodeManager {
return hostingNode;
}
public static SoftwareFacet getOperativeSystem() {
SoftwareFacet osSoftwareFacet = new SoftwareFacetImpl();
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
osSoftwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
osSoftwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
osSoftwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version"));
if(osSoftwareFacet.getGroup().compareTo("Linux")==0) {
// Adding Linux Distribution Info
LinuxDistributionInfo linuxDistributionInfo = new LinuxDistributionInfo();
Map<String, String> map = linuxDistributionInfo.getInfo();
Set<String> keys = map.keySet();
for(String key : keys) {
switch (key) {
case SoftwareFacet.NAME:
case SoftwareFacet.GROUP_PROPERTY:
case SoftwareFacet.VERSION_PROPERTY:
osSoftwareFacet.setAdditionalProperty("Distribution-" + key, map.get(key));
break;
default:
osSoftwareFacet.setAdditionalProperty(key, map.get(key));
break;
}
}
}
return osSoftwareFacet;
}
private HostingNode instantiateHostingNode() {
logger.info("Creating {}", HostingNode.NAME);
@ -295,13 +328,10 @@ public class HostingNodeManager {
hostingNode.addFacet(cpuFacet);
}
SoftwareFacet osSoftwareFacet = new SoftwareFacetImpl();
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
osSoftwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
osSoftwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
osSoftwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version"));
SoftwareFacet osSoftwareFacet = getOperativeSystem();
hostingNode.addFacet(osSoftwareFacet);
SmartGearsConfiguration config = ProviderFactory.provider().smartgearsConfiguration();
SoftwareFacet smartgearsSoftwareFacet = new SoftwareFacetImpl();
smartgearsSoftwareFacet.setGroup("gCube");

View File

@ -0,0 +1,94 @@
package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class LinuxDistributionInfo {
private static final Logger logger = LoggerFactory.getLogger(LinuxDistributionInfo.class);
public static final String LSB_RELEASE_COMMAND = "lsb_release -a";
public static final String OS_RELEASE_FILE_PATH = "/etc/os-release";
protected Map<String, String> info;
protected Map<String, String> getInfoViaLsbReleaseCommand() throws IOException {
logger.trace("Going to exec {}", LSB_RELEASE_COMMAND);
Process process = Runtime.getRuntime().exec(LSB_RELEASE_COMMAND);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Map<String, String> map = parseBufferedReader(bufferedReader);
bufferedReader.close();
return map;
}
private Map<String, String> parseBufferedReader(BufferedReader bufferedReader) throws IOException {
Map<String, String> map = new HashMap<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] nameValue = parseLine(line);
map.put(nameValue[0], nameValue[1]);
}
return map;
}
private String[] parseLine(String line) {
String[] splitted = line.split(":");
if (splitted.length < 2) {
splitted = line.split("=");
}
String[] ret = new String[2];
ret[0] = splitted[0].trim();
ret[1] = splitted[1].trim().replace("\"", "");
return ret;
}
private Map<String, String> getInfoViaFile(File file) throws IOException {
logger.trace("Going to read file {}", file.getAbsolutePath());
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
Map<String, String> map = parseBufferedReader(bufferedReader);
bufferedReader.close();
return map;
}
protected Map<String, String> getInfoViaOsReleaseFile() throws IOException {
File osReleaseFile = new File(OS_RELEASE_FILE_PATH);
return getInfoViaFile(osReleaseFile);
}
private Map<String, String> retriveInfo() {
try {
return getInfoViaLsbReleaseCommand();
} catch (IOException e) {
}
try {
return getInfoViaOsReleaseFile();
}catch (IOException e) {
}
return null;
}
public Map<String, String> getInfo() {
if (info == null) {
info = retriveInfo();
}
return info;
}
}

View File

@ -1,10 +1,14 @@
package org.gcube.smartgears.handler.resourceregistry;
import java.lang.management.ManagementFactory;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.LinuxDistributionInfo;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,7 +18,7 @@ public class HostingNodeManagerTest {
private static final Logger logger = LoggerFactory.getLogger(HostingNodeManagerTest.class);
@Test
public void compare() throws Exception {
public void testOSAndMemInfo() throws Exception {
logger.debug("Runtime.getRuntime().maxMemory() : {}", Runtime.getRuntime().maxMemory());
logger.debug("Runtime.getRuntime().totalMemory() : {}", Runtime.getRuntime().totalMemory());
logger.debug("Runtime.getRuntime().freeMemory() : {}", Runtime.getRuntime().freeMemory());
@ -31,7 +35,7 @@ public class HostingNodeManagerTest {
Object free = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize");
logger.debug("Free Memory : {}", free);
/* Keep this code to performs checks
/* Keep this code to performs checks */
java.lang.management.OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
@SuppressWarnings("restriction")
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
@ -41,7 +45,30 @@ public class HostingNodeManagerTest {
long totalMemory = sunmxbean.getTotalPhysicalMemorySize();
logger.debug("freeMemory : {}", freeMemory);
logger.debug("totalMemory : {}", totalMemory);
*/
logger.debug(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
logger.debug(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
logger.debug(mxbean.getVersion()); // softwareFacet
logger.debug(System.getProperty("os.name"));
logger.debug(System.getProperty("os.arch"));
}
@Test
public void testGetLinuxDistributionInfo() {
LinuxDistributionInfo l = new LinuxDistributionInfo();
Map<String,String> map = l.getInfo();
logger.info("{}", map);
}
@Test
public void testGetOSFacet() {
SoftwareFacet softwareFacet = HostingNodeManager.getOperativeSystem();
logger.info("{}", softwareFacet);
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="TRACE" />
<logger name="org.gcube.smartgears.handler.resourceregistry" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>