Skip to content

allow configuration of /etc/hosts entries #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions seedemu/layers/EtcHosts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from seedemu.core import Emulator, Layer, Node
from seedemu.core.enums import NetworkType
from typing import List

class EtcHosts(Layer):
"""!
Expand All @@ -8,16 +9,19 @@ class EtcHosts(Layer):
This layer setups host names for all nodes.
"""

def __init__(self):
def __init__(self, only_hosts: bool = True):
"""!
@brief EtcHosts Layer constructor
@param only_hosts whether or not to create entries
for all nodes inluding routers etc. or just hosts
"""
self._only_hosts = only_hosts
super().__init__()
self.addDependency('Base', False, False)

def getName(self) -> str:
return "EtcHosts"

def __getAllIpAddress(self, node: Node) -> list:
"""!
@brief Get the IP address of the local interface for this node.
Expand All @@ -31,22 +35,28 @@ def __getAllIpAddress(self, node: Node) -> list:
pass
else:
addresses.append(address)

return addresses

def _getSupportedNodeTypes(self) -> List[str]:
if self._only_hosts:
return ['hnode']
else:
return ['hnode', 'snode', 'rnode', 'rs']

def render(self, emulator: Emulator):
hosts_file_content = []
nodes = []
reg = emulator.getRegistry()
for ((scope, type, name), node) in reg.getAll().items():
if type in ['hnode', 'snode', 'rnode', 'rs']:
if type in self._getSupportedNodeTypes():
addresses = self.__getAllIpAddress(node)
for address in addresses:
hosts_file_content.append(f"{address} {' '.join(node.getHostNames())}")
nodes.append(node)

sorted_hosts_file_content = sorted(hosts_file_content, key=lambda x: tuple(map(int, x.split()[0].split('.'))))

for node in nodes:
node.setFile("/tmp/etc-hosts", '\n'.join(sorted_hosts_file_content))
node.insertStartCommand(0, "cat /tmp/etc-hosts >> /etc/hosts")
2 changes: 1 addition & 1 deletion tests/internet/host_mgmt/emulator-code/test-emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from seedemu.core import Emulator

emu = Emulator()
etc_hosts = EtcHosts()
etc_hosts = EtcHosts(only_hosts=False)

# Load the pre-built mini-internet component
emu.load('../../mini_internet/emulator-code/base-component.bin')
Expand Down