@@ -100,7 +100,12 @@ def add_host(self, name, cls=DAQHost, ip_addr=None, env_vars=None, vol_maps=None
100100 params ['tmpdir' ] = os .path .join (tmpdir , 'nodes' ) if tmpdir else None
101101 params ['env_vars' ] = env_vars if env_vars else []
102102 params ['vol_maps' ] = vol_maps if vol_maps else []
103- host = self .net .addHost (name , cls , ** params )
103+ try :
104+ host = self ._retry_func (partial (self .net .addHost , name , cls , ** params ))
105+ except Exception as e :
106+ # If addHost fails, ip allocation needs to be explicityly cleaned up.
107+ self ._reset_mininet_next_ip ()
108+ raise e
104109 try :
105110 switch_link = self ._retry_func (
106111 partial (self .net .addLink , self .pri , host , port1 = port , fast = False ))
@@ -145,14 +150,17 @@ def _switch_del_intf(self, switch, intf):
145150 del switch .ports [intf ]
146151 del switch .nameToIntf [intf .name ]
147152
153+ def _reset_mininet_next_ip (self ):
154+ # Resets Mininet's next ip so subnet ips don't run out.
155+ # IP overrides are excluded from this set.
156+ self .net .nextIP = max (self ._used_ip_indices or [0 ]) + 1
157+
148158 def remove_host (self , host ):
149159 """Remove a host from the ecosystem"""
150160 index = self .net .hosts .index (host )
151161 if host .IP () and self ._get_host_ip_index (host ) in self ._used_ip_indices :
152- # Resets Mininet's next ip so subnet ips don't run out.
153- # IP overrides are excluded from this set.
154162 self ._used_ip_indices .remove (self ._get_host_ip_index (host ))
155- self .net . nextIP = max ( self . _used_ip_indices or [ 0 ]) + 1
163+ self ._reset_mininet_next_ip ()
156164 if index :
157165 del self .net .hosts [index ]
158166 if host in self .switch_links :
0 commit comments