-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New grains fetching detailed hypervisor info for Xen domains #46459
Conversation
Should these grains be documented? Is there any page/doco describing grains? |
@damon-atkins I do not believe there is any documentation. If you look at https://docs.saltstack.com/en/latest/topics/grains/, it links straight to https://github.com/saltstack/salt/blob/develop/salt/grains/core.py for "documentation". And otherwise it suggests to use the grains module to discover what is available. |
@ixs Can you clean up the lint violations? https://jenkins.saltstack.com/job/PR/job/salt-pr-lint-n/19996/violations/file/salt/grains/core.py/ |
@rallytime done. |
@ixs Looks like there are still some errors. You can run the lint check yourself if you like by following these instructions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a couple of requested changes here, too.
salt/grains/core.py
Outdated
try: | ||
version = {} | ||
for file in ('major', 'minor', 'extra'): | ||
with salt.utils.fopen('/sys/hypervisor/version/{}'.format(file), 'r') as fhr: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the salt.utils.files.fopen
path here, instead of the salt.utils.fopen
? We've split out the utils init file into separate files in 2018.3 and develop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. In that case you might also want to update your linter messages as that referes to salt.utils.fopen()
salt/grains/core.py
Outdated
# Try to get the exact hypervisor version from sysfs | ||
try: | ||
version = {} | ||
for file in ('major', 'minor', 'extra'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file
is a reserved keyword in Python. Can you change this to another variable name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
salt/grains/core.py
Outdated
13: 'memory_op_vnode_supported', | ||
14: 'ARM_SMCCC_supported'} | ||
try: | ||
with salt.utils.fopen('/sys/hypervisor/properties/features', 'r') as fhr: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here about the utils path. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Xen exposes a lot of detail about the hypervisor in /sys/hypervisor. Parse the hypervisor version and the capabilities and expose these as grains. This allows for detailed targeting and appropriate actions, e.g. when running on XenServer install the XenServer guest-tools. Sample output: virtual_hv_features: 000020f0 virtual_hv_features_list: - pae_pgdir_above_4gb - mmu_pt_update_preserve_ad - gnttab_map_avail_bits - memory_op_vnode_supported virtual_hv_version: 4.6.6-2.el7 virtual_hv_version_info: - 4 - 6 - .6-2.el7
salt/grains/core.py
Outdated
version[fn] = fhr.read().strip() | ||
grains['virtual_hv_version'] = '{}.{}{}'.format(version['major'], version['minor'], version['extra']) | ||
grains['virtual_hv_version_info'] = (version['major'], version['minor'], version['extra']) | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bare except
s are bad, at minimum except Exception
should be used. But when you know the potenial exceptions, you should use them. In this case, except (IOError, OSError, KeyError):
salt/grains/core.py
Outdated
with salt.utils.files.fopen('/sys/hypervisor/version/{}'.format(fn), 'r') as fhr: | ||
version[fn] = fhr.read().strip() | ||
grains['virtual_hv_version'] = '{}.{}{}'.format(version['major'], version['minor'], version['extra']) | ||
grains['virtual_hv_version_info'] = (version['major'], version['minor'], version['extra']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be using a list instead of a tuple here, for a couple reasons:
- The tuple will be lost (converted to list) anyway if/when the data is serialized using msgpack, json, etc.
- We allow grains to be added via YAML, which does not support tuples.
salt/grains/core.py
Outdated
version = {} | ||
for fn in ('major', 'minor', 'extra'): | ||
with salt.utils.files.fopen('/sys/hypervisor/version/{}'.format(fn), 'r') as fhr: | ||
version[fn] = fhr.read().strip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be decoded to unicode using:
version[fn] = salt.utils.stringutils.to_unicode(fhr.read().strip())
This may also require that salt.utils.stringutils
is added to the imports.
salt/grains/core.py
Outdated
14: 'ARM_SMCCC_supported'} | ||
try: | ||
with salt.utils.files.fopen('/sys/hypervisor/properties/features', 'r') as fhr: | ||
features = fhr.read().strip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above with decoding to unicode.
salt/grains/core.py
Outdated
with salt.utils.files.fopen('/sys/hypervisor/properties/features', 'r') as fhr: | ||
features = fhr.read().strip() | ||
enabled_features = [] | ||
for bit, feat in xen_feature_table.items(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of xen_feature_table.items()
, please use six.iteritems(xen_feature_table)
. This ensures that Python 2 uses a generator over the key/value pairs.
salt/grains/core.py
Outdated
enabled_features.append(feat) | ||
grains['virtual_hv_features'] = features | ||
grains['virtual_hv_features_list'] = enabled_features | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above with the bare exception. Try to narrow this down to the exceptions you'd expect to be raised.
@ixs Are you planning to implement the review feedback on this? |
@ixs We did not hear back from you on the review feedback so we are going to close this. When you're ready to come back to it, please leave a comment here and we will happily re-open it. Thanks! |
@cachedout thanks for the review and the suggestions. Finally gotten around to finish this. Please reopen. |
@ixs I have re-opened this. Looking forward to seeing your updates. :) |
@rallytime Thanks. The new update is already up on github and I believe this PR has the latest changes with all requested work done. |
Hi @ixs - This change is causing some test failures in the https://jenkins.saltstack.com/job/PR/job/salt-pr-linode-cent7-py3/4121/#showFailuresLink |
This would happen if someone runs on bare metal, an admittedly increasingly rare situation...
@rallytime Ohh, that's new. Fixed. |
What does this PR do?
Add new grains to parse hypervisor details
New Behavior
Xen exposes a lot of detail about the hypervisor in /sys/hypervisor.
Parse the hypervisor version and the capabilities and expose these
as grains.
This allows for detailed targeting and appropriate actions, e.g. when
running on XenServer install the XenServer guest-tools.
Sample output:
Tests written?
No. It looked like the whole _virtual part has no tests at all.
Commits signed with GPG?
No