diff options
| author | Barry Warsaw <barry@python.org> | 2015-01-26 17:05:21 -0500 | 
|---|---|---|
| committer | Barry Warsaw <barry@python.org> | 2015-01-26 17:05:21 -0500 | 
| commit | fe99f7d6d87e88320933957b2933288c6eb2986d (patch) | |
| tree | 82aff0c8aecaf6e87cfb77aea04d944965bc25f5 /cloudinit/util.py | |
| parent | fabff4aec884467729fc372bb67f240752c15511 (diff) | |
| parent | 78915c97c18d678db10e0fde0d9306823c5f4610 (diff) | |
| download | vyos-cloud-init-fe99f7d6d87e88320933957b2933288c6eb2986d.tar.gz vyos-cloud-init-fe99f7d6d87e88320933957b2933288c6eb2986d.zip  | |
Trunk merged and ported.
Diffstat (limited to 'cloudinit/util.py')
| -rw-r--r-- | cloudinit/util.py | 61 | 
1 files changed, 61 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index d594b611..32c19ba2 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -90,6 +90,9 @@ def encode_text(text, encoding='utf-8'):          return text      return text.encode(encoding) +# Path for DMI Data +DMI_SYS_PATH = "/sys/class/dmi/id" +  class ProcessExecutionError(IOError): @@ -2049,3 +2052,61 @@ def human2bytes(size):          raise ValueError("'%s': cannot be negative" % size_in)      return int(num * mpliers[mplier]) + + +def _read_dmi_syspath(key): +    """ +    Reads dmi data with from /sys/class/dmi/id +    """ + +    dmi_key = "{}/{}".format(DMI_SYS_PATH, key) +    LOG.debug("querying dmi data {}".format(dmi_key)) +    try: +        if not os.path.exists(dmi_key): +            LOG.debug("did not find {}".format(dmi_key)) +            return None + +        key_data = load_file(dmi_key) +        if not key_data: +            LOG.debug("{} did not return any data".format(key)) +            return None + +        LOG.debug("dmi data {} returned {}".format(dmi_key, key_data)) +        return key_data.strip() + +    except Exception as e: +        logexc(LOG, "failed read of {}".format(dmi_key), e) +        return None + + +def _call_dmidecode(key, dmidecode_path): +    """ +    Calls out to dmidecode to get the data out. This is mostly for supporting +    OS's without /sys/class/dmi/id support. +    """ +    try: +        cmd = [dmidecode_path, "--string", key] +        (result, _err) = subp(cmd) +        LOG.debug("dmidecode returned '{}' for '{}'".format(result, key)) +        return result +    except OSError as _err: +        LOG.debug('failed dmidecode cmd: {}\n{}'.format(cmd, _err.message)) +        return None + + +def read_dmi_data(key): +    """ +    Wrapper for reading DMI data. This tries to determine whether the DMI +    Data can be read directly, otherwise it will fallback to using dmidecode. +    """ +    if os.path.exists(DMI_SYS_PATH): +        return _read_dmi_syspath(key) + +    dmidecode_path = which('dmidecode') +    if dmidecode_path: +        return _call_dmidecode(key, dmidecode_path) + +    LOG.warn("did not find either path {} or dmidecode command".format( +             DMI_SYS_PATH)) + +    return None  | 
