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-01-23 03:04:24 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import stat
|
2015-01-14 03:26:32 +02:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
2015-01-23 03:04:24 +02:00
|
|
|
import socket
|
2015-02-09 03:10:04 +02:00
|
|
|
import shutil
|
2015-01-23 03:04:24 +02:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
from uuid import UUID, uuid4
|
2015-06-07 22:51:33 +03:00
|
|
|
from gns3server.utils.interfaces import is_interface_up
|
2015-01-21 04:02:22 +02:00
|
|
|
from ..config import Config
|
2015-02-09 03:10:04 +02:00
|
|
|
from ..utils.asyncio import wait_run_in_executor
|
2015-01-20 13:46:15 +02:00
|
|
|
from .project_manager import ProjectManager
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-24 04:00:34 +02:00
|
|
|
from .nios.nio_udp import NIOUDP
|
|
|
|
from .nios.nio_tap import NIOTAP
|
2015-05-06 23:59:01 +03:00
|
|
|
from .nios.nio_nat import NIONAT
|
2015-02-24 04:00:34 +02:00
|
|
|
from .nios.nio_generic_ethernet import NIOGenericEthernet
|
2015-06-17 18:11:25 +03:00
|
|
|
from ..utils.images import md5sum, remove_checksum
|
2015-01-23 03:04:24 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-14 19:52:02 +02:00
|
|
|
class BaseManager:
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
2015-04-08 20:17:34 +03:00
|
|
|
Base class for all Manager classes.
|
|
|
|
Responsible of management of a VM pool of the same type.
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
|
2015-02-28 07:12:43 +02:00
|
|
|
_convert_lock = None
|
2015-02-26 03:55:35 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
def __init__(self):
|
2015-01-19 23:43:35 +02:00
|
|
|
|
2015-02-28 07:12:43 +02:00
|
|
|
BaseManager._convert_lock = asyncio.Lock()
|
2015-01-14 03:26:32 +02:00
|
|
|
self._vms = {}
|
2015-01-19 23:43:35 +02:00
|
|
|
self._port_manager = None
|
2015-01-21 04:02:22 +02:00
|
|
|
self._config = Config.instance()
|
2015-01-14 03:26:32 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def instance(cls):
|
|
|
|
"""
|
2015-01-19 00:41:53 +02:00
|
|
|
Singleton to return only one instance of BaseManager.
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-01-19 23:43:35 +02:00
|
|
|
:returns: instance of BaseManager
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
|
2015-01-16 18:09:45 +02:00
|
|
|
if not hasattr(cls, "_instance") or cls._instance is None:
|
2015-01-14 03:26:32 +02:00
|
|
|
cls._instance = cls()
|
|
|
|
return cls._instance
|
|
|
|
|
2015-01-22 00:21:15 +02:00
|
|
|
@property
|
|
|
|
def module_name(self):
|
|
|
|
"""
|
|
|
|
Returns the module name.
|
|
|
|
|
|
|
|
:returns: module name
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
2015-01-19 12:22:24 +02:00
|
|
|
@property
|
|
|
|
def port_manager(self):
|
|
|
|
"""
|
2015-01-21 04:02:22 +02:00
|
|
|
Returns the port manager.
|
2015-01-19 12:22:24 +02:00
|
|
|
|
|
|
|
:returns: Port manager
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._port_manager
|
|
|
|
|
|
|
|
@port_manager.setter
|
|
|
|
def port_manager(self, new_port_manager):
|
|
|
|
|
2015-01-19 23:43:35 +02:00
|
|
|
self._port_manager = new_port_manager
|
2015-01-19 12:22:24 +02:00
|
|
|
|
2015-01-21 04:02:22 +02:00
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
"""
|
|
|
|
Returns the server config.
|
|
|
|
|
|
|
|
:returns: Config
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._config
|
|
|
|
|
2015-01-23 06:31:26 +02:00
|
|
|
@asyncio.coroutine
|
2015-01-23 08:40:51 +02:00
|
|
|
def unload(self):
|
|
|
|
|
2015-02-05 23:24:06 +02:00
|
|
|
tasks = []
|
2015-02-04 22:48:29 +02:00
|
|
|
for vm_id in self._vms.keys():
|
2015-02-05 23:24:06 +02:00
|
|
|
tasks.append(asyncio.async(self.close_vm(vm_id)))
|
|
|
|
|
|
|
|
if tasks:
|
|
|
|
done, _ = yield from asyncio.wait(tasks)
|
|
|
|
for future in done:
|
|
|
|
try:
|
|
|
|
future.result()
|
|
|
|
except Exception as e:
|
|
|
|
log.error("Could not close VM {}".format(e), exc_info=1)
|
|
|
|
continue
|
2015-01-20 03:30:57 +02:00
|
|
|
|
2015-01-23 08:40:51 +02:00
|
|
|
if hasattr(BaseManager, "_instance"):
|
|
|
|
BaseManager._instance = None
|
2015-01-23 22:01:23 +02:00
|
|
|
log.debug("Module {} unloaded".format(self.module_name))
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-05 02:13:35 +02:00
|
|
|
def get_vm(self, vm_id, project_id=None):
|
2015-01-14 03:26:32 +02:00
|
|
|
"""
|
|
|
|
Returns a VM instance.
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
:param vm_id: VM identifier
|
2015-02-05 02:13:35 +02:00
|
|
|
:param project_id: Project identifier
|
2015-01-14 03:26:32 +02:00
|
|
|
|
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
2015-02-05 02:13:35 +02:00
|
|
|
if project_id:
|
|
|
|
# check the project_id exists
|
|
|
|
project = ProjectManager.instance().get_project(project_id)
|
|
|
|
|
2015-01-20 03:30:57 +02:00
|
|
|
try:
|
2015-02-04 22:48:29 +02:00
|
|
|
UUID(vm_id, version=4)
|
2015-01-20 03:30:57 +02:00
|
|
|
except ValueError:
|
2015-02-05 02:13:35 +02:00
|
|
|
raise aiohttp.web.HTTPBadRequest(text="VM ID {} is not a valid UUID".format(vm_id))
|
2015-01-20 03:30:57 +02:00
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
if vm_id not in self._vms:
|
2015-02-05 02:13:35 +02:00
|
|
|
raise aiohttp.web.HTTPNotFound(text="VM ID {} doesn't exist".format(vm_id))
|
|
|
|
|
|
|
|
vm = self._vms[vm_id]
|
|
|
|
if project_id:
|
|
|
|
if vm.project.id != project.id:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't belong to VM {}".format(project_id, vm.name))
|
|
|
|
|
2015-02-15 21:18:12 +02:00
|
|
|
return vm
|
2015-01-14 03:26:32 +02:00
|
|
|
|
2015-02-26 03:55:35 +02:00
|
|
|
@asyncio.coroutine
|
2015-03-01 22:05:51 +02:00
|
|
|
def convert_old_project(self, project, legacy_id, name):
|
2015-02-26 03:55:35 +02:00
|
|
|
"""
|
2015-03-01 22:05:51 +02:00
|
|
|
Convert projects made before version 1.3
|
2015-02-26 03:55:35 +02:00
|
|
|
|
|
|
|
:param project: Project instance
|
|
|
|
:param legacy_id: old identifier
|
2015-03-01 22:05:51 +02:00
|
|
|
:param name: node name
|
2015-02-26 03:55:35 +02:00
|
|
|
|
2015-03-01 22:05:51 +02:00
|
|
|
:returns: new identifier
|
2015-02-26 03:55:35 +02:00
|
|
|
"""
|
|
|
|
|
2015-03-01 22:05:51 +02:00
|
|
|
new_id = str(uuid4())
|
2015-03-09 03:13:01 +02:00
|
|
|
legacy_project_files_path = os.path.join(project.path, "{}-files".format(project.name))
|
2015-03-01 03:55:53 +02:00
|
|
|
new_project_files_path = os.path.join(project.path, "project-files")
|
|
|
|
if os.path.exists(legacy_project_files_path) and not os.path.exists(new_project_files_path):
|
|
|
|
# move the project files
|
2015-03-01 22:05:51 +02:00
|
|
|
log.info("Converting old project...")
|
2015-03-01 03:55:53 +02:00
|
|
|
try:
|
|
|
|
log.info('Moving "{}" to "{}"'.format(legacy_project_files_path, new_project_files_path))
|
|
|
|
yield from wait_run_in_executor(shutil.move, legacy_project_files_path, new_project_files_path)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not move project files directory: {} to {} {}".format(legacy_project_files_path,
|
2015-03-01 22:05:51 +02:00
|
|
|
new_project_files_path, e))
|
2015-03-01 03:55:53 +02:00
|
|
|
|
2015-03-09 03:13:01 +02:00
|
|
|
if project.is_local() is False:
|
2015-03-10 05:46:23 +02:00
|
|
|
legacy_remote_project_path = os.path.join(project.location, project.name, self.module_name.lower())
|
|
|
|
new_remote_project_path = os.path.join(project.path, "project-files", self.module_name.lower())
|
|
|
|
if os.path.exists(legacy_remote_project_path) and not os.path.exists(new_remote_project_path):
|
|
|
|
# move the legacy remote project (remote servers only)
|
|
|
|
log.info("Converting old remote project...")
|
2015-03-09 03:13:01 +02:00
|
|
|
try:
|
2015-03-10 05:46:23 +02:00
|
|
|
log.info('Moving "{}" to "{}"'.format(legacy_remote_project_path, new_remote_project_path))
|
|
|
|
yield from wait_run_in_executor(shutil.move, legacy_remote_project_path, new_remote_project_path)
|
2015-03-09 03:13:01 +02:00
|
|
|
except OSError as e:
|
2015-03-10 05:46:23 +02:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not move directory: {} to {} {}".format(legacy_remote_project_path,
|
|
|
|
new_remote_project_path, e))
|
2015-03-09 01:45:29 +02:00
|
|
|
|
2015-02-26 03:55:35 +02:00
|
|
|
if hasattr(self, "get_legacy_vm_workdir"):
|
2015-03-01 03:55:53 +02:00
|
|
|
# rename old project VM working dir
|
2015-03-01 22:05:51 +02:00
|
|
|
log.info("Converting old VM working directory...")
|
2015-02-26 03:55:35 +02:00
|
|
|
legacy_vm_dir = self.get_legacy_vm_workdir(legacy_id, name)
|
2015-03-01 03:55:53 +02:00
|
|
|
legacy_vm_working_path = os.path.join(new_project_files_path, legacy_vm_dir)
|
2015-03-01 22:05:51 +02:00
|
|
|
new_vm_working_path = os.path.join(new_project_files_path, self.module_name.lower(), new_id)
|
2015-03-02 22:04:30 +02:00
|
|
|
if os.path.exists(legacy_vm_working_path) and not os.path.exists(new_vm_working_path):
|
|
|
|
try:
|
|
|
|
log.info('Moving "{}" to "{}"'.format(legacy_vm_working_path, new_vm_working_path))
|
|
|
|
yield from wait_run_in_executor(shutil.move, legacy_vm_working_path, new_vm_working_path)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not move VM working directory: {} to {} {}".format(legacy_vm_working_path,
|
|
|
|
new_vm_working_path, e))
|
2015-03-01 00:00:00 +02:00
|
|
|
|
2015-03-01 22:05:51 +02:00
|
|
|
return new_id
|
2015-02-26 03:55:35 +02:00
|
|
|
|
2015-01-14 03:26:32 +02:00
|
|
|
@asyncio.coroutine
|
2015-02-04 22:48:29 +02:00
|
|
|
def create_vm(self, name, project_id, vm_id, *args, **kwargs):
|
2015-01-20 13:46:15 +02:00
|
|
|
"""
|
|
|
|
Create a new VM
|
|
|
|
|
2015-01-21 04:02:22 +02:00
|
|
|
:param name: VM name
|
2015-02-04 22:48:29 +02:00
|
|
|
:param project_id: Project identifier
|
|
|
|
:param vm_id: restore a VM identifier
|
2015-01-20 13:46:15 +02:00
|
|
|
"""
|
2015-01-21 00:28:40 +02:00
|
|
|
|
2015-03-02 16:35:36 +02:00
|
|
|
if vm_id in self._vms:
|
|
|
|
return self._vms[vm_id]
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
project = ProjectManager.instance().get_project(project_id)
|
2015-02-26 03:55:35 +02:00
|
|
|
if vm_id and isinstance(vm_id, int):
|
|
|
|
with (yield from BaseManager._convert_lock):
|
2015-03-01 22:05:51 +02:00
|
|
|
vm_id = yield from self.convert_old_project(project, vm_id, name)
|
2015-01-20 03:30:57 +02:00
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
if not vm_id:
|
|
|
|
vm_id = str(uuid4())
|
2015-01-20 03:30:57 +02:00
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
vm = self._VM_CLASS(name, vm_id, project, self, *args, **kwargs)
|
2015-01-22 02:41:35 +02:00
|
|
|
if asyncio.iscoroutinefunction(vm.create):
|
|
|
|
yield from vm.create()
|
|
|
|
else:
|
|
|
|
vm.create()
|
2015-02-04 22:48:29 +02:00
|
|
|
self._vms[vm.id] = vm
|
2015-02-03 03:56:13 +02:00
|
|
|
project.add_vm(vm)
|
2015-01-14 03:26:32 +02:00
|
|
|
return vm
|
2015-01-22 12:34:10 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-02-04 22:48:29 +02:00
|
|
|
def close_vm(self, vm_id):
|
2015-01-22 12:34:10 +02:00
|
|
|
"""
|
|
|
|
Delete a VM
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
:param vm_id: VM identifier
|
|
|
|
|
2015-01-23 12:28:58 +02:00
|
|
|
:returns: VM instance
|
2015-01-22 12:34:10 +02:00
|
|
|
"""
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
vm = self.get_vm(vm_id)
|
2015-01-23 04:06:17 +02:00
|
|
|
if asyncio.iscoroutinefunction(vm.close):
|
|
|
|
yield from vm.close()
|
2015-01-22 12:34:10 +02:00
|
|
|
else:
|
2015-01-23 04:06:17 +02:00
|
|
|
vm.close()
|
2015-01-23 12:28:58 +02:00
|
|
|
return vm
|
|
|
|
|
2015-03-02 04:20:33 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def project_closing(self, project):
|
|
|
|
"""
|
|
|
|
Called when a project is about to be closed.
|
|
|
|
|
|
|
|
:param project: Project instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2015-02-16 07:13:24 +02:00
|
|
|
@asyncio.coroutine
|
2015-02-28 01:51:17 +02:00
|
|
|
def project_closed(self, project):
|
2015-02-16 07:13:24 +02:00
|
|
|
"""
|
|
|
|
Called when a project is closed.
|
|
|
|
|
2015-02-28 01:51:17 +02:00
|
|
|
:param project: Project instance
|
|
|
|
"""
|
|
|
|
|
2015-03-02 18:17:28 +02:00
|
|
|
for vm in project.vms:
|
|
|
|
if vm.id in self._vms:
|
|
|
|
del self._vms[vm.id]
|
2015-02-28 01:51:17 +02:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def project_moved(self, project):
|
|
|
|
"""
|
|
|
|
Called when a project is moved
|
|
|
|
|
|
|
|
:param project: project instance
|
2015-02-16 07:13:24 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2015-03-21 22:52:17 +02:00
|
|
|
@asyncio.coroutine
|
|
|
|
def project_committed(self, project):
|
|
|
|
"""
|
|
|
|
Called when a project is committed.
|
|
|
|
|
|
|
|
:param project: Project instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2015-01-23 12:28:58 +02:00
|
|
|
@asyncio.coroutine
|
2015-02-04 22:48:29 +02:00
|
|
|
def delete_vm(self, vm_id):
|
2015-01-23 12:28:58 +02:00
|
|
|
"""
|
|
|
|
Delete a VM. VM working directory will be destroy when
|
|
|
|
we receive a commit.
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
:param vm_id: VM identifier
|
2015-01-23 12:28:58 +02:00
|
|
|
:returns: VM instance
|
|
|
|
"""
|
|
|
|
|
2015-02-04 22:48:29 +02:00
|
|
|
vm = yield from self.close_vm(vm_id)
|
2015-01-23 12:28:58 +02:00
|
|
|
vm.project.mark_vm_for_destruction(vm)
|
2015-02-04 22:48:29 +02:00
|
|
|
del self._vms[vm.id]
|
2015-01-23 12:28:58 +02:00
|
|
|
return vm
|
2015-01-23 03:04:24 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _has_privileged_access(executable):
|
|
|
|
"""
|
|
|
|
Check if an executable can access Ethernet and TAP devices in
|
|
|
|
RAW mode.
|
|
|
|
|
|
|
|
:param executable: executable path
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# do not check anything on Windows
|
|
|
|
return True
|
|
|
|
|
|
|
|
if os.geteuid() == 0:
|
|
|
|
# we are root, so we should have privileged access.
|
|
|
|
return True
|
|
|
|
if os.stat(executable).st_mode & stat.S_ISUID or os.stat(executable).st_mode & stat.S_ISGID:
|
|
|
|
# the executable has set UID bit.
|
|
|
|
return True
|
|
|
|
|
|
|
|
# test if the executable has the CAP_NET_RAW capability (Linux only)
|
|
|
|
if sys.platform.startswith("linux") and "security.capability" in os.listxattr(executable):
|
|
|
|
try:
|
|
|
|
caps = os.getxattr(executable, "security.capability")
|
|
|
|
# test the 2nd byte and check if the 13th bit (CAP_NET_RAW) is set
|
|
|
|
if struct.unpack("<IIIII", caps)[1] & 1 << 13:
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
log.error("could not determine if CAP_NET_RAW capability is set for {}: {}".format(executable, e))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def create_nio(self, executable, nio_settings):
|
|
|
|
"""
|
|
|
|
Creates a new NIO.
|
|
|
|
|
|
|
|
:param nio_settings: information to create the NIO
|
|
|
|
|
|
|
|
:returns: a NIO object
|
|
|
|
"""
|
|
|
|
|
|
|
|
nio = None
|
|
|
|
if nio_settings["type"] == "nio_udp":
|
|
|
|
lport = nio_settings["lport"]
|
|
|
|
rhost = nio_settings["rhost"]
|
|
|
|
rport = nio_settings["rport"]
|
|
|
|
try:
|
2015-06-07 06:37:34 +03:00
|
|
|
info = socket.getaddrinfo(rhost, rport, socket.AF_UNSPEC, socket.SOCK_DGRAM, 0, socket.AI_PASSIVE)
|
|
|
|
if not info:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="getaddrinfo returns an empty list on {}:{}".format(rhost, rport))
|
|
|
|
for res in info:
|
|
|
|
af, socktype, proto, _, sa = res
|
|
|
|
with socket.socket(af, socktype, proto) as sock:
|
|
|
|
sock.connect(sa)
|
2015-01-23 03:04:24 +02:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
|
2015-02-24 04:00:34 +02:00
|
|
|
nio = NIOUDP(lport, rhost, rport)
|
2015-01-23 03:04:24 +02:00
|
|
|
elif nio_settings["type"] == "nio_tap":
|
|
|
|
tap_device = nio_settings["tap_device"]
|
2015-06-07 22:51:33 +03:00
|
|
|
if not is_interface_up(tap_device):
|
2015-07-22 07:58:28 +03:00
|
|
|
raise aiohttp.web.HTTPConflict(text="TAP interface {} does not exist or is down".format(tap_device))
|
2015-04-28 11:43:27 +03:00
|
|
|
# FIXME: check for permissions on tap device
|
|
|
|
# if not self._has_privileged_access(executable):
|
2015-04-27 23:38:15 +03:00
|
|
|
# raise aiohttp.web.HTTPForbidden(text="{} has no privileged access to {}.".format(executable, tap_device))
|
2015-02-24 04:00:34 +02:00
|
|
|
nio = NIOTAP(tap_device)
|
2015-02-13 17:41:18 +02:00
|
|
|
elif nio_settings["type"] == "nio_generic_ethernet":
|
2015-06-07 22:51:33 +03:00
|
|
|
ethernet_device = nio_settings["ethernet_device"]
|
|
|
|
if not is_interface_up(ethernet_device):
|
2015-07-22 07:58:28 +03:00
|
|
|
raise aiohttp.web.HTTPConflict(text="Ethernet interface {} does not exist or is down".format(ethernet_device))
|
2015-06-07 22:51:33 +03:00
|
|
|
nio = NIOGenericEthernet(ethernet_device)
|
2015-05-06 23:59:01 +03:00
|
|
|
elif nio_settings["type"] == "nio_nat":
|
|
|
|
nio = NIONAT()
|
2015-01-23 03:04:24 +02:00
|
|
|
assert nio is not None
|
|
|
|
return nio
|
2015-04-14 19:46:55 +03:00
|
|
|
|
|
|
|
def get_abs_image_path(self, path):
|
|
|
|
"""
|
|
|
|
Get the absolute path of an image
|
|
|
|
|
|
|
|
:param path: file path
|
|
|
|
:return: file path
|
|
|
|
"""
|
|
|
|
|
2015-04-25 02:27:32 +03:00
|
|
|
if not path:
|
|
|
|
return ""
|
2015-04-14 19:46:55 +03:00
|
|
|
img_directory = self.get_images_directory()
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
s = os.path.split(path)
|
2015-05-04 11:57:08 +03:00
|
|
|
path = os.path.normpath(os.path.join(img_directory, *s))
|
|
|
|
|
|
|
|
# Compatibility with old topologies we look in parent directory
|
|
|
|
# We look at first in new location
|
|
|
|
if not os.path.exists(path):
|
|
|
|
old_path = os.path.normpath(os.path.join(img_directory, '..', *s))
|
|
|
|
if os.path.exists(old_path):
|
|
|
|
return old_path
|
|
|
|
|
|
|
|
return path
|
2015-04-14 19:46:55 +03:00
|
|
|
return path
|
|
|
|
|
|
|
|
def get_relative_image_path(self, path):
|
|
|
|
"""
|
|
|
|
Get a path relative to images directory path
|
|
|
|
or an abspath if the path is not located inside
|
|
|
|
image directory
|
|
|
|
|
|
|
|
:param path: file path
|
|
|
|
:return: file path
|
|
|
|
"""
|
|
|
|
|
2015-04-25 02:27:32 +03:00
|
|
|
if not path:
|
|
|
|
return ""
|
2015-04-14 19:46:55 +03:00
|
|
|
img_directory = self.get_images_directory()
|
|
|
|
path = self.get_abs_image_path(path)
|
|
|
|
if os.path.dirname(path) == img_directory:
|
|
|
|
return os.path.basename(path)
|
|
|
|
return path
|
|
|
|
|
2015-04-13 15:33:13 +03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def list_images(self):
|
|
|
|
"""
|
|
|
|
Return the list of available images for this VM type
|
|
|
|
|
|
|
|
:returns: Array of hash
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
files = os.listdir(self.get_images_directory())
|
|
|
|
except FileNotFoundError:
|
|
|
|
return []
|
|
|
|
files.sort()
|
|
|
|
images = []
|
|
|
|
for filename in files:
|
2015-06-17 18:11:25 +03:00
|
|
|
if filename[0] != "." and not filename.endswith(".md5sum"):
|
2015-04-13 15:33:13 +03:00
|
|
|
images.append({"filename": filename})
|
|
|
|
return images
|
|
|
|
|
2015-04-14 19:46:55 +03:00
|
|
|
def get_images_directory(self):
|
|
|
|
"""
|
|
|
|
Get the image directory on disk
|
|
|
|
"""
|
2015-04-25 02:27:32 +03:00
|
|
|
|
2015-04-14 19:46:55 +03:00
|
|
|
raise NotImplementedError
|
2015-04-24 11:15:23 +03:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def write_image(self, filename, stream):
|
|
|
|
directory = self.get_images_directory()
|
|
|
|
path = os.path.join(directory, os.path.basename(filename))
|
|
|
|
log.info("Writting image file %s", path)
|
|
|
|
try:
|
2015-06-17 18:11:25 +03:00
|
|
|
remove_checksum(path)
|
2015-04-24 11:15:23 +03:00
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
with open(path, 'wb+') as f:
|
|
|
|
while True:
|
|
|
|
packet = yield from stream.read(512)
|
|
|
|
if not packet:
|
|
|
|
break
|
|
|
|
f.write(packet)
|
|
|
|
os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
|
2015-06-17 18:11:25 +03:00
|
|
|
md5sum(path)
|
2015-04-24 11:15:23 +03:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="Could not write image: {} to {}".format(filename, e))
|