New hostnames management for the Dynamips module.

pull/21/head^2
grossmj 10 years ago
parent d7b9ed33f8
commit 3a0439c9ae

@ -38,7 +38,7 @@ class ATMSW(object):
""" """
Creates a new ATM switch. Creates a new ATM switch.
Optional request parameters: Mandatory request parameters:
- name (switch name) - name (switch name)
Response parameters: Response parameters:
@ -52,10 +52,8 @@ class ATMSW(object):
if request and not self.validate_request(request, ATMSW_CREATE_SCHEMA): if request and not self.validate_request(request, ATMSW_CREATE_SCHEMA):
return return
name = None
if request and "name" in request:
name = request["name"]
name = request["name"]
try: try:
if not self._hypervisor_manager: if not self._hypervisor_manager:
self.start_hypervisor_manager() self.start_hypervisor_manager()

@ -37,7 +37,7 @@ class ETHHUB(object):
""" """
Creates a new Ethernet hub. Creates a new Ethernet hub.
Optional request parameters: Mandatory request parameters:
- name (hub name) - name (hub name)
Response parameters: Response parameters:
@ -51,10 +51,7 @@ class ETHHUB(object):
if request and not self.validate_request(request, ETHHUB_CREATE_SCHEMA): if request and not self.validate_request(request, ETHHUB_CREATE_SCHEMA):
return return
name = None name = request["name"]
if request and "name" in request:
name = request["name"]
try: try:
if not self._hypervisor_manager: if not self._hypervisor_manager:
self.start_hypervisor_manager() self.start_hypervisor_manager()

@ -37,7 +37,7 @@ class ETHSW(object):
""" """
Creates a new Ethernet switch. Creates a new Ethernet switch.
Optional request parameters: Mandatory request parameters:
- name (switch name) - name (switch name)
Response parameters: Response parameters:
@ -51,10 +51,7 @@ class ETHSW(object):
if request and not self.validate_request(request, ETHSW_CREATE_SCHEMA): if request and not self.validate_request(request, ETHSW_CREATE_SCHEMA):
return return
name = None name = request["name"]
if request and "name" in request:
name = request["name"]
try: try:
if not self._hypervisor_manager: if not self._hypervisor_manager:
self.start_hypervisor_manager() self.start_hypervisor_manager()

@ -37,7 +37,7 @@ class FRSW(object):
""" """
Creates a new Frame-Relay switch. Creates a new Frame-Relay switch.
Optional request parameters: Mandatory request parameters:
- name (switch name) - name (switch name)
Response parameters: Response parameters:
@ -51,10 +51,7 @@ class FRSW(object):
if request and not self.validate_request(request, FRSW_CREATE_SCHEMA): if request and not self.validate_request(request, FRSW_CREATE_SCHEMA):
return return
name = None name = request["name"]
if request and "name" in request:
name = request["name"]
try: try:
if not self._hypervisor_manager: if not self._hypervisor_manager:
self.start_hypervisor_manager() self.start_hypervisor_manager()

@ -105,12 +105,12 @@ class VM(object):
Creates a new VM (router). Creates a new VM (router).
Mandatory request parameters: Mandatory request parameters:
- name (vm name)
- platform (platform name e.g. c7200) - platform (platform name e.g. c7200)
- image (path to IOS image) - image (path to IOS image)
- ram (amount of RAM in MB) - ram (amount of RAM in MB)
Optional request parameters: Optional request parameters:
- name (vm name)
- console (console port number) - console (console port number)
- aux (auxiliary console port number) - aux (auxiliary console port number)
- mac_addr (MAC address) - mac_addr (MAC address)
@ -127,9 +127,7 @@ class VM(object):
if not self.validate_request(request, VM_CREATE_SCHEMA): if not self.validate_request(request, VM_CREATE_SCHEMA):
return return
name = None name = request["name"]
if "name" in request:
name = request["name"]
platform = request["platform"] platform = request["platform"]
image = request["image"] image = request["image"]
ram = request["ram"] ram = request["ram"]

