gns3-server/gns3server/modules/vpcs/__init__.py

621 lines
20 KiB
Python
Raw Normal View History

2014-05-06 19:06:10 +03: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/>.
"""
VPCS server module.
2014-05-06 19:06:10 +03:00
"""
import os
import sys
import base64
import tempfile
import struct
import socket
import shutil
from gns3server.modules import IModule
from gns3server.config import Config
import gns3server.jsonrpc as jsonrpc
2014-05-14 00:00:35 +03:00
from .vpcs_device import VPCSDevice
from .vpcs_error import VPCSError
2014-05-06 19:06:10 +03:00
from .nios.nio_udp import NIO_UDP
from .nios.nio_tap import NIO_TAP
from ..attic import find_unused_port
from .schemas import VPCS_CREATE_SCHEMA
from .schemas import VPCS_DELETE_SCHEMA
from .schemas import VPCS_UPDATE_SCHEMA
from .schemas import VPCS_START_SCHEMA
from .schemas import VPCS_STOP_SCHEMA
from .schemas import VPCS_RELOAD_SCHEMA
from .schemas import VPCS_ALLOCATE_UDP_PORT_SCHEMA
from .schemas import VPCS_ADD_NIO_SCHEMA
from .schemas import VPCS_DELETE_NIO_SCHEMA
import logging
log = logging.getLogger(__name__)
2014-05-14 00:00:35 +03:00
class VPCS(IModule):
2014-05-06 19:06:10 +03:00
"""
VPCS module.
2014-05-06 19:06:10 +03:00
:param name: module name
:param args: arguments for the module
:param kwargs: named arguments for the module
"""
def __init__(self, name, *args, **kwargs):
2014-05-14 01:09:47 +03:00
# get the VPCS location
2014-05-06 19:06:10 +03:00
config = Config.instance()
vpcs_config = config.get_section_config(name.upper())
self._vpcs = vpcs_config.get("vpcs")
if not self._vpcs or not os.path.isfile(self._vpcs):
paths = [os.getcwd()] + os.environ["PATH"].split(":")
# look for VPCS in the current working directory and $PATH
for path in paths:
try:
if "vpcs" in os.listdir(path) and os.access(os.path.join(path, "vpcs"), os.X_OK):
self._vpcs = os.path.join(path, "vpcs")
break
except OSError:
continue
2014-05-06 19:06:10 +03:00
if not self._vpcs:
2014-05-14 01:09:47 +03:00
log.warning("VPCS binary couldn't be found!")
elif not os.access(self._vpcs, os.X_OK):
2014-05-14 01:09:47 +03:00
log.warning("VPCS is not executable")
2014-05-06 19:06:10 +03:00
# a new process start when calling IModule
IModule.__init__(self, name, *args, **kwargs)
self._vpcs_instances = {}
2014-05-19 04:12:46 +03:00
self._console_start_port_range = 4512
self._console_end_port_range = 5000
self._allocated_udp_ports = []
self._udp_start_port_range = 40001
self._udp_end_port_range = 40512
2014-05-06 19:06:10 +03:00
self._host = kwargs["host"]
self._projects_dir = kwargs["projects_dir"]
self._tempdir = kwargs["temp_dir"]
self._working_dir = self._projects_dir
def stop(self, signum=None):
"""
Properly stops the module.
:param signum: signal number (if called by the signal handler)
"""
# self._vpcs_callback.stop()
2014-05-14 01:09:47 +03:00
# delete all VPCS instances
for vpcs_id in self._vpcs_instances:
vpcs_instance = self._vpcs_instances[vpcs_id]
vpcs_instance.delete()
2014-05-06 19:06:10 +03:00
IModule.stop(self, signum) # this will stop the I/O loop
def get_vpcs_instance(self, vpcs_id):
2014-05-06 19:06:10 +03:00
"""
2014-05-14 01:09:47 +03:00
Returns a VPCS device instance.
2014-05-06 19:06:10 +03:00
2014-05-14 01:09:47 +03:00
:param vpcs_id: VPCS device identifier
2014-05-06 19:06:10 +03:00
2014-05-14 01:09:47 +03:00
:returns: VPCSDevice instance
2014-05-06 19:06:10 +03:00
"""
if vpcs_id not in self._vpcs_instances:
2014-05-14 01:09:47 +03:00
log.debug("VPCS device ID {} doesn't exist".format(vpcs_id), exc_info=1)
self.send_custom_error("VPCS device ID {} doesn't exist".format(vpcs_id))
2014-05-06 19:06:10 +03:00
return None
return self._vpcs_instances[vpcs_id]
2014-05-06 19:06:10 +03:00
@IModule.route("vpcs.reset")
2014-05-06 19:06:10 +03:00
def reset(self, request):
"""
Resets the module.
:param request: JSON request
"""
# delete all vpcs instances
for vpcs_id in self._vpcs_instances:
vpcs_instance = self._vpcs_instances[vpcs_id]
vpcs_instance.delete()
2014-05-06 19:06:10 +03:00
# resets the instance IDs
2014-05-14 00:00:35 +03:00
VPCSDevice.reset()
2014-05-06 19:06:10 +03:00
self._vpcs_instances.clear()
2014-05-19 04:12:46 +03:00
self._allocated_udp_ports.clear()
2014-05-06 19:06:10 +03:00
2014-05-14 01:09:47 +03:00
log.info("VPCS module has been reset")
2014-05-06 19:06:10 +03:00
@IModule.route("vpcs.settings")
2014-05-06 19:06:10 +03:00
def settings(self, request):
"""
Set or update settings.
Optional request parameters:
2014-05-19 04:12:46 +03:00
- path (path to vpcs)
2014-05-06 19:06:10 +03:00
- working_dir (path to a working directory)
- project_name
- console_start_port_range
- console_end_port_range
- udp_start_port_range
- udp_end_port_range
:param request: JSON request
"""
if request == None:
self.send_param_error()
return
2014-05-19 04:12:46 +03:00
if "path" in request and request["path"]:
self._vpcs = request["path"]
2014-05-14 01:09:47 +03:00
log.info("VPCS path set to {}".format(self._vpcs))
2014-05-19 04:12:46 +03:00
for vpcs_id in self._vpcs_instances:
vpcs_instance = self._vpcs_instances[vpcs_id]
vpcs_instance.path = self._vpcs
2014-05-06 19:06:10 +03:00
if "working_dir" in request:
new_working_dir = request["working_dir"]
log.info("this server is local with working directory path to {}".format(new_working_dir))
else:
2014-05-19 04:12:46 +03:00
new_working_dir = os.path.join(self._projects_dir, request["project_name"])
2014-05-06 19:06:10 +03:00
log.info("this server is remote with working directory path to {}".format(new_working_dir))
if self._projects_dir != self._working_dir != new_working_dir:
if not os.path.isdir(new_working_dir):
try:
shutil.move(self._working_dir, new_working_dir)
except OSError as e:
log.error("could not move working directory from {} to {}: {}".format(self._working_dir,
new_working_dir,
e))
return
# update the working directory if it has changed
if self._working_dir != new_working_dir:
self._working_dir = new_working_dir
for vpcs_id in self._vpcs_instances:
vpcs_instance = self._vpcs_instances[vpcs_id]
vpcs_instance.working_dir = self._working_dir
2014-05-06 19:06:10 +03:00
if "console_start_port_range" in request and "console_end_port_range" in request:
self._console_start_port_range = request["console_start_port_range"]
self._console_end_port_range = request["console_end_port_range"]
if "udp_start_port_range" in request and "udp_end_port_range" in request:
self._udp_start_port_range = request["udp_start_port_range"]
self._udp_end_port_range = request["udp_end_port_range"]
log.debug("received request {}".format(request))
@IModule.route("vpcs.create")
def vpcs_create(self, request):
2014-05-06 19:06:10 +03:00
"""
2014-05-14 01:09:47 +03:00
Creates a new VPCS instance.
2014-05-06 19:06:10 +03:00
Optional request parameters:
2014-05-14 01:09:47 +03:00
- name (VPCS name)
- console (VPCS console port)
2014-05-06 19:06:10 +03:00
Response parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
- name (VPCS name)
2014-05-06 19:06:10 +03:00
- default settings
:param request: JSON request
"""
# validate the request
2014-05-19 04:12:46 +03:00
if request and not self.validate_request(request, VPCS_CREATE_SCHEMA):
2014-05-06 19:06:10 +03:00
return
name = console = None
if request:
name = request.get("name")
console = request.get("console")
2014-05-06 19:06:10 +03:00
try:
try:
os.makedirs(self._working_dir)
except FileExistsError:
pass
except OSError as e:
raise VPCSError("Could not create working directory {}".format(e))
2014-05-06 19:06:10 +03:00
if not self._vpcs:
raise VPCSError("No path to a VPCS executable has been set")
2014-05-19 04:12:46 +03:00
vpcs_instance = VPCSDevice(self._vpcs,
self._working_dir,
self._host,
name,
console,
2014-05-19 04:12:46 +03:00
self._console_start_port_range,
self._console_end_port_range)
2014-05-14 00:00:35 +03:00
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
response = {"name": vpcs_instance.name,
"id": vpcs_instance.id}
2014-05-06 19:06:10 +03:00
defaults = vpcs_instance.defaults()
2014-05-06 19:06:10 +03:00
response.update(defaults)
self._vpcs_instances[vpcs_instance.id] = vpcs_instance
2014-05-06 19:06:10 +03:00
self.send_response(response)
@IModule.route("vpcs.delete")
def vpcs_delete(self, request):
2014-05-06 19:06:10 +03:00
"""
2014-05-14 01:09:47 +03:00
Deletes a VPCS instance.
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
Response parameter:
- True on success
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_DELETE_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
try:
2014-05-19 21:05:30 +03:00
vpcs_instance.clean_delete()
del self._vpcs_instances[request["id"]]
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(True)
@IModule.route("vpcs.update")
def vpcs_update(self, request):
2014-05-06 19:06:10 +03:00
"""
2014-05-14 01:09:47 +03:00
Updates a VPCS instance
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
Optional request parameters:
- any setting to update
2014-05-19 04:12:46 +03:00
- script_file_base64 (base64 encoded)
2014-05-06 19:06:10 +03:00
Response parameters:
- updated settings
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_UPDATE_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
2014-05-19 04:12:46 +03:00
config_path = os.path.join(vpcs_instance.working_dir, "startup.vpc")
try:
2014-05-19 04:12:46 +03:00
if "script_file_base64" in request:
# a new startup-config has been pushed
config = base64.decodestring(request["script_file_base64"].encode("utf-8")).decode("utf-8")
config = config.replace("\r", "")
config = config.replace('%h', vpcs_instance.name)
try:
with open(config_path, "w") as f:
2014-05-19 04:12:46 +03:00
log.info("saving script file to {}".format(config_path))
f.write(config)
except OSError as e:
raise VPCSError("Could not save the configuration {}: {}".format(config_path, e))
2014-05-19 04:12:46 +03:00
# update the request with the new local startup-config path
request["script_file"] = os.path.basename(config_path)
elif "script_file" in request:
if os.path.isfile(request["script_file"]) and request["script_file"] != config_path:
# this is a local file set in the GUI
try:
with open(request["script_file"], "r") as f:
config = f.read()
with open(config_path, "w") as f:
config = config.replace("\r", "")
config = config.replace('%h', vpcs_instance.name)
f.write(config)
request["script_file"] = os.path.basename(config_path)
except OSError as e:
raise VPCSError("Could not save the configuration from {} to {}: {}".format(request["script_file"], config_path, e))
elif not os.path.isfile(config_path):
2014-05-19 22:14:57 +03:00
raise VPCSError("Startup-config {} could not be found on this server".format(request["script_file"]))
except VPCSError as e:
self.send_custom_error(str(e))
return
2014-05-19 04:12:46 +03:00
2014-05-14 01:09:47 +03:00
# update the VPCS settings
2014-05-19 04:12:46 +03:00
response = {}
2014-05-06 19:06:10 +03:00
for name, value in request.items():
if hasattr(vpcs_instance, name) and getattr(vpcs_instance, name) != value:
2014-05-06 19:06:10 +03:00
try:
setattr(vpcs_instance, name, value)
2014-05-06 19:06:10 +03:00
response[name] = value
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(response)
@IModule.route("vpcs.start")
2014-05-06 19:06:10 +03:00
def vm_start(self, request):
"""
2014-05-14 01:09:47 +03:00
Starts a VPCS instance.
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
Response parameters:
- True on success
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_START_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
try:
vpcs_instance.start()
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(True)
@IModule.route("vpcs.stop")
2014-05-06 19:06:10 +03:00
def vm_stop(self, request):
"""
2014-05-14 01:09:47 +03:00
Stops a VPCS instance.
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
Response parameters:
- True on success
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_STOP_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
try:
vpcs_instance.stop()
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(True)
@IModule.route("vpcs.reload")
2014-05-06 19:06:10 +03:00
def vm_reload(self, request):
"""
2014-05-14 01:09:47 +03:00
Reloads a VPCS instance.
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS identifier)
2014-05-06 19:06:10 +03:00
Response parameters:
- True on success
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_RELOAD_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
try:
if vpcs_instance.is_running():
vpcs_instance.stop()
vpcs_instance.start()
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(True)
@IModule.route("vpcs.allocate_udp_port")
2014-05-06 19:06:10 +03:00
def allocate_udp_port(self, request):
"""
Allocates a UDP port in order to create an UDP NIO.
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS identifier)
2014-05-06 19:06:10 +03:00
- port_id (unique port identifier)
Response parameters:
- port_id (unique port identifier)
- lport (allocated local port)
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_ALLOCATE_UDP_PORT_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
try:
2014-05-19 04:12:46 +03:00
port = find_unused_port(self._udp_start_port_range,
self._udp_end_port_range,
host=self._host,
socket_type="UDP",
ignore_ports=self._allocated_udp_ports)
except Exception as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
2014-05-19 04:12:46 +03:00
self._allocated_udp_ports.append(port)
log.info("{} [id={}] has allocated UDP port {} with host {}".format(vpcs_instance.name,
vpcs_instance.id,
port,
self._host))
response = {"lport": port}
2014-05-06 19:06:10 +03:00
response["port_id"] = request["port_id"]
self.send_response(response)
@IModule.route("vpcs.add_nio")
2014-05-06 19:06:10 +03:00
def add_nio(self, request):
"""
2014-05-14 01:09:47 +03:00
Adds an NIO (Network Input/Output) for a VPCS instance.
2014-05-06 19:06:10 +03:00
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
- port (port number)
- port_id (unique port identifier)
- nio (one of the following)
- type "nio_udp"
- lport (local port)
- rhost (remote host)
- rport (remote port)
- type "nio_tap"
- tap_device (TAP device name e.g. tap0)
Response parameters:
- port_id (unique port identifier)
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_ADD_NIO_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
port = request["port"]
try:
nio = None
if request["nio"]["type"] == "nio_udp":
lport = request["nio"]["lport"]
rhost = request["nio"]["rhost"]
rport = request["nio"]["rport"]
2014-05-18 03:07:16 +03:00
try:
#TODO: handle IPv6
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
sock.connect((rhost, rport))
except OSError as e:
raise VPCSError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
2014-05-06 19:06:10 +03:00
nio = NIO_UDP(lport, rhost, rport)
elif request["nio"]["type"] == "nio_tap":
tap_device = request["nio"]["tap_device"]
2014-05-19 04:12:46 +03:00
if not self.has_privileged_access(self._vpcs, tap_device):
raise VPCSError("{} has no privileged access to {}.".format(self._vpcs, tap_device))
2014-05-06 19:06:10 +03:00
nio = NIO_TAP(tap_device)
if not nio:
raise VPCSError("Requested NIO does not exist or is not supported: {}".format(request["nio"]["type"]))
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
try:
2014-05-19 04:12:46 +03:00
vpcs_instance.port_add_nio_binding(port, nio)
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response({"port_id": request["port_id"]})
@IModule.route("vpcs.delete_nio")
2014-05-06 19:06:10 +03:00
def delete_nio(self, request):
"""
Deletes an NIO (Network Input/Output).
Mandatory request parameters:
2014-05-14 01:09:47 +03:00
- id (VPCS instance identifier)
2014-05-06 19:06:10 +03:00
- port (port identifier)
Response parameters:
- True on success
:param request: JSON request
"""
# validate the request
if not self.validate_request(request, VPCS_DELETE_NIO_SCHEMA):
return
# get the instance
vpcs_instance = self.get_vpcs_instance(request["id"])
if not vpcs_instance:
2014-05-06 19:06:10 +03:00
return
port = request["port"]
try:
2014-05-19 04:12:46 +03:00
nio = vpcs_instance.port_remove_nio_binding(port)
if isinstance(nio, NIO_UDP) and nio.lport in self._allocated_udp_ports:
self._allocated_udp_ports.remove(nio.lport)
except VPCSError as e:
2014-05-06 19:06:10 +03:00
self.send_custom_error(str(e))
return
self.send_response(True)
@IModule.route("vpcs.echo")
2014-05-06 19:06:10 +03:00
def echo(self, request):
"""
Echo end point for testing purposes.
:param request: JSON request
"""
if request == None:
self.send_param_error()
else:
log.debug("received request {}".format(request))
self.send_response(request)