summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-26 17:05:21 -0500
committerBarry Warsaw <barry@python.org>2015-01-26 17:05:21 -0500
commitfe99f7d6d87e88320933957b2933288c6eb2986d (patch)
tree82aff0c8aecaf6e87cfb77aea04d944965bc25f5 /cloudinit
parentfabff4aec884467729fc372bb67f240752c15511 (diff)
parent78915c97c18d678db10e0fde0d9306823c5f4610 (diff)
downloadvyos-cloud-init-fe99f7d6d87e88320933957b2933288c6eb2986d.tar.gz
vyos-cloud-init-fe99f7d6d87e88320933957b2933288c6eb2986d.zip
Trunk merged and ported.
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/sources/DataSourceAltCloud.py27
-rw-r--r--cloudinit/sources/DataSourceCloudSigma.py22
-rw-r--r--cloudinit/sources/DataSourceSmartOS.py25
-rw-r--r--cloudinit/util.py61
4 files changed, 85 insertions, 50 deletions
diff --git a/cloudinit/sources/DataSourceAltCloud.py b/cloudinit/sources/DataSourceAltCloud.py
index 69053d0b..fb528ae5 100644
--- a/cloudinit/sources/DataSourceAltCloud.py
+++ b/cloudinit/sources/DataSourceAltCloud.py
@@ -40,7 +40,6 @@ LOG = logging.getLogger(__name__)
CLOUD_INFO_FILE = '/etc/sysconfig/cloud-info'
# Shell command lists
-CMD_DMI_SYSTEM = ['/usr/sbin/dmidecode', '--string', 'system-product-name']
CMD_PROBE_FLOPPY = ['/sbin/modprobe', 'floppy']
CMD_UDEVADM_SETTLE = ['/sbin/udevadm', 'settle', '--quiet', '--timeout=5']
@@ -100,11 +99,7 @@ class DataSourceAltCloud(sources.DataSource):
'''
Description:
Get the type for the cloud back end this instance is running on
- by examining the string returned by:
- dmidecode --string system-product-name
-
- On VMWare/vSphere dmidecode returns: RHEV Hypervisor
- On VMWare/vSphere dmidecode returns: VMware Virtual Platform
+ by examining the string returned by reading the dmi data.
Input:
None
@@ -117,26 +112,20 @@ class DataSourceAltCloud(sources.DataSource):
uname_arch = os.uname()[4]
if uname_arch.startswith("arm") or uname_arch == "aarch64":
- # Disabling because dmidecode in CMD_DMI_SYSTEM crashes kvm process
+ # Disabling because dmi data is not available on ARM processors
LOG.debug("Disabling AltCloud datasource on arm (LP: #1243287)")
return 'UNKNOWN'
- cmd = CMD_DMI_SYSTEM
- try:
- (cmd_out, _err) = util.subp(cmd)
- except ProcessExecutionError as _err:
- LOG.debug(('Failed command: %s\n%s') % \
- (' '.join(cmd), _err.message))
- return 'UNKNOWN'
- except OSError as _err:
- LOG.debug(('Failed command: %s\n%s') % \
- (' '.join(cmd), _err.message))
+ system_name = util.read_dmi_data("system-product-name")
+ if not system_name:
return 'UNKNOWN'
- if cmd_out.upper().startswith('RHEV'):
+ sys_name = system_name.upper()
+
+ if sys_name.startswith('RHEV'):
return 'RHEV'
- if cmd_out.upper().startswith('VMWARE'):
+ if sys_name.startswith('VMWARE'):
return 'VSPHERE'
return 'UNKNOWN'
diff --git a/cloudinit/sources/DataSourceCloudSigma.py b/cloudinit/sources/DataSourceCloudSigma.py
index 707cd0ce..76597116 100644
--- a/cloudinit/sources/DataSourceCloudSigma.py
+++ b/cloudinit/sources/DataSourceCloudSigma.py
@@ -44,27 +44,25 @@ class DataSourceCloudSigma(sources.DataSource):
def is_running_in_cloudsigma(self):
"""
- Uses dmidecode to detect if this instance of cloud-init is running
+ Uses dmi data to detect if this instance of cloud-init is running
in the CloudSigma's infrastructure.
"""
uname_arch = os.uname()[4]
if uname_arch.startswith("arm") or uname_arch == "aarch64":
- # Disabling because dmidecode in CMD_DMI_SYSTEM crashes kvm process
+ # Disabling because dmi data on ARM processors
LOG.debug("Disabling CloudSigma datasource on arm (LP: #1243287)")
return False
- dmidecode_path = util.which('dmidecode')
- if not dmidecode_path:
+ LOG.debug("determining hypervisor product name via dmi data")
+ sys_product_name = util.read_dmi_data("system-product-name")
+ if not sys_product_name:
+ LOG.warn("failed to get hypervisor product name via dmi data")
return False
+ else:
+ LOG.debug("detected hypervisor as {}".format(sys_product_name))
+ return 'cloudsigma' in sys_product_name.lower()
- LOG.debug("Determining hypervisor product name via dmidecode")
- try:
- cmd = [dmidecode_path, "--string", "system-product-name"]
- system_product_name, _ = util.subp(cmd)
- return 'cloudsigma' in system_product_name.lower()
- except:
- LOG.warn("Failed to get hypervisor product name via dmidecode")
-
+ LOG.warn("failed to query dmi data for system product name")
return False
def get_data(self):
diff --git a/cloudinit/sources/DataSourceSmartOS.py b/cloudinit/sources/DataSourceSmartOS.py
index d3ed40c5..f59ad3d6 100644
--- a/cloudinit/sources/DataSourceSmartOS.py
+++ b/cloudinit/sources/DataSourceSmartOS.py
@@ -370,26 +370,13 @@ def query_data(noun, seed_device, seed_timeout, strip=False, default=None,
def dmi_data():
- sys_uuid, sys_type = None, None
- dmidecode_path = util.which('dmidecode')
- if not dmidecode_path:
- return False
+ sys_uuid = util.read_dmi_data("system-uuid")
+ sys_type = util.read_dmi_data("system-product-name")
+
+ if not sys_uuid or not sys_type:
+ return None
- sys_uuid_cmd = [dmidecode_path, "-s", "system-uuid"]
- try:
- LOG.debug("Getting hostname from dmidecode")
- (sys_uuid, _err) = util.subp(sys_uuid_cmd)
- except Exception as e:
- util.logexc(LOG, "Failed to get system UUID", e)
-
- sys_type_cmd = [dmidecode_path, "-s", "system-product-name"]
- try:
- LOG.debug("Determining hypervisor product name via dmidecode")
- (sys_type, _err) = util.subp(sys_type_cmd)
- except Exception as e:
- util.logexc(LOG, "Failed to get system UUID", e)
-
- return (sys_uuid.lower().strip(), sys_type.strip())
+ return (sys_uuid.lower(), sys_type)
def write_boot_content(content, content_f, link=None, shebang=False,
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