Moved location for finding machine_id to salt utils files, and had grains import from there

This commit is contained in:
David Murphy 2024-03-19 18:07:22 -06:00 committed by Daniel Wozniak
parent 37164221e5
commit f98b7073cd
4 changed files with 28 additions and 13 deletions

View file

@ -3048,13 +3048,7 @@ def get_machine_id():
if platform.system() == "AIX":
return _aix_get_machine_id()
locations = ["/etc/machine-id", "/var/lib/dbus/machine-id"]
existing_locations = [loc for loc in locations if os.path.exists(loc)]
if not existing_locations:
return {}
else:
with salt.utils.files.fopen(existing_locations[0]) as machineid:
return {"machine_id": machineid.read().strip()}
return salt.utils.files.local_get_machine_id()
def cwd():

View file

@ -904,3 +904,18 @@ def get_encoding(path):
return "ASCII"
raise CommandExecutionError("Could not detect file encoding")
def local_get_machine_id():
"""
Provide the machine-id for machine/virtualization combination
"""
# Provides:
# machine-id
locations = ["/etc/machine-id", "/var/lib/dbus/machine-id"]
existing_locations = [loc for loc in locations if os.path.exists(loc)]
if not existing_locations:
return {}
else:
with salt.utils.files.fopen(existing_locations[0]) as machineid:
return {"machine_id": machineid.read().strip()}

View file

@ -30,7 +30,6 @@ import salt.utils.path
import salt.utils.platform
import salt.utils.versions
from salt.ext.tornado import gen
from salt.grains.core import get_machine_id
log = logging.getLogger(__name__)
@ -1076,7 +1075,9 @@ class SignalHandlingProcess(Process):
msg += ". Exiting"
log.debug(msg)
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
mach_id = salt.utils.files.local_get_machine_id().get(
"machine_id", "no_machine_id_available"
)
log.debug(
"exiting for process id %s and machine identifer %s", os.getpid(), mach_id
)

View file

@ -19,7 +19,6 @@ import salt.utils.gitfs
import salt.utils.path
import salt.utils.platform
import salt.utils.process
from salt.grains.core import get_machine_id
from salt.utils.immutabletypes import freeze
from salt.utils.verify import verify_env
@ -378,7 +377,9 @@ def test_git_provider_mp_gen_lock(main_class, caplog):
Check that gen_lock is obtains lock, and then releases, provider.lock()
"""
# get machine_identifier
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
mach_id = salt.utils.files.local_get_machine_id().get(
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid()
test_msg1 = (
@ -412,7 +413,9 @@ def test_git_provider_mp_lock_dead_pid(main_class, caplog):
Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
"""
# get machine_identifier
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
mach_id = salt.utils.files.local_get_machine_id().get(
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid()
test_msg1 = (
@ -484,7 +487,9 @@ def test_git_provider_mp_lock_bad_machine(main_class, caplog):
Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
"""
# get machine_identifier
mach_id = get_machine_id().get("machine_id", "no_machine_id_available")
mach_id = salt.utils.files.local_get_machine_id().get(
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid()
provider = main_class.remotes[0]