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