Skip to content

Commit ad8e1e3

Browse files
committed
allow configuration of /etc/hosts entries
1 parent 5f96f56 commit ad8e1e3

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

seedemu/layers/EtcHosts.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from seedemu.core import Emulator, Layer, Node
22
from seedemu.core.enums import NetworkType
3+
from typing import List
34

45
class EtcHosts(Layer):
56
"""!
@@ -8,16 +9,19 @@ class EtcHosts(Layer):
89
This layer setups host names for all nodes.
910
"""
1011

11-
def __init__(self):
12+
def __init__(self, only_hosts: bool = True):
1213
"""!
1314
@brief EtcHosts Layer constructor
15+
@param only_hosts whether or not to create entries
16+
for all nodes inluding routers etc. or just hosts
1417
"""
18+
self._only_hosts = only_hosts
1519
super().__init__()
1620
self.addDependency('Base', False, False)
1721

1822
def getName(self) -> str:
1923
return "EtcHosts"
20-
24+
2125
def __getAllIpAddress(self, node: Node) -> list:
2226
"""!
2327
@brief Get the IP address of the local interface for this node.
@@ -31,22 +35,28 @@ def __getAllIpAddress(self, node: Node) -> list:
3135
pass
3236
else:
3337
addresses.append(address)
34-
38+
3539
return addresses
3640

41+
def _getSupportedNodeTypes(self) -> List[str]:
42+
if self._only_hosts:
43+
return ['hnode']
44+
else:
45+
return ['hnode', 'snode', 'rnode', 'rs']
46+
3747
def render(self, emulator: Emulator):
3848
hosts_file_content = []
3949
nodes = []
4050
reg = emulator.getRegistry()
4151
for ((scope, type, name), node) in reg.getAll().items():
42-
if type in ['hnode', 'snode', 'rnode', 'rs']:
52+
if type in self._getSupportedNodeTypes():
4353
addresses = self.__getAllIpAddress(node)
4454
for address in addresses:
4555
hosts_file_content.append(f"{address} {' '.join(node.getHostNames())}")
4656
nodes.append(node)
4757

4858
sorted_hosts_file_content = sorted(hosts_file_content, key=lambda x: tuple(map(int, x.split()[0].split('.'))))
49-
59+
5060
for node in nodes:
5161
node.setFile("/tmp/etc-hosts", '\n'.join(sorted_hosts_file_content))
5262
node.insertStartCommand(0, "cat /tmp/etc-hosts >> /etc/hosts")

0 commit comments

Comments
 (0)