~oddbloke/cloud-init/fix-azure

« back to all changes in this revision

Viewing changes to cloudinit/util.py

  • Committer: Scott Moser
  • Date: 2015-01-22 17:14:02 UTC
  • mfrom: (1048.2.3 no_demidecode)
  • Revision ID: smoser@ubuntu.com-20150122171402-hw3ft1nc8iowfuc3
remove dependency on dmidecode on linux

on linux we can read dmi information from /sys rather than using 
dmidecode binary, and thus removing a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
# Helper utils to see if running in a container
73
73
CONTAINER_TESTS = ['running-in-container', 'lxc-is-container']
74
74
 
 
75
# Path for DMI Data
 
76
DMI_SYS_PATH = "/sys/class/dmi/id"
 
77
 
75
78
 
76
79
class ProcessExecutionError(IOError):
77
80
 
2011
2014
        raise ValueError("'%s': cannot be negative" % size_in)
2012
2015
 
2013
2016
    return int(num * mpliers[mplier])
 
2017
 
 
2018
 
 
2019
def _read_dmi_syspath(key):
 
2020
    """
 
2021
    Reads dmi data with from /sys/class/dmi/id
 
2022
    """
 
2023
 
 
2024
    dmi_key = "{}/{}".format(DMI_SYS_PATH, key)
 
2025
    LOG.debug("querying dmi data {}".format(dmi_key))
 
2026
    try:
 
2027
        if not os.path.exists(dmi_key):
 
2028
            LOG.debug("did not find {}".format(dmi_key))
 
2029
            return None
 
2030
 
 
2031
        key_data = load_file(dmi_key)
 
2032
        if not key_data:
 
2033
            LOG.debug("{} did not return any data".format(key))
 
2034
            return None
 
2035
 
 
2036
        LOG.debug("dmi data {} returned {}".format(dmi_key, key_data))
 
2037
        return key_data.strip()
 
2038
 
 
2039
    except Exception as e:
 
2040
        logexc(LOG, "failed read of {}".format(dmi_key), e)
 
2041
        return None
 
2042
 
 
2043
 
 
2044
def _call_dmidecode(key, dmidecode_path):
 
2045
    """
 
2046
    Calls out to dmidecode to get the data out. This is mostly for supporting
 
2047
    OS's without /sys/class/dmi/id support.
 
2048
    """
 
2049
    try:
 
2050
        cmd = [dmidecode_path, "--string", key]
 
2051
        (result, _err) = subp(cmd)
 
2052
        LOG.debug("dmidecode returned '{}' for '{}'".format(result, key))
 
2053
        return result
 
2054
    except OSError, _err:
 
2055
        LOG.debug('failed dmidecode cmd: {}\n{}'.format(cmd, _err.message))
 
2056
        return None
 
2057
 
 
2058
 
 
2059
def read_dmi_data(key):
 
2060
    """
 
2061
    Wrapper for reading DMI data. This tries to determine whether the DMI
 
2062
    Data can be read directly, otherwise it will fallback to using dmidecode.
 
2063
    """
 
2064
    if os.path.exists(DMI_SYS_PATH):
 
2065
        return _read_dmi_syspath(key)
 
2066
 
 
2067
    dmidecode_path = which('dmidecode')
 
2068
    if dmidecode_path:
 
2069
        return _call_dmidecode(key, dmidecode_path)
 
2070
 
 
2071
    LOG.warn("did not find either path {} or dmidecode command".format(
 
2072
             DMI_SYS_PATH))
 
2073
 
 
2074
    return None