@ -37,22 +37,16 @@ class ATMSwitch(object):
_allocated_names = [] _allocated_names = []
_instance_count = 1 _instance_count = 1
def __init__(self, hypervisor, name=None): 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))
# create an unique ID # create an unique ID
self._id = ATMSwitch._instance_count self._id = ATMSwitch._instance_count
ATMSwitch._instance_count += 1 ATMSwitch._instance_count += 1
# let's create a unique name if none has been chosen
if not name:
name_id = self._id
while True:
name = "ATM" + str(name_id)
# check if the name has already been allocated to another switch
if name not in self._allocated_names:
break
name_id += 1
self._allocated_names.append(name) self._allocated_names.append(name)
self._hypervisor = hypervisor self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces self._name = '"' + name + '"' # put name into quotes to protect spaces
@ -102,6 +96,10 @@ class ATMSwitch(object):
:param new_name: New name for this switch :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_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces 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, self._hypervisor.send("atmsw rename {name} {new_name}".format(name=self._name,

@ -20,6 +20,8 @@ Interface for Dynamips NIO bridge module ("nio_bridge").
http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L538 http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L538
""" """
from ..dynamips_error import DynamipsError
class Bridge(object): class Bridge(object):
""" """
@ -58,6 +60,10 @@ class Bridge(object):
:param new_name: New name for this bridge :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_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces 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, self._hypervisor.send("nio_bridge rename {name} {new_name}".format(name=self._name,

@ -39,7 +39,7 @@ class C1700(Router):
1710 is not supported. 1710 is not supported.
""" """
def __init__(self, hypervisor, name=None, chassis="1720"): def __init__(self, hypervisor, name, chassis="1720"):
Router.__init__(self, hypervisor, name, platform="c1700") Router.__init__(self, hypervisor, name, platform="c1700")
# Set default values for this platform # Set default values for this platform

@ -54,7 +54,7 @@ class C2600(Router):
"2650XM": C2600_MB_1FE, "2650XM": C2600_MB_1FE,
"2651XM": C2600_MB_2FE} "2651XM": C2600_MB_2FE}
def __init__(self, hypervisor, name=None, chassis="2610"): def __init__(self, hypervisor, name, chassis="2610"):
Router.__init__(self, hypervisor, name, platform="c2600") Router.__init__(self, hypervisor, name, platform="c2600")
# Set default values for this platform # Set default values for this platform

@ -35,7 +35,7 @@ class C2691(Router):
:param name: name for this router :param name: name for this router
""" """
def __init__(self, hypervisor, name=None): def __init__(self, hypervisor, name):
Router.__init__(self, hypervisor, name, platform="c2691") Router.__init__(self, hypervisor, name, platform="c2691")
# Set default values for this platform # Set default values for this platform

@ -37,7 +37,7 @@ class C3600(Router):
3620, 3640 or 3660 (default = 3640). 3620, 3640 or 3660 (default = 3640).
""" """
def __init__(self, hypervisor, name=None, chassis="3640"): def __init__(self, hypervisor, name, chassis="3640"):
Router.__init__(self, hypervisor, name, platform="c3600") Router.__init__(self, hypervisor, name, platform="c3600")
# Set default values for this platform # Set default values for this platform

@ -35,7 +35,7 @@ class C3725(Router):
:param name: name for this router :param name: name for this router
""" """
def __init__(self, hypervisor, name=None): def __init__(self, hypervisor, name):
Router.__init__(self, hypervisor, name, platform="c3725") Router.__init__(self, hypervisor, name, platform="c3725")
# Set default values for this platform # Set default values for this platform

@ -35,7 +35,7 @@ class C3745(Router):
:param name: name for this router :param name: name for this router
""" """
def __init__(self, hypervisor, name=None): def __init__(self, hypervisor, name):
Router.__init__(self, hypervisor, name, platform="c3745") Router.__init__(self, hypervisor, name, platform="c3745")
# Set default values for this platform # Set default values for this platform

@ -38,7 +38,7 @@ class C7200(Router):
:param npe: default NPE :param npe: default NPE
""" """
def __init__(self, hypervisor, name=None, npe="npe-400"): def __init__(self, hypervisor, name, npe="npe-400"):
Router.__init__(self, hypervisor, name, platform="c7200") Router.__init__(self, hypervisor, name, platform="c7200")
# Set default values for this platform # Set default values for this platform

@ -37,22 +37,16 @@ class EthernetSwitch(object):
_allocated_names = [] _allocated_names = []
_instance_count = 1 _instance_count = 1
def __init__(self, hypervisor, name=None): 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))
# create an unique ID # create an unique ID
self._id = EthernetSwitch._instance_count self._id = EthernetSwitch._instance_count
EthernetSwitch._instance_count += 1 EthernetSwitch._instance_count += 1
# let's create a unique name if none has been chosen
if not name:
name_id = self._id
while True:
name = "SW" + str(name_id)
# check if the name has already been allocated to another switch
if name not in self._allocated_names:
break
name_id += 1
self._allocated_names.append(name) self._allocated_names.append(name)
self._hypervisor = hypervisor self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces self._name = '"' + name + '"' # put name into quotes to protect spaces
@ -102,6 +96,9 @@ class EthernetSwitch(object):
:param new_name: New name for this switch :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_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces 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, self._hypervisor.send("ethsw rename {name} {new_name}".format(name=self._name,

@ -37,22 +37,16 @@ class FrameRelaySwitch(object):
_allocated_names = [] _allocated_names = []
_instance_count = 1 _instance_count = 1
def __init__(self, hypervisor, name=None): 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))
# create an unique ID # create an unique ID
self._id = FrameRelaySwitch._instance_count self._id = FrameRelaySwitch._instance_count
FrameRelaySwitch._instance_count += 1 FrameRelaySwitch._instance_count += 1
# let's create a unique name if none has been chosen
if not name:
name_id = self._id
while True:
name = "FR" + str(name_id)
# check if the name has already been allocated to another switch
if name not in self._allocated_names:
break
name_id += 1
self._allocated_names.append(name) self._allocated_names.append(name)
self._hypervisor = hypervisor self._hypervisor = hypervisor
self._name = '"' + name + '"' # put name into quotes to protect spaces self._name = '"' + name + '"' # put name into quotes to protect spaces
@ -102,6 +96,9 @@ class FrameRelaySwitch(object):
:param new_name: New name for this switch :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_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces 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, self._hypervisor.send("frsw rename {name} {new_name}".format(name=self._name,

@ -38,20 +38,14 @@ class Hub(Bridge):
def __init__(self, hypervisor, name): 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))
# create an unique ID # create an unique ID
self._id = Hub._instance_count self._id = Hub._instance_count
Hub._instance_count += 1 Hub._instance_count += 1
# let's create a unique name if none has been chosen
if not name:
name_id = self._id
while True:
name = "Hub" + str(name_id)
# check if the name has already been allocated to another switch
if name not in self._allocated_names:
break
name_id += 1
self._mapping = {} self._mapping = {}
Bridge.__init__(self, hypervisor, name) Bridge.__init__(self, hypervisor, name)

