mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix inline comments parsing in hosts.rm_host
Converts string to bytes to avoid the error "Passed invalid arguments: a bytes-like object is required, not 'str'." when hosts.rm_host is used in a hosts file that have host entries with inline comments.
This commit is contained in:
parent
88769f2058
commit
be7f141ce1
2 changed files with 9 additions and 17 deletions
1
changelog/60351.fixed
Normal file
1
changelog/60351.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Now hosts.rm_host parses /etc/hosts that have comments in hosts entries and is able to remove hosts entries or aliases.
|
|
@ -1,22 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Manage the information in the hosts file
|
||||
"""
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import errno
|
||||
import logging
|
||||
import os
|
||||
|
||||
# Import salt libs
|
||||
import salt.utils.files
|
||||
import salt.utils.odict as odict
|
||||
import salt.utils.stringutils
|
||||
|
||||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
from salt.ext.six.moves import range
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -67,7 +60,7 @@ def _list_hosts():
|
|||
if not line:
|
||||
continue
|
||||
if line.startswith("#"):
|
||||
ret.setdefault("comment-{0}".format(count), []).append(line)
|
||||
ret.setdefault("comment-{}".format(count), []).append(line)
|
||||
count += 1
|
||||
continue
|
||||
comment = None
|
||||
|
@ -81,7 +74,7 @@ def _list_hosts():
|
|||
ret.setdefault(ip, {}).update({"comment": comment})
|
||||
else:
|
||||
ret.setdefault(ip, {}).setdefault("aliases", []).extend(comps)
|
||||
except (IOError, OSError) as exc:
|
||||
except OSError as exc:
|
||||
salt.utils.files.process_read_exception(exc, hfn, ignore=errno.ENOENT)
|
||||
# Don't set __context__ since we weren't able to read from the
|
||||
# hosts file.
|
||||
|
@ -257,7 +250,7 @@ def rm_host(ip, alias):
|
|||
comps = tmpline.split()
|
||||
comment = None
|
||||
if b"#" in tmpline:
|
||||
host_info, comment = tmpline.split("#")
|
||||
host_info, comment = tmpline.split(b"#")
|
||||
comment = salt.utils.stringutils.to_bytes(comment).lstrip()
|
||||
else:
|
||||
host_info = tmpline
|
||||
|
@ -314,7 +307,7 @@ def add_host(ip, alias):
|
|||
__context__.pop("hosts._list_hosts", None)
|
||||
|
||||
inserted = False
|
||||
for i, h in six.iteritems(hosts):
|
||||
for i, h in hosts.items():
|
||||
for j in range(len(h)):
|
||||
if isinstance(h, list):
|
||||
if h[j].startswith("#") and i == ip:
|
||||
|
@ -363,17 +356,17 @@ def set_comment(ip, comment):
|
|||
|
||||
def _write_hosts(hosts):
|
||||
lines = []
|
||||
for ip, host_info in six.iteritems(hosts):
|
||||
for ip, host_info in hosts.items():
|
||||
if ip:
|
||||
if ip.startswith("comment"):
|
||||
line = "".join(host_info)
|
||||
else:
|
||||
if "comment" in host_info:
|
||||
line = "{0}\t\t{1}\t\t# {2}".format(
|
||||
line = "{}\t\t{}\t\t# {}".format(
|
||||
ip, " ".join(host_info["aliases"]), host_info["comment"]
|
||||
)
|
||||
else:
|
||||
line = "{0}\t\t{1}".format(ip, " ".join(host_info["aliases"]))
|
||||
line = "{}\t\t{}".format(ip, " ".join(host_info["aliases"]))
|
||||
lines.append(line)
|
||||
|
||||
hfn = _get_or_create_hostfile()
|
||||
|
@ -383,7 +376,5 @@ def _write_hosts(hosts):
|
|||
# /etc/hosts needs to end with a newline so that some utils
|
||||
# that read it do not break
|
||||
ofile.write(
|
||||
salt.utils.stringutils.to_str(
|
||||
line.strip() + six.text_type(os.linesep)
|
||||
)
|
||||
salt.utils.stringutils.to_str(line.strip() + str(os.linesep))
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue