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-15 23:50:36 +00:00
|
|
|
from .vm_error import VMError
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
class BaseManager:
|
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):
|
|
|
|
self._vms = {}
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
:returns: instance of Manager
|
|
|
|
"""
|
|
|
|
|
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-19 10:22:24 +00:00
|
|
|
@property
|
|
|
|
def port_manager(self):
|
|
|
|
"""
|
|
|
|
Returns the port_manager for this VMs
|
|
|
|
|
|
|
|
:returns: Port manager
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._port_manager
|
|
|
|
|
|
|
|
@port_manager.setter
|
|
|
|
def port_manager(self, new_port_manager):
|
|
|
|
self._port_manager = new_port_manager
|
|
|
|
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
@classmethod
|
|
|
|
@asyncio.coroutine
|
|
|
|
def destroy(cls):
|
|
|
|
cls._instance = None
|
|
|
|
|
2015-01-16 15:20:10 +00:00
|
|
|
def get_vm(self, vm_id):
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
Returns a VM instance.
|
|
|
|
|
|
|
|
:param vm_id: VM identifier
|
|
|
|
|
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
if vm_id not in self._vms:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="ID {} doesn't exist".format(vm_id))
|
|
|
|
return self._vms[vm_id]
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def create_vm(self, vmname, identifier=None):
|
|
|
|
if not identifier:
|
|
|
|
for i in range(1, 1024):
|
|
|
|
if i not in self._vms:
|
|
|
|
identifier = i
|
|
|
|
break
|
|
|
|
if identifier == 0:
|
2015-01-15 23:50:36 +00:00
|
|
|
raise VMError("Maximum number of VM instances reached")
|
2015-01-14 01:26:32 +00:00
|
|
|
else:
|
|
|
|
if identifier in self._vms:
|
2015-01-15 23:50:36 +00:00
|
|
|
raise VMError("VM identifier {} is already used by another VM instance".format(identifier))
|
2015-01-19 10:22:24 +00:00
|
|
|
vm = self._VM_CLASS(vmname, identifier, self)
|
2015-01-14 01:26:32 +00:00
|
|
|
yield from vm.wait_for_creation()
|
|
|
|
self._vms[vm.id] = vm
|
|
|
|
return vm
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def start_vm(self, vm_id):
|
2015-01-16 15:20:10 +00:00
|
|
|
vm = self.get_vm(vm_id)
|
2015-01-14 01:26:32 +00:00
|
|
|
yield from vm.start()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def stop_vm(self, vm_id):
|
2015-01-16 15:20:10 +00:00
|
|
|
vm = self.get_vm(vm_id)
|
2015-01-14 01:26:32 +00:00
|
|
|
yield from vm.stop()
|