Fix UnicodeDecodeError when parsing hosts file with non-ascii

This commit is contained in:
Erik Johnson 2018-06-13 01:12:58 -05:00
parent e6a4744f85
commit 5e62d6d45f
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -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()