Mercurial > hg > release > thermostat-0.9
changeset 75:d7179f3eceb7
use /etc/os-release if possible to obtain distro information
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Wed, 22 Feb 2012 11:35:40 -0500 |
parents | 61fb3c8f88b8 |
children | d6ad2a329ee8 |
files | agent/src/main/java/com/redhat/thermostat/backend/system/DistributionIdentity.java agent/src/main/java/com/redhat/thermostat/backend/system/DistributionInformation.java agent/src/main/java/com/redhat/thermostat/backend/system/DistributionInformationSource.java agent/src/main/java/com/redhat/thermostat/backend/system/EtcOsRelease.java agent/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java agent/src/main/java/com/redhat/thermostat/backend/system/LsbRelease.java agent/src/test/java/com/redhat/thermostat/backend/system/DistributionIdentityTest.java agent/src/test/java/com/redhat/thermostat/backend/system/EtcOsReleaseTest.java agent/src/test/java/com/redhat/thermostat/backend/system/LsbReleaseTest.java |
diffstat | 9 files changed, 487 insertions(+), 131 deletions(-) [+] |
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/DistributionIdentity.java Tue Feb 21 22:17:37 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,124 +0,0 @@ -/* - * Copyright 2012 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.backend.system; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.utils.LoggingUtils; - -/** - * Implementation note: this relies on the {@code lsb_release} program to work. - */ -public class DistributionIdentity { - - public static final String UNKNOWN_NAME = "Unknown Distribution"; - public static final String UNKNOWN_VERSION = "Unknown Version"; - - private static final String DISTRIBUTION_NAME = "distributor id"; - private static final String DISTRIBUTION_VERSION = "release"; - - private static final Logger logger = LoggingUtils.getLogger(DistributionIdentity.class); - - private final String name; - private final String version; - - public DistributionIdentity() { - String tempName = UNKNOWN_NAME; - String tempVersion = UNKNOWN_VERSION; - BufferedReader reader = null; - try { - Process lsbProc = Runtime.getRuntime().exec(new String[] { "lsb_release", "-a" }); - InputStream progOutput = lsbProc.getInputStream(); - reader = new BufferedReader(new InputStreamReader(progOutput)); - String line; - while ((line = reader.readLine()) != null) { - int sepLocation = line.indexOf(":"); - if (sepLocation != -1) { - String key = line.substring(0, sepLocation).toLowerCase(); - if (key.equals(DISTRIBUTION_NAME)) { - tempName = line.substring(sepLocation + 1).trim(); - } else if (key.equals(DISTRIBUTION_VERSION)) { - tempVersion = line.substring(sepLocation + 1).trim(); - } - } - } - int exitValue = lsbProc.waitFor(); - if (exitValue != 0) { - logger.log(Level.WARNING, "unable to identify distribution, problems running 'lsb_release'"); - } - } catch (IOException e) { - logger.log(Level.WARNING, "unable to identify distribution", e); - } catch (InterruptedException e) { - logger.log(Level.WARNING, "unable to identify distribution", e); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - logger.log(Level.WARNING, "unable to close a child's output stream"); - } - } - } - name = tempName; - version = tempVersion; - - logger.log(Level.FINE, "distro-name: " + name); - logger.log(Level.FINE, "distro-version: " + version); - } - - /** - * @return the name of the distribution, or {@link #UNKNOWN_NAME} if it can not be - * identified - */ - public String getName() { - return name; - } - - /** - * @return the release of the distribution or {@link #UNKNOWN_VERSION} if it can not be - * identified - */ - public String getVersion() { - return version; - } - -} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/backend/system/DistributionInformation.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,90 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class DistributionInformation { + + public static final String UNKNOWN_NAME = "Unknown Distribution"; + public static final String UNKNOWN_VERSION = "Unknown Version"; + + private static final Logger logger = LoggingUtils.getLogger(DistributionInformation.class); + + private final String name; + private final String version; + + public DistributionInformation(String name, String version) { + this.name = name; + this.version = version; + } + + public static DistributionInformation get() { + try { + return new EtcOsRelease().getDistributionInformation(); + } catch (IOException e) { + logger.log(Level.WARNING, "unable to use os-release", e); + } + try { + return new LsbRelease().getDistributionInformation(); + } catch (IOException e) { + logger.log(Level.WARNING, "unable to use lsb_release", e); + } + return new DistributionInformation(UNKNOWN_NAME, UNKNOWN_VERSION); + } + + /** + * @return the name of the distribution, or {@link #UNKNOWN_NAME} if it can not be + * identified + */ + public String getName() { + return name; + } + + /** + * @return the release of the distribution or {@link #UNKNOWN_VERSION} if it can not be + * identified + */ + public String getVersion() { + return version; + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/backend/system/DistributionInformationSource.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,45 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import java.io.IOException; + +public interface DistributionInformationSource { + + public DistributionInformation getDistributionInformation() throws IOException; + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/backend/system/EtcOsRelease.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,107 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class EtcOsRelease implements DistributionInformationSource { + + private static final Logger logger = LoggingUtils.getLogger(EtcOsRelease.class); + + private static final String OS_RELEASE = "/etc/os-release"; + + @Override + public DistributionInformation getDistributionInformation() throws IOException { + return getFromOsRelease(); + } + + public DistributionInformation getFromOsRelease() throws IOException { + return getFromOsRelease(OS_RELEASE); + } + + public DistributionInformation getFromOsRelease(String releaseFile) throws IOException { + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(releaseFile)); + return getFromOsRelease(reader); + } finally { + try { + if (reader != null) { + reader.close(); + } + } catch (IOException e) { + logger.log(Level.WARNING, "unable to close input stream", e); + } + } + } + + public DistributionInformation getFromOsRelease(BufferedReader reader) throws IOException { + String name = "Linux"; + String version = DistributionInformation.UNKNOWN_VERSION; + String line = null; + while ((line = reader.readLine()) != null) { + if (line.startsWith("NAME=")) { + name = readShellVariable("NAME", line); + } + if (line.startsWith("VERSION=")) { + version = readShellVariable("VERSION", line); + } + } + return new DistributionInformation(name, version); + } + + /** Reads and parses a shell variable declaration: {@code FOO="bar"} + * + * @return the value of the shell variable + */ + private String readShellVariable(String variableName, String line) { + // TODO we should try to handle shell quotes better + String result = line.substring((variableName + "=").length()); + if (result.startsWith("\"") && result.endsWith("\"")) { + result = result.substring(1, result.length()-1); + } + return result; + } + + +}
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java Tue Feb 21 22:17:37 2012 +0100 +++ b/agent/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java Wed Feb 22 11:35:40 2012 -0500 @@ -65,7 +65,7 @@ hostname = Constants.AGENT_LOCAL_HOSTNAME; } logger.log(Level.FINEST, "hostname: " + hostname); - DistributionIdentity identifier = new DistributionIdentity(); + DistributionInformation identifier = DistributionInformation.get(); String osName = identifier.getName() + " " + identifier.getVersion(); logger.log(Level.FINEST, "osName: " + osName);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/backend/system/LsbRelease.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,111 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class LsbRelease implements DistributionInformationSource { + + private static final Logger logger = LoggingUtils.getLogger(LsbRelease.class); + + private static final String DISTRIBUTION_NAME = "distributor id"; + private static final String DISTRIBUTION_VERSION = "release"; + + @Override + public DistributionInformation getDistributionInformation() + throws IOException { + return getFromLsbRelease(); + } + + public DistributionInformation getFromLsbRelease() throws IOException { + + BufferedReader reader = null; + try { + Process lsbProc = Runtime.getRuntime().exec(new String[] { "lsb_release", "-a" }); + InputStream progOutput = lsbProc.getInputStream(); + reader = new BufferedReader(new InputStreamReader(progOutput)); + DistributionInformation result = getFromLsbRelease(reader); + int exitValue = lsbProc.waitFor(); + if (exitValue != 0) { + logger.log(Level.WARNING, "unable to identify distribution, problems running 'lsb_release'"); + } + return result; + } catch (InterruptedException e) { + throw new IOException(e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + logger.log(Level.WARNING, "unable to close a child's output stream"); + } + } + } + + } + + public DistributionInformation getFromLsbRelease(BufferedReader reader) throws IOException { + String name = DistributionInformation.UNKNOWN_NAME; + String version = DistributionInformation.UNKNOWN_VERSION; + + String line; + while ((line = reader.readLine()) != null) { + int sepLocation = line.indexOf(":"); + if (sepLocation != -1) { + String key = line.substring(0, sepLocation).toLowerCase(); + if (key.equals(DISTRIBUTION_NAME)) { + name = line.substring(sepLocation + 1).trim(); + } else if (key.equals(DISTRIBUTION_VERSION)) { + version = line.substring(sepLocation + 1).trim(); + } + } + } + + logger.log(Level.FINE, "distro-name: " + name); + logger.log(Level.FINE, "distro-version: " + version); + + return new DistributionInformation(name, version); + } + +}
--- a/agent/src/test/java/com/redhat/thermostat/backend/system/DistributionIdentityTest.java Tue Feb 21 22:17:37 2012 +0100 +++ b/agent/src/test/java/com/redhat/thermostat/backend/system/DistributionIdentityTest.java Wed Feb 22 11:35:40 2012 -0500 @@ -47,24 +47,24 @@ @Test public void testName() { if (TestUtils.isLinux()) { - DistributionIdentity identifier = new DistributionIdentity(); - String name = identifier.getName(); + DistributionInformation info = DistributionInformation.get(); + String name = info.getName(); assertNotNull(name); assertTrue(name.length() > 0); assertFalse(name.startsWith(":")); - assertFalse(name.equals(DistributionIdentity.UNKNOWN_NAME)); + assertFalse(name.equals(DistributionInformation.UNKNOWN_NAME)); } } @Test public void testVersion() { if (TestUtils.isLinux()) { - DistributionIdentity identifier = new DistributionIdentity(); - String version = identifier.getVersion(); + DistributionInformation info = DistributionInformation.get(); + String version = info.getVersion(); assertNotNull(version); assertTrue(version.length()> 0); assertFalse(version.startsWith(":")); - assertFalse(version.equals(DistributionIdentity.UNKNOWN_VERSION)); + assertFalse(version.equals(DistributionInformation.UNKNOWN_VERSION)); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/test/java/com/redhat/thermostat/backend/system/EtcOsReleaseTest.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,64 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; + +import org.junit.Test; + +public class EtcOsReleaseTest { + + @Test + public void testName() throws IOException, InterruptedException { + BufferedReader reader = new BufferedReader(new StringReader("NAME=\"Name\"\n")); + DistributionInformation info = new EtcOsRelease().getFromOsRelease(reader); + assertEquals("Name", info.getName()); + } + + + @Test + public void testVersion() throws IOException { + BufferedReader reader = new BufferedReader(new StringReader("VERSION=\"Version\"\n")); + DistributionInformation info = new EtcOsRelease().getFromOsRelease(reader); + assertEquals("Version", info.getVersion()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/test/java/com/redhat/thermostat/backend/system/LsbReleaseTest.java Wed Feb 22 11:35:40 2012 -0500 @@ -0,0 +1,63 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.backend.system; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; + +import org.junit.Test; + +public class LsbReleaseTest { + + @Test + public void testName() throws IOException, InterruptedException { + BufferedReader reader = new BufferedReader(new StringReader("Distributor ID: Name")); + DistributionInformation info = new LsbRelease().getFromLsbRelease(reader); + assertEquals("Name", info.getName()); + } + + @Test + public void testVersion() throws IOException { + BufferedReader reader = new BufferedReader(new StringReader("Release: Version")); + DistributionInformation info = new LsbRelease().getFromLsbRelease(reader); + assertEquals("Version", info.getVersion()); + } + +}