2015-02-10 03:24:13 +02:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Interface for Dynamips virtual Machine module ("vm")
|
|
|
|
http://github.com/GNS3/dynamips/blob/master/README.hypervisor#L77
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
2017-02-27 13:48:05 +02:00
|
|
|
import re
|
2015-02-16 07:13:24 +02:00
|
|
|
import glob
|
2015-02-17 01:53:50 +02:00
|
|
|
import base64
|
2017-01-06 16:16:19 +02:00
|
|
|
import shutil
|
2015-03-29 03:09:53 +03:00
|
|
|
import binascii
|
2015-02-10 03:24:13 +02:00
|
|
|
import logging
|
2017-01-06 16:16:19 +02:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
from ...base_node import BaseNode
|
2015-02-16 07:13:24 +02:00
|
|
|
from ..dynamips_error import DynamipsError
|
2016-06-13 16:52:31 +03:00
|
|
|
|
|
|
|
from gns3server.utils.file_watcher import FileWatcher
|
2018-10-15 13:05:49 +03:00
|
|
|
from gns3server.utils.asyncio import wait_run_in_executor, monitor_process
|
2015-06-17 18:11:25 +03:00
|
|
|
from gns3server.utils.images import md5sum
|
2015-02-16 07:13:24 +02:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2016-05-11 20:35:36 +03:00
|
|
|
class Router(BaseNode):
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Dynamips router implementation.
|
2015-02-11 06:50:02 +02:00
|
|
|
|
|
|
|
:param name: The name of this router
|
2016-05-11 20:35:36 +03:00
|
|
|
:param node_id: Node identifier
|
2015-02-11 06:50:02 +02:00
|
|
|
:param project: Project instance
|
|
|
|
:param manager: Parent VM Manager
|
2015-02-12 04:21:34 +02:00
|
|
|
:param dynamips_id: ID to use with Dynamips
|
2015-02-27 01:15:44 +02:00
|
|
|
:param console: console port
|
2020-07-29 09:53:51 +03:00
|
|
|
:param console_type: console type
|
2015-02-27 01:15:44 +02:00
|
|
|
:param aux: auxiliary console port
|
2020-07-29 09:53:51 +03:00
|
|
|
:param aux_type: auxiliary console type
|
2015-02-11 06:50:02 +02:00
|
|
|
:param platform: Platform of this router
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
_status = {0: "inactive", 1: "shutting down", 2: "running", 3: "suspended"}
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name,
|
|
|
|
node_id,
|
|
|
|
project,
|
|
|
|
manager,
|
|
|
|
dynamips_id=None,
|
|
|
|
console=None,
|
|
|
|
console_type="telnet",
|
|
|
|
aux=None,
|
|
|
|
aux_type="none",
|
|
|
|
platform="c7200",
|
|
|
|
hypervisor=None,
|
|
|
|
ghost_flag=False,
|
|
|
|
):
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
name, node_id, project, manager, console=console, console_type=console_type, aux=aux, aux_type=aux_type
|
|
|
|
)
|
|
|
|
|
|
|
|
self._working_directory = os.path.join(
|
|
|
|
self.project.module_working_directory(self.manager.module_name.lower()), self.id
|
|
|
|
)
|
2017-05-03 18:55:13 +03:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.join(self._working_directory, "configs"), exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Can't create the dynamips config directory: {str(e)}")
|
2017-01-10 13:15:31 +02:00
|
|
|
if dynamips_id:
|
|
|
|
self._convert_before_2_0_0_b3(dynamips_id)
|
|
|
|
|
2015-02-16 07:13:24 +02:00
|
|
|
self._hypervisor = hypervisor
|
2015-02-12 04:21:34 +02:00
|
|
|
self._dynamips_id = dynamips_id
|
2015-02-10 03:24:13 +02:00
|
|
|
self._platform = platform
|
|
|
|
self._image = ""
|
|
|
|
self._ram = 128 # Megabytes
|
|
|
|
self._nvram = 128 # Kilobytes
|
|
|
|
self._mmap = True
|
|
|
|
self._sparsemem = True
|
|
|
|
self._clock_divisor = 8
|
|
|
|
self._idlepc = ""
|
|
|
|
self._idlemax = 500
|
|
|
|
self._idlesleep = 30
|
|
|
|
self._ghost_file = ""
|
|
|
|
self._ghost_status = 0
|
2022-01-19 13:58:36 +02:00
|
|
|
self._exec_area = 64
|
2015-02-10 03:24:13 +02:00
|
|
|
self._disk0 = 0 # Megabytes
|
|
|
|
self._disk1 = 0 # Megabytes
|
2015-06-05 23:54:22 +03:00
|
|
|
self._auto_delete_disks = False
|
2015-02-12 04:21:34 +02:00
|
|
|
self._mac_addr = ""
|
2015-02-10 03:24:13 +02:00
|
|
|
self._system_id = "FTX0945W0MY" # processor board ID in IOS
|
|
|
|
self._slots = []
|
|
|
|
self._ghost_flag = ghost_flag
|
2016-06-13 16:52:31 +03:00
|
|
|
self._memory_watcher = None
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if not ghost_flag:
|
2015-02-12 04:21:34 +02:00
|
|
|
if not dynamips_id:
|
2015-10-07 12:34:27 +03:00
|
|
|
self._dynamips_id = manager.get_dynamips_id(project.id)
|
2015-02-12 04:21:34 +02:00
|
|
|
else:
|
2015-10-07 12:34:27 +03:00
|
|
|
self._dynamips_id = dynamips_id
|
|
|
|
manager.take_dynamips_id(project.id, dynamips_id)
|
2015-02-12 04:21:34 +02:00
|
|
|
else:
|
2015-02-16 07:13:24 +02:00
|
|
|
log.info("Creating a new ghost IOS instance")
|
2015-02-24 02:42:55 +02:00
|
|
|
if self._console:
|
|
|
|
# Ghost VMs do not need a console port.
|
2016-02-29 11:38:30 +02:00
|
|
|
self.console = None
|
|
|
|
|
2015-02-12 04:21:34 +02:00
|
|
|
self._dynamips_id = 0
|
|
|
|
self._name = "Ghost"
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2017-01-10 13:15:31 +02:00
|
|
|
def _convert_before_2_0_0_b3(self, dynamips_id):
|
|
|
|
"""
|
|
|
|
Before 2.0.0 beta3 the node didn't have a folder by node
|
|
|
|
when we start we move the file, we can't do it in the topology
|
|
|
|
conversion due to case of remote servers
|
|
|
|
"""
|
|
|
|
dynamips_dir = self.project.module_working_directory(self.manager.module_name.lower())
|
2021-04-13 12:07:58 +03:00
|
|
|
for path in glob.glob(os.path.join(glob.escape(dynamips_dir), "configs", f"i{dynamips_id}_*")):
|
2017-01-10 13:15:31 +02:00
|
|
|
dst = os.path.join(self._working_directory, "configs", os.path.basename(path))
|
|
|
|
if not os.path.exists(dst):
|
|
|
|
try:
|
|
|
|
shutil.move(path, dst)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.error(f"Can't move {path}: {str(e)}")
|
2018-04-16 10:45:43 +03:00
|
|
|
continue
|
2021-04-13 12:07:58 +03:00
|
|
|
for path in glob.glob(os.path.join(glob.escape(dynamips_dir), f"*_i{dynamips_id}_*")):
|
2017-01-10 13:15:31 +02:00
|
|
|
dst = os.path.join(self._working_directory, os.path.basename(path))
|
|
|
|
if not os.path.exists(dst):
|
|
|
|
try:
|
|
|
|
shutil.move(path, dst)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.error(f"Can't move {path}: {str(e)}")
|
2018-04-16 10:45:43 +03:00
|
|
|
continue
|
2017-01-06 16:16:19 +02:00
|
|
|
|
2021-04-17 17:04:28 +03:00
|
|
|
def asdict(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
router_info = {
|
|
|
|
"name": self.name,
|
|
|
|
"usage": self.usage,
|
|
|
|
"node_id": self.id,
|
|
|
|
"node_directory": os.path.join(self._working_directory),
|
|
|
|
"project_id": self.project.id,
|
|
|
|
"dynamips_id": self._dynamips_id,
|
|
|
|
"platform": self._platform,
|
|
|
|
"image": self._image,
|
|
|
|
"image_md5sum": md5sum(self._image),
|
|
|
|
"ram": self._ram,
|
|
|
|
"nvram": self._nvram,
|
|
|
|
"mmap": self._mmap,
|
|
|
|
"sparsemem": self._sparsemem,
|
|
|
|
"clock_divisor": self._clock_divisor,
|
|
|
|
"idlepc": self._idlepc,
|
|
|
|
"idlemax": self._idlemax,
|
|
|
|
"idlesleep": self._idlesleep,
|
|
|
|
"exec_area": self._exec_area,
|
|
|
|
"disk0": self._disk0,
|
|
|
|
"disk1": self._disk1,
|
|
|
|
"auto_delete_disks": self._auto_delete_disks,
|
|
|
|
"status": self.status,
|
|
|
|
"console": self.console,
|
|
|
|
"console_type": self.console_type,
|
|
|
|
"aux": self.aux,
|
|
|
|
"aux_type": self.aux_type,
|
|
|
|
"mac_addr": self._mac_addr,
|
|
|
|
"system_id": self._system_id,
|
|
|
|
}
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-11-19 10:53:43 +02:00
|
|
|
router_info["image"] = self.manager.get_relative_image_path(self._image, self.project.path)
|
2015-03-11 23:04:11 +02:00
|
|
|
|
2015-02-17 01:53:50 +02:00
|
|
|
# add the slots
|
|
|
|
slot_number = 0
|
|
|
|
for slot in self._slots:
|
|
|
|
if slot:
|
|
|
|
slot = str(slot)
|
2015-07-27 00:51:55 +03:00
|
|
|
router_info["slot" + str(slot_number)] = slot
|
2015-02-17 01:53:50 +02:00
|
|
|
slot_number += 1
|
|
|
|
|
|
|
|
# add the wics
|
2015-03-04 17:01:56 +02:00
|
|
|
if len(self._slots) > 0 and self._slots[0] and self._slots[0].wics:
|
2015-02-17 01:53:50 +02:00
|
|
|
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])
|
2015-07-27 00:51:55 +03:00
|
|
|
else:
|
|
|
|
router_info["wic" + str(wic_slot_number)] = None
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
return router_info
|
|
|
|
|
2016-06-13 16:52:31 +03:00
|
|
|
def _memory_changed(self, path):
|
|
|
|
"""
|
|
|
|
Called when the NVRAM file has changed
|
|
|
|
"""
|
2018-10-15 13:05:49 +03:00
|
|
|
asyncio.ensure_future(self.save_configs())
|
2016-06-13 16:52:31 +03:00
|
|
|
|
2015-02-16 07:13:24 +02:00
|
|
|
@property
|
|
|
|
def dynamips_id(self):
|
|
|
|
"""
|
|
|
|
Returns the Dynamips VM ID.
|
|
|
|
|
|
|
|
:return: Dynamips VM identifier
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._dynamips_id
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def create(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-16 07:13:24 +02:00
|
|
|
if not self._hypervisor:
|
2017-01-06 16:16:19 +02:00
|
|
|
# We start the hypervisor is the dynamips folder and next we change to node dir
|
|
|
|
# this allow the creation of common files in the dynamips folder
|
2021-04-13 12:16:50 +03:00
|
|
|
self._hypervisor = await self.manager.start_new_hypervisor(
|
|
|
|
working_dir=self.project.module_working_directory(self.manager.module_name.lower())
|
|
|
|
)
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._hypervisor.set_working_dir(self._working_directory)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm create "{name}" {id} {platform}'.format(name=self._name, id=self._dynamips_id, platform=self._platform)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if not self._ghost_flag:
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router {platform} "{name}" [{id}] has been created'.format(
|
|
|
|
name=self._name, platform=self._platform, id=self._id
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2020-07-29 09:53:51 +03:00
|
|
|
if self._console is not None:
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_con_tcp_port "{self._name}" {self._console}')
|
2015-02-27 01:15:44 +02:00
|
|
|
|
2016-02-29 11:38:30 +02:00
|
|
|
if self.aux is not None:
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_aux_tcp_port "{self._name}" {self.aux}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# get the default base MAC address
|
2021-04-13 12:07:58 +03:00
|
|
|
mac_addr = await self._hypervisor.send(f'{self._platform} get_mac_addr "{self._name}"')
|
2015-02-10 03:24:13 +02:00
|
|
|
self._mac_addr = mac_addr[0]
|
|
|
|
|
|
|
|
self._hypervisor.devices.append(self)
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_status(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Returns the status of this router
|
|
|
|
|
|
|
|
:returns: inactive, shutting down, running or suspended.
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
status = await self._hypervisor.send(f'vm get_status "{self._name}"')
|
2015-04-01 18:39:37 +03:00
|
|
|
if len(status) == 0:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Can't get vm {self._name} status")
|
2015-02-10 03:24:13 +02:00
|
|
|
return self._status[int(status[0])]
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def start(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Starts this router.
|
|
|
|
At least the IOS image must be set before it can start.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2015-02-10 03:24:13 +02:00
|
|
|
if status == "suspended":
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.resume()
|
2015-02-10 03:24:13 +02:00
|
|
|
elif status == "inactive":
|
|
|
|
|
|
|
|
if not os.path.isfile(self._image) or not os.path.exists(self._image):
|
|
|
|
if os.path.islink(self._image):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
f'IOS image "{self._image}" linked to "{os.path.realpath(self._image)}" is not accessible'
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'IOS image "{self._image}" is not accessible')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
with open(self._image, "rb") as f:
|
|
|
|
# read the first 7 bytes of the file.
|
|
|
|
elf_header_start = f.read(7)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'Cannot read ELF header for IOS image "{self._image}": {e}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# IOS images must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
|
2021-04-13 12:16:50 +03:00
|
|
|
if elf_header_start != b"\x7fELF\x01\x02\x01":
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'"{self._image}" is not a valid IOS image')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-10-13 00:57:37 +03:00
|
|
|
# check if there is enough RAM to run
|
|
|
|
if not self._ghost_flag:
|
|
|
|
self.check_available_ram(self.ram)
|
|
|
|
|
2018-03-12 08:38:50 +02:00
|
|
|
# config paths are relative to the working directory configured on Dynamips hypervisor
|
2021-04-13 12:07:58 +03:00
|
|
|
startup_config_path = os.path.join("configs", f"i{self._dynamips_id}_startup-config.cfg")
|
|
|
|
private_config_path = os.path.join("configs", f"i{self._dynamips_id}_private-config.cfg")
|
2017-02-02 20:13:47 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
if not os.path.exists(os.path.join(self._working_directory, private_config_path)) or not os.path.getsize(
|
|
|
|
os.path.join(self._working_directory, private_config_path)
|
|
|
|
):
|
2017-02-02 20:13:47 +02:00
|
|
|
# an empty private-config can prevent a router to boot.
|
2021-04-13 12:16:50 +03:00
|
|
|
private_config_path = ""
|
2018-03-12 08:38:50 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm set_config "{name}" "{startup}" "{private}"'.format(
|
|
|
|
name=self._name, startup=startup_config_path, private=private_config_path
|
|
|
|
)
|
|
|
|
)
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm start "{self._name}"')
|
2015-03-04 17:01:56 +02:00
|
|
|
self.status = "started"
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'router "{self._name}" [{self._id}] has been started')
|
2016-06-13 16:52:31 +03:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
self._memory_watcher = FileWatcher(self._memory_files(), self._memory_changed, strategy="hash", delay=30)
|
2015-03-04 17:01:56 +02:00
|
|
|
monitor_process(self._hypervisor.process, self._termination_callback)
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _termination_callback(self, returncode):
|
2015-03-04 17:01:56 +02:00
|
|
|
"""
|
2015-05-13 22:53:42 +03:00
|
|
|
Called when the process has stopped.
|
2015-03-04 17:01:56 +02:00
|
|
|
|
|
|
|
:param returncode: Process returncode
|
|
|
|
"""
|
2015-05-13 22:53:42 +03:00
|
|
|
|
2015-07-04 23:08:03 +03:00
|
|
|
if self.status == "started":
|
|
|
|
self.status = "stopped"
|
|
|
|
log.info("Dynamips hypervisor process has stopped, return code: %d", returncode)
|
|
|
|
if returncode != 0:
|
2021-04-13 12:16:50 +03:00
|
|
|
self.project.emit(
|
|
|
|
"log.error",
|
|
|
|
{
|
|
|
|
"message": f"Dynamips hypervisor process has stopped, return code: {returncode}\n{self._hypervisor.read_stdout()}"
|
|
|
|
},
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def stop(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Stops this router.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2015-02-10 03:24:13 +02:00
|
|
|
if status != "inactive":
|
2016-05-22 04:13:36 +03:00
|
|
|
try:
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm stop "{self._name}"')
|
2016-05-22 04:13:36 +03:00
|
|
|
except DynamipsError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Could not stop {self._name}: {e}")
|
2015-03-04 17:01:56 +02:00
|
|
|
self.status = "stopped"
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}] has been stopped')
|
2016-06-13 16:52:31 +03:00
|
|
|
if self._memory_watcher:
|
|
|
|
self._memory_watcher.close()
|
|
|
|
self._memory_watcher = None
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.save_configs()
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def reload(self):
|
2015-02-13 04:15:35 +02:00
|
|
|
"""
|
|
|
|
Reload this router.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.stop()
|
|
|
|
await self.start()
|
2015-02-13 04:15:35 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def suspend(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Suspends this router.
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2015-02-10 03:24:13 +02:00
|
|
|
if status == "running":
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm suspend "{self._name}"')
|
2016-05-14 05:41:58 +03:00
|
|
|
self.status = "suspended"
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}] has been suspended')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def resume(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Resumes this suspended router
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2017-01-09 14:24:23 +02:00
|
|
|
if status == "suspended":
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm resume "{self._name}"')
|
2017-01-09 14:24:23 +02:00
|
|
|
self.status = "started"
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}] has been resumed')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def is_running(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Checks if this router is running.
|
|
|
|
|
|
|
|
:returns: True if running, False otherwise
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2015-02-10 03:24:13 +02:00
|
|
|
if status == "running":
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def close(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
if not (await super().close()):
|
2016-02-29 11:38:30 +02:00
|
|
|
return False
|
2015-02-24 04:00:34 +02:00
|
|
|
|
|
|
|
for adapter in self._slots:
|
|
|
|
if adapter is not None:
|
|
|
|
for nio in adapter.ports.values():
|
2017-07-10 21:38:28 +03:00
|
|
|
if nio:
|
2018-10-15 13:05:49 +03:00
|
|
|
await nio.close()
|
2017-07-10 21:38:28 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._stop_ubridge()
|
2015-02-24 04:00:34 +02:00
|
|
|
|
2015-02-22 02:24:39 +02:00
|
|
|
if self in self._hypervisor.devices:
|
|
|
|
self._hypervisor.devices.remove(self)
|
2015-02-15 21:18:12 +02:00
|
|
|
if self._hypervisor and not self._hypervisor.devices:
|
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.stop()
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm delete "{self._name}"')
|
2016-05-22 04:13:36 +03:00
|
|
|
except DynamipsError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Could not stop and delete {self._name}: {e}")
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.hypervisor.stop()
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-06-05 23:54:22 +03:00
|
|
|
if self._auto_delete_disks:
|
|
|
|
# delete nvram and disk files
|
2021-04-13 12:16:50 +03:00
|
|
|
files = glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_disk[0-1]")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_slot[0-1]")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_nvram")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_flash[0-1]")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_rom")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_bootflash")
|
|
|
|
)
|
|
|
|
files += glob.glob(
|
|
|
|
os.path.join(glob.escape(self._working_directory), f"{self.platform}_i{self.dynamips_id}_ssa")
|
|
|
|
)
|
2015-06-05 23:54:22 +03:00
|
|
|
for file in files:
|
|
|
|
try:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.debug(f"Deleting file {file}")
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_run_in_executor(os.remove, file)
|
2015-06-05 23:54:22 +03:00
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Could not delete file {file}: {e}")
|
2015-06-05 23:54:22 +03:00
|
|
|
continue
|
2016-10-18 17:17:49 +03:00
|
|
|
self.manager.release_dynamips_id(self.project.id, self.dynamips_id)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def platform(self):
|
|
|
|
"""
|
|
|
|
Returns the platform of this router.
|
|
|
|
|
|
|
|
:returns: platform name (string):
|
|
|
|
c7200, c3745, c3725, c3600, c2691, c2600 or c1700
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._platform
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hypervisor(self):
|
|
|
|
"""
|
|
|
|
Returns the current hypervisor.
|
|
|
|
|
|
|
|
:returns: hypervisor instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._hypervisor
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def list(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Returns all VM instances
|
|
|
|
|
|
|
|
:returns: list of all VM instances
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
vm_list = await self._hypervisor.send("vm list")
|
2015-02-10 03:24:13 +02:00
|
|
|
return vm_list
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def list_con_ports(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Returns all VM console TCP ports
|
|
|
|
|
|
|
|
:returns: list of port numbers
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
port_list = await self._hypervisor.send("vm list_con_ports")
|
2015-02-10 03:24:13 +02:00
|
|
|
return port_list
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_debug_level(self, level):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the debug level for this router (default is 0).
|
|
|
|
|
|
|
|
:param level: level number
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_debug_level "{self._name}" {level}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def image(self):
|
|
|
|
"""
|
|
|
|
Returns this IOS image for this router.
|
|
|
|
|
|
|
|
:returns: path to IOS image file
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._image
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_image(self, image):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the IOS image for this router.
|
|
|
|
There is no default.
|
|
|
|
|
|
|
|
:param image: path to IOS image file
|
|
|
|
"""
|
|
|
|
|
2018-11-19 10:53:43 +02:00
|
|
|
image = self.manager.get_abs_image_path(image, self.project.path)
|
2015-02-26 02:19:37 +02:00
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_ios "{self._name}" "{image}"')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: has a new IOS image set: "{image}"'.format(
|
|
|
|
name=self._name, id=self._id, image=image
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
self._image = image
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ram(self):
|
|
|
|
"""
|
|
|
|
Returns the amount of RAM allocated to this router.
|
|
|
|
|
|
|
|
:returns: amount of RAM in Mbytes (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ram
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_ram(self, ram):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets amount of RAM allocated to this router
|
|
|
|
|
|
|
|
:param ram: amount of RAM in Mbytes (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._ram == ram:
|
|
|
|
return
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_ram "{self._name}" {ram}')
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: RAM updated from {old_ram}MB to {new_ram}MB'.format(
|
|
|
|
name=self._name, id=self._id, old_ram=self._ram, new_ram=ram
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._ram = ram
|
|
|
|
|
|
|
|
@property
|
|
|
|
def nvram(self):
|
|
|
|
"""
|
|
|
|
Returns the mount of NVRAM allocated to this router.
|
|
|
|
|
|
|
|
:returns: amount of NVRAM in Kbytes (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._nvram
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_nvram(self, nvram):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets amount of NVRAM allocated to this router
|
|
|
|
|
|
|
|
:param nvram: amount of NVRAM in Kbytes (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._nvram == nvram:
|
|
|
|
return
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_nvram "{self._name}" {nvram}')
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: NVRAM updated from {old_nvram}KB to {new_nvram}KB'.format(
|
|
|
|
name=self._name, id=self._id, old_nvram=self._nvram, new_nvram=nvram
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._nvram = nvram
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mmap(self):
|
|
|
|
"""
|
|
|
|
Returns True if a mapped file is used to simulate this router memory.
|
|
|
|
|
|
|
|
:returns: boolean either mmap is activated or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._mmap
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_mmap(self, mmap):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Enable/Disable use of a mapped file to simulate router memory.
|
|
|
|
By default, a mapped file is used. This is a bit slower, but requires less memory.
|
|
|
|
|
|
|
|
:param mmap: activate/deactivate mmap (boolean)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if mmap:
|
|
|
|
flag = 1
|
|
|
|
else:
|
|
|
|
flag = 0
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_ram_mmap "{self._name}" {flag}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if mmap:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: mmap enabled')
|
2015-02-10 03:24:13 +02:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: mmap disabled')
|
2015-02-10 03:24:13 +02:00
|
|
|
self._mmap = mmap
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sparsemem(self):
|
|
|
|
"""
|
|
|
|
Returns True if sparse memory is used on this router.
|
|
|
|
|
|
|
|
:returns: boolean either mmap is activated or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._sparsemem
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_sparsemem(self, sparsemem):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Enable/disable use of sparse memory
|
|
|
|
|
|
|
|
:param sparsemem: activate/deactivate sparsemem (boolean)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sparsemem:
|
|
|
|
flag = 1
|
|
|
|
else:
|
|
|
|
flag = 0
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_sparse_mem "{self._name}" {flag}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if sparsemem:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: sparse memory enabled')
|
2015-02-10 03:24:13 +02:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: sparse memory disabled')
|
2015-02-10 03:24:13 +02:00
|
|
|
self._sparsemem = sparsemem
|
|
|
|
|
|
|
|
@property
|
|
|
|
def clock_divisor(self):
|
|
|
|
"""
|
|
|
|
Returns the clock divisor value for this router.
|
|
|
|
|
|
|
|
:returns: clock divisor value (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._clock_divisor
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_clock_divisor(self, clock_divisor):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the clock divisor value. The higher is the value, the faster is the clock in the
|
|
|
|
virtual machine. The default is 4, but it is often required to adjust it.
|
|
|
|
|
|
|
|
:param clock_divisor: clock divisor value (integer)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_clock_divisor "{self._name}" {clock_divisor}')
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: clock divisor updated from {old_clock} to {new_clock}'.format(
|
|
|
|
name=self._name, id=self._id, old_clock=self._clock_divisor, new_clock=clock_divisor
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._clock_divisor = clock_divisor
|
|
|
|
|
|
|
|
@property
|
|
|
|
def idlepc(self):
|
|
|
|
"""
|
|
|
|
Returns the idle Pointer Counter (PC).
|
|
|
|
|
|
|
|
:returns: idlepc value (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._idlepc
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_idlepc(self, idlepc):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the idle Pointer Counter (PC)
|
|
|
|
|
|
|
|
:param idlepc: idlepc value (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not idlepc:
|
|
|
|
idlepc = "0x0"
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if not is_running:
|
|
|
|
# router is not running
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_idle_pc "{self._name}" {idlepc}')
|
2015-02-10 03:24:13 +02:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_idle_pc_online "{self._name}" 0 {idlepc}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: idle-PC set to {idlepc}')
|
2015-02-10 03:24:13 +02:00
|
|
|
self._idlepc = idlepc
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_idle_pc_prop(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Gets the idle PC proposals.
|
|
|
|
Takes 1000 measurements and records up to 10 idle PC proposals.
|
|
|
|
There is a 10ms wait between each measurement.
|
|
|
|
|
|
|
|
:returns: list of idle PC proposal
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2016-05-19 17:21:35 +03:00
|
|
|
was_auto_started = False
|
2015-02-10 03:24:13 +02:00
|
|
|
if not is_running:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.start()
|
2016-05-19 17:21:35 +03:00
|
|
|
was_auto_started = True
|
2018-10-15 13:05:49 +03:00
|
|
|
await asyncio.sleep(20) # leave time to the router to boot
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}] has started calculating Idle-PC values')
|
2015-02-10 03:24:13 +02:00
|
|
|
begin = time.time()
|
2021-04-13 12:07:58 +03:00
|
|
|
idlepcs = await self._hypervisor.send(f'vm get_idle_pc_prop "{self._name}" 0')
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}] has finished calculating Idle-PC values after {time:.4f} seconds'.format(
|
|
|
|
name=self._name, id=self._id, time=time.time() - begin
|
|
|
|
)
|
|
|
|
)
|
2016-05-19 17:21:35 +03:00
|
|
|
if was_auto_started:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.stop()
|
2015-02-10 03:24:13 +02:00
|
|
|
return idlepcs
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def show_idle_pc_prop(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Dumps the idle PC proposals (previously generated).
|
|
|
|
|
|
|
|
:returns: list of idle PC proposal
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if not is_running:
|
|
|
|
# router is not running
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'Router "{self._name}" is not running')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
proposals = await self._hypervisor.send(f'vm show_idle_pc_prop "{self._name}" 0')
|
2015-02-10 03:24:13 +02:00
|
|
|
return proposals
|
|
|
|
|
|
|
|
@property
|
|
|
|
def idlemax(self):
|
|
|
|
"""
|
|
|
|
Returns CPU idle max value.
|
|
|
|
|
|
|
|
:returns: idle max (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._idlemax
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_idlemax(self, idlemax):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets CPU idle max value
|
|
|
|
|
|
|
|
:param idlemax: idle max value (integer)
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if is_running: # router is running
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_idle_max "{self._name}" 0 {idlemax}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: idlemax updated from {old_idlemax} to {new_idlemax}'.format(
|
|
|
|
name=self._name, id=self._id, old_idlemax=self._idlemax, new_idlemax=idlemax
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
self._idlemax = idlemax
|
|
|
|
|
|
|
|
@property
|
|
|
|
def idlesleep(self):
|
|
|
|
"""
|
|
|
|
Returns CPU idle sleep time value.
|
|
|
|
|
|
|
|
:returns: idle sleep (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._idlesleep
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_idlesleep(self, idlesleep):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets CPU idle sleep time value.
|
|
|
|
|
|
|
|
:param idlesleep: idle sleep value (integer)
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if is_running: # router is running
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm set_idle_sleep_time "{name}" 0 {idlesleep}'.format(name=self._name, idlesleep=idlesleep)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: idlesleep updated from {old_idlesleep} to {new_idlesleep}'.format(
|
|
|
|
name=self._name, id=self._id, old_idlesleep=self._idlesleep, new_idlesleep=idlesleep
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
self._idlesleep = idlesleep
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ghost_file(self):
|
|
|
|
"""
|
|
|
|
Returns ghost RAM file.
|
|
|
|
|
|
|
|
:returns: path to ghost file
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ghost_file
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_ghost_file(self, ghost_file):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets ghost RAM file
|
|
|
|
|
|
|
|
:ghost_file: path to ghost file
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm set_ghost_file "{name}" "{ghost_file}"'.format(name=self._name, ghost_file=ghost_file)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: ghost file set to "{ghost_file}"'.format(
|
|
|
|
name=self._name, id=self._id, ghost_file=ghost_file
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
self._ghost_file = ghost_file
|
|
|
|
|
|
|
|
def formatted_ghost_file(self):
|
|
|
|
"""
|
|
|
|
Returns a properly formatted ghost file name.
|
|
|
|
|
|
|
|
:returns: formatted ghost_file name (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
# replace specials characters in 'drive:\filename' in Linux and Dynamips in MS Windows or viceversa.
|
2021-04-13 12:07:58 +03:00
|
|
|
ghost_file = f"{os.path.basename(self._image)}-{self._ram}.ghost"
|
2021-04-13 12:16:50 +03:00
|
|
|
ghost_file = ghost_file.replace("\\", "-").replace("/", "-").replace(":", "-")
|
2015-02-10 03:24:13 +02:00
|
|
|
return ghost_file
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ghost_status(self):
|
|
|
|
"""Returns ghost RAM status
|
|
|
|
|
|
|
|
:returns: ghost status (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._ghost_status
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_ghost_status(self, ghost_status):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets ghost RAM status
|
|
|
|
|
|
|
|
:param ghost_status: state flag indicating status
|
|
|
|
0 => Do not use IOS ghosting
|
|
|
|
1 => This is a ghost instance
|
|
|
|
2 => Use an existing ghost instance
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm set_ghost_status "{name}" {ghost_status}'.format(name=self._name, ghost_status=ghost_status)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: ghost status set to {ghost_status}'.format(
|
|
|
|
name=self._name, id=self._id, ghost_status=ghost_status
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._ghost_status = ghost_status
|
|
|
|
|
|
|
|
@property
|
|
|
|
def exec_area(self):
|
|
|
|
"""
|
|
|
|
Returns the exec area value.
|
|
|
|
|
|
|
|
:returns: exec area value (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._exec_area
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_exec_area(self, exec_area):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the exec area value.
|
|
|
|
The exec area is a pool of host memory used to store pages
|
|
|
|
translated by the JIT (they contain the native code
|
|
|
|
corresponding to MIPS code pages).
|
|
|
|
|
|
|
|
:param exec_area: exec area value (integer)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm set_exec_area "{name}" {exec_area}'.format(name=self._name, exec_area=exec_area)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: exec area updated from {old_exec}MB to {new_exec}MB'.format(
|
|
|
|
name=self._name, id=self._id, old_exec=self._exec_area, new_exec=exec_area
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._exec_area = exec_area
|
|
|
|
|
|
|
|
@property
|
|
|
|
def disk0(self):
|
|
|
|
"""
|
|
|
|
Returns the size (MB) for PCMCIA disk0.
|
|
|
|
|
|
|
|
:returns: disk0 size (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._disk0
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_disk0(self, disk0):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the size (MB) for PCMCIA disk0.
|
|
|
|
|
|
|
|
:param disk0: disk0 size (integer)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_disk0 "{self._name}" {disk0}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: disk0 updated from {old_disk0}MB to {new_disk0}MB'.format(
|
|
|
|
name=self._name, id=self._id, old_disk0=self._disk0, new_disk0=disk0
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._disk0 = disk0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def disk1(self):
|
|
|
|
"""
|
|
|
|
Returns the size (MB) for PCMCIA disk1.
|
|
|
|
|
|
|
|
:returns: disk1 size (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._disk1
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_disk1(self, disk1):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the size (MB) for PCMCIA disk1.
|
|
|
|
|
|
|
|
:param disk1: disk1 size (integer)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_disk1 "{self._name}" {disk1}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: disk1 updated from {old_disk1}MB to {new_disk1}MB'.format(
|
|
|
|
name=self._name, id=self._id, old_disk1=self._disk1, new_disk1=disk1
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._disk1 = disk1
|
|
|
|
|
2015-06-05 23:54:22 +03:00
|
|
|
@property
|
|
|
|
def auto_delete_disks(self):
|
|
|
|
"""
|
|
|
|
Returns True if auto delete disks is enabled on this router.
|
|
|
|
|
|
|
|
:returns: boolean either auto delete disks is activated or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._auto_delete_disks
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_auto_delete_disks(self, auto_delete_disks):
|
2015-06-05 23:54:22 +03:00
|
|
|
"""
|
|
|
|
Enable/disable use of auto delete disks
|
|
|
|
|
|
|
|
:param auto_delete_disks: activate/deactivate auto delete disks (boolean)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if auto_delete_disks:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: auto delete disks enabled')
|
2015-06-05 23:54:22 +03:00
|
|
|
else:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: auto delete disks disabled')
|
2015-06-05 23:54:22 +03:00
|
|
|
self._auto_delete_disks = auto_delete_disks
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_console(self, console):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the TCP console port.
|
|
|
|
|
|
|
|
:param console: console port (integer)
|
|
|
|
"""
|
|
|
|
|
2016-02-29 11:38:30 +02:00
|
|
|
self.console = console
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_con_tcp_port "{self._name}" {self.console}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_console_type(self, console_type):
|
2018-03-24 14:11:21 +03:00
|
|
|
"""
|
|
|
|
Sets the console type.
|
|
|
|
|
|
|
|
:param console_type: console type
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.console_type != console_type:
|
2018-10-15 13:05:49 +03:00
|
|
|
status = await self.get_status()
|
2018-03-24 14:11:21 +03:00
|
|
|
if status == "running":
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'"{name}" must be stopped to change the console type to {console_type}'.format(
|
|
|
|
name=self._name, console_type=console_type
|
|
|
|
)
|
|
|
|
)
|
2018-03-24 14:11:21 +03:00
|
|
|
|
|
|
|
self.console_type = console_type
|
|
|
|
|
|
|
|
if self._console and console_type == "telnet":
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_con_tcp_port "{self._name}" {self._console}')
|
2018-03-24 14:11:21 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_aux(self, aux):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the TCP auxiliary port.
|
|
|
|
|
|
|
|
:param aux: console auxiliary port (integer)
|
|
|
|
"""
|
|
|
|
|
2016-02-29 11:38:30 +02:00
|
|
|
self.aux = aux
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm set_aux_tcp_port "{self._name}" {aux}')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_cpu_usage(self, cpu_id=0):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Shows cpu usage in seconds, "cpu_id" is ignored.
|
|
|
|
|
|
|
|
:returns: cpu usage in seconds
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
cpu_usage = await self._hypervisor.send(f'vm cpu_usage "{self._name}" {cpu_id}')
|
2015-02-10 03:24:13 +02:00
|
|
|
return int(cpu_usage[0])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mac_addr(self):
|
|
|
|
"""
|
|
|
|
Returns the MAC address.
|
|
|
|
|
|
|
|
:returns: the MAC address (hexadecimal format: hh:hh:hh:hh:hh:hh)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._mac_addr
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_mac_addr(self, mac_addr):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the MAC address.
|
|
|
|
|
|
|
|
:param mac_addr: a MAC address (hexadecimal format: hh:hh:hh:hh:hh:hh)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'{platform} set_mac_addr "{name}" {mac_addr}'.format(
|
|
|
|
platform=self._platform, name=self._name, mac_addr=mac_addr
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: MAC address updated from {old_mac} to {new_mac}'.format(
|
|
|
|
name=self._name, id=self._id, old_mac=self._mac_addr, new_mac=mac_addr
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._mac_addr = mac_addr
|
|
|
|
|
|
|
|
@property
|
|
|
|
def system_id(self):
|
|
|
|
"""
|
|
|
|
Returns the system ID.
|
|
|
|
|
|
|
|
:returns: the system ID (also called board processor ID)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._system_id
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_system_id(self, system_id):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Sets the system ID.
|
|
|
|
|
|
|
|
:param system_id: a system ID (also called board processor ID)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'{platform} set_system_id "{name}" {system_id}'.format(
|
|
|
|
platform=self._platform, name=self._name, system_id=system_id
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: system ID updated from {old_id} to {new_id}'.format(
|
|
|
|
name=self._name, id=self._id, old_id=self._system_id, new_id=system_id
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
self._system_id = system_id
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_slot_bindings(self):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Returns slot bindings.
|
|
|
|
|
|
|
|
:returns: slot bindings (adapter names) list
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
slot_bindings = await self._hypervisor.send(f'vm slot_bindings "{self._name}"')
|
2015-02-10 03:24:13 +02:00
|
|
|
return slot_bindings
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_add_binding(self, slot_number, adapter):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Adds a slot binding (a module into a slot).
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
2015-02-10 03:24:13 +02:00
|
|
|
:param adapter: device to add in the corresponding slot
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
slot = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'Slot {slot_number} does not exist on router "{self._name}"')
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if slot is not None:
|
|
|
|
current_adapter = slot
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} is already occupied by adapter {adapter} on router "{name}"'.format(
|
|
|
|
name=self._name, slot_number=slot_number, adapter=current_adapter
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Only c7200, c3600 and c3745 (NM-4T only) support new adapter while running
|
2021-04-13 12:16:50 +03:00
|
|
|
if is_running and not (
|
|
|
|
(self._platform == "c7200" and not str(adapter).startswith("C7200"))
|
|
|
|
and not (self._platform == "c3600" and self.chassis == "3660")
|
|
|
|
and not (self._platform == "c3745" and adapter == "NM-4T")
|
|
|
|
):
|
|
|
|
raise DynamipsError(
|
|
|
|
'Adapter {adapter} cannot be added while router "{name}" is running'.format(
|
|
|
|
adapter=adapter, name=self._name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_add_binding "{name}" {slot_number} 0 {adapter}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, adapter=adapter
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: adapter {adapter} inserted into slot {slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, adapter=adapter, slot_number=slot_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
self._slots[slot_number] = adapter
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Generate an OIR event if the router is running
|
|
|
|
if is_running:
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_oir_start "{name}" {slot_number} 0'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: OIR start event sent to slot {slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, slot_number=slot_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_remove_binding(self, slot_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Removes a slot binding (a module from a slot).
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
if adapter is None:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'No adapter in slot {slot_number} on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Only c7200, c3600 and c3745 (NM-4T only) support to remove adapter while running
|
2021-04-13 12:16:50 +03:00
|
|
|
if is_running and not (
|
|
|
|
(self._platform == "c7200" and not str(adapter).startswith("C7200"))
|
|
|
|
and not (self._platform == "c3600" and self.chassis == "3660")
|
|
|
|
and not (self._platform == "c3745" and adapter == "NM-4T")
|
|
|
|
):
|
|
|
|
raise DynamipsError(
|
|
|
|
'Adapter {adapter} cannot be removed while router "{name}" is running'.format(
|
|
|
|
adapter=adapter, name=self._name
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Generate an OIR event if the router is running
|
|
|
|
if is_running:
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_oir_stop "{name}" {slot_number} 0'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: OIR stop event sent to slot {slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, slot_number=slot_number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_remove_binding "{name}" {slot_number} 0'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: adapter {adapter} removed from slot {slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, adapter=adapter, slot_number=slot_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
self._slots[slot_number] = None
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def install_wic(self, wic_slot_number, wic):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Installs a WIC adapter into this router.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param wic_slot_number: WIC slot number
|
2015-02-10 03:24:13 +02:00
|
|
|
:param wic: WIC to be installed
|
|
|
|
"""
|
|
|
|
|
|
|
|
# WICs are always installed on adapters in slot 0
|
2015-02-14 00:11:14 +02:00
|
|
|
slot_number = 0
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Do not check if slot has an adapter because adapters with WICs interfaces
|
|
|
|
# must be inserted by default in the router and cannot be removed.
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if wic_slot_number > len(adapter.wics) - 1:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"WIC slot {wic_slot_number} doesn't exist")
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if not adapter.wic_slot_available(wic_slot_number):
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"WIC slot {wic_slot_number} is already occupied by another WIC")
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2020-10-27 11:38:01 +02:00
|
|
|
if await self.is_running():
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'WIC "{wic}" cannot be added while router "{name}" is running'.format(wic=wic, name=self._name)
|
|
|
|
)
|
2020-10-27 11:38:01 +02:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
# Dynamips WICs slot IDs start on a multiple of 16
|
|
|
|
# WIC1 = 16, WIC2 = 32 and WIC3 = 48
|
2015-02-14 00:11:14 +02:00
|
|
|
internal_wic_slot_number = 16 * (wic_slot_number + 1)
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_add_binding "{name}" {slot_number} {wic_slot_number} {wic}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, wic_slot_number=internal_wic_slot_number, wic=wic
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: {wic} inserted into WIC slot {wic_slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, wic=wic, wic_slot_number=wic_slot_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter.install_wic(wic_slot_number, wic)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def uninstall_wic(self, wic_slot_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Uninstalls a WIC adapter from this router.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param wic_slot_number: WIC slot number
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# WICs are always installed on adapters in slot 0
|
2015-02-14 00:11:14 +02:00
|
|
|
slot_number = 0
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
# Do not check if slot has an adapter because adapters with WICs interfaces
|
|
|
|
# must be inserted by default in the router and cannot be removed.
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if wic_slot_number > len(adapter.wics) - 1:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"WIC slot {wic_slot_number} doesn't exist")
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if adapter.wic_slot_available(wic_slot_number):
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"No WIC is installed in WIC slot {wic_slot_number}")
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2020-10-27 11:38:01 +02:00
|
|
|
if await self.is_running():
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'WIC cannot be removed from slot {wic_slot_number} while router "{name}" is running'.format(
|
|
|
|
wic_slot_number=wic_slot_number, name=self._name
|
|
|
|
)
|
|
|
|
)
|
2020-10-27 11:38:01 +02:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
# Dynamips WICs slot IDs start on a multiple of 16
|
|
|
|
# WIC1 = 16, WIC2 = 32 and WIC3 = 48
|
2015-02-14 00:11:14 +02:00
|
|
|
internal_wic_slot_number = 16 * (wic_slot_number + 1)
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_remove_binding "{name}" {slot_number} {wic_slot_number}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, wic_slot_number=internal_wic_slot_number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: {wic} removed from WIC slot {wic_slot_number}'.format(
|
|
|
|
name=self._name, id=self._id, wic=adapter.wics[wic_slot_number], wic_slot_number=wic_slot_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter.uninstall_wic(wic_slot_number)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def get_slot_nio_bindings(self, slot_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Returns slot NIO bindings.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
:returns: list of NIO bindings
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
nio_bindings = await self._hypervisor.send(
|
|
|
|
'vm slot_nio_bindings "{name}" {slot_number}'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
return nio_bindings
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_add_nio_binding(self, slot_number, port_number, nio):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Adds a slot NIO binding.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
:param nio: NIO instance to add to the slot/port
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-03-27 18:20:31 +03:00
|
|
|
|
|
|
|
if adapter is None:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Adapter is missing in slot {slot_number}")
|
2015-03-27 18:20:31 +03:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if not adapter.port_exists(port_number):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} does not exist on adapter {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
|
2015-07-21 04:22:20 +03:00
|
|
|
try:
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_add_nio_binding "{name}" {slot_number} {port_number} {nio}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number, nio=nio
|
|
|
|
)
|
|
|
|
)
|
2015-07-21 04:22:20 +03:00
|
|
|
except DynamipsError:
|
|
|
|
# in case of error try to remove and add the nio binding
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_remove_nio_binding "{name}" {slot_number} {port_number}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_add_nio_binding "{name}" {slot_number} {port_number} {nio}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number, nio=nio
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: NIO {nio_name} bound to port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, nio_name=nio.name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.slot_enable_nio(slot_number, port_number)
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter.add_nio(port_number, nio)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_update_nio_binding(self, slot_number, port_number, nio):
|
2017-07-12 12:33:32 +03:00
|
|
|
"""
|
|
|
|
Update a slot NIO binding.
|
|
|
|
|
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
|
|
|
:param nio: NIO instance to add to the slot/port
|
|
|
|
"""
|
2018-10-27 10:47:17 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await nio.update()
|
2017-07-12 12:33:32 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_remove_nio_binding(self, slot_number, port_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Removes a slot NIO binding.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
:returns: removed NIO instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-03-27 18:20:31 +03:00
|
|
|
|
2015-03-29 03:09:53 +03:00
|
|
|
if adapter is None:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Adapter is missing in slot {slot_number}")
|
2015-03-27 18:20:31 +03:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
if not adapter.port_exists(port_number):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} does not exist on adapter {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
|
2019-04-01 15:47:31 +03:00
|
|
|
await self.stop_capture(slot_number, port_number)
|
2018-10-15 13:05:49 +03:00
|
|
|
await self.slot_disable_nio(slot_number, port_number)
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_remove_nio_binding "{name}" {slot_number} {port_number}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
|
|
|
|
nio = adapter.get_nio(port_number)
|
2015-03-11 18:53:09 +02:00
|
|
|
if nio is None:
|
|
|
|
return
|
2018-10-15 13:05:49 +03:00
|
|
|
await nio.close()
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter.remove_nio(port_number)
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: NIO {nio_name} removed from port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, nio_name=nio.name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
return nio
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_enable_nio(self, slot_number, port_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Enables a slot NIO binding.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if is_running: # running router
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_enable_nio "{name}" {slot_number} {port_number}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: NIO enabled on port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-13 15:43:28 +02:00
|
|
|
|
2018-10-27 10:47:17 +03:00
|
|
|
def get_nio(self, slot_number, port_number):
|
|
|
|
"""
|
|
|
|
Gets an slot NIO binding.
|
|
|
|
|
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
|
|
|
|
|
|
|
:returns: NIO instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
adapter = self._slots[slot_number]
|
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2018-10-27 10:47:17 +03:00
|
|
|
if not adapter.port_exists(port_number):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} does not exist on adapter {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2018-10-27 10:47:17 +03:00
|
|
|
|
|
|
|
nio = adapter.get_nio(port_number)
|
|
|
|
|
|
|
|
if not nio:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {slot_number}/{port_number} is not connected".format(
|
|
|
|
slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2018-10-27 10:47:17 +03:00
|
|
|
return nio
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def slot_disable_nio(self, slot_number, port_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Disables a slot NIO binding.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
is_running = await self.is_running()
|
2015-02-10 03:24:13 +02:00
|
|
|
if is_running: # running router
|
2021-04-13 12:16:50 +03:00
|
|
|
await self._hypervisor.send(
|
|
|
|
'vm slot_disable_nio "{name}" {slot_number} {port_number}'.format(
|
|
|
|
name=self._name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: NIO disabled on port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def start_capture(self, slot_number, port_number, output_file, data_link_type="DLT_EN10MB"):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Starts a packet capture.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
:param output_file: PCAP destination file for the capture
|
|
|
|
:param data_link_type: PCAP data link type (DLT_*), default is DLT_EN10MB
|
|
|
|
"""
|
|
|
|
|
2016-05-16 19:45:03 +03:00
|
|
|
try:
|
2021-04-13 12:16:50 +03:00
|
|
|
open(output_file, "w+").close()
|
2016-05-16 19:45:03 +03:00
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f'Can not write capture to "{output_file}": {str(e)}')
|
2016-05-16 19:45:03 +03:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
if not adapter.port_exists(port_number):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} does not exist on adapter {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
data_link_type = data_link_type.lower()
|
|
|
|
if data_link_type.startswith("dlt_"):
|
|
|
|
data_link_type = data_link_type[4:]
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
nio = adapter.get_nio(port_number)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-06-02 00:42:17 +03:00
|
|
|
if not nio:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {slot_number}/{port_number} is not connected".format(
|
|
|
|
slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-06-02 00:42:17 +03:00
|
|
|
|
2015-02-10 03:24:13 +02:00
|
|
|
if nio.input_filter[0] is not None and nio.output_filter[0] is not None:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} has already a filter applied on {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2019-04-01 16:58:18 +03:00
|
|
|
await nio.start_packet_capture(output_file, data_link_type)
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: starting packet capture on port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, nio_name=nio.name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-13 15:43:28 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def stop_capture(self, slot_number, port_number):
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
Stops a packet capture.
|
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
:param slot_number: slot number
|
|
|
|
:param port_number: port number
|
2015-02-10 03:24:13 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2015-02-14 00:11:14 +02:00
|
|
|
adapter = self._slots[slot_number]
|
2015-02-10 03:24:13 +02:00
|
|
|
except IndexError:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
'Slot {slot_number} does not exist on router "{name}"'.format(name=self._name, slot_number=slot_number)
|
|
|
|
)
|
2015-02-14 00:11:14 +02:00
|
|
|
if not adapter.port_exists(port_number):
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {port_number} does not exist on adapter {adapter}".format(
|
|
|
|
adapter=adapter, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2015-02-14 00:11:14 +02:00
|
|
|
nio = adapter.get_nio(port_number)
|
2015-06-02 00:42:17 +03:00
|
|
|
|
|
|
|
if not nio:
|
2021-04-13 12:16:50 +03:00
|
|
|
raise DynamipsError(
|
|
|
|
"Port {slot_number}/{port_number} is not connected".format(
|
|
|
|
slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-06-02 00:42:17 +03:00
|
|
|
|
2019-04-01 15:47:31 +03:00
|
|
|
if not nio.capturing:
|
|
|
|
return
|
2019-04-01 16:58:18 +03:00
|
|
|
await nio.stop_packet_capture()
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
log.info(
|
|
|
|
'Router "{name}" [{id}]: stopping packet capture on port {slot_number}/{port_number}'.format(
|
|
|
|
name=self._name, id=self._id, nio_name=nio.name, slot_number=slot_number, port_number=port_number
|
|
|
|
)
|
|
|
|
)
|
2015-02-10 03:24:13 +02:00
|
|
|
|
|
|
|
def _create_slots(self, numslots):
|
|
|
|
"""
|
|
|
|
Creates the appropriate number of slots for this router.
|
|
|
|
|
|
|
|
:param numslots: number of slots to create
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._slots = numslots * [None]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def slots(self):
|
|
|
|
"""
|
|
|
|
Returns the slots for this router.
|
|
|
|
|
|
|
|
:return: slot list
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._slots
|
|
|
|
|
2017-07-20 18:29:42 +03:00
|
|
|
@property
|
|
|
|
def startup_config_path(self):
|
|
|
|
"""
|
|
|
|
:returns: Path of the startup config
|
|
|
|
"""
|
2021-04-13 12:07:58 +03:00
|
|
|
return os.path.join(self._working_directory, "configs", f"i{self._dynamips_id}_startup-config.cfg")
|
2017-07-20 18:29:42 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def private_config_path(self):
|
|
|
|
"""
|
|
|
|
:returns: Path of the private config
|
|
|
|
"""
|
2021-04-13 12:07:58 +03:00
|
|
|
return os.path.join(self._working_directory, "configs", f"i{self._dynamips_id}_private-config.cfg")
|
2017-07-20 18:29:42 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def set_name(self, new_name):
|
2015-02-17 01:53:50 +02:00
|
|
|
"""
|
|
|
|
Renames this router.
|
|
|
|
|
|
|
|
:param new_name: new name string
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm rename "{self._name}" "{new_name}"')
|
2019-07-10 16:40:11 +03:00
|
|
|
|
2017-02-02 20:13:47 +02:00
|
|
|
# change the hostname in the startup-config
|
2017-07-20 18:29:42 +03:00
|
|
|
if os.path.isfile(self.startup_config_path):
|
2017-02-02 20:13:47 +02:00
|
|
|
try:
|
2017-07-20 18:29:42 +03:00
|
|
|
with open(self.startup_config_path, "r+", encoding="utf-8", errors="replace") as f:
|
2017-02-02 20:13:47 +02:00
|
|
|
old_config = f.read()
|
2020-01-07 19:24:47 +02:00
|
|
|
new_config = re.sub(r"hostname .+$", "hostname " + new_name, old_config, flags=re.MULTILINE)
|
2017-02-02 20:13:47 +02:00
|
|
|
f.seek(0)
|
|
|
|
f.write(new_config)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Could not amend the configuration {self.startup_config_path}: {e}")
|
2015-02-17 01:53:50 +02:00
|
|
|
|
2017-02-02 20:13:47 +02:00
|
|
|
# change the hostname in the private-config
|
2017-07-20 18:29:42 +03:00
|
|
|
if os.path.isfile(self.private_config_path):
|
2017-02-02 20:13:47 +02:00
|
|
|
try:
|
2017-07-20 18:29:42 +03:00
|
|
|
with open(self.private_config_path, "r+", encoding="utf-8", errors="replace") as f:
|
2017-02-02 20:13:47 +02:00
|
|
|
old_config = f.read()
|
|
|
|
new_config = old_config.replace(self.name, new_name)
|
|
|
|
f.seek(0)
|
|
|
|
f.write(new_config)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Could not amend the configuration {self.private_config_path}: {e}")
|
2015-02-17 01:53:50 +02:00
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f'Router "{self._name}" [{self._id}]: renamed to "{new_name}"')
|
2015-02-17 01:53:50 +02:00
|
|
|
self._name = new_name
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def extract_config(self):
|
2015-02-17 01:53:50 +02:00
|
|
|
"""
|
|
|
|
Gets the contents of the config files
|
|
|
|
startup-config and private-config from NVRAM.
|
|
|
|
|
|
|
|
:returns: tuple (startup-config, private-config) base64 encoded
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2021-04-13 12:07:58 +03:00
|
|
|
reply = await self._hypervisor.send(f'vm extract_config "{self._name}"')
|
2015-02-19 02:48:02 +02:00
|
|
|
except DynamipsError:
|
2015-02-19 12:33:25 +02:00
|
|
|
# for some reason Dynamips gets frozen when it does not find the magic number in the NVRAM file.
|
2015-02-17 01:53:50 +02:00
|
|
|
return None, None
|
2021-04-13 12:16:50 +03:00
|
|
|
reply = reply[0].rsplit(" ", 2)[-2:]
|
2015-02-17 01:53:50 +02:00
|
|
|
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
|
|
|
|
return startup_config, private_config
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def save_configs(self):
|
2015-02-17 01:53:50 +02:00
|
|
|
"""
|
|
|
|
Saves the startup-config and private-config to files.
|
|
|
|
"""
|
|
|
|
|
2017-02-02 20:13:47 +02:00
|
|
|
try:
|
|
|
|
config_path = os.path.join(self._working_directory, "configs")
|
|
|
|
os.makedirs(config_path, exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Could could not create configuration directory {config_path}: {e}")
|
2016-06-20 12:41:39 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
startup_config_base64, private_config_base64 = await self.extract_config()
|
2017-02-02 20:13:47 +02:00
|
|
|
if startup_config_base64:
|
2017-07-20 18:29:42 +03:00
|
|
|
startup_config = self.startup_config_path
|
2016-06-20 12:41:39 +03:00
|
|
|
try:
|
2017-02-02 20:13:47 +02:00
|
|
|
config = base64.b64decode(startup_config_base64).decode("utf-8", errors="replace")
|
|
|
|
config = "!\n" + config.replace("\r", "")
|
|
|
|
config_path = os.path.join(self._working_directory, startup_config)
|
|
|
|
with open(config_path, "wb") as f:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f"saving startup-config to {startup_config}")
|
2017-02-02 20:13:47 +02:00
|
|
|
f.write(config.encode("utf-8"))
|
|
|
|
except (binascii.Error, OSError) as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Could not save the startup configuration {config_path}: {e}")
|
2017-02-02 20:13:47 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
if private_config_base64 and base64.b64decode(private_config_base64) != b"\nkerberos password \nend\n":
|
2017-07-20 18:29:42 +03:00
|
|
|
private_config = self.private_config_path
|
2017-02-02 20:13:47 +02:00
|
|
|
try:
|
|
|
|
config = base64.b64decode(private_config_base64).decode("utf-8", errors="replace")
|
|
|
|
config_path = os.path.join(self._working_directory, private_config)
|
|
|
|
with open(config_path, "wb") as f:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.info(f"saving private-config to {private_config}")
|
2017-02-02 20:13:47 +02:00
|
|
|
f.write(config.encode("utf-8"))
|
|
|
|
except (binascii.Error, OSError) as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
raise DynamipsError(f"Could not save the private configuration {config_path}: {e}")
|
2015-02-10 03:24:13 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def delete(self):
|
2015-02-16 07:13:24 +02:00
|
|
|
"""
|
2018-03-15 09:17:39 +02:00
|
|
|
Deletes this VM (including all its files).
|
2015-02-16 07:13:24 +02:00
|
|
|
"""
|
2018-03-15 09:17:39 +02:00
|
|
|
|
2017-01-06 16:16:19 +02:00
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_run_in_executor(shutil.rmtree, self._working_directory)
|
2017-01-06 16:16:19 +02:00
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Could not delete file {e}")
|
2015-02-16 07:13:24 +02:00
|
|
|
|
2015-10-07 12:34:27 +03:00
|
|
|
self.manager.release_dynamips_id(self._project.id, self._dynamips_id)
|
2015-09-22 15:39:21 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def clean_delete(self):
|
2015-02-16 07:13:24 +02:00
|
|
|
"""
|
|
|
|
Deletes this router & associated files (nvram, disks etc.)
|
|
|
|
"""
|
|
|
|
|
2021-04-13 12:07:58 +03:00
|
|
|
await self._hypervisor.send(f'vm clean_delete "{self._name}"')
|
2015-02-16 07:13:24 +02:00
|
|
|
self._hypervisor.devices.remove(self)
|
2017-01-06 16:16:19 +02:00
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await wait_run_in_executor(shutil.rmtree, self._working_directory)
|
2017-01-06 16:16:19 +02:00
|
|
|
except OSError as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Could not delete file {e}")
|
|
|
|
log.info(f'Router "{self._name}" [{self._id}] has been deleted (including associated files)')
|
2016-06-13 16:52:31 +03:00
|
|
|
|
|
|
|
def _memory_files(self):
|
2018-03-15 09:17:39 +02:00
|
|
|
|
2016-06-13 16:52:31 +03:00
|
|
|
return [
|
2021-04-13 12:07:58 +03:00
|
|
|
os.path.join(self._working_directory, f"{self.platform}_i{self.dynamips_id}_rom"),
|
2021-04-13 12:16:50 +03:00
|
|
|
os.path.join(self._working_directory, f"{self.platform}_i{self.dynamips_id}_nvram"),
|
2016-06-13 16:52:31 +03:00
|
|
|
]
|