2014-05-06 16:06:10 +00: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/>.
|
|
|
|
|
|
|
|
"""
|
2014-05-13 21:11:37 +00:00
|
|
|
VPCS server module.
|
2014-05-06 16:06:10 +00: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-13 21:00:35 +00:00
|
|
|
from .vpcs_device import VPCSDevice
|
|
|
|
from .vpcs_error import VPCSError
|
2014-05-06 16:06:10 +00: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-13 21:00:35 +00:00
|
|
|
class VPCS(IModule):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 21:11:37 +00:00
|
|
|
VPCS module.
|
2014-05-06 16:06:10 +00: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-13 22:09:47 +00:00
|
|
|
# get the VPCS location
|
2014-05-06 16:06:10 +00:00
|
|
|
config = Config.instance()
|
2014-05-13 16:17:28 +00:00
|
|
|
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):
|
|
|
|
vpcs_in_cwd = os.path.join(os.getcwd(), "vpcs")
|
|
|
|
if os.path.isfile(vpcs_in_cwd):
|
|
|
|
self._vpcs = vpcs_in_cwd
|
2014-05-06 16:06:10 +00:00
|
|
|
else:
|
2014-05-13 16:17:28 +00:00
|
|
|
# look for vpcs if none is defined or accessible
|
2014-05-06 16:06:10 +00:00
|
|
|
for path in os.environ["PATH"].split(":"):
|
|
|
|
try:
|
2014-05-12 14:16:37 +00:00
|
|
|
if "vpcs" in os.listdir(path) and os.access(os.path.join(path, "vpcs"), os.X_OK):
|
2014-05-13 16:17:28 +00:00
|
|
|
self._vpcs = os.path.join(path, "vpcs")
|
2014-05-06 16:06:10 +00:00
|
|
|
break
|
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
if not self._vpcs:
|
2014-05-13 22:09:47 +00:00
|
|
|
log.warning("VPCS binary couldn't be found!")
|
2014-05-13 16:17:28 +00:00
|
|
|
elif not os.access(self._vpcs, os.X_OK):
|
2014-05-13 22:09:47 +00:00
|
|
|
log.warning("VPCS is not executable")
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
# a new process start when calling IModule
|
|
|
|
IModule.__init__(self, name, *args, **kwargs)
|
2014-05-13 16:17:28 +00:00
|
|
|
self._vpcs_instances = {}
|
2014-05-06 16:06:10 +00:00
|
|
|
self._console_start_port_range = 4001
|
|
|
|
self._console_end_port_range = 4512
|
|
|
|
self._allocated_console_ports = []
|
|
|
|
self._current_console_port = self._console_start_port_range
|
|
|
|
self._udp_start_port_range = 30001
|
|
|
|
self._udp_end_port_range = 40001
|
|
|
|
self._current_udp_port = self._udp_start_port_range
|
|
|
|
self._host = kwargs["host"]
|
|
|
|
self._projects_dir = kwargs["projects_dir"]
|
|
|
|
self._tempdir = kwargs["temp_dir"]
|
|
|
|
self._working_dir = self._projects_dir
|
|
|
|
|
|
|
|
# check every 5 seconds
|
2014-05-13 16:17:28 +00:00
|
|
|
#self._vpcs_callback = self.add_periodic_callback(self._check_vpcs_is_alive, 5000)
|
|
|
|
#self._vpcs_callback.start()
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
def stop(self, signum=None):
|
|
|
|
"""
|
|
|
|
Properly stops the module.
|
|
|
|
|
|
|
|
:param signum: signal number (if called by the signal handler)
|
|
|
|
"""
|
|
|
|
|
2014-05-13 23:17:26 +00:00
|
|
|
# self._vpcs_callback.stop()
|
2014-05-13 22:09:47 +00:00
|
|
|
# delete all VPCS instances
|
2014-05-13 16:17:28 +00:00
|
|
|
for vpcs_id in self._vpcs_instances:
|
|
|
|
vpcs_instance = self._vpcs_instances[vpcs_id]
|
|
|
|
vpcs_instance.delete()
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
IModule.stop(self, signum) # this will stop the I/O loop
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
def _check_vpcs_is_alive(self):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Periodic callback to check if VPCS is alive
|
|
|
|
for each VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Sends a notification to the client if not.
|
|
|
|
"""
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
for vpcs_id in self._vpcs_instances:
|
|
|
|
vpcs_instance = self._vpcs_instances[vpcs_id]
|
|
|
|
if vpcs_instance.started and (not vpcs_instance.is_running() or not vpcs_instance.is_vpcs_running()):
|
2014-05-06 16:06:10 +00:00
|
|
|
notification = {"module": self.name,
|
2014-05-13 16:17:28 +00:00
|
|
|
"id": vpcs_id,
|
|
|
|
"name": vpcs_instance.name}
|
|
|
|
if not vpcs_instance.is_running():
|
|
|
|
stdout = vpcs_instance.read_vpcs_stdout()
|
2014-05-13 22:09:47 +00:00
|
|
|
notification["message"] = "VPCS has stopped running"
|
2014-05-06 16:06:10 +00:00
|
|
|
notification["details"] = stdout
|
2014-05-13 16:17:28 +00:00
|
|
|
self.send_notification("{}.vpcs_stopped".format(self.name), notification)
|
|
|
|
vpcs_instance.stop()
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
def get_vpcs_instance(self, vpcs_id):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Returns a VPCS device instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-13 22:09:47 +00:00
|
|
|
:param vpcs_id: VPCS device identifier
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-13 22:09:47 +00:00
|
|
|
:returns: VPCSDevice instance
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
if vpcs_id not in self._vpcs_instances:
|
2014-05-13 22:09:47 +00: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 16:06:10 +00:00
|
|
|
return None
|
2014-05-13 16:17:28 +00:00
|
|
|
return self._vpcs_instances[vpcs_id]
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.reset")
|
2014-05-06 16:06:10 +00:00
|
|
|
def reset(self, request):
|
|
|
|
"""
|
|
|
|
Resets the module.
|
|
|
|
|
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
2014-05-13 16:17:28 +00: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 16:06:10 +00:00
|
|
|
|
|
|
|
# resets the instance IDs
|
2014-05-13 21:00:35 +00:00
|
|
|
VPCSDevice.reset()
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
self._vpcs_instances.clear()
|
2014-05-06 16:06:10 +00:00
|
|
|
self._remote_server = False
|
|
|
|
self._current_console_port = self._console_start_port_range
|
|
|
|
self._current_udp_port = self._udp_start_port_range
|
|
|
|
|
2014-05-13 22:09:47 +00:00
|
|
|
log.info("VPCS module has been reset")
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.settings")
|
2014-05-06 16:06:10 +00:00
|
|
|
def settings(self, request):
|
|
|
|
"""
|
|
|
|
Set or update settings.
|
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- 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-13 16:17:28 +00:00
|
|
|
if "vpcs" in request and request["vpcs"]:
|
|
|
|
self._vpcs = request["vpcs"]
|
2014-05-13 22:09:47 +00:00
|
|
|
log.info("VPCS path set to {}".format(self._vpcs))
|
2014-05-06 16:06:10 +00: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:
|
|
|
|
new_working_dir = os.path.join(self._projects_dir, request["project_name"] + ".gns3")
|
|
|
|
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
|
2014-05-13 16:17:28 +00:00
|
|
|
for vpcs_id in self._vpcs_instances:
|
|
|
|
vpcs_instance = self._vpcs_instances[vpcs_id]
|
|
|
|
vpcs_instance.working_dir = self._working_dir
|
2014-05-06 16:06:10 +00: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))
|
|
|
|
|
|
|
|
def test_result(self, message, result="error"):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
return {"result": result, "message": message}
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.test_settings")
|
2014-05-06 16:06:10 +00:00
|
|
|
def test_settings(self, request):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = []
|
|
|
|
|
|
|
|
self.send_response(response)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.create")
|
2014-05-13 16:17:28 +00:00
|
|
|
def vpcs_create(self, request):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Creates a new VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- path (path to the VPCS executable)
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Optional request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- name (VPCS name)
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Response parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
|
|
|
- name (VPCS name)
|
2014-05-06 16:06:10 +00:00
|
|
|
- default settings
|
|
|
|
|
|
|
|
:param request: JSON request
|
|
|
|
"""
|
|
|
|
|
|
|
|
# validate the request
|
|
|
|
if not self.validate_request(request, VPCS_CREATE_SCHEMA):
|
|
|
|
return
|
|
|
|
|
|
|
|
name = None
|
|
|
|
if "name" in request:
|
|
|
|
name = request["name"]
|
2014-05-15 15:27:46 +00:00
|
|
|
base_script_file = None
|
|
|
|
if "base_script_file" in request:
|
|
|
|
base_script_file = request["base_script_file"]
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_path = request["path"]
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
os.makedirs(self._working_dir)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
except OSError as e:
|
2014-05-13 21:11:37 +00:00
|
|
|
raise VPCSError("Could not create working directory {}".format(e))
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-16 17:42:43 +00:00
|
|
|
# a new base-script-file has been pushed
|
|
|
|
if "base_script_file_base64" in request:
|
|
|
|
config = base64.decodestring(request["base_script_file_base64"].encode("utf-8")).decode("utf-8")
|
|
|
|
config = "!\n" + config.replace("\r", "")
|
|
|
|
#config = config.replace('%h', vpcs_instance.name)
|
|
|
|
config_path = os.path.join(self._working_dir, "base-script-file")
|
|
|
|
try:
|
|
|
|
with open(config_path, "w") as f:
|
|
|
|
log.info("saving base-script-file to {}".format(config_path))
|
|
|
|
f.write(config)
|
|
|
|
except OSError as e:
|
|
|
|
raise VPCSError("Could not save the configuration {}: {}".format(config_path, e))
|
|
|
|
# update the request with the new local base-script-file path
|
|
|
|
request["base_script_file"] = os.path.basename(config_path)
|
|
|
|
|
|
|
|
vpcs_instance = VPCSDevice(vpcs_path, config_path, self._working_dir, host=self._host, name=name)
|
|
|
|
|
2014-05-06 16:06:10 +00:00
|
|
|
# find a console port
|
|
|
|
if self._current_console_port > self._console_end_port_range:
|
|
|
|
self._current_console_port = self._console_start_port_range
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.console = find_unused_port(self._current_console_port, self._console_end_port_range, self._host)
|
2014-05-06 16:06:10 +00:00
|
|
|
except Exception as e:
|
2014-05-13 21:11:37 +00:00
|
|
|
raise VPCSError(e)
|
2014-05-06 16:06:10 +00:00
|
|
|
self._current_console_port += 1
|
2014-05-13 21:00:35 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
response = {"name": vpcs_instance.name,
|
|
|
|
"id": vpcs_instance.id}
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
defaults = vpcs_instance.defaults()
|
2014-05-06 16:06:10 +00:00
|
|
|
response.update(defaults)
|
2014-05-13 16:17:28 +00:00
|
|
|
self._vpcs_instances[vpcs_instance.id] = vpcs_instance
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_response(response)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.delete")
|
2014-05-13 16:17:28 +00:00
|
|
|
def vpcs_delete(self, request):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Deletes a VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.delete()
|
|
|
|
del self._vpcs_instances[request["id"]]
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.send_response(True)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.update")
|
2014-05-13 16:17:28 +00:00
|
|
|
def vpcs_update(self, request):
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Updates a VPCS instance
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Optional request parameters:
|
|
|
|
- any setting to update
|
2014-05-16 17:42:43 +00:00
|
|
|
- base_script_file_base64 (script-file base64 encoded)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
response = {}
|
2014-05-16 17:42:43 +00:00
|
|
|
try:
|
|
|
|
# a new base-script-file has been pushed
|
|
|
|
if "base_script_file_base64" in request:
|
|
|
|
config = base64.decodestring(request["base_script_file_base64"].encode("utf-8")).decode("utf-8")
|
|
|
|
config = "!\n" + config.replace("\r", "")
|
|
|
|
config = config.replace('%h', vpcs_instance.name)
|
|
|
|
config_path = os.path.join(vpcs_instance.working_dir, "base-script-file")
|
|
|
|
try:
|
|
|
|
with open(config_path, "w") as f:
|
|
|
|
log.info("saving base-script-file to {}".format(config_path))
|
|
|
|
f.write(config)
|
|
|
|
except OSError as e:
|
|
|
|
raise VPCSError("Could not save the configuration {}: {}".format(config_path, e))
|
|
|
|
# update the request with the new local base-script-file path
|
|
|
|
request["base_script_file"] = os.path.basename(config_path)
|
|
|
|
|
|
|
|
except VPCSError as e:
|
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
2014-05-13 22:09:47 +00:00
|
|
|
# update the VPCS settings
|
2014-05-06 16:06:10 +00:00
|
|
|
for name, value in request.items():
|
2014-05-13 16:17:28 +00:00
|
|
|
if hasattr(vpcs_instance, name) and getattr(vpcs_instance, name) != value:
|
2014-05-06 16:06:10 +00:00
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
setattr(vpcs_instance, name, value)
|
2014-05-06 16:06:10 +00:00
|
|
|
response[name] = value
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.send_response(response)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.start")
|
2014-05-06 16:06:10 +00:00
|
|
|
def vm_start(self, request):
|
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Starts a VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-05-13 22:09:47 +00:00
|
|
|
log.debug("starting VPCS with command: {}".format(vpcs_instance.command()))
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.vpcs = self._vpcs
|
|
|
|
vpcs_instance.start()
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
self.send_response(True)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.stop")
|
2014-05-06 16:06:10 +00:00
|
|
|
def vm_stop(self, request):
|
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Stops a VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.stop()
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
self.send_response(True)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.reload")
|
2014-05-06 16:06:10 +00:00
|
|
|
def vm_reload(self, request):
|
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Reloads a VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS identifier)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
if vpcs_instance.is_running():
|
|
|
|
vpcs_instance.stop()
|
|
|
|
vpcs_instance.start()
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
self.send_response(True)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.allocate_udp_port")
|
2014-05-06 16:06:10 +00:00
|
|
|
def allocate_udp_port(self, request):
|
|
|
|
"""
|
|
|
|
Allocates a UDP port in order to create an UDP NIO.
|
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS identifier)
|
2014-05-06 16:06:10 +00: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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
# find a UDP port
|
|
|
|
if self._current_udp_port >= self._udp_end_port_range:
|
|
|
|
self._current_udp_port = self._udp_start_port_range
|
|
|
|
try:
|
|
|
|
port = find_unused_port(self._current_udp_port, self._udp_end_port_range, host=self._host, socket_type="UDP")
|
|
|
|
except Exception as e:
|
2014-05-13 21:11:37 +00:00
|
|
|
raise VPCSError(e)
|
2014-05-06 16:06:10 +00:00
|
|
|
self._current_udp_port += 1
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
log.info("{} [id={}] has allocated UDP port {} with host {}".format(vpcs_instance.name,
|
|
|
|
vpcs_instance.id,
|
2014-05-06 16:06:10 +00:00
|
|
|
port,
|
|
|
|
self._host))
|
|
|
|
response = {"lport": port}
|
|
|
|
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
response["port_id"] = request["port_id"]
|
|
|
|
self.send_response(response)
|
|
|
|
|
|
|
|
def _check_for_privileged_access(self, device):
|
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Check if VPCS can access Ethernet and TAP devices.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
:param device: device name
|
|
|
|
"""
|
|
|
|
|
2014-05-13 16:17:28 +00:00
|
|
|
# we are root, so vpcs should have privileged access too
|
2014-05-06 16:06:10 +00:00
|
|
|
if os.geteuid() == 0:
|
|
|
|
return
|
|
|
|
|
2014-05-13 22:09:47 +00:00
|
|
|
# test if VPCS has the CAP_NET_RAW capability
|
2014-05-13 16:17:28 +00:00
|
|
|
if "security.capability" in os.listxattr(self._vpcs):
|
2014-05-06 16:06:10 +00:00
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
caps = os.getxattr(self._vpcs, "security.capability")
|
2014-05-06 16:06:10 +00:00
|
|
|
# test the 2nd byte and check if the 13th bit (CAP_NET_RAW) is set
|
|
|
|
if struct.unpack("<IIIII", caps)[1] & 1 << 13:
|
|
|
|
return
|
|
|
|
except Exception as e:
|
2014-05-13 16:17:28 +00:00
|
|
|
log.error("could not determine if CAP_NET_RAW capability is set for {}: {}".format(self._vpcs, e))
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
2014-05-13 21:11:37 +00:00
|
|
|
raise VPCSError("{} has no privileged access to {}.".format(self._vpcs, device))
|
2014-05-06 16:06:10 +00:00
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.add_nio")
|
2014-05-06 16:06:10 +00:00
|
|
|
def add_nio(self, request):
|
|
|
|
"""
|
2014-05-13 22:09:47 +00:00
|
|
|
Adds an NIO (Network Input/Output) for a VPCS instance.
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00:00
|
|
|
- slot (slot number)
|
|
|
|
- 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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
slot = request["slot"]
|
|
|
|
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 00:07:16 +00: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 16:06:10 +00:00
|
|
|
nio = NIO_UDP(lport, rhost, rport)
|
|
|
|
elif request["nio"]["type"] == "nio_tap":
|
|
|
|
tap_device = request["nio"]["tap_device"]
|
|
|
|
self._check_for_privileged_access(tap_device)
|
|
|
|
nio = NIO_TAP(tap_device)
|
|
|
|
if not nio:
|
2014-05-13 21:11:37 +00:00
|
|
|
raise VPCSError("Requested NIO does not exist or is not supported: {}".format(request["nio"]["type"]))
|
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.slot_add_nio_binding(slot, port, nio)
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.send_response({"port_id": request["port_id"]})
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.delete_nio")
|
2014-05-06 16:06:10 +00:00
|
|
|
def delete_nio(self, request):
|
|
|
|
"""
|
|
|
|
Deletes an NIO (Network Input/Output).
|
|
|
|
|
|
|
|
Mandatory request parameters:
|
2014-05-13 22:09:47 +00:00
|
|
|
- id (VPCS instance identifier)
|
2014-05-06 16:06:10 +00:00
|
|
|
- slot (slot identifier)
|
|
|
|
- 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
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance = self.get_vpcs_instance(request["id"])
|
|
|
|
if not vpcs_instance:
|
2014-05-06 16:06:10 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
slot = request["slot"]
|
|
|
|
port = request["port"]
|
|
|
|
try:
|
2014-05-13 16:17:28 +00:00
|
|
|
vpcs_instance.slot_remove_nio_binding(slot, port)
|
2014-05-13 21:11:37 +00:00
|
|
|
except VPCSError as e:
|
2014-05-06 16:06:10 +00:00
|
|
|
self.send_custom_error(str(e))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.send_response(True)
|
|
|
|
|
2014-05-12 14:16:37 +00:00
|
|
|
@IModule.route("vpcs.echo")
|
2014-05-06 16:06:10 +00: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)
|