mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-25 00:08:11 +00:00
Fix template migration issues from GUI to controller. Fixes https://github.com/GNS3/gns3-gui/issues/2803
This commit is contained in:
parent
61c87e57a4
commit
06ce0868ec
@ -175,7 +175,6 @@ class Controller:
|
||||
|
||||
try:
|
||||
if not os.path.exists(self._config_file):
|
||||
await self._import_gns3_gui_conf()
|
||||
self._config_loaded = True
|
||||
self.save()
|
||||
with open(self._config_file) as f:
|
||||
@ -254,118 +253,6 @@ class Controller:
|
||||
os.makedirs(images_path, exist_ok=True)
|
||||
return images_path
|
||||
|
||||
async def _import_gns3_gui_conf(self):
|
||||
"""
|
||||
Import old config from GNS3 GUI
|
||||
"""
|
||||
|
||||
config_file = os.path.join(os.path.dirname(self._config_file), "gns3_gui.conf")
|
||||
if os.path.exists(config_file):
|
||||
with open(config_file) as f:
|
||||
settings = json.load(f)
|
||||
server_settings = settings.get("Servers", {})
|
||||
for remote in server_settings.get("remote_servers", []):
|
||||
try:
|
||||
await self.add_compute(host=remote.get("host", "localhost"),
|
||||
port=remote.get("port", 3080),
|
||||
protocol=remote.get("protocol", "http"),
|
||||
name=remote.get("url"),
|
||||
user=remote.get("user"),
|
||||
password=remote.get("password"))
|
||||
except aiohttp.web.HTTPConflict:
|
||||
pass # if the server is broken we skip it
|
||||
if "vm" in server_settings:
|
||||
vmname = None
|
||||
vm_settings = server_settings["vm"]
|
||||
if vm_settings["virtualization"] == "VMware":
|
||||
engine = "vmware"
|
||||
vmname = vm_settings.get("vmname", "")
|
||||
elif vm_settings["virtualization"] == "VirtualBox":
|
||||
engine = "virtualbox"
|
||||
vmname = vm_settings.get("vmname", "")
|
||||
else:
|
||||
engine = "remote"
|
||||
# In case of remote server we match the compute with url parameter
|
||||
for compute in self._computes.values():
|
||||
if compute.host == vm_settings.get("remote_vm_host") and compute.port == vm_settings.get("remote_vm_port"):
|
||||
vmname = compute.name
|
||||
|
||||
if vm_settings.get("auto_stop", True):
|
||||
when_exit = "stop"
|
||||
else:
|
||||
when_exit = "keep"
|
||||
|
||||
self.gns3vm.settings = {
|
||||
"engine": engine,
|
||||
"enable": vm_settings.get("auto_start", False),
|
||||
"when_exit": when_exit,
|
||||
"headless": vm_settings.get("headless", False),
|
||||
"vmname": vmname
|
||||
}
|
||||
|
||||
vms = []
|
||||
for vm in settings.get("Qemu", {}).get("vms", []):
|
||||
vm["template_type"] = "qemu"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("IOU", {}).get("devices", []):
|
||||
vm["template_type"] = "iou"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Docker", {}).get("containers", []):
|
||||
vm["template_type"] = "docker"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("cloud_nodes", []):
|
||||
vm["template_type"] = "cloud"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("ethernet_switches", []):
|
||||
vm["template_type"] = "ethernet_switch"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Builtin", {}).get("ethernet_hubs", []):
|
||||
vm["template_type"] = "ethernet_hub"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("Dynamips", {}).get("routers", []):
|
||||
vm["template_type"] = "dynamips"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VMware", {}).get("vms", []):
|
||||
vm["template_type"] = "vmware"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VirtualBox", {}).get("vms", []):
|
||||
vm["template_type"] = "virtualbox"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("VPCS", {}).get("nodes", []):
|
||||
vm["template_type"] = "vpcs"
|
||||
vms.append(vm)
|
||||
for vm in settings.get("TraceNG", {}).get("nodes", []):
|
||||
vm["template_type"] = "traceng"
|
||||
vms.append(vm)
|
||||
|
||||
for vm in vms:
|
||||
# remove deprecated properties
|
||||
for prop in vm.copy():
|
||||
if prop in ["enable_remote_console", "use_ubridge", "acpi_shutdown"]:
|
||||
del vm[prop]
|
||||
|
||||
# remove deprecated default_symbol and hover_symbol
|
||||
# and set symbol if not present
|
||||
deprecated = ["default_symbol", "hover_symbol"]
|
||||
if len([prop for prop in vm.keys() if prop in deprecated]) > 0:
|
||||
if "default_symbol" in vm.keys():
|
||||
del vm["default_symbol"]
|
||||
if "hover_symbol" in vm.keys():
|
||||
del vm["hover_symbol"]
|
||||
|
||||
if "symbol" not in vm.keys():
|
||||
vm["symbol"] = ":/symbols/computer.svg"
|
||||
|
||||
vm.setdefault("template_id", str(uuid.uuid4()))
|
||||
try:
|
||||
template = Template(vm["template_id"], vm)
|
||||
template.__json__() # Check if loaded without error
|
||||
self.template_manager.templates[template.id] = template
|
||||
except KeyError as e:
|
||||
# template data is not complete (missing name or type)
|
||||
log.warning("Cannot load template {} ('{}'): missing key {}".format(vm["template_id"], vm.get("name", "unknown"), e))
|
||||
continue
|
||||
|
||||
async def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs):
|
||||
"""
|
||||
Add a server to the dictionary of computes controlled by this controller
|
||||
|
@ -106,97 +106,6 @@ def test_import_computes_1_x(controller, controller_config_path, async_run):
|
||||
assert compute.password is None
|
||||
|
||||
|
||||
def test_import_gns3vm_1_x(controller, controller_config_path, async_run):
|
||||
"""
|
||||
At first start the server should import the
|
||||
gns3vm settings from the gns3_gui 1.X
|
||||
"""
|
||||
gns3_gui_conf = {
|
||||
"Servers": {
|
||||
"vm": {
|
||||
"adjust_local_server_ip": True,
|
||||
"auto_start": True,
|
||||
"auto_stop": False,
|
||||
"headless": True,
|
||||
"remote_vm_host": "",
|
||||
"remote_vm_password": "",
|
||||
"remote_vm_port": 3080,
|
||||
"remote_vm_protocol": "http",
|
||||
"remote_vm_url": "",
|
||||
"remote_vm_user": "",
|
||||
"virtualization": "VMware",
|
||||
"vmname": "GNS3 VM",
|
||||
"vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx"
|
||||
}
|
||||
}
|
||||
}
|
||||
config_dir = os.path.dirname(controller_config_path)
|
||||
os.makedirs(config_dir, exist_ok=True)
|
||||
with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f:
|
||||
json.dump(gns3_gui_conf, f)
|
||||
|
||||
controller.gns3vm.settings["engine"] = None
|
||||
async_run(controller._load_controller_settings())
|
||||
assert controller.gns3vm.settings["engine"] == "vmware"
|
||||
assert controller.gns3vm.settings["enable"]
|
||||
assert controller.gns3vm.settings["headless"]
|
||||
assert controller.gns3vm.settings["when_exit"] == "keep"
|
||||
assert controller.gns3vm.settings["vmname"] == "GNS3 VM"
|
||||
|
||||
|
||||
def test_import_remote_gns3vm_1_x(controller, controller_config_path, async_run):
|
||||
"""
|
||||
At first start the server should import the
|
||||
computes and remote GNS3 VM from the gns3_gui 1.X
|
||||
"""
|
||||
gns3_gui_conf = {
|
||||
"Servers": {
|
||||
"remote_servers": [
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"password": "",
|
||||
"port": 3080,
|
||||
"protocol": "http",
|
||||
"url": "http://127.0.0.1:3080",
|
||||
"user": ""
|
||||
},
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"password": "",
|
||||
"port": 3081,
|
||||
"protocol": "http",
|
||||
"url": "http://127.0.0.1:3081",
|
||||
"user": ""
|
||||
}
|
||||
],
|
||||
"vm": {
|
||||
"adjust_local_server_ip": True,
|
||||
"auto_start": True,
|
||||
"auto_stop": False,
|
||||
"headless": True,
|
||||
"remote_vm_host": "127.0.0.1",
|
||||
"remote_vm_password": "",
|
||||
"remote_vm_port": 3081,
|
||||
"remote_vm_protocol": "http",
|
||||
"remote_vm_url": "http://127.0.0.1:3081",
|
||||
"remote_vm_user": "",
|
||||
"virtualization": "remote",
|
||||
"vmname": "GNS3 VM",
|
||||
"vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx"
|
||||
}
|
||||
}
|
||||
}
|
||||
config_dir = os.path.dirname(controller_config_path)
|
||||
os.makedirs(config_dir, exist_ok=True)
|
||||
with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f:
|
||||
json.dump(gns3_gui_conf, f)
|
||||
|
||||
with asyncio_patch("gns3server.controller.compute.Compute.connect"):
|
||||
async_run(controller._load_controller_settings())
|
||||
assert controller.gns3vm.settings["engine"] == "remote"
|
||||
assert controller.gns3vm.settings["vmname"] == "http://127.0.0.1:3081"
|
||||
|
||||
|
||||
def test_load_projects(controller, projects_dir, async_run):
|
||||
controller.save()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user