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-02-16 05:13:24 +00:00
|
|
|
|
import os
|
2015-01-14 17:52:02 +00:00
|
|
|
|
import logging
|
2015-02-16 05:13:24 +00:00
|
|
|
|
import aiohttp
|
|
|
|
|
import shutil
|
|
|
|
|
import asyncio
|
2015-03-17 15:31:45 +00:00
|
|
|
|
import tempfile
|
2015-10-12 21:57:37 +00:00
|
|
|
|
import psutil
|
|
|
|
|
import platform
|
2015-02-16 05:13:24 +00:00
|
|
|
|
|
2016-05-02 15:13:23 +00:00
|
|
|
|
from gns3server.utils import parse_version
|
2015-02-16 05:13:24 +00:00
|
|
|
|
from ..utils.asyncio import wait_run_in_executor
|
2015-09-13 20:52:25 +00:00
|
|
|
|
from ..ubridge.hypervisor import Hypervisor
|
2015-03-17 21:18:55 +00:00
|
|
|
|
from .vm_error import VMError
|
2015-02-16 05:13:24 +00:00
|
|
|
|
|
2015-09-13 20:52:25 +00:00
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-01-15 23:50:36 +00:00
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
|
class BaseVM:
|
2015-01-18 22:41:53 +00:00
|
|
|
|
|
2015-02-19 10:33:25 +00:00
|
|
|
|
"""
|
|
|
|
|
Base vm implementation.
|
|
|
|
|
|
|
|
|
|
:param name: name of this IOU vm
|
|
|
|
|
:param vm_id: IOU instance identifier
|
|
|
|
|
:param project: Project instance
|
|
|
|
|
:param manager: parent VM Manager
|
|
|
|
|
:param console: TCP console port
|
2016-02-29 09:38:30 +00:00
|
|
|
|
:param aux: TCP aux console port
|
|
|
|
|
:param allocate_aux: Boolean if true will allocate an aux console port
|
2015-02-19 10:33:25 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
def __init__(self, name, vm_id, project, manager, console=None, console_type="telnet", aux=None, allocate_aux=False):
|
2015-01-16 00:43:06 +00:00
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
|
self._name = name
|
2015-12-01 09:54:51 +00:00
|
|
|
|
self._usage = ""
|
2015-02-04 20:48:29 +00:00
|
|
|
|
self._id = vm_id
|
2015-01-20 11:46:15 +00:00
|
|
|
|
self._project = project
|
2015-01-19 10:22:24 +00:00
|
|
|
|
self._manager = manager
|
2015-02-19 10:33:25 +00:00
|
|
|
|
self._console = console
|
2016-02-29 09:38:30 +00:00
|
|
|
|
self._aux = aux
|
2015-07-03 22:06:25 +00:00
|
|
|
|
self._console_type = console_type
|
2015-03-17 15:31:45 +00:00
|
|
|
|
self._temporary_directory = None
|
2015-07-22 04:58:28 +00:00
|
|
|
|
self._hw_virtualization = False
|
2015-09-13 20:52:25 +00:00
|
|
|
|
self._ubridge_hypervisor = None
|
2016-02-29 09:38:30 +00:00
|
|
|
|
self._closed = False
|
2015-03-04 15:01:56 +00:00
|
|
|
|
self._vm_status = "stopped"
|
2016-02-02 17:25:17 +00:00
|
|
|
|
self._command_line = ""
|
2016-03-01 14:01:45 +00:00
|
|
|
|
self._allocate_aux = allocate_aux
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
|
|
if self._console is not None:
|
2015-12-07 11:26:46 +00:00
|
|
|
|
if console_type == "vnc":
|
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(self._console, self._project, port_range_start=5900, port_range_end=6000)
|
|
|
|
|
else:
|
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(self._console, self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
|
|
|
|
|
# We need to allocate aux before giving a random console port
|
|
|
|
|
if self._aux is not None:
|
|
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(self._aux, self._project)
|
|
|
|
|
|
|
|
|
|
if self._console is None:
|
2015-07-03 22:06:25 +00:00
|
|
|
|
if console_type == "vnc":
|
|
|
|
|
# VNC is a special case and the range must be 5900-6000
|
2015-12-07 11:26:46 +00:00
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project, port_range_start=5900, port_range_end=6000)
|
2015-07-03 22:06:25 +00:00
|
|
|
|
else:
|
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project)
|
2015-01-14 17:52:02 +00:00
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
if self._aux is None and allocate_aux:
|
|
|
|
|
self._aux = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
|
2015-04-08 17:17:34 +00:00
|
|
|
|
log.debug("{module}: {name} [{id}] initialized. Console port {console}".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id,
|
|
|
|
|
console=self._console))
|
2015-01-21 22:21:15 +00:00
|
|
|
|
|
2015-01-22 10:49:22 +00:00
|
|
|
|
def __del__(self):
|
2015-01-23 02:06:17 +00:00
|
|
|
|
|
2015-03-17 15:31:45 +00:00
|
|
|
|
if self._temporary_directory is not None:
|
|
|
|
|
if os.path.exists(self._temporary_directory):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
shutil.rmtree(self._temporary_directory, ignore_errors=True)
|
2015-01-19 10:22:24 +00:00
|
|
|
|
|
2015-03-04 15:01:56 +00:00
|
|
|
|
@property
|
|
|
|
|
def status(self):
|
|
|
|
|
"""Return current VM status"""
|
|
|
|
|
|
|
|
|
|
return self._vm_status
|
|
|
|
|
|
|
|
|
|
@status.setter
|
|
|
|
|
def status(self, status):
|
|
|
|
|
|
|
|
|
|
self._vm_status = status
|
|
|
|
|
self._project.emit("vm.{}".format(status), self)
|
|
|
|
|
|
2016-02-02 17:25:17 +00:00
|
|
|
|
@property
|
|
|
|
|
def command_line(self):
|
|
|
|
|
"""Return command used to start the VM"""
|
|
|
|
|
|
|
|
|
|
return self._command_line
|
|
|
|
|
|
|
|
|
|
@command_line.setter
|
|
|
|
|
def command_line(self, command_line):
|
|
|
|
|
|
|
|
|
|
self._command_line = command_line
|
|
|
|
|
|
2015-01-20 11:46:15 +00:00
|
|
|
|
@property
|
|
|
|
|
def project(self):
|
2015-01-21 02:02:22 +00:00
|
|
|
|
"""
|
|
|
|
|
Returns the VM current project.
|
|
|
|
|
|
|
|
|
|
:returns: Project instance.
|
|
|
|
|
"""
|
|
|
|
|
|
2015-01-20 11:46:15 +00:00
|
|
|
|
return self._project
|
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
|
@property
|
2015-01-20 01:30:57 +00:00
|
|
|
|
def name(self):
|
2015-01-14 01:26:32 +00:00
|
|
|
|
"""
|
2015-01-20 01:30:57 +00:00
|
|
|
|
Returns the name for this VM.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
|
:returns: name
|
2015-01-14 01:26:32 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
|
def name(self, new_name):
|
|
|
|
|
"""
|
|
|
|
|
Sets the name of this VM.
|
|
|
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
|
"""
|
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
|
log.info("{module}: {name} [{id}] renamed to {new_name}".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id,
|
|
|
|
|
new_name=new_name))
|
2015-01-20 01:30:57 +00:00
|
|
|
|
self._name = new_name
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-12-01 09:54:51 +00:00
|
|
|
|
@property
|
|
|
|
|
def usage(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the usage for this VM.
|
|
|
|
|
|
|
|
|
|
:returns: usage
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self._usage
|
|
|
|
|
|
|
|
|
|
@usage.setter
|
|
|
|
|
def usage(self, new_usage):
|
|
|
|
|
"""
|
|
|
|
|
Sets the usage of this VM.
|
|
|
|
|
|
|
|
|
|
:param new_usage: usage
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._usage = new_usage
|
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
|
@property
|
2015-02-04 20:48:29 +00:00
|
|
|
|
def id(self):
|
2015-01-14 01:26:32 +00:00
|
|
|
|
"""
|
2015-02-04 20:48:29 +00:00
|
|
|
|
Returns the ID for this VM.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
|
:returns: VM identifier (string)
|
2015-01-14 01:26:32 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
|
return self._id
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
|
@property
|
|
|
|
|
def manager(self):
|
2015-01-18 22:41:53 +00:00
|
|
|
|
"""
|
2015-01-20 01:30:57 +00:00
|
|
|
|
Returns the manager for this VM.
|
|
|
|
|
|
|
|
|
|
:returns: instance of manager
|
2015-01-18 22:41:53 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
|
return self._manager
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-01-20 13:31:47 +00:00
|
|
|
|
@property
|
|
|
|
|
def working_dir(self):
|
|
|
|
|
"""
|
|
|
|
|
Return VM working directory
|
|
|
|
|
"""
|
|
|
|
|
|
2015-01-23 10:28:58 +00:00
|
|
|
|
return self._project.vm_working_directory(self)
|
2015-01-20 13:31:47 +00:00
|
|
|
|
|
2015-03-17 15:31:45 +00:00
|
|
|
|
@property
|
|
|
|
|
def temporary_directory(self):
|
|
|
|
|
if self._temporary_directory is None:
|
2015-03-17 21:18:55 +00:00
|
|
|
|
try:
|
|
|
|
|
self._temporary_directory = tempfile.mkdtemp()
|
|
|
|
|
except OSError as e:
|
|
|
|
|
raise VMError("Can't create temporary directory: {}".format(e))
|
2015-03-17 15:31:45 +00:00
|
|
|
|
return self._temporary_directory
|
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
|
def create(self):
|
2015-01-18 22:41:53 +00:00
|
|
|
|
"""
|
2015-01-20 01:30:57 +00:00
|
|
|
|
Creates the VM.
|
2015-01-18 22:41:53 +00:00
|
|
|
|
"""
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
|
log.info("{module}: {name} [{id}] created".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id))
|
2015-01-14 01:26:32 +00:00
|
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def delete(self):
|
|
|
|
|
"""
|
|
|
|
|
Delete the VM (including all its files).
|
|
|
|
|
"""
|
|
|
|
|
|
2015-02-16 09:11:46 +00:00
|
|
|
|
directory = self.project.vm_working_directory(self)
|
2015-02-16 05:13:24 +00:00
|
|
|
|
if os.path.exists(directory):
|
|
|
|
|
try:
|
|
|
|
|
yield from wait_run_in_executor(shutil.rmtree, directory)
|
|
|
|
|
except OSError as e:
|
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not delete the VM working directory: {}".format(e))
|
|
|
|
|
|
2015-01-15 23:50:36 +00:00
|
|
|
|
def start(self):
|
2015-01-14 17:52:02 +00:00
|
|
|
|
"""
|
|
|
|
|
Starts the VM process.
|
|
|
|
|
"""
|
2015-01-18 22:41:53 +00:00
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2015-01-19 12:47:20 +00:00
|
|
|
|
def stop(self):
|
|
|
|
|
"""
|
|
|
|
|
Starts the VM process.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2015-01-19 12:47:20 +00:00
|
|
|
|
raise NotImplementedError
|
2015-01-22 10:49:22 +00:00
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
@asyncio.coroutine
|
2015-01-23 02:06:17 +00:00
|
|
|
|
def close(self):
|
2015-01-22 10:49:22 +00:00
|
|
|
|
"""
|
2015-01-23 02:06:17 +00:00
|
|
|
|
Close the VM process.
|
2015-01-22 10:49:22 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
if self._closed:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
log.info("{module}: '{name}' [{id}]: is closing".format(
|
|
|
|
|
module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id))
|
|
|
|
|
|
|
|
|
|
if self._console:
|
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
|
|
|
|
self._console = None
|
|
|
|
|
|
|
|
|
|
if self._aux:
|
|
|
|
|
self._manager.port_manager.release_tcp_port(self._aux, self._project)
|
|
|
|
|
self._aux = None
|
|
|
|
|
|
|
|
|
|
self._closed = True
|
|
|
|
|
return True
|
|
|
|
|
|
2016-03-01 14:01:45 +00:00
|
|
|
|
@property
|
|
|
|
|
def allocate_aux(self):
|
|
|
|
|
"""
|
|
|
|
|
:returns: Boolean allocate or not an aux console
|
|
|
|
|
"""
|
|
|
|
|
return self._allocate_aux
|
|
|
|
|
|
2016-03-01 14:33:07 +00:00
|
|
|
|
@allocate_aux.setter
|
|
|
|
|
def allocate_aux(self, allocate_aux):
|
|
|
|
|
"""
|
|
|
|
|
:returns: Boolean allocate or not an aux console
|
|
|
|
|
"""
|
|
|
|
|
self._allocate_aux = allocate_aux
|
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
@property
|
|
|
|
|
def aux(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the aux console port of this VM.
|
|
|
|
|
|
|
|
|
|
:returns: aux console port
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self._aux
|
|
|
|
|
|
|
|
|
|
@aux.setter
|
|
|
|
|
def aux(self, aux):
|
|
|
|
|
"""
|
|
|
|
|
Changes the aux port
|
|
|
|
|
|
|
|
|
|
:params aux: Console port (integer) or None to free the port
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if aux == self._aux:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if self._aux:
|
|
|
|
|
self._manager.port_manager.release_tcp_port(self._aux, self._project)
|
|
|
|
|
self._aux = None
|
|
|
|
|
if aux is not None:
|
|
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(aux, self._project)
|
|
|
|
|
log.info("{module}: '{name}' [{id}]: aux port set to {port}".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id,
|
|
|
|
|
port=aux))
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def console(self):
|
|
|
|
|
"""
|
2015-07-03 22:06:25 +00:00
|
|
|
|
Returns the console port of this VM.
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
|
|
:returns: console port
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self._console
|
|
|
|
|
|
|
|
|
|
@console.setter
|
|
|
|
|
def console(self, console):
|
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
|
Changes the console port
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
:params console: Console port (integer) or None to free the port
|
2015-02-19 10:33:25 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if console == self._console:
|
|
|
|
|
return
|
2015-10-16 16:15:27 +00:00
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
if self._console_type == "vnc" and console is not None and console < 5900:
|
2016-04-06 12:57:52 +00:00
|
|
|
|
raise VMError("VNC console require a port superior or equal to 5900 currently it's {}".format(console))
|
2015-10-16 16:15:27 +00:00
|
|
|
|
|
2015-02-19 10:33:25 +00:00
|
|
|
|
if self._console:
|
2015-03-21 23:19:12 +00:00
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
self._console = None
|
|
|
|
|
if console is not None:
|
2016-04-06 12:57:52 +00:00
|
|
|
|
if self.console_type == "vnc":
|
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(console, self._project, port_range_start=5900, port_range_end=6000)
|
|
|
|
|
else:
|
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(console, self._project)
|
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
|
log.info("{module}: '{name}' [{id}]: console port set to {port}".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id,
|
|
|
|
|
port=console))
|
2015-07-03 22:06:25 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def console_type(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the console type for this VM.
|
|
|
|
|
|
|
|
|
|
:returns: console type (string)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self._console_type
|
|
|
|
|
|
|
|
|
|
@console_type.setter
|
|
|
|
|
def console_type(self, console_type):
|
|
|
|
|
"""
|
|
|
|
|
Sets the console type for this VM.
|
|
|
|
|
|
|
|
|
|
:param console_type: console type (string)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
log.info('QEMU VM "{name}" [{id}] has set the console type to {console_type}'.format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
console_type=console_type))
|
|
|
|
|
|
|
|
|
|
if console_type != self._console_type:
|
|
|
|
|
# get a new port if the console type change
|
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
|
|
|
|
if console_type == "vnc":
|
|
|
|
|
# VNC is a special case and the range must be 5900-6000
|
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project, 5900, 6000)
|
|
|
|
|
else:
|
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
|
|
|
|
|
self._console_type = console_type
|
|
|
|
|
log.info("{module}: '{name}' [{id}]: console type set to {console_type}".format(module=self.manager.module_name,
|
|
|
|
|
name=self.name,
|
|
|
|
|
id=self.id,
|
|
|
|
|
console_type=console_type))
|
2015-07-22 04:58:28 +00:00
|
|
|
|
|
2015-09-13 20:52:25 +00:00
|
|
|
|
@property
|
|
|
|
|
def ubridge_path(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the uBridge executable path.
|
|
|
|
|
|
|
|
|
|
:returns: path to uBridge
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
path = self._manager.config.get_section_config("Server").get("ubridge_path", "ubridge")
|
|
|
|
|
if path == "ubridge":
|
|
|
|
|
path = shutil.which("ubridge")
|
2016-01-22 09:07:55 +00:00
|
|
|
|
|
2016-04-08 14:12:41 +00:00
|
|
|
|
if path is None or len(path) == 0:
|
2016-01-22 09:07:55 +00:00
|
|
|
|
raise VMError("uBridge is not installed")
|
2015-09-13 20:52:25 +00:00
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def _start_ubridge(self):
|
|
|
|
|
"""
|
|
|
|
|
Starts uBridge (handles connections to and from this VMware VM).
|
|
|
|
|
"""
|
|
|
|
|
|
2015-09-14 21:05:25 +00:00
|
|
|
|
if not self._manager.has_privileged_access(self.ubridge_path):
|
|
|
|
|
raise VMError("uBridge requires root access or capability to interact with network adapters")
|
|
|
|
|
|
2015-09-13 20:52:25 +00:00
|
|
|
|
server_config = self._manager.config.get_section_config("Server")
|
|
|
|
|
server_host = server_config.get("host")
|
|
|
|
|
self._ubridge_hypervisor = Hypervisor(self._project, self.ubridge_path, self.working_dir, server_host)
|
|
|
|
|
|
|
|
|
|
log.info("Starting new uBridge hypervisor {}:{}".format(self._ubridge_hypervisor.host, self._ubridge_hypervisor.port))
|
|
|
|
|
yield from self._ubridge_hypervisor.start()
|
|
|
|
|
log.info("Hypervisor {}:{} has successfully started".format(self._ubridge_hypervisor.host, self._ubridge_hypervisor.port))
|
|
|
|
|
yield from self._ubridge_hypervisor.connect()
|
|
|
|
|
|
2015-07-22 04:58:28 +00:00
|
|
|
|
@property
|
|
|
|
|
def hw_virtualization(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns either the VM is using hardware virtualization or not.
|
|
|
|
|
|
|
|
|
|
:return: boolean
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return self._hw_virtualization
|
2015-10-12 21:57:37 +00:00
|
|
|
|
|
|
|
|
|
def check_available_ram(self, requested_ram):
|
|
|
|
|
"""
|
|
|
|
|
Sends a warning notification if there is not enough RAM on the system to allocate requested RAM.
|
|
|
|
|
|
|
|
|
|
:param requested_ram: requested amount of RAM in MB
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
available_ram = int(psutil.virtual_memory().available / (1024 * 1024))
|
|
|
|
|
percentage_left = psutil.virtual_memory().percent
|
|
|
|
|
if requested_ram > available_ram:
|
|
|
|
|
message = '"{}" requires {}MB of RAM to run but there is only {}MB - {}% of RAM left on "{}"'.format(self.name,
|
|
|
|
|
requested_ram,
|
|
|
|
|
available_ram,
|
|
|
|
|
percentage_left,
|
|
|
|
|
platform.node())
|
|
|
|
|
self.project.emit("log.warning", {"message": message})
|