mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #46649 from terminalmage/issue46595
Make server_id consistent on Python 3
This commit is contained in:
commit
0c2dce0416
1 changed files with 24 additions and 1 deletions
|
@ -2404,7 +2404,30 @@ def get_server_id():
|
|||
|
||||
if salt.utils.is_proxy():
|
||||
return {}
|
||||
return {'server_id': abs(hash(__opts__.get('id', '')) % (2 ** 31))}
|
||||
id_ = __opts__.get('id', '')
|
||||
id_hash = None
|
||||
py_ver = sys.version_info[:2]
|
||||
if py_ver >= (3, 3):
|
||||
# Python 3.3 enabled hash randomization, so we need to shell out to get
|
||||
# a reliable hash.
|
||||
py_bin = 'python{0}.{1}'.format(*py_ver)
|
||||
id_hash = __salt__['cmd.run'](
|
||||
[py_bin, '-c', 'print(hash("{0}"))'.format(id_)],
|
||||
env={'PYTHONHASHSEED': '0'}
|
||||
)
|
||||
try:
|
||||
id_hash = int(id_hash)
|
||||
except (TypeError, ValueError):
|
||||
log.debug(
|
||||
'Failed to hash the ID to get the server_id grain. Result of '
|
||||
'hash command: %s', id_hash
|
||||
)
|
||||
id_hash = None
|
||||
if id_hash is None:
|
||||
# Python < 3.3 or error encountered above
|
||||
id_hash = hash(id_)
|
||||
|
||||
return {'server_id': abs(id_hash % (2 ** 31))}
|
||||
|
||||
|
||||
def get_master():
|
||||
|
|
Loading…
Add table
Reference in a new issue