mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix UnicodeDecodeError when parsing hosts file with non-ascii
This commit is contained in:
parent
e6a4744f85
commit
5e62d6d45f
1 changed files with 12 additions and 8 deletions
|
@ -148,14 +148,18 @@ def _generate_minion_id():
|
|||
addr=hosts.first() or 'localhost (N/A)', message=socket.gaierror)
|
||||
)
|
||||
# Universal method for everywhere (Linux, Slowlaris, Windows etc)
|
||||
for f_name in ['/etc/hostname', '/etc/nodename', '/etc/hosts',
|
||||
r'{win}\system32\drivers\etc\hosts'.format(win=os.getenv('WINDIR'))]:
|
||||
if not os.path.exists(f_name):
|
||||
continue
|
||||
with salt.utils.files.fopen(f_name) as f_hdl:
|
||||
for hst in (line.strip().split('#')[0].strip().split() or None for line in f_hdl.read().split(os.linesep)):
|
||||
if hst and (hst[0][:4] in ['127.', '::1'] or len(hst) == 1):
|
||||
hosts.extend(hst)
|
||||
for f_name in ('/etc/hostname', '/etc/nodename', '/etc/hosts',
|
||||
r'{win}\system32\drivers\etc\hosts'.format(win=os.getenv('WINDIR'))):
|
||||
try:
|
||||
with salt.utils.files.fopen(f_name) as f_hdl:
|
||||
for line in f_hdl:
|
||||
line = salt.utils.stringutils.to_unicode(line)
|
||||
hst = line.strip().split('#')[0].strip().split()
|
||||
if hst:
|
||||
if hst[0][:4] in ('127.', '::1') or len(hst) == 1:
|
||||
hosts.extend(hst)
|
||||
except IOError:
|
||||
pass
|
||||
|
||||
# include public and private ipaddresses
|
||||
return hosts.extend([addr for addr in ip_addrs()
|
||||
|
|
Loading…
Add table
Reference in a new issue