Moved get_machine_identifier to salt utils platfrom as per reviewers request

This commit is contained in:
David Murphy 2024-03-28 14:13:38 -06:00 committed by Daniel Wozniak
parent fcbb59cf17
commit 848c7ad5bf
7 changed files with 28 additions and 33 deletions

View file

@ -39,6 +39,7 @@ import salt.utils.pkg.rpm
import salt.utils.platform import salt.utils.platform
import salt.utils.stringutils import salt.utils.stringutils
from salt.utils.network import _clear_interfaces, _get_interfaces from salt.utils.network import _clear_interfaces, _get_interfaces
from salt.utils.platform import get_machine_identifier as _get_machine_identifier
from salt.utils.platform import linux_distribution as _linux_distribution from salt.utils.platform import linux_distribution as _linux_distribution
try: try:
@ -3048,7 +3049,7 @@ def get_machine_id():
if platform.system() == "AIX": if platform.system() == "AIX":
return _aix_get_machine_id() return _aix_get_machine_id()
return salt.utils.files.get_machine_identifier() return _get_machine_identifier()
def cwd(): def cwd():

View file

@ -905,18 +905,3 @@ def get_encoding(path):
return "ASCII" return "ASCII"
raise CommandExecutionError("Could not detect file encoding") raise CommandExecutionError("Could not detect file encoding")
def get_machine_identifier():
"""
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

@ -41,6 +41,7 @@ from salt.config import DEFAULT_MASTER_OPTS as _DEFAULT_MASTER_OPTS
from salt.exceptions import FileserverConfigError, GitLockError, get_error_message from salt.exceptions import FileserverConfigError, GitLockError, get_error_message
from salt.utils.event import tagify from salt.utils.event import tagify
from salt.utils.odict import OrderedDict from salt.utils.odict import OrderedDict
from salt.utils.platform import get_machine_identifier as _get_machine_identifier
from salt.utils.process import os_is_running as pid_exists from salt.utils.process import os_is_running as pid_exists
from salt.utils.versions import Version from salt.utils.versions import Version
@ -249,7 +250,7 @@ class GitProvider:
return str(y) return str(y)
# get machine_identifier # get machine_identifier
self.mach_id = salt.utils.files.get_machine_identifier().get( self.mach_id = _get_machine_identifier().get(
"machine_id", "no_machine_id_available" "machine_id", "no_machine_id_available"
) )

View file

@ -12,6 +12,7 @@ import sys
import distro import distro
from salt.utils.decorators import memoize as real_memoize from salt.utils.decorators import memoize as real_memoize
from salt.utils.files import fopen as _fopen
def linux_distribution(full_distribution_name=True): def linux_distribution(full_distribution_name=True):
@ -239,3 +240,18 @@ def spawning_platform():
Salt, however, will force macOS to spawning by default on all python versions Salt, however, will force macOS to spawning by default on all python versions
""" """
return multiprocessing.get_start_method(allow_none=False) == "spawn" return multiprocessing.get_start_method(allow_none=False) == "spawn"
def get_machine_identifier():
"""
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 _fopen(existing_locations[0]) as machineid:
return {"machine_id": machineid.read().strip()}

View file

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

View file

@ -2,10 +2,10 @@ import os.path
import pytest import pytest
import salt.utils.files
from salt.fileserver.gitfs import PER_REMOTE_ONLY, PER_REMOTE_OVERRIDES from salt.fileserver.gitfs import PER_REMOTE_ONLY, PER_REMOTE_OVERRIDES
from salt.utils.gitfs import GitFS, GitPython, Pygit2 from salt.utils.gitfs import GitFS, GitPython, Pygit2
from salt.utils.immutabletypes import ImmutableDict, ImmutableList from salt.utils.immutabletypes import ImmutableDict, ImmutableList
from salt.utils.platform import get_machine_identifier as _get_machine_identifier
pytestmark = [ pytestmark = [
pytest.mark.slow_test, pytest.mark.slow_test,
@ -249,9 +249,7 @@ def _test_lock(opts):
g.fetch_remotes() g.fetch_remotes()
assert len(g.remotes) == 1 assert len(g.remotes) == 1
repo = g.remotes[0] repo = g.remotes[0]
mach_id = salt.utils.files.get_machine_identifier().get( mach_id = _get_machine_identifier().get("machine_id", "no_machine_id_available")
"machine_id", "no_machine_id_available"
)
assert repo.get_salt_working_dir() in repo._get_lock_file() assert repo.get_salt_working_dir() in repo._get_lock_file()
assert repo.lock() == ( assert repo.lock() == (
[ [

View file

@ -20,6 +20,7 @@ import salt.utils.path
import salt.utils.platform import salt.utils.platform
import salt.utils.process import salt.utils.process
from salt.utils.immutabletypes import freeze from salt.utils.immutabletypes import freeze
from salt.utils.platform import get_machine_identifier as _get_machine_identifier
from salt.utils.verify import verify_env from salt.utils.verify import verify_env
try: try:
@ -379,9 +380,7 @@ def test_git_provider_mp_gen_lock(main_class, caplog):
Check that gen_lock is obtains lock, and then releases, provider.lock() Check that gen_lock is obtains lock, and then releases, provider.lock()
""" """
# get machine_identifier # get machine_identifier
mach_id = salt.utils.files.get_machine_identifier().get( mach_id = _get_machine_identifier().get("machine_id", "no_machine_id_available")
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid() cur_pid = os.getpid()
test_msg1 = ( test_msg1 = (
@ -418,9 +417,7 @@ 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 Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
""" """
# get machine_identifier # get machine_identifier
mach_id = salt.utils.files.get_machine_identifier().get( mach_id = _get_machine_identifier().get("machine_id", "no_machine_id_available")
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid() cur_pid = os.getpid()
test_msg1 = ( test_msg1 = (
@ -493,9 +490,7 @@ 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 Check that lock obtains lock, if previous pid in lock file doesn't exist for same machine id
""" """
# get machine_identifier # get machine_identifier
mach_id = salt.utils.files.get_machine_identifier().get( mach_id = _get_machine_identifier().get("machine_id", "no_machine_id_available")
"machine_id", "no_machine_id_available"
)
cur_pid = os.getpid() cur_pid = os.getpid()
provider = main_class.remotes[0] provider = main_class.remotes[0]