2015-02-11 13:31:21 +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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
IOU server module.
|
|
|
|
"""
|
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
import os
|
2015-02-11 13:31:21 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from ..base_manager import BaseManager
|
|
|
|
from .iou_error import IOUError
|
|
|
|
from .iou_vm import IOUVM
|
|
|
|
|
|
|
|
|
|
|
|
class IOU(BaseManager):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-02-11 13:31:21 +00:00
|
|
|
_VM_CLASS = IOUVM
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-02-11 13:31:21 +00:00
|
|
|
super().__init__()
|
|
|
|
self._free_application_ids = list(range(1, 512))
|
|
|
|
self._used_application_ids = {}
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def create_vm(self, *args, **kwargs):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Creates a new IOU VM.
|
|
|
|
|
|
|
|
:returns: IOUVM instance
|
|
|
|
"""
|
2015-02-11 13:31:21 +00:00
|
|
|
|
|
|
|
vm = yield from super().create_vm(*args, **kwargs)
|
|
|
|
try:
|
|
|
|
self._used_application_ids[vm.id] = self._free_application_ids.pop(0)
|
|
|
|
except IndexError:
|
2015-04-08 17:17:34 +00:00
|
|
|
raise IOUError("Cannot create a new IOU VM (limit of 512 VMs reached on this host)")
|
2015-02-11 13:31:21 +00:00
|
|
|
return vm
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-03-06 14:48:16 +00:00
|
|
|
def close_vm(self, vm_id, *args, **kwargs):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Closes an IOU VM.
|
|
|
|
|
|
|
|
:returns: IOUVM instance
|
|
|
|
"""
|
2015-02-11 13:31:21 +00:00
|
|
|
|
|
|
|
vm = self.get_vm(vm_id)
|
2015-03-26 14:49:51 +00:00
|
|
|
if vm_id in self._used_application_ids:
|
|
|
|
i = self._used_application_ids[vm_id]
|
|
|
|
self._free_application_ids.insert(0, i)
|
|
|
|
del self._used_application_ids[vm_id]
|
2015-03-06 14:48:16 +00:00
|
|
|
yield from super().close_vm(vm_id, *args, **kwargs)
|
|
|
|
return vm
|
2015-02-11 13:31:21 +00:00
|
|
|
|
2015-06-06 21:15:03 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def project_committed(self, project):
|
|
|
|
"""
|
|
|
|
Called when a project has been committed.
|
|
|
|
|
|
|
|
:param project: Project instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
# save the configs when the project is committed
|
|
|
|
for vm in self._vms.copy().values():
|
|
|
|
if vm.project.id == project.id:
|
|
|
|
vm.save_configs()
|
|
|
|
|
2015-02-11 13:31:21 +00:00
|
|
|
def get_application_id(self, vm_id):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Get an unique application identifier for IOU.
|
|
|
|
|
|
|
|
:param vm_id: IOU VM identifier
|
2015-02-11 13:31:21 +00:00
|
|
|
|
2015-04-08 17:17:34 +00:00
|
|
|
:returns: IOU application identifier
|
2015-02-11 13:31:21 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
return self._used_application_ids.get(vm_id, 1)
|
2015-02-25 15:35:13 +00:00
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_legacy_vm_workdir(legacy_vm_id, name):
|
2015-02-25 15:35:13 +00:00
|
|
|
"""
|
|
|
|
Returns the name of the legacy working directory (pre 1.3) name for a VM.
|
|
|
|
|
|
|
|
:param legacy_vm_id: legacy VM identifier (integer)
|
2015-02-26 01:55:35 +00:00
|
|
|
:param name: VM name (not used)
|
|
|
|
|
2015-02-25 15:35:13 +00:00
|
|
|
:returns: working directory name
|
|
|
|
"""
|
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
return os.path.join("iou", "device-{}".format(legacy_vm_id))
|
2015-04-14 16:46:55 +00:00
|
|
|
|
|
|
|
def get_images_directory(self):
|
|
|
|
"""
|
|
|
|
Return the full path of the images directory on disk
|
|
|
|
"""
|
|
|
|
return os.path.join(os.path.expanduser(self.config.get_section_config("Server").get("images_path", "~/GNS3/images")), "IOU")
|