Skip to content

Commit 5281e81

Browse files
authored
Fix: no underscores in topology.name generated from input directory (#2497)
Underscores in topology.name are used by clab to generate hostnames, and some Linux distros don't grasp that a clumsy person might have underscores in hostnames. This fix ensures there are no weird characters in topology.name that is generated from the input directory (and 'default' is used when nothing is left after the cleanup). We don't check what the user specifies in the 'name' argument (they should know better ;) Fixes #2424
1 parent 24d5fa0 commit 5281e81

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

netsim/augment/topology.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,33 @@
1515
from ..data.validate import validate_attributes,get_object_attributes
1616
from ..data.types import must_be_list,must_be_string,must_be_dict
1717

18+
"""
19+
Generate topology name from the lab topology file location:
20+
21+
* Take the first entry in the 'input' list (the original topology name)
22+
* Expand it to full path, then take the rightmost directory name from the path
23+
* Make an ID out of the directory name (removing everything but ASCII letters
24+
and numbers)
25+
* Remove underscores from ID (see #2424)
26+
* If nothing is left, assume the topology name is 'default'
27+
"""
28+
def name_from_path(topology: Box) -> str:
29+
topo_name = os.path.basename(os.path.dirname(os.path.realpath(topology['input'][0])))
30+
topo_name = strings.make_id(topo_name).replace('_','')
31+
if not topo_name:
32+
topo_name = 'default'
33+
return topo_name
34+
35+
"""
36+
Basic topology sanity check:
37+
38+
* It should have a name (set one from lab topology directory if needed) that
39+
should be a string
40+
* The 'module' attribute (if present) must be a list
41+
"""
1842
def topology_sanity_check(topology: Box) -> None:
1943
if not 'name' in topology:
20-
topo_name = os.path.basename(os.path.dirname(os.path.realpath(topology['input'][0])))[:12]
21-
for bad_char in (' ','.'):
22-
topo_name = topo_name.replace(bad_char,'_')
23-
topology.name = strings.make_id(topo_name)
44+
topology.name = name_from_path(topology)[:12]
2445

2546
if 'module' in topology:
2647
must_be_list(topology,'module','')
@@ -29,6 +50,9 @@ def topology_sanity_check(topology: Box) -> None:
2950
if must_be_string(topology,'name','',module='topology'):
3051
topology.defaults.name = topology.name
3152

53+
"""
54+
Check required topology elements (currently: nodes)
55+
"""
3256
def check_required_elements(topology: Box) -> None:
3357
invalid_topo = False
3458
for rq in ['nodes']:

0 commit comments

Comments
 (0)