1
1
from seedemu .core import Emulator , Layer , Node
2
2
from seedemu .core .enums import NetworkType
3
+ from typing import List
3
4
4
5
class EtcHosts (Layer ):
5
6
"""!
@@ -8,16 +9,19 @@ class EtcHosts(Layer):
8
9
This layer setups host names for all nodes.
9
10
"""
10
11
11
- def __init__ (self ):
12
+ def __init__ (self , only_hosts : bool = True ):
12
13
"""!
13
14
@brief EtcHosts Layer constructor
15
+ @param only_hosts whether or not to create entries
16
+ for all nodes inluding routers etc. or just hosts
14
17
"""
18
+ self ._only_hosts = only_hosts
15
19
super ().__init__ ()
16
20
self .addDependency ('Base' , False , False )
17
21
18
22
def getName (self ) -> str :
19
23
return "EtcHosts"
20
-
24
+
21
25
def __getAllIpAddress (self , node : Node ) -> list :
22
26
"""!
23
27
@brief Get the IP address of the local interface for this node.
@@ -31,22 +35,28 @@ def __getAllIpAddress(self, node: Node) -> list:
31
35
pass
32
36
else :
33
37
addresses .append (address )
34
-
38
+
35
39
return addresses
36
40
41
+ def _getSupportedNodeTypes (self ) -> List [str ]:
42
+ if self ._only_hosts :
43
+ return ['hnode' ]
44
+ else :
45
+ return ['hnode' , 'snode' , 'rnode' , 'rs' ]
46
+
37
47
def render (self , emulator : Emulator ):
38
48
hosts_file_content = []
39
49
nodes = []
40
50
reg = emulator .getRegistry ()
41
51
for ((scope , type , name ), node ) in reg .getAll ().items ():
42
- if type in [ 'hnode' , 'snode' , 'rnode' , 'rs' ] :
52
+ if type in self . _getSupportedNodeTypes () :
43
53
addresses = self .__getAllIpAddress (node )
44
54
for address in addresses :
45
55
hosts_file_content .append (f"{ address } { ' ' .join (node .getHostNames ())} " )
46
56
nodes .append (node )
47
57
48
58
sorted_hosts_file_content = sorted (hosts_file_content , key = lambda x : tuple (map (int , x .split ()[0 ].split ('.' ))))
49
-
59
+
50
60
for node in nodes :
51
61
node .setFile ("/tmp/etc-hosts" , '\n ' .join (sorted_hosts_file_content ))
52
62
node .insertStartCommand (0 , "cat /tmp/etc-hosts >> /etc/hosts" )
0 commit comments