2014-02-01 01:31:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2014-03-03 00:20:03 +02:00
|
|
|
import base64
|
2014-02-01 01:31:34 +02:00
|
|
|
from gns3server.modules import IModule
|
|
|
|
from ..dynamips_error import DynamipsError
|
|
|
|
|
|
|
|
from ..nodes.c1700 import C1700
|
|
|
|
from ..nodes.c2600 import C2600
|
|
|
|
from ..nodes.c2691 import C2691
|
|
|
|
from ..nodes.c3600 import C3600
|
|
|
|
from ..nodes.c3725 import C3725
|
|
|
|
from ..nodes.c3745 import C3745
|
|
|
|
from ..nodes.c7200 import C7200
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
from ..schemas.vm import VM_CREATE_SCHEMA
|
|
|
|
from ..schemas.vm import VM_DELETE_SCHEMA
|
|
|
|
from ..schemas.vm import VM_START_SCHEMA
|
|
|
|
from ..schemas.vm import VM_STOP_SCHEMA
|
|
|
|
from ..schemas.vm import VM_SUSPEND_SCHEMA
|
|
|
|
from ..schemas.vm import VM_RELOAD_SCHEMA
|
|
|
|
from ..schemas.vm import VM_UPDATE_SCHEMA
|
2014-06-26 12:06:58 +03:00
|
|
|
from ..schemas.vm import VM_START_CAPTURE_SCHEMA
|
|
|
|
from ..schemas.vm import VM_STOP_CAPTURE_SCHEMA
|
2014-04-23 21:31:33 +03:00
|
|
|
from ..schemas.vm import VM_SAVE_CONFIG_SCHEMA
|
|
|
|
from ..schemas.vm import VM_IDLEPCS_SCHEMA
|
|
|
|
from ..schemas.vm import VM_ALLOCATE_UDP_PORT_SCHEMA
|
|
|
|
from ..schemas.vm import VM_ADD_NIO_SCHEMA
|
|
|
|
from ..schemas.vm import VM_DELETE_NIO_SCHEMA
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
PLATFORMS = {'c1700': C1700,
|
|
|
|
'c2600': C2600,
|
|
|
|
'c2691': C2691,
|
|
|
|
'c3725': C3725,
|
|
|
|
'c3745': C3745,
|
|
|
|
'c3600': C3600,
|
|
|
|
'c7200': C7200}
|
|
|
|
|
|
|
|
|
|
|
|
class VM(object):
|
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.create")
|
|
|
|
def vm_create(self, request):
|
|
|
|
"""
|
|
|
|
Creates a new VM (router).
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
2014-05-27 12:59:22 +03:00
|
|
|
- name (vm name)
|
2014-02-26 20:47:12 +02:00
|
|
|
- platform (platform name e.g. c7200)
|
|
|
|
- image (path to IOS image)
|
|
|
|
- ram (amount of RAM in MB)
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- console (console port number)
|
|
|
|
- aux (auxiliary console port number)
|
|
|
|
- mac_addr (MAC address)
|
2014-03-19 02:14:30 +02:00
|
|
|
- chassis (router chassis model)
|
2014-02-26 20:47:12 +02:00
|
|
|
|
|
|
|
Response parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
- name (vm name)
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_CREATE_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-05-27 12:59:22 +03:00
|
|
|
name = request["name"]
|
2014-02-26 20:47:12 +02:00
|
|
|
platform = request["platform"]
|
|
|
|
image = request["image"]
|
|
|
|
ram = request["ram"]
|
2014-03-03 00:20:03 +02:00
|
|
|
hypervisor = None
|
2014-06-15 14:18:33 +03:00
|
|
|
chassis = request.get("chassis")
|
|
|
|
router_id = request.get("router_id")
|
2014-02-26 20:47:12 +02:00
|
|
|
|
|
|
|
try:
|
2014-03-03 00:20:03 +02:00
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
if platform not in PLATFORMS:
|
|
|
|
raise DynamipsError("Unknown router platform: {}".format(platform))
|
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
if not self._hypervisor_manager:
|
2014-03-16 05:41:04 +02:00
|
|
|
self.start_hypervisor_manager()
|
2014-03-03 00:20:03 +02:00
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
hypervisor = self._hypervisor_manager.allocate_hypervisor_for_router(image, ram)
|
|
|
|
|
2014-03-19 02:14:30 +02:00
|
|
|
if chassis:
|
2014-06-15 14:18:33 +03:00
|
|
|
router = PLATFORMS[platform](hypervisor, name, router_id, chassis=chassis)
|
2014-03-19 02:14:30 +02:00
|
|
|
else:
|
2014-06-15 14:18:33 +03:00
|
|
|
router = PLATFORMS[platform](hypervisor, name, router_id)
|
2014-02-26 20:47:12 +02:00
|
|
|
router.ram = ram
|
|
|
|
router.image = image
|
|
|
|
router.sparsemem = self._hypervisor_manager.sparse_memory_support
|
|
|
|
router.mmap = self._hypervisor_manager.mmap_support
|
|
|
|
if "console" in request:
|
|
|
|
router.console = request["console"]
|
|
|
|
if "aux" in request:
|
|
|
|
router.aux = request["aux"]
|
|
|
|
if "mac_addr" in request:
|
|
|
|
router.mac_addr = request["mac_addr"]
|
|
|
|
|
|
|
|
# JIT sharing support
|
|
|
|
if self._hypervisor_manager.jit_sharing_support:
|
|
|
|
jitsharing_groups = hypervisor.jitsharing_groups
|
|
|
|
ios_image = os.path.basename(image)
|
|
|
|
if ios_image in jitsharing_groups:
|
|
|
|
router.jit_sharing_group = jitsharing_groups[ios_image]
|
|
|
|
else:
|
|
|
|
new_jit_group = -1
|
|
|
|
for jit_group in range(0, 127):
|
|
|
|
if jit_group not in jitsharing_groups.values():
|
|
|
|
new_jit_group = jit_group
|
|
|
|
break
|
|
|
|
if new_jit_group == -1:
|
|
|
|
raise DynamipsError("All JIT groups are allocated!")
|
|
|
|
router.jit_sharing_group = new_jit_group
|
|
|
|
|
|
|
|
# Ghost IOS support
|
|
|
|
if self._hypervisor_manager.ghost_ios_support:
|
|
|
|
self.set_ghost_ios(router)
|
|
|
|
|
|
|
|
except DynamipsError as e:
|
2014-03-03 00:20:03 +02:00
|
|
|
dynamips_stdout = ""
|
|
|
|
if hypervisor:
|
|
|
|
hypervisor.decrease_memory_load(ram)
|
|
|
|
if hypervisor.memory_load == 0 and not hypervisor.devices:
|
|
|
|
hypervisor.stop()
|
|
|
|
self._hypervisor_manager.hypervisors.remove(hypervisor)
|
|
|
|
dynamips_stdout = hypervisor.read_stdout()
|
|
|
|
self.send_custom_error(str(e) + dynamips_stdout)
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
response = {"name": router.name,
|
|
|
|
"id": router.id}
|
|
|
|
defaults = router.defaults()
|
|
|
|
response.update(defaults)
|
|
|
|
self._routers[router.id] = router
|
|
|
|
self.send_response(response)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.delete")
|
|
|
|
def vm_delete(self, request):
|
|
|
|
"""
|
|
|
|
Deletes a VM (router).
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_DELETE_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
2014-02-01 01:31:34 +02:00
|
|
|
router_id = request["id"]
|
2014-04-23 21:31:33 +03:00
|
|
|
router = self.get_device_instance(router_id, self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
try:
|
2014-05-06 21:22:38 +03:00
|
|
|
router.clean_delete()
|
2014-02-21 02:39:03 +02:00
|
|
|
self._hypervisor_manager.unallocate_hypervisor_for_router(router)
|
2014-03-11 23:45:04 +02:00
|
|
|
del self._routers[router_id]
|
2014-02-01 01:31:34 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
|
|
|
self.send_response(True)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.start")
|
|
|
|
def vm_start(self, request):
|
|
|
|
"""
|
|
|
|
Starts a VM (router)
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_START_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
try:
|
|
|
|
router.start()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
self.send_response(True)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.stop")
|
|
|
|
def vm_stop(self, request):
|
|
|
|
"""
|
|
|
|
Stops a VM (router)
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_STOP_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
try:
|
|
|
|
router.stop()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
|
|
|
self.send_response(True)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.suspend")
|
|
|
|
def vm_suspend(self, request):
|
|
|
|
"""
|
|
|
|
Suspends a VM (router)
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_SUSPEND_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
try:
|
|
|
|
router.suspend()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
|
|
|
self.send_response(True)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
2014-02-21 02:39:03 +02:00
|
|
|
@IModule.route("dynamips.vm.reload")
|
|
|
|
def vm_reload(self, request):
|
|
|
|
"""
|
|
|
|
Reloads a VM (router)
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-21 02:39:03 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_RELOAD_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
2014-02-21 02:39:03 +02:00
|
|
|
try:
|
|
|
|
if router.get_status() != "inactive":
|
|
|
|
router.stop()
|
|
|
|
router.start()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
|
|
|
self.send_response(True)
|
2014-02-21 02:39:03 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
@IModule.route("dynamips.vm.update")
|
|
|
|
def vm_update(self, request):
|
|
|
|
"""
|
|
|
|
Updates settings for a VM (router).
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- any setting to update
|
2014-03-03 00:20:03 +02:00
|
|
|
- startup_config_base64 (startup-config base64 encoded)
|
2014-03-19 02:14:30 +02:00
|
|
|
- private_config_base64 (private-config base64 encoded)
|
2014-02-26 20:47:12 +02:00
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- updated settings
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_UPDATE_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-02-01 01:31:34 +02:00
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
response = {}
|
2014-03-03 00:20:03 +02:00
|
|
|
try:
|
2014-06-18 16:22:57 +03:00
|
|
|
startup_config_path = os.path.join(router.hypervisor.working_dir, "configs", "i{}_startup-config.cfg".format(router.id))
|
|
|
|
private_config_path = os.path.join(router.hypervisor.working_dir, "configs", "i{}_private-config.cfg".format(router.id))
|
2014-05-08 23:20:12 +03:00
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
# a new startup-config has been pushed
|
|
|
|
if "startup_config_base64" in request:
|
2014-04-28 19:03:03 +03:00
|
|
|
# update the request with the new local startup-config path
|
2014-05-08 23:20:12 +03:00
|
|
|
request["startup_config"] = self.create_config_from_base64(request["startup_config_base64"], router, startup_config_path)
|
2014-03-19 02:14:30 +02:00
|
|
|
|
|
|
|
# a new private-config has been pushed
|
|
|
|
if "private_config_base64" in request:
|
2014-04-28 19:03:03 +03:00
|
|
|
# update the request with the new local private-config path
|
2014-05-08 23:20:12 +03:00
|
|
|
request["private_config"] = self.create_config_from_base64(request["private_config_base64"], router, private_config_path)
|
|
|
|
|
|
|
|
if "startup_config" in request:
|
|
|
|
if os.path.isfile(request["startup_config"]) and request["startup_config"] != startup_config_path:
|
|
|
|
# this is a local file set in the GUI
|
|
|
|
request["startup_config"] = self.create_config_from_file(request["startup_config"], router, startup_config_path)
|
|
|
|
router.set_config(request["startup_config"])
|
|
|
|
else:
|
|
|
|
router.set_config(request["startup_config"])
|
2014-07-03 18:02:25 +03:00
|
|
|
response["startup_config"] = request["startup_config"]
|
2014-05-08 23:20:12 +03:00
|
|
|
|
2014-04-28 19:03:03 +03:00
|
|
|
if "private_config" in request:
|
2014-05-08 23:20:12 +03:00
|
|
|
if os.path.isfile(request["private_config"]) and request["private_config"] != private_config_path:
|
|
|
|
# this is a local file set in the GUI
|
|
|
|
request["private_config"] = self.create_config_from_file(request["private_config"], router, private_config_path)
|
|
|
|
router.set_config(router.startup_config, request["private_config"])
|
|
|
|
else:
|
|
|
|
router.set_config(router.startup_config, request["private_config"])
|
2014-07-03 18:02:25 +03:00
|
|
|
response["private_config"] = request["private_config"]
|
2014-03-19 02:14:30 +02:00
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
# update the settings
|
|
|
|
for name, value in request.items():
|
|
|
|
if hasattr(router, name) and getattr(router, name) != value:
|
|
|
|
try:
|
|
|
|
setattr(router, name, value)
|
2014-04-23 21:31:33 +03:00
|
|
|
response[name] = value
|
2014-02-01 01:31:34 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
elif name.startswith("slot") and value in ADAPTER_MATRIX:
|
|
|
|
slot_id = int(name[-1])
|
|
|
|
adapter_name = value
|
|
|
|
adapter = ADAPTER_MATRIX[adapter_name]()
|
|
|
|
try:
|
|
|
|
if router.slots[slot_id] and type(router.slots[slot_id]) != type(adapter):
|
|
|
|
router.slot_remove_binding(slot_id)
|
|
|
|
router.slot_add_binding(slot_id, adapter)
|
2014-04-23 21:31:33 +03:00
|
|
|
response[name] = value
|
2014-02-01 01:31:34 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
elif name.startswith("slot") and value == None:
|
|
|
|
slot_id = int(name[-1])
|
|
|
|
if router.slots[slot_id]:
|
|
|
|
try:
|
|
|
|
router.slot_remove_binding(slot_id)
|
2014-04-23 21:31:33 +03:00
|
|
|
response[name] = value
|
2014-03-03 00:20:03 +02:00
|
|
|
except DynamipsError as e:
|
2014-02-01 01:31:34 +02:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
elif name.startswith("wic") and value in WIC_MATRIX:
|
|
|
|
wic_slot_id = int(name[-1])
|
|
|
|
wic_name = value
|
|
|
|
wic = WIC_MATRIX[wic_name]()
|
|
|
|
try:
|
|
|
|
if router.slots[0].wics[wic_slot_id] and type(router.slots[0].wics[wic_slot_id]) != type(wic):
|
|
|
|
router.uninstall_wic(wic_slot_id)
|
|
|
|
router.install_wic(wic_slot_id, wic)
|
2014-04-23 21:31:33 +03:00
|
|
|
response[name] = value
|
2014-02-01 01:31:34 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
elif name.startswith("wic") and value == None:
|
|
|
|
wic_slot_id = int(name[-1])
|
|
|
|
if router.slots[0].wics and router.slots[0].wics[wic_slot_id]:
|
2014-03-03 00:20:03 +02:00
|
|
|
try:
|
|
|
|
router.uninstall_wic(wic_slot_id)
|
2014-04-23 21:31:33 +03:00
|
|
|
response[name] = value
|
2014-03-03 00:20:03 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
# Update the ghost IOS file in case the RAM size has changed
|
|
|
|
if self._hypervisor_manager.ghost_ios_support:
|
2014-05-21 03:06:28 +03:00
|
|
|
try:
|
|
|
|
self.set_ghost_ios(router)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
2014-02-01 01:31:34 +02:00
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
self.send_response(response)
|
2014-02-01 01:31:34 +02:00
|
|
|
|
2014-06-26 12:06:58 +03:00
|
|
|
@IModule.route("dynamips.vm.start_capture")
|
|
|
|
def vm_start_capture(self, request):
|
|
|
|
"""
|
|
|
|
Starts a packet capture.
|
|
|
|
|
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
- port_id (port identifier)
|
|
|
|
- slot (slot number)
|
|
|
|
- port (port number)
|
|
|
|
- capture_file_name
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- data_link_type (PCAP DLT_* value)
|
|
|
|
|
|
|
|
Response parameters:
|
|
|
|
- port_id (port identifier)
|
|
|
|
- capture_file_path (path to the capture file)
|
|
|
|
|
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_START_CAPTURE_SCHEMA):
|
|
|
|
return
|
|
|
|
|
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
|
|
|
return
|
|
|
|
|
|
|
|
slot = request["slot"]
|
|
|
|
port = request["port"]
|
|
|
|
capture_file_name = request["capture_file_name"]
|
|
|
|
data_link_type = request.get("data_link_type")
|
|
|
|
|
|
|
|
try:
|
|
|
|
capture_file_path = os.path.join(router.hypervisor.working_dir, "captures", capture_file_name)
|
|
|
|
router.start_capture(slot, port, capture_file_path, data_link_type)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
response = {"port_id": request["port_id"],
|
|
|
|
"capture_file_path": capture_file_path}
|
|
|
|
self.send_response(response)
|
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.stop_capture")
|
|
|
|
def vm_stop_capture(self, request):
|
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
- port_id (port identifier)
|
|
|
|
- slot (slot number)
|
|
|
|
- port (port number)
|
|
|
|
|
|
|
|
Response parameters:
|
|
|
|
- port_id (port identifier)
|
|
|
|
|
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_STOP_CAPTURE_SCHEMA):
|
|
|
|
return
|
|
|
|
|
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
|
|
|
return
|
|
|
|
|
|
|
|
slot = request["slot"]
|
|
|
|
port = request["port"]
|
|
|
|
try:
|
|
|
|
router.stop_capture(slot, port)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
response = {"port_id": request["port_id"]}
|
|
|
|
self.send_response(response)
|
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
@IModule.route("dynamips.vm.save_config")
|
|
|
|
def vm_save_config(self, request):
|
|
|
|
"""
|
|
|
|
Save the configs for a VM (router).
|
|
|
|
|
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_SAVE_CONFIG_SCHEMA):
|
2014-03-03 00:20:03 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
try:
|
2014-03-19 02:14:30 +02:00
|
|
|
if router.startup_config or router.private_config:
|
|
|
|
|
|
|
|
startup_config_base64, private_config_base64 = router.extract_config()
|
2014-03-03 00:20:03 +02:00
|
|
|
if startup_config_base64:
|
|
|
|
try:
|
|
|
|
config = base64.decodestring(startup_config_base64.encode("utf-8")).decode("utf-8")
|
|
|
|
config = "!\n" + config.replace("\r", "")
|
|
|
|
config_path = os.path.join(router.hypervisor.working_dir, router.startup_config)
|
|
|
|
with open(config_path, "w") as f:
|
|
|
|
log.info("saving startup-config to {}".format(router.startup_config))
|
|
|
|
f.write(config)
|
2014-04-03 01:10:59 +03:00
|
|
|
except OSError as e:
|
2014-03-19 02:14:30 +02:00
|
|
|
raise DynamipsError("Could not save the startup configuration {}: {}".format(config_path, e))
|
|
|
|
|
|
|
|
if private_config_base64:
|
|
|
|
try:
|
|
|
|
config = base64.decodestring(private_config_base64.encode("utf-8")).decode("utf-8")
|
|
|
|
config = "!\n" + config.replace("\r", "")
|
|
|
|
config_path = os.path.join(router.hypervisor.working_dir, router.private_config)
|
|
|
|
with open(config_path, "w") as f:
|
|
|
|
log.info("saving private-config to {}".format(router.private_config))
|
|
|
|
f.write(config)
|
2014-04-03 01:10:59 +03:00
|
|
|
except OSError as e:
|
2014-03-19 02:14:30 +02:00
|
|
|
raise DynamipsError("Could not save the private configuration {}: {}".format(config_path, e))
|
|
|
|
|
2014-03-03 00:20:03 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
log.warn("could not save config to {}: {}".format(router.startup_config, e))
|
|
|
|
|
2014-02-21 02:39:03 +02:00
|
|
|
@IModule.route("dynamips.vm.idlepcs")
|
|
|
|
def vm_idlepcs(self, request):
|
|
|
|
"""
|
|
|
|
Get idle-pc proposals.
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- compute (returns previously compute idle-pc values if False)
|
|
|
|
|
|
|
|
Response parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
- idlepcs (idle-pc values in an array)
|
|
|
|
|
2014-02-21 02:39:03 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_IDLEPCS_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-02-21 02:39:03 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
if "compute" in request and request["compute"] == False:
|
|
|
|
idlepcs = router.show_idle_pc_prop()
|
|
|
|
else:
|
2014-02-26 20:47:12 +02:00
|
|
|
# reset the current idle-pc value before calculating a new one
|
2014-02-21 02:39:03 +02:00
|
|
|
router.idlepc = "0x0"
|
|
|
|
idlepcs = router.get_idle_pc_prop()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
response = {"id": router.id,
|
2014-02-21 02:39:03 +02:00
|
|
|
"idlepcs": idlepcs}
|
|
|
|
self.send_response(response)
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
@IModule.route("dynamips.vm.allocate_udp_port")
|
|
|
|
def vm_allocate_udp_port(self, request):
|
|
|
|
"""
|
|
|
|
Allocates a UDP port in order to create an UDP NIO.
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
2014-02-28 06:50:46 +02:00
|
|
|
- port_id (unique port identifier)
|
2014-02-26 20:47:12 +02:00
|
|
|
|
|
|
|
Response parameters:
|
2014-02-28 06:50:46 +02:00
|
|
|
- port_id (unique port identifier)
|
2014-02-26 20:47:12 +02:00
|
|
|
- lport (allocated local port)
|
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_ALLOCATE_UDP_PORT_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
# allocate a new UDP port
|
|
|
|
response = self.allocate_udp_port(router)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-02-28 06:50:46 +02:00
|
|
|
response["port_id"] = request["port_id"]
|
2014-02-01 01:31:34 +02:00
|
|
|
self.send_response(response)
|
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.add_nio")
|
|
|
|
def vm_add_nio(self, request):
|
|
|
|
"""
|
|
|
|
Adds an NIO (Network Input/Output) for a VM (router).
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
2014-02-28 06:50:46 +02:00
|
|
|
- slot (slot number)
|
|
|
|
- port (port number)
|
|
|
|
- port_id (unique port identifier)
|
2014-04-23 21:31:33 +03:00
|
|
|
- nio (one of the following)
|
|
|
|
- type "nio_udp"
|
2014-02-26 20:47:12 +02:00
|
|
|
- lport (local port)
|
|
|
|
- rhost (remote host)
|
|
|
|
- rport (remote port)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_generic_ethernet"
|
2014-02-26 20:47:12 +02:00
|
|
|
- ethernet_device (Ethernet device name e.g. eth0)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_linux_ethernet"
|
2014-02-26 20:47:12 +02:00
|
|
|
- ethernet_device (Ethernet device name e.g. eth0)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_tap"
|
2014-02-26 20:47:12 +02:00
|
|
|
- tap_device (TAP device name e.g. tap0)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_unix"
|
2014-02-26 20:47:12 +02:00
|
|
|
- local_file (path to UNIX socket file)
|
|
|
|
- remote_file (path to UNIX socket file)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_vde"
|
2014-02-26 20:47:12 +02:00
|
|
|
- control_file (path to VDE control file)
|
|
|
|
- local_file (path to VDE local file)
|
2014-04-23 21:31:33 +03:00
|
|
|
- type "nio_null"
|
2014-02-26 20:47:12 +02:00
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- port_id (unique port identifier)
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_ADD_NIO_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
slot = request["slot"]
|
|
|
|
port = request["port"]
|
|
|
|
try:
|
|
|
|
nio = self.create_nio(router, request)
|
2014-02-26 20:47:12 +02:00
|
|
|
if not nio:
|
|
|
|
raise DynamipsError("Requested NIO doesn't exist: {}".format(request["nio"]))
|
2014-02-01 01:31:34 +02:00
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
router.slot_add_nio_binding(slot, port, nio)
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
self.send_response({"port_id": request["port_id"]})
|
2014-02-01 01:31:34 +02:00
|
|
|
|
|
|
|
@IModule.route("dynamips.vm.delete_nio")
|
|
|
|
def vm_delete_nio(self, request):
|
|
|
|
"""
|
|
|
|
Deletes an NIO (Network Input/Output).
|
|
|
|
|
2014-02-26 20:47:12 +02:00
|
|
|
Mandatory request parameters:
|
|
|
|
- id (vm identifier)
|
|
|
|
- slot (slot identifier)
|
|
|
|
- port (port identifier)
|
|
|
|
|
|
|
|
Response parameters:
|
2014-04-23 21:31:33 +03:00
|
|
|
- True on success
|
2014-02-26 20:47:12 +02:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VM_DELETE_NIO_SCHEMA):
|
2014-02-26 20:47:12 +02:00
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
# get the router instance
|
|
|
|
router = self.get_device_instance(request["id"], self._routers)
|
|
|
|
if not router:
|
2014-04-12 23:43:30 +03:00
|
|
|
return
|
2014-04-23 21:31:33 +03:00
|
|
|
|
2014-02-01 01:31:34 +02:00
|
|
|
slot = request["slot"]
|
|
|
|
port = request["port"]
|
|
|
|
try:
|
|
|
|
nio = router.slot_remove_nio_binding(slot, port)
|
|
|
|
nio.delete()
|
|
|
|
except DynamipsError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-04-23 21:31:33 +03:00
|
|
|
self.send_response(True)
|