Created
September 7, 2021 22:21
-
-
Save abenbachir/9fc4e594b3b88c43c65293c349f3f714 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import os.path | |
import platform | |
def is_vm_supported_for_extension(): | |
""" | |
Checks if the VM this extension is running on is supported by OMSAgent | |
Returns for platform.linux_distribution() vary widely in format, such as | |
'7.3.1611' returned for a VM with CentOS 7, so the first provided | |
digits must match | |
The supported distros of the OMSAgent-for-Linux are allowed to utilize | |
this VM extension. All other distros will get error code 51 | |
""" | |
supported_dists = {'redhat' : ['6', '7', '8'], 'red hat' : ['6', '7', '8'], 'rhel' : ['6', '7', '8'], # Red Hat | |
'centos' : ['6', '7', '8'], # CentOS | |
'oracle' : ['6', '7', '8'], 'ol': ['6', '7', '8'], # Oracle | |
'debian' : ['8', '9'], # Debian | |
'ubuntu' : ['14.04', '16.04', '18.04', '20.04'], # Ubuntu | |
'suse' : ['12', '15'], 'sles' : ['12', '15'], # SLES | |
'amzn' : ['2'] # AWS | |
} | |
vm_dist, vm_ver, vm_supported = '', '', False | |
try: | |
print("platform.linux_distribution()=", platform.linux_distribution()) | |
vm_dist, vm_ver, vm_id = platform.linux_distribution() | |
except AttributeError: | |
try: | |
print("platform.dist()=", platform.dist()) | |
vm_dist, vm_ver, vm_id = platform.dist() | |
except AttributeError: | |
print("Falling back to /etc/os-release distribution parsing") | |
# Fallback if either of the above fail; on some (especially newer) | |
# distros, linux_distribution() and dist() are unreliable or deprecated | |
if not vm_dist and not vm_ver: | |
try: | |
print("opening /etc/os-release") | |
with open('/etc/os-release', 'r') as fp: | |
for line in fp: | |
print(line) | |
if line.startswith('ID='): | |
vm_dist = line.split('=')[1] | |
vm_dist = vm_dist.split('-')[0] | |
vm_dist = vm_dist.replace('\"', '').replace('\n', '') | |
elif line.startswith('VERSION_ID='): | |
vm_ver = line.split('=')[1] | |
vm_ver = vm_ver.replace('\"', '').replace('\n', '') | |
print("parsed string from /etc/os-release: vm_dist=", vm_dist, "vm_ver=", vm_ver) | |
except: | |
return vm_supported, 'Indeterminate operating system', '' | |
# Find this VM distribution in the supported list | |
for supported_dist in list(supported_dists.keys()): | |
if not vm_dist.lower().startswith(supported_dist): | |
continue | |
# Check if this VM distribution version is supported | |
vm_ver_split = vm_ver.split('.') | |
for supported_ver in supported_dists[supported_dist]: | |
supported_ver_split = supported_ver.split('.') | |
# If vm_ver is at least as precise (at least as many digits) as | |
# supported_ver and matches all the supported_ver digits, then | |
# this VM is guaranteed to be supported | |
vm_ver_match = True | |
for idx, supported_ver_num in enumerate(supported_ver_split): | |
try: | |
print("idx=", idx, "supported_ver_num=", supported_ver_num) | |
supported_ver_num = int(supported_ver_num) | |
print("vm_ver_split[idx]=", vm_ver_split[idx]) | |
vm_ver_num = int(vm_ver_split[idx]) | |
except IndexError: | |
vm_ver_match = False | |
break | |
if vm_ver_num is not supported_ver_num: | |
vm_ver_match = False | |
break | |
if vm_ver_match: | |
vm_supported = True | |
break | |
if vm_supported: | |
break | |
return vm_supported, vm_dist, vm_ver | |
print(is_vm_supported_for_extension()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment