Skip to content

Commit

Permalink
fix: don't erase previous records from /etc/hosts (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: Iman Enami <[email protected]>
Co-authored-by: phvalguima <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 3b6aedf commit 6ae6bb4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/managers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,31 @@ def _update_environment(self, env: dict[str, str]) -> None:

self.workload.write(content=content, path="/etc/environment")

def _parse_etc_hosts(self) -> dict[str, str]:
"""Returns a mapping of `FQDN hostname` to IP addresses based on /etc/hosts entries."""
records = {
line for line in self.workload.read(path="/etc/hosts") if not line.startswith("#")
}
mapping = {}
for record in records:
parts = record.strip().split()
if parts and len(parts) > 1:
mapping[" ".join(parts[1:])] = parts[0]
return mapping

def _update_etc_hosts(self, entries: list[str]) -> list[str]:
"""Updates previous /etc/hosts entries based on new list of entries in the format `IP FQDN HOSTNAME`."""
mapping = self._parse_etc_hosts()
for entry in entries:
parts = entry.strip().split()
if parts and len(parts) > 1:
mapping[" ".join(parts[1:])] = parts[0]
return [f"{v} {k}" for k, v in mapping.items()]

def set_etc_hosts(self) -> None:
"""Writes to /etc/hosts with peer-related units."""
self.workload.write(content="\n".join(self.etc_hosts_entries), path="/etc/hosts")
updated_etc_hosts = self._update_etc_hosts(self.etc_hosts_entries)
self.workload.write(content=("\n".join(updated_etc_hosts) + "\n"), path="/etc/hosts")

def set_jaas_config(self) -> None:
"""Sets the ZooKeeper JAAS config."""
Expand Down

0 comments on commit 6ae6bb4

Please sign in to comment.