mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-25 16:28:11 +00:00
Adapter settings and configs for Dynamips VMs.
This commit is contained in:
parent
018e3c1451
commit
3e95bb9748
@ -220,7 +220,7 @@ class DynamipsDeviceHandler:
|
|||||||
404: "Instance doesn't exist"
|
404: "Instance doesn't exist"
|
||||||
},
|
},
|
||||||
description="Stop a packet capture on a Dynamips device instance")
|
description="Stop a packet capture on a Dynamips device instance")
|
||||||
def start_capture(request, response):
|
def stop_capture(request, response):
|
||||||
|
|
||||||
dynamips_manager = Dynamips.instance()
|
dynamips_manager = Dynamips.instance()
|
||||||
device = dynamips_manager.get_device(request.match_info["device_id"], project_id=request.match_info["project_id"])
|
device = dynamips_manager.get_device(request.match_info["device_id"], project_id=request.match_info["project_id"])
|
||||||
|
@ -57,17 +57,11 @@ class DynamipsVMHandler:
|
|||||||
request.json.get("dynamips_id"),
|
request.json.get("dynamips_id"),
|
||||||
request.json.pop("platform"))
|
request.json.pop("platform"))
|
||||||
|
|
||||||
# set VM settings
|
yield from dynamips_manager.update_vm_settings(vm, request.json)
|
||||||
for name, value in request.json.items():
|
|
||||||
if hasattr(vm, name) and getattr(vm, name) != value:
|
|
||||||
if hasattr(vm, "set_{}".format(name)):
|
|
||||||
setter = getattr(vm, "set_{}".format(name))
|
|
||||||
if asyncio.iscoroutinefunction(vm.close):
|
|
||||||
yield from setter(value)
|
|
||||||
else:
|
|
||||||
setter(value)
|
|
||||||
|
|
||||||
yield from dynamips_manager.ghost_ios_support(vm)
|
yield from dynamips_manager.ghost_ios_support(vm)
|
||||||
|
yield from dynamips_manager.create_vm_configs(vm,
|
||||||
|
request.json.get("startup_config_content"),
|
||||||
|
request.json.get("private_config_content"))
|
||||||
response.set_status(201)
|
response.set_status(201)
|
||||||
response.json(vm)
|
response.json(vm)
|
||||||
|
|
||||||
@ -112,14 +106,7 @@ class DynamipsVMHandler:
|
|||||||
dynamips_manager = Dynamips.instance()
|
dynamips_manager = Dynamips.instance()
|
||||||
vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
||||||
|
|
||||||
# set VM settings
|
yield from dynamips_manager.update_vm_settings(vm, request.json)
|
||||||
for name, value in request.json.items():
|
|
||||||
if hasattr(vm, name) and getattr(vm, name) != value:
|
|
||||||
setter = getattr(vm, "set_{}".format(name))
|
|
||||||
if asyncio.iscoroutinefunction(vm.close):
|
|
||||||
yield from setter(value)
|
|
||||||
else:
|
|
||||||
setter(value)
|
|
||||||
response.json(vm)
|
response.json(vm)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -333,7 +320,7 @@ class DynamipsVMHandler:
|
|||||||
404: "Instance doesn't exist"
|
404: "Instance doesn't exist"
|
||||||
},
|
},
|
||||||
description="Stop a packet capture on a Dynamips VM instance")
|
description="Stop a packet capture on a Dynamips VM instance")
|
||||||
def start_capture(request, response):
|
def stop_capture(request, response):
|
||||||
|
|
||||||
dynamips_manager = Dynamips.instance()
|
dynamips_manager = Dynamips.instance()
|
||||||
vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
vm = dynamips_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
||||||
|
@ -336,7 +336,7 @@ class VirtualBoxHandler:
|
|||||||
404: "Instance doesn't exist"
|
404: "Instance doesn't exist"
|
||||||
},
|
},
|
||||||
description="Stop a packet capture on a VirtualBox VM instance")
|
description="Stop a packet capture on a VirtualBox VM instance")
|
||||||
def start_capture(request, response):
|
def stop_capture(request, response):
|
||||||
|
|
||||||
vbox_manager = VirtualBox.instance()
|
vbox_manager = VirtualBox.instance()
|
||||||
vm = vbox_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
vm = vbox_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
|
||||||
|
@ -56,6 +56,51 @@ from .nios.nio_fifo import NIOFIFO
|
|||||||
from .nios.nio_mcast import NIOMcast
|
from .nios.nio_mcast import NIOMcast
|
||||||
from .nios.nio_null import NIONull
|
from .nios.nio_null import NIONull
|
||||||
|
|
||||||
|
# Adapters
|
||||||
|
from .adapters.c7200_io_2fe import C7200_IO_2FE
|
||||||
|
from .adapters.c7200_io_fe import C7200_IO_FE
|
||||||
|
from .adapters.c7200_io_ge_e import C7200_IO_GE_E
|
||||||
|
from .adapters.nm_16esw import NM_16ESW
|
||||||
|
from .adapters.nm_1e import NM_1E
|
||||||
|
from .adapters.nm_1fe_tx import NM_1FE_TX
|
||||||
|
from .adapters.nm_4e import NM_4E
|
||||||
|
from .adapters.nm_4t import NM_4T
|
||||||
|
from .adapters.pa_2fe_tx import PA_2FE_TX
|
||||||
|
from .adapters.pa_4e import PA_4E
|
||||||
|
from .adapters.pa_4t import PA_4T
|
||||||
|
from .adapters.pa_8e import PA_8E
|
||||||
|
from .adapters.pa_8t import PA_8T
|
||||||
|
from .adapters.pa_a1 import PA_A1
|
||||||
|
from .adapters.pa_fe_tx import PA_FE_TX
|
||||||
|
from .adapters.pa_ge import PA_GE
|
||||||
|
from .adapters.pa_pos_oc3 import PA_POS_OC3
|
||||||
|
from .adapters.wic_1enet import WIC_1ENET
|
||||||
|
from .adapters.wic_1t import WIC_1T
|
||||||
|
from .adapters.wic_2t import WIC_2T
|
||||||
|
|
||||||
|
|
||||||
|
ADAPTER_MATRIX = {"C7200-IO-2FE": C7200_IO_2FE,
|
||||||
|
"C7200-IO-FE": C7200_IO_FE,
|
||||||
|
"C7200-IO-GE-E": C7200_IO_GE_E,
|
||||||
|
"NM-16ESW": NM_16ESW,
|
||||||
|
"NM-1E": NM_1E,
|
||||||
|
"NM-1FE-TX": NM_1FE_TX,
|
||||||
|
"NM-4E": NM_4E,
|
||||||
|
"NM-4T": NM_4T,
|
||||||
|
"PA-2FE-TX": PA_2FE_TX,
|
||||||
|
"PA-4E": PA_4E,
|
||||||
|
"PA-4T+": PA_4T,
|
||||||
|
"PA-8E": PA_8E,
|
||||||
|
"PA-8T": PA_8T,
|
||||||
|
"PA-A1": PA_A1,
|
||||||
|
"PA-FE-TX": PA_FE_TX,
|
||||||
|
"PA-GE": PA_GE,
|
||||||
|
"PA-POS-OC3": PA_POS_OC3}
|
||||||
|
|
||||||
|
WIC_MATRIX = {"WIC-1ENET": WIC_1ENET,
|
||||||
|
"WIC-1T": WIC_1T,
|
||||||
|
"WIC-2T": WIC_2T}
|
||||||
|
|
||||||
|
|
||||||
class Dynamips(BaseManager):
|
class Dynamips(BaseManager):
|
||||||
|
|
||||||
@ -391,67 +436,93 @@ class Dynamips(BaseManager):
|
|||||||
# set the ghost file to the router
|
# set the ghost file to the router
|
||||||
yield from vm.set_ghost_status(2)
|
yield from vm.set_ghost_status(2)
|
||||||
yield from vm.set_ghost_file(ghost_file)
|
yield from vm.set_ghost_file(ghost_file)
|
||||||
#
|
|
||||||
# def create_config_from_file(self, local_base_config, router, destination_config_path):
|
@asyncio.coroutine
|
||||||
# """
|
def update_vm_settings(self, vm, settings):
|
||||||
# Creates a config file from a local base config
|
"""
|
||||||
#
|
Updates the VM settings.
|
||||||
# :param local_base_config: path the a local base config
|
|
||||||
# :param router: router instance
|
:param vm: VM instance
|
||||||
# :param destination_config_path: path to the destination config file
|
:param settings: settings to update (dict)
|
||||||
#
|
"""
|
||||||
# :returns: relative path to the created config file
|
|
||||||
# """
|
for name, value in settings.items():
|
||||||
#
|
if hasattr(vm, name) and getattr(vm, name) != value:
|
||||||
# log.info("creating config file {} from {}".format(destination_config_path, local_base_config))
|
if hasattr(vm, "set_{}".format(name)):
|
||||||
# config_path = destination_config_path
|
setter = getattr(vm, "set_{}".format(name))
|
||||||
# config_dir = os.path.dirname(destination_config_path)
|
if asyncio.iscoroutinefunction(vm.close):
|
||||||
# try:
|
yield from setter(value)
|
||||||
# os.makedirs(config_dir)
|
else:
|
||||||
# except FileExistsError:
|
setter(value)
|
||||||
# pass
|
elif name.startswith("slot") and value in ADAPTER_MATRIX:
|
||||||
# except OSError as e:
|
slot_id = int(name[-1])
|
||||||
# raise DynamipsError("Could not create configs directory: {}".format(e))
|
adapter_name = value
|
||||||
#
|
adapter = ADAPTER_MATRIX[adapter_name]()
|
||||||
# try:
|
if vm.slots[slot_id] and type(vm.slots[slot_id]) != type(adapter):
|
||||||
# with open(local_base_config, "r", errors="replace") as f:
|
yield from vm.slot_remove_binding(slot_id)
|
||||||
# config = f.read()
|
yield from vm.slot_add_binding(slot_id, adapter)
|
||||||
# with open(config_path, "w") as f:
|
elif name.startswith("slot") and value is None:
|
||||||
# config = "!\n" + config.replace("\r", "")
|
slot_id = int(name[-1])
|
||||||
# config = config.replace('%h', router.name)
|
if vm.slots[slot_id]:
|
||||||
# f.write(config)
|
yield from vm.slot_remove_binding(slot_id)
|
||||||
# except OSError as e:
|
elif name.startswith("wic") and value in WIC_MATRIX:
|
||||||
# raise DynamipsError("Could not save the configuration from {} to {}: {}".format(local_base_config, config_path, e))
|
wic_slot_id = int(name[-1])
|
||||||
# return "configs" + os.sep + os.path.basename(config_path)
|
wic_name = value
|
||||||
#
|
wic = WIC_MATRIX[wic_name]()
|
||||||
# def create_config_from_base64(self, config_base64, router, destination_config_path):
|
if vm.slots[0].wics[wic_slot_id] and type(vm.slots[0].wics[wic_slot_id]) != type(wic):
|
||||||
# """
|
yield from vm.uninstall_wic(wic_slot_id)
|
||||||
# Creates a config file from a base64 encoded config.
|
yield from vm.install_wic(wic_slot_id, wic)
|
||||||
#
|
elif name.startswith("wic") and value is None:
|
||||||
# :param config_base64: base64 encoded config
|
wic_slot_id = int(name[-1])
|
||||||
# :param router: router instance
|
if vm.slots[0].wics and vm.slots[0].wics[wic_slot_id]:
|
||||||
# :param destination_config_path: path to the destination config file
|
yield from vm.uninstall_wic(wic_slot_id)
|
||||||
#
|
|
||||||
# :returns: relative path to the created config file
|
@asyncio.coroutine
|
||||||
# """
|
def create_vm_configs(self, vm, startup_config_content, private_config_content):
|
||||||
#
|
"""
|
||||||
# log.info("creating config file {} from base64".format(destination_config_path))
|
Creates VM configs from pushed content.
|
||||||
# config = base64.decodebytes(config_base64.encode("utf-8")).decode("utf-8")
|
|
||||||
# config = "!\n" + config.replace("\r", "")
|
:param vm: VM instance
|
||||||
# config = config.replace('%h', router.name)
|
:param startup_config_content: content of the startup-config
|
||||||
# config_dir = os.path.dirname(destination_config_path)
|
:param private_config_content: content of the private-config
|
||||||
# try:
|
"""
|
||||||
# os.makedirs(config_dir)
|
|
||||||
# except FileExistsError:
|
default_startup_config_path = os.path.join(vm.project.vm_working_directory(vm), "configs", "i{}_startup-config.cfg".format(vm.dynamips_id))
|
||||||
# pass
|
default_private_config_path = os.path.join(vm.project.vm_working_directory(vm), "configs", "i{}_private-config.cfg".format(vm.dynamips_id))
|
||||||
# except OSError as e:
|
|
||||||
# raise DynamipsError("Could not create configs directory: {}".format(e))
|
if startup_config_content:
|
||||||
#
|
startup_config_path = self._create_config(vm, startup_config_content, default_startup_config_path)
|
||||||
# config_path = destination_config_path
|
yield from vm.set_config(startup_config_path)
|
||||||
# try:
|
|
||||||
# with open(config_path, "w") as f:
|
if private_config_content:
|
||||||
# log.info("saving startup-config to {}".format(config_path))
|
private_config_path = self._create_config(vm, private_config_content, default_private_config_path)
|
||||||
# f.write(config)
|
yield from vm.set_config(vm.startup_config, private_config_path)
|
||||||
# except OSError as e:
|
|
||||||
# raise DynamipsError("Could not save the configuration {}: {}".format(config_path, e))
|
def _create_config(self, vm, content, path):
|
||||||
# return "configs" + os.sep + os.path.basename(config_path)
|
"""
|
||||||
|
Creates a config file.
|
||||||
|
|
||||||
|
:param vm: VM instance
|
||||||
|
:param content: config content
|
||||||
|
:param path: path to the destination config file
|
||||||
|
|
||||||
|
:returns: relative path to the created config file
|
||||||
|
"""
|
||||||
|
|
||||||
|
log.info("Creating config file {}".format(path))
|
||||||
|
content = "!\n" + content.replace("\r", "")
|
||||||
|
content = content.replace('%h', vm.name)
|
||||||
|
config_dir = os.path.dirname(path)
|
||||||
|
try:
|
||||||
|
os.makedirs(config_dir, exist_ok=True)
|
||||||
|
except OSError as e:
|
||||||
|
raise DynamipsError("Could not create Dynamips configs directory: {}".format(e))
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(path, "w") as f:
|
||||||
|
log.info("Creating config file {}".format(path))
|
||||||
|
f.write(content)
|
||||||
|
except OSError as e:
|
||||||
|
raise DynamipsError("Could not create config file {}: {}".format(path, e))
|
||||||
|
|
||||||
|
return os.path.join("configs", os.path.basename(path))
|
||||||
|
@ -25,6 +25,7 @@ import time
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import glob
|
import glob
|
||||||
|
import base64
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -146,17 +147,19 @@ class Router(BaseVM):
|
|||||||
"mac_addr": self._mac_addr,
|
"mac_addr": self._mac_addr,
|
||||||
"system_id": self._system_id}
|
"system_id": self._system_id}
|
||||||
|
|
||||||
# FIXME: add default slots/wics
|
# add the slots
|
||||||
# slot_number = 0
|
slot_number = 0
|
||||||
# for slot in self._slots:
|
for slot in self._slots:
|
||||||
# if slot:
|
if slot:
|
||||||
# slot = str(slot)
|
slot = str(slot)
|
||||||
# router_defaults["slot" + str(slot_number)] = slot
|
router_info["slot" + str(slot_number)] = slot
|
||||||
# slot_number += 1
|
slot_number += 1
|
||||||
|
|
||||||
# if self._slots[0] and self._slots[0].wics:
|
# add the wics
|
||||||
# for wic_slot_number in range(0, len(self._slots[0].wics)):
|
if self._slots[0] and self._slots[0].wics:
|
||||||
# router_defaults["wic" + str(wic_slot_number)] = None
|
for wic_slot_number in range(0, len(self._slots[0].wics)):
|
||||||
|
if self._slots[0].wics[wic_slot_number]:
|
||||||
|
router_info["wic" + str(wic_slot_number)] = str(self._slots[0].wics[wic_slot_number])
|
||||||
|
|
||||||
return router_info
|
return router_info
|
||||||
|
|
||||||
@ -312,9 +315,9 @@ class Router(BaseVM):
|
|||||||
if self._hypervisor and not self._hypervisor.devices:
|
if self._hypervisor and not self._hypervisor.devices:
|
||||||
try:
|
try:
|
||||||
yield from self.stop()
|
yield from self.stop()
|
||||||
|
yield from self._hypervisor.send('vm delete "{}"'.format(self._name))
|
||||||
except DynamipsError:
|
except DynamipsError:
|
||||||
pass
|
pass
|
||||||
yield from self._hypervisor.send('vm delete "{}"'.format(self._name))
|
|
||||||
yield from self.hypervisor.stop()
|
yield from self.hypervisor.stop()
|
||||||
|
|
||||||
if self._console:
|
if self._console:
|
||||||
@ -1408,144 +1411,126 @@ class Router(BaseVM):
|
|||||||
|
|
||||||
self._private_config = private_config
|
self._private_config = private_config
|
||||||
|
|
||||||
# TODO: rename
|
@asyncio.coroutine
|
||||||
# def rename(self, new_name):
|
def set_name(self, new_name):
|
||||||
# """
|
"""
|
||||||
# Renames this router.
|
Renames this router.
|
||||||
#
|
|
||||||
# :param new_name: new name string
|
|
||||||
# """
|
|
||||||
#
|
|
||||||
# if self._startup_config:
|
|
||||||
# # change the hostname in the startup-config
|
|
||||||
# startup_config_path = os.path.join(self.hypervisor.working_dir, "configs", "i{}_startup-config.cfg".format(self.id))
|
|
||||||
# if os.path.isfile(startup_config_path):
|
|
||||||
# try:
|
|
||||||
# with open(startup_config_path, "r+", errors="replace") as f:
|
|
||||||
# old_config = f.read()
|
|
||||||
# new_config = old_config.replace(self.name, new_name)
|
|
||||||
# f.seek(0)
|
|
||||||
# f.write(new_config)
|
|
||||||
# except OSError as e:
|
|
||||||
# raise DynamipsError("Could not amend the configuration {}: {}".format(startup_config_path, e))
|
|
||||||
#
|
|
||||||
# if self._private_config:
|
|
||||||
# # change the hostname in the private-config
|
|
||||||
# private_config_path = os.path.join(self.hypervisor.working_dir, "configs", "i{}_private-config.cfg".format(self.id))
|
|
||||||
# if os.path.isfile(private_config_path):
|
|
||||||
# try:
|
|
||||||
# with open(private_config_path, "r+", errors="replace") as f:
|
|
||||||
# old_config = f.read()
|
|
||||||
# new_config = old_config.replace(self.name, new_name)
|
|
||||||
# f.seek(0)
|
|
||||||
# f.write(new_config)
|
|
||||||
# except OSError as e:
|
|
||||||
# raise DynamipsError("Could not amend the configuration {}: {}".format(private_config_path, e))
|
|
||||||
#
|
|
||||||
# new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces
|
|
||||||
# self._hypervisor.send("vm rename {name} {new_name}".format(name=self._name,
|
|
||||||
# new_name=new_name))
|
|
||||||
#
|
|
||||||
# log.info("router {name} [id={id}]: renamed to {new_name}".format(name=self._name,
|
|
||||||
# id=self._id,
|
|
||||||
# new_name=new_name))
|
|
||||||
# self._name = new_name
|
|
||||||
|
|
||||||
# def set_config(self, startup_config, private_config=''):
|
:param new_name: new name string
|
||||||
# """
|
"""
|
||||||
# Sets the config files that are pushed to startup-config and
|
|
||||||
# private-config in NVRAM when the instance is started.
|
module_workdir = self.project.module_working_directory(self.manager.module_name.lower())
|
||||||
#
|
if self._startup_config:
|
||||||
# :param startup_config: path to statup-config file
|
# change the hostname in the startup-config
|
||||||
# :param private_config: path to private-config file
|
startup_config_path = os.path.join(module_workdir, "configs", "i{}_startup-config.cfg".format(self._dynamips_id))
|
||||||
# (keep existing data when if an empty string)
|
if os.path.isfile(startup_config_path):
|
||||||
# """
|
try:
|
||||||
#
|
with open(startup_config_path, "r+", errors="replace") as f:
|
||||||
# if self._startup_config != startup_config or self._private_config != private_config:
|
old_config = f.read()
|
||||||
#
|
new_config = old_config.replace(self.name, new_name)
|
||||||
# self._hypervisor.send("vm set_config {name} {startup} {private}".format(name=self._name,
|
f.seek(0)
|
||||||
# startup='"' + startup_config + '"',
|
f.write(new_config)
|
||||||
# private='"' + private_config + '"'))
|
except OSError as e:
|
||||||
#
|
raise DynamipsError("Could not amend the configuration {}: {}".format(startup_config_path, e))
|
||||||
# log.info("router {name} [id={id}]: has a startup-config set: {startup}".format(name=self._name,
|
|
||||||
# id=self._id,
|
if self._private_config:
|
||||||
# startup='"' + startup_config + '"'))
|
# change the hostname in the private-config
|
||||||
#
|
private_config_path = os.path.join(module_workdir, "configs", "i{}_private-config.cfg".format(self._dynamips_id))
|
||||||
# self._startup_config = startup_config
|
if os.path.isfile(private_config_path):
|
||||||
#
|
try:
|
||||||
# if private_config:
|
with open(private_config_path, "r+", errors="replace") as f:
|
||||||
# log.info("router {name} [id={id}]: has a private-config set: {private}".format(name=self._name,
|
old_config = f.read()
|
||||||
# id=self._id,
|
new_config = old_config.replace(self.name, new_name)
|
||||||
# private='"' + private_config + '"'))
|
f.seek(0)
|
||||||
#
|
f.write(new_config)
|
||||||
# self._private_config = private_config
|
except OSError as e:
|
||||||
#
|
raise DynamipsError("Could not amend the configuration {}: {}".format(private_config_path, e))
|
||||||
# def extract_config(self):
|
|
||||||
# """
|
yield from self._hypervisor.send('vm rename "{name}" "{new_name}"'.format(name=self._name, new_name=new_name))
|
||||||
# Gets the contents of the config files
|
log.info('Router "{name}" [{id}]: renamed to "{new_name}"'.format(name=self._name, id=self._id, new_name=new_name))
|
||||||
# startup-config and private-config from NVRAM.
|
self._name = new_name
|
||||||
#
|
|
||||||
# :returns: tuple (startup-config, private-config) base64 encoded
|
@asyncio.coroutine
|
||||||
# """
|
def set_config(self, startup_config, private_config=''):
|
||||||
#
|
"""
|
||||||
# try:
|
Sets the config files that are pushed to startup-config and
|
||||||
# reply = self._hypervisor.send("vm extract_config {}".format(self._name))[0].rsplit(' ', 2)[-2:]
|
private-config in NVRAM when the instance is started.
|
||||||
# except IOError:
|
|
||||||
# #for some reason Dynamips gets frozen when it does not find the magic number in the NVRAM file.
|
:param startup_config: path to statup-config file
|
||||||
# return None, None
|
:param private_config: path to private-config file
|
||||||
# startup_config = reply[0][1:-1] # get statup-config and remove single quotes
|
(keep existing data when if an empty string)
|
||||||
# private_config = reply[1][1:-1] # get private-config and remove single quotes
|
"""
|
||||||
# return startup_config, private_config
|
|
||||||
#
|
startup_config = startup_config.replace("\\", '/')
|
||||||
# def push_config(self, startup_config, private_config='(keep)'):
|
private_config = private_config.replace("\\", '/')
|
||||||
# """
|
|
||||||
# Pushes configuration to the config files startup-config and private-config in NVRAM.
|
if self._startup_config != startup_config or self._private_config != private_config:
|
||||||
# The data is a Base64 encoded string, or '(keep)' to keep existing data.
|
|
||||||
#
|
yield from self._hypervisor.send('vm set_config "{name}" "{startup}" "{private}"'.format(name=self._name,
|
||||||
# :param startup_config: statup-config string base64 encoded
|
startup=startup_config,
|
||||||
# :param private_config: private-config string base64 encoded
|
private=private_config))
|
||||||
# (keep existing data when if the value is ('keep'))
|
|
||||||
# """
|
log.info('Router "{name}" [{id}]: has a new startup-config set: "{startup}"'.format(name=self._name,
|
||||||
#
|
id=self._id,
|
||||||
# self._hypervisor.send("vm push_config {name} {startup} {private}".format(name=self._name,
|
startup=startup_config))
|
||||||
# startup=startup_config,
|
|
||||||
# private=private_config))
|
self._startup_config = startup_config
|
||||||
#
|
|
||||||
# log.info("router {name} [id={id}]: new startup-config pushed".format(name=self._name,
|
if private_config:
|
||||||
# id=self._id))
|
log.info('Router "{name}" [{id}]: has a new private-config set: "{private}"'.format(name=self._name,
|
||||||
#
|
id=self._id,
|
||||||
# if private_config != '(keep)':
|
private=private_config))
|
||||||
# log.info("router {name} [id={id}]: new private-config pushed".format(name=self._name,
|
|
||||||
# id=self._id))
|
self._private_config = private_config
|
||||||
#
|
|
||||||
# def save_configs(self):
|
@asyncio.coroutine
|
||||||
# """
|
def extract_config(self):
|
||||||
# Saves the startup-config and private-config to files.
|
"""
|
||||||
# """
|
Gets the contents of the config files
|
||||||
#
|
startup-config and private-config from NVRAM.
|
||||||
# if self.startup_config or self.private_config:
|
|
||||||
# startup_config_base64, private_config_base64 = self.extract_config()
|
:returns: tuple (startup-config, private-config) base64 encoded
|
||||||
# if startup_config_base64:
|
"""
|
||||||
# try:
|
|
||||||
# config = base64.decodebytes(startup_config_base64.encode("utf-8")).decode("utf-8")
|
try:
|
||||||
# config = "!\n" + config.replace("\r", "")
|
reply = yield from self._hypervisor.send("vm extract_config {}".format(self._name))[0].rsplit(' ', 2)[-2:]
|
||||||
# config_path = os.path.join(self.hypervisor.working_dir, self.startup_config)
|
except IOError:
|
||||||
# with open(config_path, "w") as f:
|
#for some reason Dynamips gets frozen when it does not find the magic number in the NVRAM file.
|
||||||
# log.info("saving startup-config to {}".format(self.startup_config))
|
return None, None
|
||||||
# f.write(config)
|
startup_config = reply[0][1:-1] # get statup-config and remove single quotes
|
||||||
# except OSError as e:
|
private_config = reply[1][1:-1] # get private-config and remove single quotes
|
||||||
# raise DynamipsError("Could not save the startup configuration {}: {}".format(config_path, e))
|
return startup_config, private_config
|
||||||
#
|
|
||||||
# if private_config_base64:
|
@asyncio.coroutine
|
||||||
# try:
|
def save_configs(self):
|
||||||
# config = base64.decodebytes(private_config_base64.encode("utf-8")).decode("utf-8")
|
"""
|
||||||
# config = "!\n" + config.replace("\r", "")
|
Saves the startup-config and private-config to files.
|
||||||
# config_path = os.path.join(self.hypervisor.working_dir, self.private_config)
|
"""
|
||||||
# with open(config_path, "w") as f:
|
|
||||||
# log.info("saving private-config to {}".format(self.private_config))
|
if self.startup_config or self.private_config:
|
||||||
# f.write(config)
|
module_workdir = self.project.module_working_directory(self.manager.module_name.lower())
|
||||||
# except OSError as e:
|
startup_config_base64, private_config_base64 = yield from self.extract_config()
|
||||||
# raise DynamipsError("Could not save the private configuration {}: {}".format(config_path, e))
|
if startup_config_base64:
|
||||||
|
try:
|
||||||
|
config = base64.decodebytes(startup_config_base64.encode("utf-8")).decode("utf-8")
|
||||||
|
config = "!\n" + config.replace("\r", "")
|
||||||
|
config_path = os.path.join(module_workdir, self.startup_config)
|
||||||
|
with open(config_path, "w") as f:
|
||||||
|
log.info("saving startup-config to {}".format(self.startup_config))
|
||||||
|
f.write(config)
|
||||||
|
except OSError as e:
|
||||||
|
raise DynamipsError("Could not save the startup configuration {}: {}".format(config_path, e))
|
||||||
|
|
||||||
|
if private_config_base64:
|
||||||
|
try:
|
||||||
|
config = base64.decodebytes(private_config_base64.encode("utf-8")).decode("utf-8")
|
||||||
|
config = "!\n" + config.replace("\r", "")
|
||||||
|
config_path = os.path.join(module_workdir, self.private_config)
|
||||||
|
with open(config_path, "w") as f:
|
||||||
|
log.info("saving private-config to {}".format(self.private_config))
|
||||||
|
f.write(config)
|
||||||
|
except OSError as e:
|
||||||
|
raise DynamipsError("Could not save the private configuration {}: {}".format(config_path, e))
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
"""
|
"""
|
||||||
@ -1555,6 +1540,20 @@ class Router(BaseVM):
|
|||||||
# delete the VM files
|
# delete the VM files
|
||||||
project_dir = os.path.join(self.project.module_working_directory(self.manager.module_name.lower()))
|
project_dir = os.path.join(self.project.module_working_directory(self.manager.module_name.lower()))
|
||||||
files = glob.glob(os.path.join(project_dir, "{}_i{}*".format(self._platform, self._dynamips_id)))
|
files = glob.glob(os.path.join(project_dir, "{}_i{}*".format(self._platform, self._dynamips_id)))
|
||||||
|
|
||||||
|
module_workdir = self.project.module_working_directory(self.manager.module_name.lower())
|
||||||
|
# delete the startup-config
|
||||||
|
if self._startup_config:
|
||||||
|
startup_config_path = os.path.join(module_workdir, "configs", "i{}_startup-config.cfg".format(self._dynamips_id))
|
||||||
|
if os.path.isfile(startup_config_path):
|
||||||
|
files.append(startup_config_path)
|
||||||
|
|
||||||
|
# delete the private-config
|
||||||
|
if self._private_config:
|
||||||
|
private_config_path = os.path.join(module_workdir, "configs", "i{}_private-config.cfg".format(self._dynamips_id))
|
||||||
|
if os.path.isfile(private_config_path):
|
||||||
|
files.append(private_config_path)
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
try:
|
try:
|
||||||
log.debug("Deleting file {}".format(file))
|
log.debug("Deleting file {}".format(file))
|
||||||
@ -1571,17 +1570,4 @@ class Router(BaseVM):
|
|||||||
|
|
||||||
yield from self._hypervisor.send('vm clean_delete "{}"'.format(self._name))
|
yield from self._hypervisor.send('vm clean_delete "{}"'.format(self._name))
|
||||||
self._hypervisor.devices.remove(self)
|
self._hypervisor.devices.remove(self)
|
||||||
|
|
||||||
# if self._startup_config:
|
|
||||||
# # delete the startup-config
|
|
||||||
# startup_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}.cfg".format(self.name))
|
|
||||||
# if os.path.isfile(startup_config_path):
|
|
||||||
# os.remove(startup_config_path)
|
|
||||||
#
|
|
||||||
# if self._private_config:
|
|
||||||
# # delete the private-config
|
|
||||||
# private_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}-private.cfg".format(self.name))
|
|
||||||
# if os.path.isfile(private_config_path):
|
|
||||||
# os.remove(private_config_path)
|
|
||||||
|
|
||||||
log.info('Router "{name}" [{id}] has been deleted (including associated files)'.format(name=self._name, id=self._id))
|
log.info('Router "{name}" [{id}] has been deleted (including associated files)'.format(name=self._name, id=self._id))
|
||||||
|
@ -62,11 +62,19 @@ VM_CREATE_SCHEMA = {
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"minLength": 1,
|
"minLength": 1,
|
||||||
},
|
},
|
||||||
|
"startup_config_content": {
|
||||||
|
"description": "Content of IOS startup configuration file",
|
||||||
|
"type": "string",
|
||||||
|
},
|
||||||
"private_config": {
|
"private_config": {
|
||||||
"description": "path to the IOS private configuration file",
|
"description": "path to the IOS private configuration file",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"minLength": 1,
|
"minLength": 1,
|
||||||
},
|
},
|
||||||
|
"private_config_content": {
|
||||||
|
"description": "Content of IOS private configuration file",
|
||||||
|
"type": "string",
|
||||||
|
},
|
||||||
"ram": {
|
"ram": {
|
||||||
"description": "amount of RAM in MB",
|
"description": "amount of RAM in MB",
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
|
Loading…
Reference in New Issue
Block a user