1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-18 14:28:08 +00:00
gns3-server/gns3server/modules/project.py

151 lines
4.0 KiB
Python
Raw Normal View History

2015-01-19 15:23:41 +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/>.
import aiohttp
2015-01-19 15:23:41 +00:00
import os
import tempfile
2015-01-23 10:28:58 +00:00
import shutil
from uuid import UUID, uuid4
2015-01-19 15:23:41 +00:00
class Project:
"""
A project contains a list of VM.
In theory VM are isolated project/project.
:param uuid: Force project uuid (None by default auto generate an UUID)
:param location: Parent path of the project. (None should create a tmp directory)
"""
2015-01-19 21:43:35 +00:00
def __init__(self, uuid=None, location=None):
2015-01-19 15:23:41 +00:00
if uuid is None:
2015-01-19 21:43:35 +00:00
self._uuid = str(uuid4())
2015-01-19 15:23:41 +00:00
else:
try:
UUID(uuid, version=4)
except ValueError:
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(uuid))
2015-01-19 21:43:35 +00:00
self._uuid = uuid
2015-01-19 15:23:41 +00:00
2015-01-19 21:43:35 +00:00
self._location = location
2015-01-19 15:23:41 +00:00
if location is None:
2015-01-19 21:43:35 +00:00
self._location = tempfile.mkdtemp()
2015-01-19 15:23:41 +00:00
2015-01-23 13:07:10 +00:00
self._vms = set()
2015-01-23 10:28:58 +00:00
self._vms_to_destroy = set()
2015-01-21 10:33:24 +00:00
self._path = os.path.join(self._location, self._uuid)
try:
2015-01-21 22:21:15 +00:00
os.makedirs(os.path.join(self._path, "vms"), exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
2015-01-19 21:43:35 +00:00
@property
def uuid(self):
return self._uuid
2015-01-19 15:23:41 +00:00
@property
def location(self):
return self._location
@property
def path(self):
return self._path
2015-01-23 13:07:10 +00:00
@property
def vms(self):
return self._vms
2015-01-23 10:28:58 +00:00
def vm_working_directory(self, vm):
"""
Return a working directory for a specific VM.
If the directory doesn't exist, the directory is created.
2015-01-23 10:28:58 +00:00
:param vm: An instance of VM
:returns: A string with a VM working directory
"""
2015-01-23 10:28:58 +00:00
workdir = os.path.join(self._path, vm.manager.module_name.lower(), vm.uuid)
try:
2015-01-22 02:28:52 +00:00
os.makedirs(workdir, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create VM working directory: {}".format(e))
2015-01-22 02:28:52 +00:00
return workdir
2015-01-23 10:28:58 +00:00
def mark_vm_for_destruction(self, vm):
"""
:param vm: An instance of VM
"""
self.remove_vm(vm)
2015-01-23 10:28:58 +00:00
self._vms_to_destroy.add(vm)
2015-01-19 15:23:41 +00:00
def __json__(self):
2015-01-19 21:43:35 +00:00
2015-01-19 15:23:41 +00:00
return {
2015-01-19 21:43:35 +00:00
"uuid": self._uuid,
"location": self._location
2015-01-19 15:23:41 +00:00
}
2015-01-23 10:28:58 +00:00
2015-01-23 13:07:10 +00:00
def add_vm(self, vm):
"""
Add a VM to the project.
In theory this should be called by the VM manager.
2015-01-23 13:07:10 +00:00
:params vm: A VM instance
"""
self._vms.add(vm)
def remove_vm(self, vm):
"""
Remove a VM from the project.
In theory this should be called by the VM manager.
:params vm: A VM instance
"""
if vm in self._vms:
self._vms.remove(vm)
2015-01-23 10:48:20 +00:00
def close(self):
"""Close the project, but keep informations on disk"""
2015-01-23 13:07:10 +00:00
for vm in self._vms:
vm.close()
2015-01-23 10:48:20 +00:00
2015-01-23 10:28:58 +00:00
def commit(self):
"""Write project changes on disk"""
2015-01-23 10:48:20 +00:00
2015-01-23 10:28:58 +00:00
while self._vms_to_destroy:
vm = self._vms_to_destroy.pop()
directory = self.vm_working_directory(vm)
if os.path.exists(directory):
shutil.rmtree(directory)
self.remove_vm(vm)
2015-01-23 10:48:20 +00:00
def delete(self):
"""Remove project from disk"""
self.close()
if os.path.exists(self.path):
shutil.rmtree(self.path)