@ -50,22 +50,18 @@ class Router(object):
2: "running", 2: "running",
3: "suspended"} 3: "suspended"}
def __init__(self, hypervisor, name=None, platform="c7200", ghost_flag=False): def __init__(self, hypervisor, name, platform="c7200", ghost_flag=False):
if not ghost_flag: 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))
# create an unique ID # create an unique ID
self._id = Router._instance_count self._id = Router._instance_count
Router._instance_count += 1 Router._instance_count += 1
# let's create a unique name if none has been chosen
if not name:
name_id = self._id
while True:
name = "R" + str(name_id)
# check if the name has already been allocated to another router
if name not in self._allocated_names:
break
name_id += 1
else: else:
log.info("creating a new ghost IOS file") log.info("creating a new ghost IOS file")
self._id = 0 self._id = 0
@ -581,10 +577,10 @@ class Router(object):
reply = self._hypervisor.send("vm extract_config {}".format(self._name))[0].rsplit(' ', 2)[-2:] reply = self._hypervisor.send("vm extract_config {}".format(self._name))[0].rsplit(' ', 2)[-2:]
except IOError: except IOError:
#for some reason Dynamips gets frozen when it does not find the magic number in the NVRAM file. #for some reason Dynamips gets frozen when it does not find the magic number in the NVRAM file.
return (None, None) return None, None
startup_config = reply[0][1:-1] # get statup-config and remove single quotes startup_config = reply[0][1:-1] # get statup-config and remove single quotes
private_config = reply[1][1:-1] # get private-config and remove single quotes private_config = reply[1][1:-1] # get private-config and remove single quotes
return (startup_config, private_config) return startup_config, private_config
def push_config(self, startup_config, private_config='(keep)'): def push_config(self, startup_config, private_config='(keep)'):
""" """
@ -727,7 +723,7 @@ class Router(object):
else: else:
flag = 0 flag = 0
self._hypervisor.send("vm set_sparse_mem {name} {sparsemem}".format(name=self._name, self._hypervisor.send("vm set_sparse_mem {name} {sparsemem}".format(name=self._name,
sparsemem=flag)) sparsemem=flag))
if sparsemem: if sparsemem:
log.info("router {name} [id={id}]: sparse memory enabled".format(name=self._name, log.info("router {name} [id={id}]: sparse memory enabled".format(name=self._name,

@ -27,6 +27,7 @@ ATMSW_CREATE_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name"]
} }
ATMSW_DELETE_SCHEMA = { ATMSW_DELETE_SCHEMA = {

@ -27,6 +27,7 @@ ETHHUB_CREATE_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name"]
} }
ETHHUB_DELETE_SCHEMA = { ETHHUB_DELETE_SCHEMA = {

@ -27,6 +27,7 @@ ETHSW_CREATE_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name"]
} }
ETHSW_DELETE_SCHEMA = { ETHSW_DELETE_SCHEMA = {

@ -27,6 +27,7 @@ FRSW_CREATE_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name"]
} }
FRSW_DELETE_SCHEMA = { FRSW_DELETE_SCHEMA = {

@ -66,7 +66,7 @@ VM_CREATE_SCHEMA = {
} }
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["platform", "image", "ram"] "required": ["name", "platform", "image", "ram"]
} }
VM_DELETE_SCHEMA = { VM_DELETE_SCHEMA = {

Loading…
Cancel
Save