mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Use the project working directory for VPCS VM
This commit is contained in:
1 parent
f5ed9fbcf1
commit
db41076ce5
7 files changed
+60
-29
No files matched your search
@@ -33,11 +33,13 @@ class BaseVM:
|
||||
|
||||
# TODO: When delete release console ports
|
||||
|
||||
|
||||
@property
|
||||
def project(self):
|
||||
"""Return VM current project"""
|
||||
return self._project
|
||||
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""
|
||||
@@ -48,6 +50,7 @@ class BaseVM:
|
||||
|
||||
return self._name
|
||||
|
||||
|
||||
@name.setter
|
||||
def name(self, new_name):
|
||||
"""
|
||||
@@ -58,6 +61,7 @@ class BaseVM:
|
||||
|
||||
self._name = new_name
|
||||
|
||||
|
||||
@property
|
||||
def uuid(self):
|
||||
"""
|
||||
@@ -68,6 +72,7 @@ class BaseVM:
|
||||
|
||||
return self._uuid
|
||||
|
||||
|
||||
@property
|
||||
def manager(self):
|
||||
"""
|
||||
@@ -78,6 +83,16 @@ class BaseVM:
|
||||
|
||||
return self._manager
|
||||
|
||||
|
||||
@property
|
||||
def working_dir(self):
|
||||
"""
|
||||
Return VM working directory
|
||||
"""
|
||||
|
||||
return self._project.vm_working_directory(self._uuid)
|
||||
|
||||
|
||||
def create(self):
|
||||
"""
|
||||
Creates the VM.
|
||||
@@ -85,6 +100,7 @@ class BaseVM:
|
||||
|
||||
return
|
||||
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
Starts the VM process.
|
||||
@@ -92,6 +108,7 @@ class BaseVM:
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
Starts the VM process.
|
||||
|
||||
@@ -45,23 +45,41 @@ class Project:
|
||||
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, "files"))
|
||||
os.mkdir(os.path.join(self._path, "vms"))
|
||||
|
||||
|
||||
@property
|
||||
def uuid(self):
|
||||
|
||||
return self._uuid
|
||||
|
||||
|
||||
@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
|
||||
|
||||
|
||||
def __json__(self):
|
||||
|
||||
return {
|
||||
|
||||
@@ -51,11 +51,10 @@ class VPCSVM(BaseVM):
|
||||
:param uuid: VPCS instance UUID
|
||||
:param project: Project instance
|
||||
:param manager: parent VM Manager
|
||||
:param working_dir: path to a working directory
|
||||
:param console: TCP console port
|
||||
"""
|
||||
|
||||
def __init__(self, name, uuid, project, manager, working_dir=None, console=None):
|
||||
def __init__(self, name, uuid, project, manager, console=None):
|
||||
|
||||
super().__init__(name, uuid, project, manager)
|
||||
|
||||
@@ -63,9 +62,6 @@ class VPCSVM(BaseVM):
|
||||
|
||||
self._console = console
|
||||
|
||||
# TODO: remove working_dir
|
||||
self._working_dir = "/tmp"
|
||||
|
||||
self._command = []
|
||||
self._process = None
|
||||
self._vpcs_stdout_file = ""
|
||||
@@ -75,14 +71,6 @@ class VPCSVM(BaseVM):
|
||||
self._script_file = ""
|
||||
self._ethernet_adapter = EthernetAdapter() # one adapter with 1 Ethernet interface
|
||||
|
||||
# working_dir_path = os.path.join(working_dir, "vpcs", "pc-{}".format(self._id))
|
||||
#
|
||||
# if vpcs_id and not os.path.isdir(working_dir_path):
|
||||
# raise VPCSError("Working directory {} doesn't exist".format(working_dir_path))
|
||||
#
|
||||
# # create the vm own working directory
|
||||
# self.working_dir = working_dir_path
|
||||
#
|
||||
try:
|
||||
if not self._console:
|
||||
self._console = self._manager.port_manager.get_free_console_port()
|
||||
@@ -133,7 +121,7 @@ class VPCSVM(BaseVM):
|
||||
|
||||
if self._script_file:
|
||||
# update the startup.vpc
|
||||
config_path = os.path.join(self._working_dir, "startup.vpc")
|
||||
config_path = os.path.join(self.working_dir, "startup.vpc")
|
||||
if os.path.isfile(config_path):
|
||||
try:
|
||||
with open(config_path, "r+", errors="replace") as f:
|
||||
@@ -155,7 +143,7 @@ class VPCSVM(BaseVM):
|
||||
"""
|
||||
# TODO: should be async
|
||||
try:
|
||||
output = subprocess.check_output([self._path, "-v"], cwd=self._working_dir)
|
||||
output = subprocess.check_output([self._path, "-v"], cwd=self.working_dir)
|
||||
match = re.search("Welcome to Virtual PC Simulator, version ([0-9a-z\.]+)", output.decode("utf-8"))
|
||||
if match:
|
||||
version = match.group(1)
|
||||
@@ -179,7 +167,7 @@ class VPCSVM(BaseVM):
|
||||
self._command = self._build_command()
|
||||
try:
|
||||
log.info("starting VPCS: {}".format(self._command))
|
||||
self._vpcs_stdout_file = os.path.join(self._working_dir, "vpcs.log")
|
||||
self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log")
|
||||
log.info("logging to {}".format(self._vpcs_stdout_file))
|
||||
flags = 0
|
||||
if sys.platform.startswith("win32"):
|
||||
@@ -188,7 +176,7 @@ class VPCSVM(BaseVM):
|
||||
self._process = yield from asyncio.create_subprocess_exec(*self._command,
|
||||
stdout=fd,
|
||||
stderr=subprocess.STDOUT,
|
||||
cwd=self._working_dir,
|
||||
cwd=self.working_dir,
|
||||
creationflags=flags)
|
||||
log.info("VPCS instance {} started PID={}".format(self.name, self._process.pid))
|
||||
self._started = True
|
||||
|
||||
Reference in new issue
Block a user