mirror of https://github.com/GNS3/gns3-server
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.6 KiB
95 lines
2.6 KiB
![]()
11 years ago
|
# -*- coding: utf-8 -*-
|
||
|
#
|
||
![]()
10 years ago
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
||
![]()
11 years ago
|
#
|
||
|
# 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/>.
|
||
|
|
||
|
"""
|
||
![]()
10 years ago
|
VPCS server module.
|
||
![]()
11 years ago
|
"""
|
||
|
|
||
![]()
10 years ago
|
import os
|
||
![]()
10 years ago
|
import asyncio
|
||
|
|
||
![]()
10 years ago
|
from ..base_manager import BaseManager
|
||
![]()
10 years ago
|
from .vpcs_error import VPCSError
|
||
![]()
10 years ago
|
from .vpcs_vm import VPCSVM
|
||
![]()
11 years ago
|
|
||
|
|
||
![]()
10 years ago
|
class VPCS(BaseManager):
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
_VM_CLASS = VPCSVM
|
||
![]()
10 years ago
|
|
||
|
def __init__(self):
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
super().__init__()
|
||
![]()
10 years ago
|
self._free_mac_ids = {}
|
||
![]()
10 years ago
|
self._used_mac_ids = {}
|
||
|
|
||
|
@asyncio.coroutine
|
||
|
def create_vm(self, *args, **kwargs):
|
||
![]()
10 years ago
|
"""
|
||
|
Creates a new VPCS VM.
|
||
|
|
||
|
:returns: VPCSVM instance
|
||
|
"""
|
||
![]()
10 years ago
|
|
||
|
vm = yield from super().create_vm(*args, **kwargs)
|
||
![]()
10 years ago
|
self._free_mac_ids.setdefault(vm.project.id, list(range(0, 255)))
|
||
![]()
10 years ago
|
try:
|
||
![]()
10 years ago
|
self._used_mac_ids[vm.id] = self._free_mac_ids[vm.project.id].pop(0)
|
||
![]()
10 years ago
|
except IndexError:
|
||
![]()
10 years ago
|
raise VPCSError("Cannot create a new VPCS VM (limit of 255 VMs reached on this host)")
|
||
![]()
10 years ago
|
return vm
|
||
|
|
||
|
@asyncio.coroutine
|
||
![]()
10 years ago
|
def close_vm(self, vm_id, *args, **kwargs):
|
||
![]()
10 years ago
|
"""
|
||
|
Closes a VPCS VM.
|
||
|
|
||
|
:returns: VPCSVM instance
|
||
|
"""
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
vm = self.get_vm(vm_id)
|
||
![]()
10 years ago
|
if vm_id in self._used_mac_ids:
|
||
![]()
10 years ago
|
i = self._used_mac_ids[vm_id]
|
||
|
self._free_mac_ids[vm.project.id].insert(0, i)
|
||
![]()
10 years ago
|
del self._used_mac_ids[vm_id]
|
||
![]()
10 years ago
|
yield from super().close_vm(vm_id, *args, **kwargs)
|
||
|
return vm
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
def get_mac_id(self, vm_id):
|
||
![]()
10 years ago
|
"""
|
||
![]()
10 years ago
|
Get an unique VPCS MAC id (offset)
|
||
|
|
||
|
:param vm_id: VPCS VM identifier
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
:returns: VPCS MAC identifier
|
||
![]()
10 years ago
|
"""
|
||
|
|
||
![]()
10 years ago
|
return self._used_mac_ids.get(vm_id, 1)
|
||
![]()
10 years ago
|
|
||
|
@staticmethod
|
||
![]()
10 years ago
|
def get_legacy_vm_workdir(legacy_vm_id, name):
|
||
![]()
10 years ago
|
"""
|
||
|
Returns the name of the legacy working directory name for a VM.
|
||
|
|
||
|
:param legacy_vm_id: legacy VM identifier (integer)
|
||
![]()
10 years ago
|
:param name: VM name (not used)
|
||
|
|
||
![]()
10 years ago
|
:returns: working directory name
|
||
|
"""
|
||
|
|
||
![]()
10 years ago
|
return os.path.join("vpcs", "pc-{}".format(legacy_vm_id))
|