Skip to content

Commit 47dfb46

Browse files
authored
Merge pull request #370 from ribetm/best_effort_osrelease
Handle os-release without version or codename
2 parents 5fdda58 + 918ab91 commit 47dfb46

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

netbox_agent/misc.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import suppress
12
from netbox_agent.config import netbox_instance as nb
23
from slugify import slugify
34
from shutil import which
@@ -28,15 +29,17 @@ def get_device_type(type):
2829

2930
def get_device_platform(device_platform):
3031
if device_platform is None:
31-
try:
32-
linux_distribution = "{name} {version_id} {release_codename}".format(
33-
**distro.os_release_info()
34-
)
35-
36-
if not linux_distribution:
37-
return None
38-
except (ModuleNotFoundError, NameError, AttributeError):
39-
return None
32+
os_release = distro.os_release_info()
33+
# Only `name` is a required field in os-release
34+
for template in (
35+
"{name} {version_id} {release_codename}",
36+
"{name} {version_id}",
37+
):
38+
with suppress(KeyError):
39+
linux_distribution = template.format(**os_release)
40+
break
41+
else:
42+
linux_distribution = os_release["name"]
4043
else:
4144
linux_distribution = device_platform
4245

0 commit comments

Comments
 (0)