Use importlib_resources instead of pkg_resources and install built-in appliances in config dir.

This commit is contained in:
grossmj committed 2022-11-07 20:12:03 +08:00
1 parent f04702d607
commit a4b24eaceb
4 files changed
+53 -37

No files matched your search

+11 -9
View File
@@ -22,6 +22,7 @@ import uuid
import socket
import shutil
import aiohttp
import importlib_resources
from ..config import Config
from .project import Project
@@ -35,7 +36,6 @@ from .symbols import Symbols
from ..version import __version__
from .topology import load_topology
from .gns3vm import GNS3VM
from ..utils.get_resource import get_resource
from .gns3vm.gns3_vm_error import GNS3VMError
import logging
@@ -65,7 +65,7 @@ class Controller:
async def start(self):
log.info("Controller is starting")
self.load_base_files()
self._load_base_files()
server_config = Config.instance().get_section_config("Server")
Config.instance().listen_for_config_changes(self._update_config)
host = server_config.get("host", "localhost")
@@ -242,6 +242,7 @@ class Controller:
if "iou_license" in controller_settings:
self._iou_license_settings = controller_settings["iou_license"]
self._appliance_manager.install_builtin_appliances()
self._appliance_manager.appliances_etag = controller_settings.get("appliances_etag")
self._appliance_manager.load_appliances()
self._template_manager.load_templates(controller_settings.get("templates"))
@@ -269,20 +270,21 @@ class Controller:
except OSError as e:
log.error(str(e))
def load_base_files(self):
def _load_base_files(self):
"""
At startup we copy base file to the user location to allow
them to customize it
"""
dst_path = self.configs_path()
src_path = get_resource('configs')
try:
for file in os.listdir(src_path):
if not os.path.exists(os.path.join(dst_path, file)):
shutil.copy(os.path.join(src_path, file), os.path.join(dst_path, file))
except OSError:
pass
for entry in importlib_resources.files('gns3server.configs').iterdir():
full_path = os.path.join(dst_path, entry.name)
if entry.is_file() and not os.path.exists(full_path):
log.debug(f"Installing base config file {entry.name} to {full_path}")
shutil.copy(str(entry), os.path.join(dst_path, entry.name))
except OSError as e:
log.error(f"Could not install base config files to {dst_path}: {e}")
def images_path(self):
"""