mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-28 03:08:14 +00:00
Load/save GNS3 VM settings on controller side.
This commit is contained in:
parent
13deecea4e
commit
234f199558
@ -15,14 +15,22 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class BaseGNS3VM:
|
class BaseGNS3VM:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
self._vmname = None
|
self._vmname = None
|
||||||
|
self._auto_start = False
|
||||||
|
self._auto_stop = False
|
||||||
self._ip_address = None
|
self._ip_address = None
|
||||||
self._port = 3080
|
self._port = 3080
|
||||||
self._headless = False
|
self._headless = False
|
||||||
@ -30,6 +38,13 @@ class BaseGNS3VM:
|
|||||||
self._ram = 1024
|
self._ram = 1024
|
||||||
self._running = False
|
self._running = False
|
||||||
|
|
||||||
|
if sys.platform.startswith("win"):
|
||||||
|
config_path = os.path.join(os.path.expandvars("%APPDATA%"), "GNS3")
|
||||||
|
else:
|
||||||
|
config_path = os.path.join(os.path.expanduser("~"), ".config", "GNS3")
|
||||||
|
self._config_file = os.path.join(config_path, "gns3_vm.conf")
|
||||||
|
self.load()
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
|
|
||||||
settings = {"vmname": self._vmname,
|
settings = {"vmname": self._vmname,
|
||||||
@ -38,10 +53,41 @@ class BaseGNS3VM:
|
|||||||
"headless": self._headless,
|
"headless": self._headless,
|
||||||
"vcpus": self._vcpus,
|
"vcpus": self._vcpus,
|
||||||
"ram": self._ram,
|
"ram": self._ram,
|
||||||
|
"auto_start": self._auto_start,
|
||||||
|
"auto_stop": self._auto_stop,
|
||||||
"engine": self._engine}
|
"engine": self._engine}
|
||||||
|
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
"""
|
||||||
|
Reload the GNS3 VM configuration from disk
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not os.path.exists(self._config_file):
|
||||||
|
self.save()
|
||||||
|
try:
|
||||||
|
with open(self._config_file) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except OSError as e:
|
||||||
|
log.critical("Cannot load %s: %s", self._config_file, str(e))
|
||||||
|
return
|
||||||
|
if "gns3vm" in data:
|
||||||
|
for name, value in data["gns3vm"].items():
|
||||||
|
if hasattr(self, name) and getattr(self, name) != value:
|
||||||
|
log.debug("GNS3 VM: set {} to {}".format(name, value))
|
||||||
|
setattr(self, name, value)
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
"""
|
||||||
|
Save the GNS3 VM configuration on disk
|
||||||
|
"""
|
||||||
|
|
||||||
|
data = {"gns3vm": self.__json__()}
|
||||||
|
os.makedirs(os.path.dirname(self._config_file), exist_ok=True)
|
||||||
|
with open(self._config_file, 'w+') as f:
|
||||||
|
json.dump(data, f, indent=4)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def vmname(self):
|
def vmname(self):
|
||||||
"""
|
"""
|
||||||
@ -175,13 +221,53 @@ class BaseGNS3VM:
|
|||||||
@ram.setter
|
@ram.setter
|
||||||
def ram(self, new_ram):
|
def ram(self, new_ram):
|
||||||
"""
|
"""
|
||||||
Sets the the amount of allocated RAM.
|
Sets the amount of allocated RAM.
|
||||||
|
|
||||||
:param new_ram: new amount of RAM.
|
:param new_ram: new amount of RAM.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._ram = new_ram
|
self._ram = new_ram
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auto_start(self):
|
||||||
|
"""
|
||||||
|
Returns whether the VM should automatically be started when GNS3 is launched
|
||||||
|
|
||||||
|
:returns: boolean
|
||||||
|
"""
|
||||||
|
|
||||||
|
return self._auto_start
|
||||||
|
|
||||||
|
@auto_start.setter
|
||||||
|
def auto_start(self, new_auto_start):
|
||||||
|
"""
|
||||||
|
Set whether the VM should automatically be started when GNS3 is launched
|
||||||
|
|
||||||
|
:param new_auto_start: boolean
|
||||||
|
"""
|
||||||
|
|
||||||
|
self._auto_start = new_auto_start
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auto_stop(self):
|
||||||
|
"""
|
||||||
|
Returns whether the VM should automatically be started when GNS3 is launched
|
||||||
|
|
||||||
|
:returns: boolean
|
||||||
|
"""
|
||||||
|
|
||||||
|
return self._auto_start
|
||||||
|
|
||||||
|
@auto_stop.setter
|
||||||
|
def auto_stop(self, new_auto_stop):
|
||||||
|
"""
|
||||||
|
Set whether the VM should automatically be stopped when GNS3 is launched
|
||||||
|
|
||||||
|
:param new_auto_stop: boolean
|
||||||
|
"""
|
||||||
|
|
||||||
|
self._auto_stop = new_auto_stop
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def engine(self):
|
def engine(self):
|
||||||
"""
|
"""
|
||||||
|
@ -73,6 +73,7 @@ class GNS3VMHandler:
|
|||||||
for name, value in request.json.items():
|
for name, value in request.json.items():
|
||||||
if hasattr(gns3_vm, name) and getattr(gns3_vm, name) != value:
|
if hasattr(gns3_vm, name) and getattr(gns3_vm, name) != value:
|
||||||
setattr(gns3_vm, name, value)
|
setattr(gns3_vm, name, value)
|
||||||
|
gns3_vm.save()
|
||||||
response.json(gns3_vm)
|
response.json(gns3_vm)
|
||||||
|
|
||||||
@Route.post(
|
@Route.post(
|
||||||
|
Loading…
Reference in New Issue
Block a user