gns3-server/gns3server/modules/vpcs/__init__.py

66 lines
1.9 KiB
Python
Raw Normal View History

2014-05-06 19:06:10 +03:00
# -*- coding: utf-8 -*-
#
2015-01-14 02:05:26 +02:00
# Copyright (C) 2015 GNS3 Technologies Inc.
2014-05-06 19:06:10 +03: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/>.
"""
VPCS server module.
2014-05-06 19:06:10 +03:00
"""
2015-01-22 17:12:21 +02:00
import asyncio
2015-01-14 19:52:02 +02:00
from ..base_manager import BaseManager
2015-01-22 17:12:21 +02:00
from .vpcs_error import VPCSError
2015-01-20 14:12:26 +02:00
from .vpcs_vm import VPCSVM
2014-05-06 19:06:10 +03:00
2015-01-14 19:52:02 +02:00
class VPCS(BaseManager):
2015-01-20 14:12:26 +02:00
_VM_CLASS = VPCSVM
2015-01-22 17:12:21 +02:00
def __init__(self):
super().__init__()
2015-01-22 19:47:27 +02:00
self._free_mac_ids = {}
2015-01-22 17:12:21 +02:00
self._used_mac_ids = {}
@asyncio.coroutine
def create_vm(self, *args, **kwargs):
vm = yield from super().create_vm(*args, **kwargs)
2015-01-22 19:47:27 +02:00
self._free_mac_ids.setdefault(vm.project.uuid, list(range(0, 255)))
2015-01-22 17:12:21 +02:00
try:
2015-01-22 19:47:27 +02:00
self._used_mac_ids[vm.uuid] = self._free_mac_ids[vm.project.uuid].pop(0)
2015-01-22 17:12:21 +02:00
except IndexError:
raise VPCSError("No mac address available")
return vm
@asyncio.coroutine
def delete_vm(self, uuid, *args, **kwargs):
2015-01-22 19:47:27 +02:00
vm = self.get_vm(uuid)
2015-01-22 17:12:21 +02:00
i = self._used_mac_ids[uuid]
2015-01-22 19:47:27 +02:00
self._free_mac_ids[vm.project.uuid].insert(0, i)
2015-01-22 17:12:21 +02:00
del self._used_mac_ids[uuid]
yield from super().delete_vm(uuid, *args, **kwargs)
def get_mac_id(self, vm_uuid):
"""
Get an unique VPCS mac id
:param vm_uuid: UUID of the VPCS vm
:returns: VPCS Mac id
"""
return self._used_mac_ids.get(vm_uuid, 1)