gns3-server/gns3server/modules/project.py

84 lines
2.2 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 os
import tempfile
from uuid import uuid4
class Project:
2015-01-20 12:24:00 +00:00
2015-01-19 15:23:41 +00:00
"""
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:
2015-01-20 11:46:15 +00:00
assert len(uuid) == 36
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-19 21:43:35 +00:00
self._path = os.path.join(self._location, self._uuid)
if os.path.exists(self._path) is False:
os.mkdir(self._path)
os.mkdir(os.path.join(self._path, "vms"))
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
def vm_working_directory(self, vm_identifier):
"""
Return a working directory for a specific VM.
If the directory doesn't exist, the directory is created.
:param vm_identifier: UUID of VM
"""
path = os.path.join(self._path, 'vms', vm_identifier)
if os.path.exists(path) is False:
os.mkdir(path)
return path
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
}