2014-05-06 16:06:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-01-14 00:05:26 +00:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2014-05-06 16:06:10 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""
|
2014-05-13 21:11:37 +00:00
|
|
|
VPCS server module.
|
2014-05-06 16:06:10 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
import os
|
2015-01-22 15:12:21 +00:00
|
|
|
import asyncio
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
from ..base_manager import BaseManager
|
2015-01-22 15:12:21 +00:00
|
|
|
from .vpcs_error import VPCSError
|
2015-01-20 12:12:26 +00:00
|
|
|
from .vpcs_vm import VPCSVM
|
2014-05-06 16:06:10 +00:00
|
|
|
|
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
class VPCS(BaseManager):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-01-20 12:12:26 +00:00
|
|
|
_VM_CLASS = VPCSVM
|
2015-01-22 15:12:21 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-01-22 15:12:21 +00:00
|
|
|
super().__init__()
|
2015-01-22 17:47:27 +00:00
|
|
|
self._free_mac_ids = {}
|
2015-01-22 15:12:21 +00:00
|
|
|
self._used_mac_ids = {}
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def create_vm(self, *args, **kwargs):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Creates a new VPCS VM.
|
|
|
|
|
|
|
|
:returns: VPCSVM instance
|
|
|
|
"""
|
2015-01-22 15:12:21 +00:00
|
|
|
|
|
|
|
vm = yield from super().create_vm(*args, **kwargs)
|
2015-02-04 20:48:29 +00:00
|
|
|
self._free_mac_ids.setdefault(vm.project.id, list(range(0, 255)))
|
2015-01-22 15:12:21 +00:00
|
|
|
try:
|
2015-02-04 20:48:29 +00:00
|
|
|
self._used_mac_ids[vm.id] = self._free_mac_ids[vm.project.id].pop(0)
|
2015-01-22 15:12:21 +00:00
|
|
|
except IndexError:
|
2015-04-08 17:17:34 +00:00
|
|
|
raise VPCSError("Cannot create a new VPCS VM (limit of 255 VMs reached on this host)")
|
2015-01-22 15:12: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 a VPCS VM.
|
|
|
|
|
|
|
|
:returns: VPCSVM instance
|
|
|
|
"""
|
2015-01-22 15:12:21 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
vm = self.get_vm(vm_id)
|
|
|
|
i = self._used_mac_ids[vm_id]
|
|
|
|
self._free_mac_ids[vm.project.id].insert(0, i)
|
2015-03-30 15:03:49 +00:00
|
|
|
if vm_id in self._used_mac_ids:
|
|
|
|
del self._used_mac_ids[vm_id]
|
2015-03-06 14:48:16 +00:00
|
|
|
yield from super().close_vm(vm_id, *args, **kwargs)
|
|
|
|
return vm
|
2015-01-22 15:12:21 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
def get_mac_id(self, vm_id):
|
2015-01-22 15:12:21 +00:00
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Get an unique VPCS MAC id (offset)
|
|
|
|
|
|
|
|
:param vm_id: VPCS VM identifier
|
2015-01-22 15:12:21 +00:00
|
|
|
|
2015-04-08 17:17:34 +00:00
|
|
|
:returns: VPCS MAC identifier
|
2015-01-22 15:12:21 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
return self._used_mac_ids.get(vm_id, 1)
|
2015-02-09 01:10:04 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2015-02-26 01:55:35 +00:00
|
|
|
def get_legacy_vm_workdir(legacy_vm_id, name):
|
2015-02-09 01:10:04 +00:00
|
|
|
"""
|
|
|
|
Returns the name of the legacy working directory 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-09 01:10:04 +00:00
|
|
|
:returns: working directory name
|
|
|
|
"""
|
|
|
|
|
2015-02-26 01:55:35 +00:00
|
|
|
return os.path.join("vpcs", "pc-{}".format(legacy_vm_id))
|