def include_config(include, opts, orig_path, verbose):
'''
Parses extra configuration file(s) specified in an include list in the
main config file.
'''
# Protect against empty option
if not include:
return opts
if isinstance(include, str):
include = [include]
for path in include:
if not os.path.isabs(path):
path = os.path.join(os.path.dirname(orig_path), path)
# Catch situation where user typos path in config; also warns for
# empty include dir (which might be by design)
if len(glob.glob(path)) == 0:
msg = "Warning parsing configuration file: 'include' path/glob '{0}' matches no files"
if verbose: log.warn(msg.format(path))
for fn_ in glob.glob(path):
try:
opts.update(_read_conf_file(fn_))
except Exception as e:
msg = 'Error parsing configuration file: {0} - {1}'
log.warn(msg.format(fn_, e))
return opts
开发者ID:fatbox,项目名称:salt,代码行数:30,代码来源:config.py
示例2: mine
def mine(tgt=None, expr_form='glob', **kwargs):
'''
Return cached mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.mine
'''
deprecated_minion = kwargs.get('minion', None)
if tgt is None and deprecated_minion is None:
log.warn("DEPRECATION WARNING: {0}".format(deprecation_warning))
tgt = '*' # targat all minions for backward compatibility
elif tgt is None and isinstance(deprecated_minion, string_types):
log.warn("DEPRECATION WARNING: {0}".format(deprecation_warning))
tgt = deprecated_minion
elif tgt is None:
return {}
pillar_util = salt.utils.master.MasterPillarUtil(tgt, expr_form,
use_cached_grains=False,
grains_fallback=False,
use_cached_pillar=False,
pillar_fallback=False,
opts=__opts__)
cached_mine = pillar_util.get_cached_mine_data()
salt.output.display_output(cached_mine, None, __opts__)
开发者ID:penta-srl,项目名称:salt,代码行数:27,代码来源:cache.py
示例3: ext_pillar
def ext_pillar(minion_id, # pylint: disable=W0613
pillar, # pylint: disable=W0613
config_file):
'''
Execute LDAP searches and return the aggregated data
'''
if os.path.isfile(config_file):
try:
#open(config_file, 'r') as raw_config:
config = _render_template(config_file) or {}
opts = yaml.safe_load(config) or {}
opts['conf_file'] = config_file
except Exception as err:
import salt.log
msg = 'Error parsing configuration file: {0} - {1}'
if salt.log.is_console_configured():
log.warn(msg.format(config_file, err))
else:
print(msg.format(config_file, err))
else:
log.debug('Missing configuration file: {0}'.format(config_file))
data = {}
for source in opts['search_order']:
config = opts[source]
result = _do_search(config)
print('source {0} got result {1}'.format(source, result))
if result:
data = _result_to_dict(data, result, config)
return data
def load_config(opts, path, env_var):
'''
Attempts to update ``opts`` dict by parsing either the file described by
``path`` or the environment variable described by ``env_var`` as YAML.
'''
if not path or not os.path.isfile(path):
path = os.environ.get(env_var, path)
# If the configuration file is missing, attempt to copy the template,
# after removing the first header line.
if not os.path.isfile(path):
template = '{0}.template'.format(path)
if os.path.isfile(template):
with open(path, 'w') as out:
with open(template, 'r') as f:
f.readline() # skip first line
out.write(f.read())
if os.path.isfile(path):
try:
opts.update(_read_conf_file(path))
opts['conf_file'] = path
except Exception as e:
import salt.log
msg = 'Error parsing configuration file: {0} - {1}'
if salt.log.is_console_configured():
log.warn(msg.format(path, e))
else:
print(msg.format(path, e))
else:
log.debug('Missing configuration file: {0}'.format(path))
开发者ID:fatbox,项目名称:salt,代码行数:30,代码来源:config.py
示例5: start
def start(self):
'''
Execute this method to start up a minion.
'''
verify_env([self.opts['pki_dir'],
self.opts['cachedir'],
self.opts['extension_modules'],
os.path.dirname(self.opts['log_file']),
])
import salt.log
salt.log.setup_logfile_logger(
self.opts['log_file'], self.opts['log_level']
)
for name, level in self.opts['log_granular_levels'].iteritems():
salt.log.set_logger_level(name, level)
import logging
# Late import so logging works correctly
import salt.minion
log = logging.getLogger(__name__)
if self.cli['daemon']:
# Late import so logging works correctly
import salt.utils
# If the minion key has not been accepted, then Salt enters a loop
# waiting for it, if we daemonize later then the minion cound halt
# the boot process waiting for a key to be accepted on the master.
# This is the latest safe place to daemonize
salt.utils.daemonize()
minion = salt.minion.Minion(self.opts)
set_pidfile(self.cli['pidfile'])
if check_user(self.opts['user'], log):
try:
minion.tune_in()
except KeyboardInterrupt:
log.warn('Stopping the Salt Minion')
raise SystemExit('\nExiting on Ctrl-c')
def start(self):
'''
Execute this method to start up a syndic.
'''
verify_env([self.opts['pki_dir'], self.opts['cachedir'],
os.path.dirname(self.opts['log_file']),
])
import salt.log
salt.log.setup_logfile_logger(
self.opts['log_file'], self.opts['log_level']
)
for name, level in self.opts['log_granular_levels'].iteritems():
salt.log.set_logger_level(name, level)
import logging
# Late import so logging works correctly
import salt.minion
log = logging.getLogger(__name__)
if check_user(self.opts['user'], log):
try:
syndic = salt.minion.Syndic(self.opts)
if self.cli['daemon']:
# Late import so logging works correctly
import salt.utils
salt.utils.daemonize()
set_pidfile(self.cli['pidfile'])
syndic.tune_in()
except KeyboardInterrupt:
log.warn('Stopping the Salt Syndic Minion')
raise SystemExit('\nExiting on Ctrl-c')
开发者ID:Dratone,项目名称:salt,代码行数:31,代码来源:__init__.py
示例7: has_battery
def has_battery():
'''
Return true if a battery exists.
'''
# We need to have the `acpi` binary installed to avoid having to do the
# old vs new acpi detection our selves.
acpi = salt.utils.which('acpi')
if acpi is None or acpi == "":
return {}
# call ACPI binary: `acpi -b` to return the battery status. As long as the
# binary exists, it will return either the status of all batteries it knows
# about in the following format:
# 'Battery X: Full, YY%' or 'No support for device type: power_supply'
# In the former, we return True, the latter we return False. I hope it's
# obvious why. ;)
result = salt.modules.cmdmod._run_quiet('acpi -b')
if 'No support for device type' in result:
return {'has_battery': 0}
elif 'Battery ' in result:
return {'has_battery': 1}
else:
log.warn('Unexpected output from `acpi -b`: {0}'.format(result))
return {}
def pillar(tgt=None, expr_form='glob', outputter=None, **kwargs):
'''
Return cached pillars of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.pillar
'''
deprecated_minion = kwargs.get('minion', None)
if tgt is None and deprecated_minion is None:
log.warn("DEPRECATION WARNING: {0}".format(DEPRECATION_WARNING))
tgt = '*' # targat all minions for backward compatibility
elif tgt is None and isinstance(deprecated_minion, string_types):
log.warn("DEPRECATION WARNING: {0}".format(DEPRECATION_WARNING))
tgt = deprecated_minion
elif tgt is None:
return {}
pillar_util = salt.utils.master.MasterPillarUtil(tgt, expr_form,
use_cached_grains=True,
grains_fallback=False,
use_cached_pillar=True,
pillar_fallback=False,
opts=__opts__)
cached_pillar = pillar_util.get_minion_pillar()
if outputter:
return {'outputter': outputter, 'data': cached_pillar}
else:
return cached_pillar
开发者ID:DavideyLee,项目名称:salt,代码行数:30,代码来源:cache.py
示例9: _linux_gpu_data
def _linux_gpu_data():
'''
num_gpus: int
gpus:
- vendor: nvidia|amd|ati|...
model: string
'''
# dominant gpu vendors to search for (MUST be lowercase for matching below)
known_vendors = ['nvidia', 'amd', 'ati', 'intel']
devs = []
try:
lspci_out = __salt__['cmd.run']('lspci -vmm')
cur_dev = {}
error = False
for line in lspci_out.splitlines():
# check for record-separating empty lines
if line == '':
if cur_dev.get('Class', '') == 'VGA compatible controller':
devs.append(cur_dev)
# XXX; may also need to search for "3D controller"
cur_dev = {}
continue
if re.match(r'^\w+:\s+.*', line):
key, val = line.split(':', 1)
cur_dev[key.strip()] = val.strip()
else:
error = True
log.debug('Unexpected lspci output: \'{0}\''.format(line))
if error:
log.warn(
'Error loading grains, unexpected linux_gpu_data output, '
'check that you have a valid shell configured and '
'permissions to run lspci command'
)
except OSError:
pass
gpus = []
for gpu in devs:
vendor_strings = gpu['Vendor'].lower().split()
# default vendor to 'unknown', overwrite if we match a known one
vendor = 'unknown'
for name in known_vendors:
# search for an 'expected' vendor name in the list of strings
if name in vendor_strings:
vendor = name
break
gpus.append({'vendor': vendor, 'model': gpu['Device']})
grains = {}
grains['num_gpus'] = len(gpus)
grains['gpus'] = gpus
return grains
开发者ID:herlo,项目名称:salt,代码行数:56,代码来源:core.py
示例10: _get_live_minion_pillar
def _get_live_minion_pillar(self, minion_id=None, minion_grains=None):
# Returns a dict of pillar data for one minion
if minion_id == None:
return {}
if not minion_grains:
log.warn('Cannot get pillar data for {0}: no grains supplied.'.format(minion_id))
return {}
log.debug('Getting live pillar for {0}'.format(minion_id))
pillar = salt.pillar.Pillar(
self.opts,
minion_grains,
minion_id,
self.env,
self.opts['ext_pillar'])
log.debug('Compiling pillar for {0}'.format(minion_id))
ret = pillar.compile_pillar()
return ret
开发者ID:jslatts,项目名称:salt,代码行数:17,代码来源:master.py
示例11: include_config
def include_config(include, orig_path, verbose):
'''
Parses extra configuration file(s) specified in an include list in the
main config file.
'''
# Protect against empty option
if not include:
return {}
if orig_path is None:
# When the passed path is None, we just want the configuration
# defaults, not actually loading the whole configuration.
return {}
if isinstance(include, str):
include = [include]
configuration = {}
for path in include:
# Allow for includes like ~/foo
path = os.path.expanduser(path)
if not os.path.isabs(path):
path = os.path.join(os.path.dirname(orig_path), path)
# Catch situation where user typos path in configuration; also warns
# for empty include directory (which might be by design)
if len(glob.glob(path)) == 0:
if verbose:
log.warn(
'Warning parsing configuration file: "include" path/glob '
'"{0}" matches no files'.format(path)
)
for fn_ in sorted(glob.glob(path)):
try:
log.debug('Including configuration from {0}'.format(fn_))
configuration.update(_read_conf_file(fn_))
except Exception as err:
log.warn(
'Error parsing configuration file: {0} - {1}'.format(
fn_, err
)
)
return configuration
开发者ID:herlo,项目名称:salt,代码行数:44,代码来源:config.py
示例12: resolve_dns
def resolve_dns(opts):
'''
Resolves the master_ip and master_uri options
'''
ret = {}
check_dns = True
if opts.get('file_client', 'remote') == 'local' and check_dns:
check_dns = False
if check_dns is True:
# Because I import salt.log below I need to re-import salt.utils here
import salt.utils
try:
ret['master_ip'] = salt.utils.dns_check(opts['master'], True)
except SaltClientError:
if opts['retry_dns']:
while True:
import salt.log
msg = ('Master hostname: {0} not found. Retrying in {1} '
'seconds').format(opts['master'], opts['retry_dns'])
if salt.log.is_console_configured():
log.warn(msg)
else:
print('WARNING: {0}'.format(msg))
time.sleep(opts['retry_dns'])
try:
ret['master_ip'] = salt.utils.dns_check(
opts['master'], True
)
break
except SaltClientError:
pass
else:
ret['master_ip'] = '127.0.0.1'
else:
ret['master_ip'] = '127.0.0.1'
ret['master_uri'] = 'tcp://{ip}:{port}'.format(ip=ret['master_ip'],
port=opts['master_port'])
return ret
开发者ID:11craft,项目名称:salt,代码行数:40,代码来源:minion.py
示例13: load_config
def load_config(path, env_var):
'''
Returns configuration dict from parsing either the file described by
``path`` or the environment variable described by ``env_var`` as YAML.
'''
if path is None:
# When the passed path is None, we just want the configuration
# defaults, not actually loading the whole configuration.
return {}
if not path or not os.path.isfile(path):
path = os.environ.get(env_var, path)
# If the configuration file is missing, attempt to copy the template,
# after removing the first header line.
if not os.path.isfile(path):
template = '{0}.template'.format(path)
if os.path.isfile(template):
import salt.utils # TODO: Need to re-import, need to find out why
log.debug('Writing {0} based on {1}'.format(path, template))
with salt.utils.fopen(path, 'w') as out:
with salt.utils.fopen(template, 'r') as ifile:
ifile.readline() # skip first line
out.write(ifile.read())
if os.path.isfile(path):
try:
opts = _read_conf_file(path)
opts['conf_file'] = path
return opts
except Exception as err:
import salt.log
msg = 'Error parsing configuration file: {0} - {1}'
if salt.log.is_console_configured():
log.warn(msg.format(path, err))
else:
print(msg.format(path, err))
else:
log.debug('Missing configuration file: {0}'.format(path))
return {}
开发者ID:herlo,项目名称:salt,代码行数:40,代码来源:config.py
示例14: apply_minion_config
def apply_minion_config(overrides=None, check_dns=True, defaults=None):
'''
Returns minion configurations dict.
'''
if defaults is None:
defaults = DEFAULT_MINION_OPTS
opts = defaults.copy()
if overrides:
opts.update(overrides)
if len(opts['sock_dir']) > len(opts['cachedir']) + 10:
opts['sock_dir'] = os.path.join(opts['cachedir'], '.salt-unix')
if 'append_domain' in opts:
opts['id'] = _append_domain(opts)
if opts.get('file_client', 'remote') == 'local' and check_dns:
check_dns = False
if check_dns is True:
# Because I import salt.log below I need to re-import salt.utils here
import salt.utils
try:
opts['master_ip'] = salt.utils.dns_check(opts['master'], True)
except SaltClientError:
if opts['retry_dns']:
while True:
import salt.log
msg = ('Master hostname: {0} not found. Retrying in {1} '
'seconds').format(opts['master'], opts['retry_dns'])
if salt.log.is_console_configured():
log.warn(msg)
else:
print('WARNING: {0}'.format(msg))
time.sleep(opts['retry_dns'])
try:
opts['master_ip'] = salt.utils.dns_check(
opts['master'], True
)
break
except SaltClientError:
pass
else:
opts['master_ip'] = '127.0.0.1'
else:
opts['master_ip'] = '127.0.0.1'
opts['master_uri'] = 'tcp://{ip}:{port}'.format(ip=opts['master_ip'],
port=opts['master_port'])
# Enabling open mode requires that the value be set to True, and
# nothing else!
opts['open_mode'] = opts['open_mode'] is True
# set up the extension_modules location from the cachedir
opts['extension_modules'] = (
opts.get('extension_modules') or
os.path.join(opts['cachedir'], 'extmods')
)
# Prepend root_dir to other paths
prepend_root_dirs = [
'pki_dir', 'cachedir', 'sock_dir', 'extension_modules', 'pidfile',
]
# These can be set to syslog, so, not actual paths on the system
for config_key in ('log_file', 'key_logfile'):
if urlparse.urlparse(opts.get(config_key, '')).scheme == '':
prepend_root_dirs.append(config_key)
prepend_root_dir(opts, prepend_root_dirs)
return opts
def _virtual(osdata):
'''
Returns what type of virtual hardware is under the hood, kvm or physical
'''
# This is going to be a monster, if you are running a vm you can test this
# grain with please submit patches!
# Provides:
# virtual
grains = {'virtual': 'physical'}
lspci = salt.utils.which('lspci')
dmidecode = salt.utils.which('dmidecode')
for command in ('dmidecode', 'lspci'):
which = salt.utils.which(command)
if which is None:
continue
ret = __salt__['cmd.run_all'](which)
if ret['retcode'] > 0:
if salt.log.is_logging_configured():
log.warn(
'Although \'{0}\' was found in path, the current user '
'cannot execute it. Grains output might not be '
'accurate.'.format(command)
)
continue
output = ret['stdout']
if command == 'dmidecode':
# Product Name: VirtualBox
if 'Vendor: QEMU' in output:
# FIXME: Make this detect between kvm or qemu
grains['virtual'] = 'kvm'
if 'Vendor: Bochs' in output:
grains['virtual'] = 'kvm'
elif 'VirtualBox' in output:
grains['virtual'] = 'VirtualBox'
# Product Name: VMware Virtual Platform
elif 'VMware' in output:
grains['virtual'] = 'VMware'
# Manufacturer: Microsoft Corporation
# Product Name: Virtual Machine
elif 'Manufacturer: Microsoft' in output and 'Virtual Machine' in output:
grains['virtual'] = 'VirtualPC'
# Manufacturer: Parallels Software International Inc.
elif 'Parallels Software' in output:
grains['virtual'] = 'Parallels'
# Break out of the loop, lspci parsing is not necessary
break
elif command == 'lspci':
# dmidecode not available or the user does not have the necessary
# permissions
model = output.lower()
if 'vmware' in model:
grains['virtual'] = 'VMware'
# 00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
elif 'virtualbox' in model:
grains['virtual'] = 'VirtualBox'
elif 'qemu' in model:
grains['virtual'] = 'kvm'
elif 'virtio' in model:
grains['virtual'] = 'kvm'
# Break out of the loop so the next log message is not issued
break
else:
log.warn(
'Both \'dmidecode\' and \'lspci\' failed to execute, either '
'because they do not exist on the system of the user running '
'this instance does not have the necessary permissions to '
'execute them. Grains output might not be accurate.'
)
choices = ('Linux', 'OpenBSD', 'HP-UX')
isdir = os.path.isdir
if osdata['kernel'] in choices:
if isdir('/proc/vz'):
if os.path.isfile('/proc/vz/version'):
grains['virtual'] = 'openvzhn'
else:
grains['virtual'] = 'openvzve'
elif isdir('/proc/sys/xen') or isdir('/sys/bus/xen') or isdir('/proc/xen'):
if os.path.isfile('/proc/xen/xsd_kva'):
# Tested on CentOS 5.3 / 2.6.18-194.26.1.el5xen
# Tested on CentOS 5.4 / 2.6.18-164.15.1.el5xen
grains['virtual_subtype'] = 'Xen Dom0'
else:
if grains.get('productname', '') == 'HVM domU':
# Requires dmidecode!
grains['virtual_subtype'] = 'Xen HVM DomU'
elif os.path.isfile('/proc/xen/capabilities') and os.access('/proc/xen/capabilities', os.R_OK):
caps = open('/proc/xen/capabilities')
if 'control_d' not in caps.read():
# Tested on CentOS 5.5 / 2.6.18-194.3.1.el5xen
grains['virtual_subtype'] = 'Xen PV DomU'
else:
# Shouldn't get to this, but just in case
grains['virtual_subtype'] = 'Xen Dom0'
#.........这里部分代码省略.........
def _linux_gpu_data():
"""
num_gpus: int
gpus:
- vendor: nvidia|amd|ati|...
model: string
"""
lspci = salt.utils.which("lspci")
if not lspci:
log.info("The `lspci` binary is not available on the system. GPU grains " "will not be available.")
return {}
elif __opts__.get("enable_gpu_grains", None) is False:
log.info(
"Skipping lspci call because enable_gpu_grains was set to False "
"in the config. GPU grains will not be available."
)
return {}
# dominant gpu vendors to search for (MUST be lowercase for matching below)
known_vendors = ["nvidia", "amd", "ati", "intel"]
devs = []
try:
lspci_out = __salt__["cmd.run"]("lspci -vmm")
cur_dev = {}
error = False
# Add a blank element to the lspci_out.splitlines() list,
# otherwise the last device is not evaluated as a cur_dev and ignored.
lspci_list = lspci_out.splitlines()
lspci_list.append("")
for line in lspci_list:
# check for record-separating empty lines
if line == "":
if cur_dev.get("Class", "") == "VGA compatible controller":
devs.append(cur_dev)
# XXX; may also need to search for "3D controller"
cur_dev = {}
continue
if re.match(r"^\w+:\s+.*", line):
key, val = line.split(":", 1)
cur_dev[key.strip()] = val.strip()
else:
error = True
log.debug("Unexpected lspci output: '{0}'".format(line))
if error:
log.warn(
"Error loading grains, unexpected linux_gpu_data output, "
"check that you have a valid shell configured and "
"permissions to run lspci command"
)
except OSError:
pass
gpus = []
for gpu in devs:
vendor_strings = gpu["Vendor"].lower().split()
# default vendor to 'unknown', overwrite if we match a known one
vendor = "unknown"
for name in known_vendors:
# search for an 'expected' vendor name in the list of strings
if name in vendor_strings:
vendor = name
break
gpus.append({"vendor": vendor, "model": gpu["Device"]})
grains = {}
grains["num_gpus"] = len(gpus)
grains["gpus"] = gpus
return grains
开发者ID:penta-srl,项目名称:salt,代码行数:72,代码来源:core.py
示例18: _virtual
def _virtual(osdata):
"""
Returns what type of virtual hardware is under the hood, kvm or physical
"""
# This is going to be a monster, if you are running a vm you can test this
# grain with please submit patches!
# Provides:
# virtual
# virtual_subtype
grains = {"virtual": "physical"}
for command in ("dmidecode", "lspci", "dmesg"):
args = []
if osdata["kernel"] == "Darwin":
command = "system_profiler"
args = ["SPDisplaysDataType"]
cmd = salt.utils.which(command)
if not cmd:
continue
cmd = "%s %s" % (command, " ".join(args))
ret = __salt__["cmd.run_all"](cmd)
if ret["retcode"] > 0:
if salt.log.is_logging_configured():
if salt.utils.is_windows():
continue
log.warn(
"Although '{0}' was found in path, the current user "
"cannot execute it. Grains output might not be "
"accurate.".format(command)
)
continue
output = ret["stdout"]
if command == "system_profiler":
macoutput = output.lower()
if "0x1ab8" in macoutput:
grains["virtual"] = "Parallels"
if "parallels" in macoutput:
grains["virtual"] = "Parallels"
if "vmware" in macoutput:
grains["virtual"] = "VMware"
if "0x15ad" in macoutput:
grains["virtual"] = "VMware"
if "virtualbox" in macoutput:
grains["virtual"] = "VirtualBox"
# Break out of the loop so the next log message is not issued
break
elif command == "dmidecode" or command == "dmesg":
# Product Name: VirtualBox
if "Vendor: QEMU" in output:
# FIXME: Make this detect between kvm or qemu
grains["virtual"] = "kvm"
if "Vendor: Bochs" in output:
grains["virtual"] = "kvm"
# Product Name: (oVirt) www.ovirt.org
# Red Hat Community virtualization Project based on kvm
elif "Manufacturer: oVirt" in output:
grains["virtual"] = "kvm"
elif "VirtualBox" in output:
grains["virtual"] = "VirtualBox"
# Product Name: VMware Virtual Platform
elif "VMware" in output:
grains["virtual"] = "VMware"
# Manufacturer: Microsoft Corporation
# Product Name: Virtual Machine
elif ": Microsoft" in output and "Virtual Machine" in output:
grains["virtual"] = "VirtualPC"
# Manufacturer: Parallels Software International Inc.
elif "Parallels Software" in output:
grains["virtual"] = "Parallels"
# Break out of the loop, lspci parsing is not necessary
break
elif command == "lspci":
# dmidecode not available or the user does not have the necessary
# permissions
model = output.lower()
if "vmware" in model:
grains["virtual"] = "VMware"
# 00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
elif "virtualbox" in model:
grains["virtual"] = "VirtualBox"
elif "qemu" in model:
grains["virtual"] = "kvm"
elif "virtio" in model:
grains["virtual"] = "kvm"
# Break out of the loop so the next log message is not issued
break
else:
log.warn(
"The tools 'dmidecode', 'lspci' and 'dmesg' failed to execute "
"because they do not exist on the system of the user running "
"this instance or the user does not have the necessary permissions "
"to execute them. Grains output might not be accurate."
)
#.........这里部分代码省略.........
开发者ID:penta-srl,项目名称:salt,代码行数:101,代码来源:core.py
示例19: _linux_gpu_data
def _linux_gpu_data():
'''
num_gpus: int
gpus:
- vendor: nvidia|amd|ati|...
model: string
'''
lspci = salt.utils.which('lspci')
if not lspci:
log.info(
'The `lspci` binary is not available on the system. GPU grains '
'will not be available.'
)
return {}
elif __opts__.get('enable_gpu_grains', None) is False:
log.info(
'Skipping lspci call because enable_gpu_grains was set to False '
'in the config. GPU grains will not be available.'
)
return {}
# dominant gpu vendors to search for (MUST be lowercase for matching below)
known_vendors = ['nvidia', 'amd', 'ati', 'intel']
devs = []
try:
lspci_out = __salt__['cmd.run']('lspci -vmm')
cur_dev = {}
error = False
# Add a blank element to the lspci_out.splitlines() list,
# otherwise the last device is not evaluated as a cur_dev and ignored.
lspci_list = lspci_out.splitlines()
lspci_list.append('')
for line in lspci_list:
# check for record-separating empty lines
if line == '':
if cur_dev.get('Class', '') == 'VGA compatible controller':
devs.append(cur_dev)
# XXX; may also need to search for "3D controller"
cur_dev = {}
continue
if re.match(r'^\w+:\s+.*', line):
key, val = line.split(':', 1)
cur_dev[key.strip()] = val.strip()
else:
error = True
log.debug('Unexpected lspci output: \'{0}\''.format(line))
if error:
log.warn(
'Error loading grains, unexpected linux_gpu_data output, '
'check that you have a valid shell configured and '
'permissions to run lspci command'
)
except OSError:
pass
gpus = []
for gpu in devs:
vendor_strings = gpu['Vendor'].lower().split()
# default vendor to 'unknown', overwrite if we match a known one
vendor = 'unknown'
for name in known_vendors:
# search for an 'expected' vendor name in the list of strings
if name in vendor_strings:
vendor = name
break
gpus.append({'vendor': vendor, 'model': gpu['Device']})
grains = {}
grains['num_gpus'] = len(gpus)
grains['gpus'] = gpus
return grains
请发表评论