Dirty stop start for VPCS

This commit is contained in:
Julien Duponchelle
2015-01-14 18:52:02 +01:00
parent 482fdf9031
commit 6c35cc304e
15 changed files with 811 additions and 20 deletions

View File

@ -17,18 +17,74 @@
import asyncio
from .vm_error import VMError
from .device_error import DeviceError
from .attic import find_unused_port
import logging
log = logging.getLogger(__name__)
class BaseVM:
_allocated_console_ports = []
def __init__(self, name, identifier):
self._loop = asyncio.get_event_loop()
self._allocate_console()
self._queue = asyncio.Queue()
self._name = name
self._id = identifier
self._created = asyncio.Future()
self._worker = asyncio.async(self._run())
log.info("{type} device {name} [id={id}] has been created".format(
type=self.__class__.__name__,
name=self._name,
id=self._id))
def _allocate_console(self):
if not self._console:
# allocate a console port
try:
self._console = find_unused_port(self._console_start_port_range,
self._console_end_port_range,
self._console_host,
ignore_ports=self._allocated_console_ports)
except Exception as e:
raise DeviceError(e)
if self._console in self._allocated_console_ports:
raise DeviceError("Console port {} is already used by another device".format(console))
self._allocated_console_ports.append(self._console)
@property
def console(self):
"""
Returns the TCP console port.
:returns: console port (integer)
"""
return self._console
@console.setter
def console(self, console):
"""
Sets the TCP console port.
:param console: console port (integer)
"""
if console in self._allocated_console_ports:
raise VPCSError("Console port {} is already used by another VPCS device".format(console))
self._allocated_console_ports.remove(self._console)
self._console = console
self._allocated_console_ports.append(self._console)
log.info("{type} {name} [id={id}]: console port set to {port}".format(
type=self.__class__.__name__,
name=self._name,
id=self._id,
port=console))
@property
def id(self):
"""
@ -65,7 +121,7 @@ class BaseVM:
try:
yield from self._create()
self._created.set_result(True)
except VMError as e:
except DeviceError as e:
self._created.set_exception(e)
return
@ -75,7 +131,7 @@ class BaseVM:
try:
yield from asyncio.wait_for(self._execute(subcommand, args), timeout=timeout)
except asyncio.TimeoutError:
raise VMError("{} has timed out after {} seconds!".format(subcommand, timeout))
raise DeviceError("{} has timed out after {} seconds!".format(subcommand, timeout))
future.set_result(True)
except Exception as e:
future.set_exception(e)
@ -83,6 +139,15 @@ class BaseVM:
def wait_for_creation(self):
return self._created
@asyncio.coroutine
def start():
"""
Starts the VM process.
"""
raise NotImplementedError
def put(self, *args):
"""
Add to the processing queue of the VM
@ -95,5 +160,5 @@ class BaseVM:
args.insert(0, future)
self._queue.put_nowait(args)
except asyncio.qeues.QueueFull:
raise VMError("Queue is full")
raise DeviceError("Queue is full")
return future