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/>.
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
|
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-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-14 01:26:32 +00:00
|
|
|
@classmethod
|
2015-01-19 21:43:35 +00:00
|
|
|
@asyncio.coroutine # FIXME: why coroutine?
|
2015-01-14 01:26:32 +00:00
|
|
|
def destroy(cls):
|
2015-01-20 01:30:57 +00:00
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
cls._instance = None
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
def get_vm(self, uuid):
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
Returns a VM instance.
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
:param uuid: VM UUID
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
try:
|
|
|
|
UUID(uuid, version=4)
|
|
|
|
except ValueError:
|
|
|
|
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(uuid))
|
|
|
|
|
|
|
|
if uuid not in self._vms:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="UUID {} doesn't exist".format(uuid))
|
|
|
|
return self._vms[uuid]
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-01-21 02:02:22 +00:00
|
|
|
def create_vm(self, name, project_uuid, uuid, *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
|
|
|
|
:param project_uuid: UUID of Project
|
|
|
|
:param uuid: restore a VM UUID
|
2015-01-20 11:46:15 +00:00
|
|
|
"""
|
2015-01-20 22:28:40 +00:00
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
project = ProjectManager.instance().get_project(project_uuid)
|
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
|
|
|
|
|
|
|
if not uuid:
|
|
|
|
uuid = str(uuid4())
|
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
vm = self._VM_CLASS(name, uuid, 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-01-20 01:30:57 +00:00
|
|
|
self._vms[vm.uuid] = vm
|
2015-01-14 01:26:32 +00:00
|
|
|
return vm
|