2015-01-14 03:26:32 +02: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 07:13:24 +02:00
|
|
|
import os
|
2015-01-14 19:52:02 +02:00
|
|
|
import logging
|
2015-02-16 07:13:24 +02:00
|
|
|
import aiohttp
|
|
|
|
import shutil
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from ..utils.asyncio import wait_run_in_executor
|
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-16 01:50:36 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
class BaseVM:
|
2015-01-19 00:41:53 +02:00
|
|
|
|
2015-02-19 12:33:25 +02: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
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, vm_id, project, manager, console=None):
|
2015-01-16 02:43:06 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
self._name = name
|
2015-02-04 22:48:29 +02:00
|
|
|
self._id = vm_id
|
2015-01-20 13:46:15 +02:00
|
|
|
self._project = project
|
2015-01-19 12:22:24 +02:00
|
|
|
self._manager = manager
|
2015-02-19 12:33:25 +02:00
|
|
|
self._console = console
|
|
|
|
|
|
|
|
if self._console is not None:
|
2015-02-24 02:42:55 +02:00
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(self._console)
|
2015-02-19 12:33:25 +02:00
|
|
|
else:
|
2015-02-24 02:42:55 +02:00
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port()
|
2015-01-14 19:52:02 +02:00
|
|
|
|
2015-02-19 20:43:45 +02: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-22 00:21:15 +02:00
|
|
|
|
2015-01-22 12:49:22 +02:00
|
|
|
def __del__(self):
|
2015-01-23 04:06:17 +02:00
|
|
|
|
|
|
|
self.close()
|
2015-01-19 12:22:24 +02:00
|
|
|
|
2015-01-20 13:46:15 +02:00
|
|
|
@property
|
|
|
|
def project(self):
|
2015-01-21 04:02:22 +02:00
|
|
|
"""
|
|
|
|
Returns the VM current project.
|
|
|
|
|
|
|
|
:returns: Project instance.
|
|
|
|
"""
|
|
|
|
|
2015-01-20 13:46:15 +02:00
|
|
|
return self._project
|
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
@property
|
2015-01-20 03:30:57 +02:00
|
|
|
def name(self):
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
2015-01-20 03:30:57 +02:00
|
|
|
Returns the name for this VM.
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
:returns: name
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, new_name):
|
|
|
|
"""
|
|
|
|
Sets the name of this VM.
|
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
"""
|
|
|
|
|
2015-02-04 22:48:29 +02: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 03:30:57 +02:00
|
|
|
self._name = new_name
|
2015-01-14 03:26:32 +02:00
|
|
|
|
|
|
|
@property
|
2015-02-04 22:48:29 +02:00
|
|
|
def id(self):
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
2015-02-04 22:48:29 +02:00
|
|
|
Returns the ID for this VM.
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
:returns: VM identifier (string)
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
return self._id
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
@property
|
|
|
|
def manager(self):
|
2015-01-19 00:41:53 +02:00
|
|
|
"""
|
2015-01-20 03:30:57 +02:00
|
|
|
Returns the manager for this VM.
|
|
|
|
|
|
|
|
:returns: instance of manager
|
2015-01-19 00:41:53 +02:00
|
|
|
"""
|
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
return self._manager
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-20 15:31:47 +02:00
|
|
|
@property
|
|
|
|
def working_dir(self):
|
|
|
|
"""
|
|
|
|
Return VM working directory
|
|
|
|
"""
|
|
|
|
|
2015-01-23 12:28:58 +02:00
|
|
|
return self._project.vm_working_directory(self)
|
2015-01-20 15:31:47 +02:00
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
def create(self):
|
2015-01-19 00:41:53 +02:00
|
|
|
"""
|
2015-01-20 03:30:57 +02:00
|
|
|
Creates the VM.
|
2015-01-19 00:41:53 +02:00
|
|
|
"""
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
log.info("{module}: {name} [{id}] created".format(module=self.manager.module_name,
|
|
|
|
name=self.name,
|
|
|
|
id=self.id))
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-16 07:13:24 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Delete the VM (including all its files).
|
|
|
|
"""
|
|
|
|
|
2015-02-16 11:11:46 +02:00
|
|
|
directory = self.project.vm_working_directory(self)
|
2015-02-16 07:13:24 +02: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-16 01:50:36 +02:00
|
|
|
def start(self):
|
2015-01-14 19:52:02 +02:00
|
|
|
"""
|
|
|
|
Starts the VM process.
|
|
|
|
"""
|
2015-01-19 00:41:53 +02:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-01-19 14:47:20 +02:00
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Starts the VM process.
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
|
2015-01-19 14:47:20 +02:00
|
|
|
raise NotImplementedError
|
2015-01-22 12:49:22 +02:00
|
|
|
|
2015-01-23 04:06:17 +02:00
|
|
|
def close(self):
|
2015-01-22 12:49:22 +02:00
|
|
|
"""
|
2015-01-23 04:06:17 +02:00
|
|
|
Close the VM process.
|
2015-01-22 12:49:22 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
raise NotImplementedError
|
2015-02-19 12:33:25 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def console(self):
|
|
|
|
"""
|
|
|
|
Returns the console port of this VPCS vm.
|
|
|
|
|
|
|
|
:returns: console port
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._console
|
|
|
|
|
|
|
|
@console.setter
|
|
|
|
def console(self, console):
|
|
|
|
"""
|
|
|
|
Change console port
|
|
|
|
|
|
|
|
:params console: Console port (integer)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if console == self._console:
|
|
|
|
return
|
|
|
|
if self._console:
|
2015-02-24 02:42:55 +02:00
|
|
|
self._manager.port_manager.release_tcp_port(self._console)
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(console)
|
2015-02-19 12:33:25 +02: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))
|