2015-01-14 01:26:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-01-23 01:04:24 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import stat
|
2015-01-14 01:26:32 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2015-01-23 01:04:24 +00:00
|
|
|
import socket
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
from uuid import UUID, uuid4
|
2015-01-21 02:02:22 +00:00
|
|
|
from ..config import Config
|
2015-01-20 11:46:15 +00:00
|
|
|
from .project_manager import ProjectManager
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
from .nios.nio_udp import NIOUDP
|
|
|
|
from .nios.nio_tap import NIOTAP
|
2015-01-23 01:04:24 +00:00
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
class BaseManager:
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
2015-01-14 17:52:02 +00:00
|
|
|
Base class for all Manager.
|
2015-01-14 01:26:32 +00:00
|
|
|
Responsible of management of a VM pool
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
self._vms = {}
|
2015-01-19 21:43:35 +00:00
|
|
|
self._port_manager = None
|
2015-01-21 02:02:22 +00:00
|
|
|
self._config = Config.instance()
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def instance(cls):
|
|
|
|
"""
|
2015-01-18 22:41:53 +00:00
|
|
|
Singleton to return only one instance of BaseManager.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-19 21:43:35 +00:00
|
|
|
:returns: instance of BaseManager
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-16 16:09:45 +00:00
|
|
|
if not hasattr(cls, "_instance") or cls._instance is None:
|
2015-01-14 01:26:32 +00:00
|
|
|
cls._instance = cls()
|
|
|
|
return cls._instance
|
|
|
|
|
2015-01-21 22:21:15 +00:00
|
|
|
@property
|
|
|
|
def module_name(self):
|
|
|
|
"""
|
|
|
|
Returns the module name.
|
|
|
|
|
|
|
|
:returns: module name
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
2015-01-19 10:22:24 +00:00
|
|
|
@property
|
|
|
|
def port_manager(self):
|
|
|
|
"""
|
2015-01-21 02:02:22 +00:00
|
|
|
Returns the port manager.
|
2015-01-19 10:22:24 +00:00
|
|
|
|
|
|
|
:returns: Port manager
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._port_manager
|
|
|
|
|
|
|
|
@port_manager.setter
|
|
|
|
def port_manager(self, new_port_manager):
|
|
|
|
|
2015-01-19 21:43:35 +00:00
|
|
|
self._port_manager = new_port_manager
|
2015-01-19 10:22:24 +00:00
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
"""
|
|
|
|
Returns the server config.
|
|
|
|
|
|
|
|
:returns: Config
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._config
|
|
|
|
|
2015-01-23 04:31:26 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-23 06:40:51 +00:00
|
|
|
def unload(self):
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
for vm_id in self._vms.keys():
|
2015-01-23 06:40:51 +00:00
|
|
|
try:
|
2015-02-04 20:48:29 +00:00
|
|
|
yield from self.close_vm(vm_id)
|
2015-01-23 06:40:51 +00:00
|
|
|
except Exception as e:
|
2015-02-04 20:48:29 +00:00
|
|
|
log.error("Could not delete VM {}: {}".format(vm_id, e), exc_info=1)
|
2015-01-23 15:44:00 +00:00
|
|
|
continue
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-23 06:40:51 +00:00
|
|
|
if hasattr(BaseManager, "_instance"):
|
|
|
|
BaseManager._instance = None
|
2015-01-23 20:01:23 +00:00
|
|
|
log.debug("Module {} unloaded".format(self.module_name))
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-02-05 00:13:35 +00:00
|
|
|
def get_vm(self, vm_id, project_id=None):
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
Returns a VM instance.
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
:param vm_id: VM identifier
|
2015-02-05 00:13:35 +00:00
|
|
|
:param project_id: Project identifier
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
2015-02-05 00:13:35 +00:00
|
|
|
if project_id:
|
|
|
|
# check the project_id exists
|
|
|
|
project = ProjectManager.instance().get_project(project_id)
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
try:
|
2015-02-04 20:48:29 +00:00
|
|
|
UUID(vm_id, version=4)
|
2015-01-20 01:30:57 +00:00
|
|
|
except ValueError:
|
2015-02-05 00:13:35 +00:00
|
|
|
raise aiohttp.web.HTTPBadRequest(text="VM ID {} is not a valid UUID".format(vm_id))
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
if vm_id not in self._vms:
|
2015-02-05 00:13:35 +00:00
|
|
|
raise aiohttp.web.HTTPNotFound(text="VM ID {} doesn't exist".format(vm_id))
|
|
|
|
|
|
|
|
vm = self._vms[vm_id]
|
|
|
|
|
|
|
|
if project_id:
|
|
|
|
if vm.project.id != project.id:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't belong to VM {}".format(project_id, vm.name))
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
return self._vms[vm_id]
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-02-04 20:48:29 +00:00
|
|
|
def create_vm(self, name, project_id, vm_id, *args, **kwargs):
|
2015-01-20 11:46:15 +00:00
|
|
|
"""
|
|
|
|
Create a new VM
|
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
:param name: VM name
|
2015-02-04 20:48:29 +00:00
|
|
|
:param project_id: Project identifier
|
|
|
|
:param vm_id: restore a VM identifier
|
2015-01-20 11:46:15 +00:00
|
|
|
"""
|
2015-01-20 22:28:40 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
project = ProjectManager.instance().get_project(project_id)
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-20 12:04:20 +00:00
|
|
|
# TODO: support for old projects VM with normal IDs.
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
if not vm_id:
|
|
|
|
vm_id = str(uuid4())
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
vm = self._VM_CLASS(name, vm_id, project, self, *args, **kwargs)
|
2015-01-22 00:41:35 +00:00
|
|
|
if asyncio.iscoroutinefunction(vm.create):
|
|
|
|
yield from vm.create()
|
|
|
|
else:
|
|
|
|
vm.create()
|
2015-02-04 20:48:29 +00:00
|
|
|
self._vms[vm.id] = vm
|
2015-02-03 01:56:13 +00:00
|
|
|
project.add_vm(vm)
|
2015-01-14 01:26:32 +00:00
|
|
|
return vm
|
2015-01-22 10:34:10 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-02-04 20:48:29 +00:00
|
|
|
def close_vm(self, vm_id):
|
2015-01-22 10:34:10 +00:00
|
|
|
"""
|
|
|
|
Delete a VM
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
:param vm_id: VM identifier
|
|
|
|
|
2015-01-23 10:28:58 +00:00
|
|
|
:returns: VM instance
|
2015-01-22 10:34:10 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
vm = self.get_vm(vm_id)
|
2015-01-23 02:06:17 +00:00
|
|
|
if asyncio.iscoroutinefunction(vm.close):
|
|
|
|
yield from vm.close()
|
2015-01-22 10:34:10 +00:00
|
|
|
else:
|
2015-01-23 02:06:17 +00:00
|
|
|
vm.close()
|
2015-01-23 10:28:58 +00:00
|
|
|
return vm
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-02-04 20:48:29 +00:00
|
|
|
def delete_vm(self, vm_id):
|
2015-01-23 10:28:58 +00:00
|
|
|
"""
|
|
|
|
Delete a VM. VM working directory will be destroy when
|
|
|
|
we receive a commit.
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
:param vm_id: VM identifier
|
2015-01-23 10:28:58 +00:00
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
vm = yield from self.close_vm(vm_id)
|
2015-01-23 10:28:58 +00:00
|
|
|
vm.project.mark_vm_for_destruction(vm)
|
2015-02-04 20:48:29 +00:00
|
|
|
del self._vms[vm.id]
|
2015-01-23 10:28:58 +00:00
|
|
|
return vm
|
2015-01-23 01:04:24 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _has_privileged_access(executable):
|
|
|
|
"""
|
|
|
|
Check if an executable can access Ethernet and TAP devices in
|
|
|
|
RAW mode.
|
|
|
|
|
|
|
|
:param executable: executable path
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# do not check anything on Windows
|
|
|
|
return True
|
|
|
|
|
|
|
|
if os.geteuid() == 0:
|
|
|
|
# we are root, so we should have privileged access.
|
|
|
|
return True
|
|
|
|
if os.stat(executable).st_mode & stat.S_ISUID or os.stat(executable).st_mode & stat.S_ISGID:
|
|
|
|
# the executable has set UID bit.
|
|
|
|
return True
|
|
|
|
|
|
|
|
# test if the executable has the CAP_NET_RAW capability (Linux only)
|
|
|
|
if sys.platform.startswith("linux") and "security.capability" in os.listxattr(executable):
|
|
|
|
try:
|
|
|
|
caps = os.getxattr(executable, "security.capability")
|
|
|
|
# test the 2nd byte and check if the 13th bit (CAP_NET_RAW) is set
|
|
|
|
if struct.unpack("<IIIII", caps)[1] & 1 << 13:
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
log.error("could not determine if CAP_NET_RAW capability is set for {}: {}".format(executable, e))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def create_nio(self, executable, nio_settings):
|
|
|
|
"""
|
|
|
|
Creates a new NIO.
|
|
|
|
|
|
|
|
:param nio_settings: information to create the NIO
|
|
|
|
|
|
|
|
:returns: a NIO object
|
|
|
|
"""
|
|
|
|
|
|
|
|
nio = None
|
|
|
|
if nio_settings["type"] == "nio_udp":
|
|
|
|
lport = nio_settings["lport"]
|
|
|
|
rhost = nio_settings["rhost"]
|
|
|
|
rport = nio_settings["rport"]
|
|
|
|
try:
|
|
|
|
# TODO: handle IPv6
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
|
|
|
sock.connect((rhost, rport))
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
|
2015-01-24 01:33:49 +00:00
|
|
|
nio = NIOUDP(lport, rhost, rport)
|
2015-01-23 01:04:24 +00:00
|
|
|
elif nio_settings["type"] == "nio_tap":
|
|
|
|
tap_device = nio_settings["tap_device"]
|
|
|
|
if not self._has_privileged_access(executable):
|
|
|
|
raise aiohttp.web.HTTPForbidden(text="{} has no privileged access to {}.".format(executable, tap_device))
|
2015-01-24 01:33:49 +00:00
|
|
|
nio = NIOTAP(tap_device)
|
2015-01-23 01:04:24 +00:00
|
|
|
assert nio is not None
|
|
|
|
return nio
|