Hostname management refactoring.

This commit is contained in:
grossmj committed 2014-05-27 11:23:06 -06:00
1 parent 3a0439c9ae
commit a39a693cda
18 files changed
+92 -125

No files matched your search

@@ -33,6 +33,7 @@ class ATMBridge(object):
def __init__(self, hypervisor, name):
#FIXME: instance tracking
self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._hypervisor.send("atm_bridge create {}".format(self._name))
+13 -20
View File
@@ -34,20 +34,21 @@ class ATMSwitch(object):
:param name: name for this switch
"""
_allocated_names = []
_instance_count = 1
_instances = []
def __init__(self, hypervisor, name):
# check if the name is already taken
if name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another ATM switch'.format(name))
# find an instance identifier (0 < id <= 4096)
self._id = 0
for identifier in range(1, 4097):
if identifier not in self._instances:
self._id = identifier
self._instances.append(self._id)
break
# create an unique ID
self._id = ATMSwitch._instance_count
ATMSwitch._instance_count += 1
if self._id == 0:
raise DynamipsError("Maximum number of instances reached")
self._allocated_names.append(name)
self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._hypervisor.send("atmsw create {}".format(self._name))
@@ -62,11 +63,10 @@ class ATMSwitch(object):
@classmethod
def reset(cls):
"""
Resets the instance count and the allocated names list.
Resets the instance count and the allocated instances list.
"""
cls._instance_count = 1
cls._allocated_names.clear()
cls._instances.clear()
@property
def id(self):
@@ -96,11 +96,6 @@ class ATMSwitch(object):
:param new_name: New name for this switch
"""
# check if the name is already taken
if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another ATM switch'.format(new_name))
new_name_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces
self._hypervisor.send("atmsw rename {name} {new_name}".format(name=self._name,
new_name=new_name))
@@ -109,9 +104,7 @@ class ATMSwitch(object):
id=self._id,
new_name=new_name))
self._allocated_names.remove(self.name)
self._name = new_name
self._allocated_names.append(new_name_no_quotes)
@property
def hypervisor(self):
@@ -162,7 +155,7 @@ class ATMSwitch(object):
log.info("ATM switch {name} [id={id}] has been deleted".format(name=self._name,
id=self._id))
self._hypervisor.devices.remove(self)
self._allocated_names.remove(self.name)
self._instances.remove(self._id)
def has_port(self, port):
"""
@@ -31,12 +31,9 @@ class Bridge(object):
:param name: name for this bridge
"""
_allocated_names = []
def __init__(self, hypervisor, name):
self._hypervisor = hypervisor
self._allocated_names.append(name)
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._hypervisor.send("nio_bridge create {}".format(self._name))
self._hypervisor.devices.append(self)
@@ -60,18 +57,11 @@ class Bridge(object):
:param new_name: New name for this bridge
"""
# check if the name is already taken
if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another bridge'.format(new_name))
new_name_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces
self._hypervisor.send("nio_bridge rename {name} {new_name}".format(name=self._name,
new_name=new_name))
self._allocated_names.remove(self.name)
self._name = new_name
self._allocated_names.append(new_name_no_quotes)
@property
def hypervisor(self):
@@ -109,7 +99,6 @@ class Bridge(object):
self._hypervisor.send("nio_bridge delete {}".format(self._name))
self._hypervisor.devices.remove(self)
self._allocated_names.remove(self.name)
def add_nio(self, nio):
"""
@@ -34,20 +34,21 @@ class EthernetSwitch(object):
:param name: name for this switch
"""
_allocated_names = []
_instance_count = 1
_instances = []
def __init__(self, hypervisor, name):
# check if the name is already taken
if name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another Ethernet switch'.format(name))
# find an instance identifier (0 < id <= 4096)
self._id = 0
for identifier in range(1, 4097):
if identifier not in self._instances:
self._id = identifier
self._instances.append(self._id)
break
# create an unique ID
self._id = EthernetSwitch._instance_count
EthernetSwitch._instance_count += 1
if self._id == 0:
raise DynamipsError("Maximum number of instances reached")
self._allocated_names.append(name)
self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._hypervisor.send("ethsw create {}".format(self._name))
@@ -62,11 +63,10 @@ class EthernetSwitch(object):
@classmethod
def reset(cls):
"""
Resets the instance count and the allocated names list.
Resets the instance count and the allocated instances list.
"""
cls._instance_count = 1
cls._allocated_names.clear()
cls._instances.clear()
@property
def id(self):
@@ -96,10 +96,6 @@ class EthernetSwitch(object):
:param new_name: New name for this switch
"""
if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another Ethernet switch'.format(new_name))
new_name_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces
self._hypervisor.send("ethsw rename {name} {new_name}".format(name=self._name,
new_name=new_name))
@@ -108,9 +104,7 @@ class EthernetSwitch(object):
id=self._id,
new_name=new_name))
self._allocated_names.remove(self.name)
self._name = new_name
self._allocated_names.append(new_name_no_quotes)
@property
def hypervisor(self):
@@ -161,7 +155,7 @@ class EthernetSwitch(object):
log.info("Ethernet switch {name} [id={id}] has been deleted".format(name=self._name,
id=self._id))
self._hypervisor.devices.remove(self)
self._allocated_names.remove(self.name)
self._instances.remove(self._id)
def add_nio(self, nio, port):
"""
@@ -34,20 +34,21 @@ class FrameRelaySwitch(object):
:param name: name for this switch
"""
_allocated_names = []
_instance_count = 1
_instances = []
def __init__(self, hypervisor, name):
# check if the name is already taken
if name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another Frame Relay switch'.format(name))
# find an instance identifier (0 < id <= 4096)
self._id = 0
for identifier in range(1, 4097):
if identifier not in self._instances:
self._id = identifier
self._instances.append(self._id)
break
# create an unique ID
self._id = FrameRelaySwitch._instance_count
FrameRelaySwitch._instance_count += 1
if self._id == 0:
raise DynamipsError("Maximum number of instances reached")
self._allocated_names.append(name)
self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._hypervisor.send("frsw create {}".format(self._name))
@@ -62,11 +63,10 @@ class FrameRelaySwitch(object):
@classmethod
def reset(cls):
"""
Resets the instance count and the allocated names list.
Resets the instance count and the allocated instances list.
"""
cls._instance_count = 1
cls._allocated_names.clear()
cls._instances.clear()
@property
def id(self):
@@ -96,10 +96,6 @@ class FrameRelaySwitch(object):
:param new_name: New name for this switch
"""
if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another Frame Relay switch'.format(new_name))
new_name_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces
self._hypervisor.send("frsw rename {name} {new_name}".format(name=self._name,
new_name=new_name))
@@ -108,9 +104,7 @@ class FrameRelaySwitch(object):
id=self._id,
new_name=new_name))
self._allocated_names.remove(self.name)
self._name = new_name
self._allocated_names.append(new_name_no_quotes)
@property
def hypervisor(self):
@@ -161,7 +155,7 @@ class FrameRelaySwitch(object):
log.info("Frame Relay switch {name} [id={id}] has been deleted".format(name=self._name,
id=self._id))
self._hypervisor.devices.remove(self)
self._allocated_names.remove(self.name)
self._instances.remove(self._id)
def has_port(self, port):
"""
+13 -10
View File
@@ -34,17 +34,20 @@ class Hub(Bridge):
:param name: name for this hub
"""
_instance_count = 1
_instances = []
def __init__(self, hypervisor, name):
# check if the name is already taken
if name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another Ethernet hub'.format(name))
# find an instance identifier (0 < id <= 4096)
self._id = 0
for identifier in range(1, 4097):
if identifier not in self._instances:
self._id = identifier
self._instances.append(self._id)
break
# create an unique ID
self._id = Hub._instance_count
Hub._instance_count += 1
if self._id == 0:
raise DynamipsError("Maximum number of instances reached")
self._mapping = {}
Bridge.__init__(self, hypervisor, name)
@@ -55,11 +58,10 @@ class Hub(Bridge):
@classmethod
def reset(cls):
"""
Resets the instance count and the allocated names list.
Resets the instance count and the allocated instances list.
"""
cls._instance_count = 1
cls._allocated_names.clear()
cls._instances.clear()
@property
def id(self):
@@ -89,6 +91,7 @@ class Hub(Bridge):
Bridge.delete(self)
log.info("Ethernet hub {name} [id={id}] has been deleted".format(name=self._name,
id=self._id))
self._instances.remove(self._id)
def add_nio(self, nio, port):
"""
+16 -21
View File
@@ -41,10 +41,9 @@ class Router(object):
:param ghost_flag: used when creating a ghost IOS.
"""
_allocated_names = []
_instances = []
_allocated_console_ports = []
_allocated_aux_ports = []
_instance_count = 1
_status = {0: "inactive",
1: "shutting down",
2: "running",
@@ -54,20 +53,22 @@ class Router(object):
if not ghost_flag:
# check if the name is already taken
if name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another router'.format(name))
# find an instance identifier (0 < id <= 4096)
self._id = 0
for identifier in range(1, 4097):
if identifier not in self._instances:
self._id = identifier
self._instances.append(self._id)
break
# create an unique ID
self._id = Router._instance_count
Router._instance_count += 1
if self._id == 0:
raise DynamipsError("Maximum number of instances reached")
else:
log.info("creating a new ghost IOS file")
self._id = 0
name = "Ghost"
self._allocated_names.append(name)
self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces
self._platform = platform
@@ -140,11 +141,10 @@ class Router(object):
@classmethod
def reset(cls):
"""
Resets the instance count and the allocated names list.
Resets the instance count and the allocated instances list.
"""
cls._instance_count = 1
cls._allocated_names.clear()
cls._instances.clear()
cls._allocated_console_ports.clear()
cls._allocated_aux_ports.clear()
@@ -218,9 +218,6 @@ class Router(object):
:param new_name: new name string
"""
if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another router'.format(new_name))
if self._startup_config:
# change the hostname in the startup-config
startup_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}.cfg".format(self.name))
@@ -261,10 +258,7 @@ class Router(object):
log.info("router {name} [id={id}]: renamed to {new_name}".format(name=self._name,
id=self._id,
new_name=new_name))
self._allocated_names.remove(self.name)
self._name = new_name
self._allocated_names.append(new_name_no_quotes)
@property
def platform(self):
@@ -312,9 +306,9 @@ class Router(object):
self._hypervisor.send("vm delete {}".format(self._name))
self._hypervisor.devices.remove(self)
log.info("router {name} [id={id}] has been deleted".format(name=self._name, id=self._id))
self._allocated_names.remove(self.name)
if self._id in self._instances:
self._instances.remove(self._id)
if self.console:
self._allocated_console_ports.remove(self.console)
if self.aux:
@@ -341,7 +335,8 @@ class Router(object):
os.remove(private_config_path)
log.info("router {name} [id={id}] has been deleted (including associated files)".format(name=self._name, id=self._id))
self._allocated_names.remove(self.name)
if self._id in self._instances:
self._instances.remove(self._id)
if self.console:
self._allocated_console_ports.remove(self.console)
if self.aux: