Simple but substantial grains refactor

- Some of the Provides comments were wrong for windows and bsd
    - Use with statment for some of the open calls
    - Clean up some code to be a bit easier to read and maintain
    - Clean up a bunch of pylint 'errors' and unused variables
    - cmd.run has defaulted to stripping output since I made it
      so there is no point in all of these grains to re-strip().
This commit is contained in:
Jeff Schroeder 2012-11-03 20:51:31 -07:00
parent ab63cc3c2f
commit 9d6ffec115

View File

@ -54,10 +54,10 @@ if sys.platform.startswith('win'):
def _windows_cpudata(): def _windows_cpudata():
''' '''
Return the cpu information for Windows systems architecture Return some cpu information on Windows minions
''' '''
# Provides: # Provides:
# cpuarch # num_cpus
# cpu_model # cpu_model
grains = {} grains = {}
if 'NUMBER_OF_PROCESSORS' in os.environ: if 'NUMBER_OF_PROCESSORS' in os.environ:
@ -73,7 +73,7 @@ def _windows_cpudata():
def _linux_cpudata(): def _linux_cpudata():
''' '''
Return the cpu information for Linux systems architecture Return some cpu information for Linux minions
''' '''
# Provides: # Provides:
# num_cpus # num_cpus
@ -83,16 +83,19 @@ def _linux_cpudata():
cpuinfo = '/proc/cpuinfo' cpuinfo = '/proc/cpuinfo'
# Parse over the cpuinfo file # Parse over the cpuinfo file
if os.path.isfile(cpuinfo): if os.path.isfile(cpuinfo):
for line in open(cpuinfo, 'r').readlines(): with open(cpuinfo, 'r') as _fp:
comps = line.split(':') for line in _fp:
if not len(comps) > 1: comps = line.split(':')
continue if not len(comps) > 1:
if comps[0].strip() == 'processor': continue
grains['num_cpus'] = int(comps[1].strip()) + 1 key = comps[0].strip()
elif comps[0].strip() == 'model name': val = comps[1].strip()
grains['cpu_model'] = comps[1].strip() if key == 'processor':
elif comps[0].strip() == 'flags': grains['num_cpus'] = int(val) + 1
grains['cpu_flags'] = comps[1].split() elif key == 'model name':
grains['cpu_model'] = val
elif key == 'flags':
grains['cpu_flags'] = val.split()
if 'num_cpus' not in grains: if 'num_cpus' not in grains:
grains['num_cpus'] = 0 grains['num_cpus'] = 0
if 'cpu_model' not in grains: if 'cpu_model' not in grains:
@ -106,14 +109,21 @@ def _bsd_cpudata(osdata):
''' '''
Return cpu information for BSD-like systems Return cpu information for BSD-like systems
''' '''
# Provides:
# cpuarch
# num_cpus
# cpu_model
# cpu_flags
sysctl = salt.utils.which('sysctl') sysctl = salt.utils.which('sysctl')
arch = salt.utils.which('arch') arch = salt.utils.which('arch')
cmds = {} cmds = {}
if sysctl: if sysctl:
cmds['num_cpus'] = '{0} -n hw.ncpu'.format(sysctl) cmds.update({
cmds['cpu_model'] = '{0} -n hw.model'.format(sysctl) 'num_cpus': '{0} -n hw.ncpu'.format(sysctl),
cmds['cpuarch'] = '{0} -n hw.machine'.format(sysctl) 'cpuarch': '{0} -n hw.machine'.format(sysctl),
'cpu_model': '{0} -n hw.model'.format(sysctl),
})
if arch and osdata['kernel'] == 'OpenBSD': if arch and osdata['kernel'] == 'OpenBSD':
cmds['cpuarch'] = '{0} -s'.format(arch) cmds['cpuarch'] = '{0} -s'.format(arch)
@ -137,14 +147,12 @@ def _sunos_cpudata(osdata):
# cpu_model # cpu_model
grains = {'num_cpus': 0} grains = {'num_cpus': 0}
grains['cpuarch'] = __salt__['cmd.run']('uname -p').strip() grains['cpuarch'] = __salt__['cmd.run']('uname -p')
for line in __salt__['cmd.run']( psrinfo = '/usr/sbin/psrinfo 2>/dev/null'
'/usr/sbin/psrinfo 2>/dev/null' for line in __salt__['cmd.run'](psrinfo).splitlines():
).split('\n'):
grains['num_cpus'] += 1 grains['num_cpus'] += 1
grains['cpu_model'] = __salt__['cmd.run']( kstat_info = 'kstat -p cpu_info:*:*:implementation'
'kstat -p cpu_info:*:*:implementation' grains['cpu_model'] = __salt__['cmd.run'](kstat_info).split()[1].strip()
).split()[1].strip()
return grains return grains
@ -168,10 +176,11 @@ def _memdata(osdata):
elif osdata['kernel'] in ('FreeBSD', 'OpenBSD'): elif osdata['kernel'] in ('FreeBSD', 'OpenBSD'):
sysctl = salt.utils.which('sysctl') sysctl = salt.utils.which('sysctl')
if sysctl: if sysctl:
mem = __salt__['cmd.run']('{0} -n hw.physmem'.format(sysctl)).strip() mem = __salt__['cmd.run']('{0} -n hw.physmem'.format(sysctl))
grains['mem_total'] = str(int(mem) / 1024 / 1024) grains['mem_total'] = str(int(mem) / 1024 / 1024)
elif osdata['kernel'] == 'SunOS': elif osdata['kernel'] == 'SunOS':
for line in __salt__['cmd.run']('/usr/sbin/prtconf 2>/dev/null').split('\n'): prtconf = '/usr/sbin/prtconf 2>/dev/null'
for line in __salt__['cmd.run'](prtconf).splitlines():
comps = line.split(' ') comps = line.split(' ')
if comps[0].strip() == 'Memory' and comps[1].strip() == 'size:': if comps[0].strip() == 'Memory' and comps[1].strip() == 'size:':
grains['mem_total'] = int(comps[2].strip()) grains['mem_total'] = int(comps[2].strip())
@ -194,17 +203,15 @@ def _virtual(osdata):
# grain with please submit patches! # grain with please submit patches!
# Provides: # Provides:
# virtual # virtual
# virtual_subtype
grains = {'virtual': 'physical'} grains = {'virtual': 'physical'}
lspci = salt.utils.which('lspci')
dmidecode = salt.utils.which('dmidecode')
for command in ('dmidecode', 'lspci'): for command in ('dmidecode', 'lspci'):
which = salt.utils.which(command) cmd = salt.utils.which(command)
if which is None: if not cmd:
continue continue
ret = __salt__['cmd.run_all'](which) ret = __salt__['cmd.run_all'](cmd)
if ret['retcode'] > 0: if ret['retcode'] > 0:
if salt.log.is_logging_configured(): if salt.log.is_logging_configured():
@ -306,12 +313,12 @@ def _virtual(osdata):
sysctl = salt.utils.which('sysctl') sysctl = salt.utils.which('sysctl')
kenv = salt.utils.which('kenv') kenv = salt.utils.which('kenv')
if kenv: if kenv:
product = __salt__['cmd.run']('{0} smbios.system.product'.format(kenv)).strip() product = __salt__['cmd.run']('{0} smbios.system.product'.format(kenv))
if product.startswith('VMware'): if product.startswith('VMware'):
grains['virtual'] = 'VMware' grains['virtual'] = 'VMware'
if sysctl: if sysctl:
model = __salt__['cmd.run']('{0} hw.model'.format(sysctl)).strip() model = __salt__['cmd.run']('{0} hw.model'.format(sysctl))
jail = __salt__['cmd.run']('{0} -n security.jail.jailed'.format(sysctl)).strip() jail = __salt__['cmd.run']('{0} -n security.jail.jailed'.format(sysctl))
if jail: if jail:
grains['virtual_subtype'] = 'jail' grains['virtual_subtype'] = 'jail'
if 'QEMU Virtual CPU' in model: if 'QEMU Virtual CPU' in model:
@ -320,7 +327,7 @@ def _virtual(osdata):
# Check if it's a "regular" zone. (i.e. Solaris 10/11 zone) # Check if it's a "regular" zone. (i.e. Solaris 10/11 zone)
zonename = salt.utils.which('zonename') zonename = salt.utils.which('zonename')
if zonename: if zonename:
zone = __salt__['cmd.run']('{0}'.format(zonename)).strip() zone = __salt__['cmd.run']('{0}'.format(zonename))
if zone != "global": if zone != "global":
grains['virtual'] = 'zone' grains['virtual'] = 'zone'
# Check if it's a branded zone (i.e. Solaris 8/9 zone) # Check if it's a branded zone (i.e. Solaris 8/9 zone)
@ -401,7 +408,7 @@ def id_():
# This maps (at most) the first ten characters (no spaces, lowercased) of # This maps (at most) the first ten characters (no spaces, lowercased) of
# 'osfullname' to the 'os' grain that Salt traditionally uses. # 'osfullname' to the 'os' grain that Salt traditionally uses.
_os_name_map = { _OS_NAME_MAP = {
'redhatente': 'RedHat', 'redhatente': 'RedHat',
'debian': 'Debian', 'debian': 'Debian',
'arch': 'Arch', 'arch': 'Arch',
@ -410,7 +417,7 @@ _os_name_map = {
} }
# Map the 'os' grain to the 'os_family' grain # Map the 'os' grain to the 'os_family' grain
_os_family_map = { _OS_FAMILY_MAP = {
'Ubuntu': 'Debian', 'Ubuntu': 'Debian',
'Fedora': 'RedHat', 'Fedora': 'RedHat',
'CentOS': 'RedHat', 'CentOS': 'RedHat',
@ -501,7 +508,7 @@ def os_data():
shortname = grains['osfullname'].replace(' ', '').lower()[:10] shortname = grains['osfullname'].replace(' ', '').lower()[:10]
# this maps the long names from the /etc/DISTRO-release files to the # this maps the long names from the /etc/DISTRO-release files to the
# traditional short names that Salt has used. # traditional short names that Salt has used.
grains['os'] = _os_name_map.get(shortname, grains['osfullname']) grains['os'] = _OS_NAME_MAP.get(shortname, grains['osfullname'])
grains.update(_linux_cpudata()) grains.update(_linux_cpudata())
elif grains['kernel'] == 'SunOS': elif grains['kernel'] == 'SunOS':
grains['os'] = 'Solaris' grains['os'] = 'Solaris'
@ -526,7 +533,7 @@ def os_data():
else: else:
# this assigns family names based on the os name # this assigns family names based on the os name
# family defaults to the os name if not found # family defaults to the os name if not found
grains['os_family'] = _os_family_map.get(grains['os'], grains['os_family'] = _OS_FAMILY_MAP.get(grains['os'],
grains['os']) grains['os'])
grains.update(_memdata(grains)) grains.update(_memdata(grains))
@ -627,7 +634,7 @@ def _dmidecode_data(regex_dict):
section_found = False section_found = False
# Look at every line for the right section # Look at every line for the right section
for line in out.split('\n'): for line in out.splitlines():
if not line: if not line:
continue continue
# We've found it, woohoo! # We've found it, woohoo!
@ -689,11 +696,16 @@ def _hw_data(osdata):
elif osdata['kernel'] == 'FreeBSD': elif osdata['kernel'] == 'FreeBSD':
kenv = salt.utils.which('kenv') kenv = salt.utils.which('kenv')
if kenv: if kenv:
grains['biosreleasedate'] = __salt__['cmd.run']('{0} smbios.bios.reldate'.format(kenv)).strip() # In theory, it will be easier to add new fields to this later
grains['biosversion'] = __salt__['cmd.run']('{0} smbios.bios.version'.format(kenv)).strip() fbsd_hwdata = {
grains['manufacturer'] = __salt__['cmd.run']('{0} smbios.system.maker'.format(kenv)).strip() 'biosversion': 'smbios.bios.version',
grains['serialnumber'] = __salt__['cmd.run']('{0} smbios.system.serial'.format(kenv)).strip() 'manufacturer': 'smbios.system.maker',
grains['productname'] = __salt__['cmd.run']('{0} smbios.system.product'.format(kenv)).strip() 'serialnumber': 'smbios.system.serial',
'productname': 'smbios.system.product',
'biosreleasedate': 'smbios.bios.reldate',
}
for key, val in fbsd_hwdata.items():
grains[key] = __salt__['cmd.run']('{0} {1}'.format(kenv, val)).strip()
elif osdata['kernel'] == 'OpenBSD': elif osdata['kernel'] == 'OpenBSD':
sysctl = salt.utils.which('sysctl') sysctl = salt.utils.which('sysctl')
hwdata = {'biosversion': 'hw.version', hwdata = {'biosversion': 'hw.version',