From 695353ee91989956f9006ae3e24e2094739285aa Mon Sep 17 00:00:00 2001 From: David Murphy < dmurphy@saltstack.com> Date: Thu, 7 Mar 2024 16:24:54 -0700 Subject: [PATCH] Updated to use grains/core.py get_machine_id for machine identifier --- salt/utils/files.py | 22 ++++++++++---------- salt/utils/gitfs.py | 4 +++- salt/utils/process.py | 4 +++- tests/pytests/unit/utils/test_gitfs_locks.py | 10 ++++++--- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/salt/utils/files.py b/salt/utils/files.py index 5f89e6aba98..d735013da88 100644 --- a/salt/utils/files.py +++ b/salt/utils/files.py @@ -907,14 +907,14 @@ def get_encoding(path): ## TBD DGM just parking here till final machine identifier work is done -def get_machine_identifier(): - """ - Provide the machine-identifier for machine/virtualization combination - """ - 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 fopen(existing_locations[0]) as machineid: - return machineid.read().strip() +## def get_machine_identifier(): +## """ +## Provide the machine-identifier for machine/virtualization combination +## """ +## 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 fopen(existing_locations[0]) as machineid: +## return machineid.read().strip() diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py index defea96d426..4fef2925dfb 100644 --- a/salt/utils/gitfs.py +++ b/salt/utils/gitfs.py @@ -39,6 +39,7 @@ import salt.utils.versions from salt.config import DEFAULT_HASH_TYPE from salt.config import DEFAULT_MASTER_OPTS as _DEFAULT_MASTER_OPTS from salt.exceptions import FileserverConfigError, GitLockError, get_error_message +from salt.grains.core import get_machine_id from salt.utils.event import tagify from salt.utils.odict import OrderedDict from salt.utils.process import os_is_running as pid_exists @@ -250,7 +251,8 @@ class GitProvider: # DGM try getting machine_identifier # get machine_identifier - self.mach_id = salt.utils.files.get_machine_identifier() + ## self.mach_id = salt.utils.files.get_machine_identifier() + self.mach_id = get_machine_id().get("machine_id", "no_machine_id_available") log.debug("DGM machine_id for lock file, machine_id %s", self.mach_id) self.global_saltenv = salt.utils.data.repack_dictlist( diff --git a/salt/utils/process.py b/salt/utils/process.py index b49a907ee93..80c3a65cae9 100644 --- a/salt/utils/process.py +++ b/salt/utils/process.py @@ -30,6 +30,7 @@ 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__) @@ -1075,7 +1076,8 @@ class SignalHandlingProcess(Process): msg += ". Exiting" log.debug(msg) - mach_id = salt.utils.files.get_machine_identifier() + ## mach_id = salt.utils.files.get_machine_identifier() + mach_id = get_machine_id().get("machine_id", "no_machine_id_available") dbg_msg = ( f"exiting for process id '{os.getpid()}' and machine identifer '{mach_id}'" ) diff --git a/tests/pytests/unit/utils/test_gitfs_locks.py b/tests/pytests/unit/utils/test_gitfs_locks.py index b5eb1fe40a7..4cb5539e57f 100644 --- a/tests/pytests/unit/utils/test_gitfs_locks.py +++ b/tests/pytests/unit/utils/test_gitfs_locks.py @@ -20,6 +20,7 @@ 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 @@ -404,7 +405,8 @@ def test_git_provider_mp_gen_lock(main_class, caplog): """ # DGM try getting machine_identifier # get machine_identifier - mach_id = salt.utils.files.get_machine_identifier() + ## mach_id = salt.utils.files.get_machine_identifier() + mach_id = get_machine_id().get("machine_id", "no_machine_id_available") cur_pid = os.getpid() test_msg1 = ( @@ -437,7 +439,8 @@ def test_git_provider_mp_lock_dead_pid(main_class, caplog): """ # DGM try getting machine_identifier # get machine_identifier - mach_id = salt.utils.files.get_machine_identifier() + ## mach_id = salt.utils.files.get_machine_identifier() + mach_id = get_machine_id().get("machine_id", "no_machine_id_available") cur_pid = os.getpid() test_msg1 = ( @@ -508,7 +511,8 @@ def test_git_provider_mp_lock_bad_machine(main_class, caplog): """ # DGM try getting machine_identifier # get machine_identifier - mach_id = salt.utils.files.get_machine_identifier() + ## mach_id = salt.utils.files.get_machine_identifier() + mach_id = get_machine_id().get("machine_id", "no_machine_id_available") cur_pid = os.getpid() provider = main_class.remotes[0]