2014-03-11 21:45:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-01-14 00:05:26 +00:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2014-03-11 21:45:04 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""
|
2016-04-15 15:57:06 +00:00
|
|
|
Reads the configuration file and store the settings for the controller & compute.
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2019-04-01 08:53:39 +00:00
|
|
|
import shutil
|
2014-03-11 21:45:04 +00:00
|
|
|
import configparser
|
|
|
|
|
2019-04-01 08:53:39 +00:00
|
|
|
from .version import __version_info__
|
2016-06-10 15:51:19 +00:00
|
|
|
from .utils.file_watcher import FileWatcher
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-08-22 15:21:03 +00:00
|
|
|
class Config:
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
|
|
|
Configuration file management using configparser.
|
2015-02-02 14:01:48 +00:00
|
|
|
|
2016-08-22 15:21:03 +00:00
|
|
|
:param files: Array of configuration files (optional)
|
2016-09-07 18:38:24 +00:00
|
|
|
:param profile: Profile settings (default use standard settings file)
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
|
|
|
|
2016-09-07 15:44:51 +00:00
|
|
|
def __init__(self, files=None, profile=None):
|
2015-02-02 14:01:48 +00:00
|
|
|
|
|
|
|
self._files = files
|
2016-09-07 15:44:51 +00:00
|
|
|
self._profile = profile
|
2016-09-30 15:34:28 +00:00
|
|
|
if files and len(files):
|
2019-11-10 19:41:39 +00:00
|
|
|
directory_name = os.path.dirname(files[0])
|
|
|
|
if not directory_name or directory_name == "":
|
2019-11-11 10:20:51 +00:00
|
|
|
files[0] = os.path.dirname(os.path.abspath(files[0])) + os.path.sep + files[0]
|
2016-09-30 15:34:28 +00:00
|
|
|
self._main_config_file = files[0]
|
|
|
|
else:
|
|
|
|
self._main_config_file = None
|
2015-02-02 14:08:46 +00:00
|
|
|
|
|
|
|
# Monitor configuration files for changes
|
2015-02-02 14:01:48 +00:00
|
|
|
self._watched_files = {}
|
2017-03-22 17:29:08 +00:00
|
|
|
self._watch_callback = []
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2019-04-01 08:53:39 +00:00
|
|
|
appname = "GNS3"
|
2019-04-14 14:31:40 +00:00
|
|
|
version = "{}.{}".format(__version_info__[0], __version_info__[1])
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2019-04-01 08:53:39 +00:00
|
|
|
if sys.platform.startswith("win"):
|
2015-02-02 14:01:48 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
# On windows, the configuration file location can be one of the following:
|
2015-03-10 17:00:32 +00:00
|
|
|
# 1: %APPDATA%/GNS3/gns3_server.ini
|
2014-03-11 21:45:04 +00:00
|
|
|
# 2: %APPDATA%/GNS3.ini
|
2015-03-10 17:00:32 +00:00
|
|
|
# 3: %COMMON_APPDATA%/GNS3/gns3_server.ini
|
2014-03-11 21:45:04 +00:00
|
|
|
# 4: %COMMON_APPDATA%/GNS3.ini
|
|
|
|
# 5: server.ini in the current working directory
|
|
|
|
|
|
|
|
appdata = os.path.expandvars("%APPDATA%")
|
|
|
|
common_appdata = os.path.expandvars("%COMMON_APPDATA%")
|
2016-08-22 15:21:03 +00:00
|
|
|
|
2016-09-07 15:44:51 +00:00
|
|
|
if self._profile:
|
2019-04-01 08:53:39 +00:00
|
|
|
legacy_user_dir = os.path.join(appdata, appname, "profiles", self._profile)
|
2019-10-09 07:20:19 +00:00
|
|
|
versioned_user_dir = os.path.join(appdata, appname, version, "profiles", self._profile)
|
2016-08-22 15:21:03 +00:00
|
|
|
else:
|
2019-04-01 08:53:39 +00:00
|
|
|
legacy_user_dir = os.path.join(appdata, appname)
|
2019-04-05 10:44:31 +00:00
|
|
|
versioned_user_dir = os.path.join(appdata, appname, version)
|
2019-04-01 08:53:39 +00:00
|
|
|
|
|
|
|
server_filename = "gns3_server.ini"
|
|
|
|
controller_filename = "gns3_controller.ini"
|
|
|
|
|
2019-04-05 10:44:31 +00:00
|
|
|
# move gns3_controller.conf to gns3_controller.ini (file was renamed in 2.2.0 on Windows)
|
2019-04-01 08:53:39 +00:00
|
|
|
old_controller_filename = os.path.join(legacy_user_dir, "gns3_controller.conf")
|
|
|
|
if os.path.exists(old_controller_filename):
|
|
|
|
try:
|
|
|
|
shutil.copyfile(old_controller_filename, os.path.join(legacy_user_dir, controller_filename))
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Cannot move old controller configuration file: {}".format(e))
|
2016-08-22 15:21:03 +00:00
|
|
|
|
2016-09-30 15:34:28 +00:00
|
|
|
if self._files is None and not hasattr(sys, "_called_from_test"):
|
2019-04-01 08:53:39 +00:00
|
|
|
self._files = [os.path.join(os.getcwd(), server_filename),
|
|
|
|
os.path.join(versioned_user_dir, server_filename),
|
2015-02-02 14:01:48 +00:00
|
|
|
os.path.join(appdata, appname + ".ini"),
|
2019-04-01 08:53:39 +00:00
|
|
|
os.path.join(common_appdata, appname, server_filename),
|
2015-07-03 08:53:41 +00:00
|
|
|
os.path.join(common_appdata, appname + ".ini")]
|
2014-03-11 21:45:04 +00:00
|
|
|
else:
|
|
|
|
|
|
|
|
# On UNIX-like platforms, the configuration file location can be one of the following:
|
2015-03-10 17:00:32 +00:00
|
|
|
# 1: $HOME/.config/GNS3/gns3_server.conf
|
2014-03-11 21:45:04 +00:00
|
|
|
# 2: $HOME/.config/GNS3.conf
|
2015-03-10 17:00:32 +00:00
|
|
|
# 3: /etc/xdg/GNS3/gns3_server.conf
|
2014-03-11 21:45:04 +00:00
|
|
|
# 4: /etc/xdg/GNS3.conf
|
2016-02-25 14:07:55 +00:00
|
|
|
# 5: gns3_server.conf in the current working directory
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
home = os.path.expanduser("~")
|
2019-04-01 08:53:39 +00:00
|
|
|
server_filename = "gns3_server.conf"
|
|
|
|
controller_filename = "gns3_controller.conf"
|
2016-08-22 15:21:03 +00:00
|
|
|
|
2016-09-07 15:44:51 +00:00
|
|
|
if self._profile:
|
2019-04-01 08:53:39 +00:00
|
|
|
legacy_user_dir = os.path.join(home, ".config", appname, "profiles", self._profile)
|
2019-10-09 07:20:19 +00:00
|
|
|
versioned_user_dir = os.path.join(home, ".config", appname, version, "profiles", self._profile)
|
2016-08-22 15:21:03 +00:00
|
|
|
else:
|
2019-04-01 08:53:39 +00:00
|
|
|
legacy_user_dir = os.path.join(home, ".config", appname)
|
2019-04-05 10:44:31 +00:00
|
|
|
versioned_user_dir = os.path.join(home, ".config", appname, version)
|
2016-08-22 15:21:03 +00:00
|
|
|
|
2016-09-30 15:34:28 +00:00
|
|
|
if self._files is None and not hasattr(sys, "_called_from_test"):
|
2019-04-01 08:53:39 +00:00
|
|
|
self._files = [os.path.join(os.getcwd(), server_filename),
|
|
|
|
os.path.join(versioned_user_dir, server_filename),
|
2015-02-02 14:01:48 +00:00
|
|
|
os.path.join(home, ".config", appname + ".conf"),
|
2019-04-01 08:53:39 +00:00
|
|
|
os.path.join("/etc/gns3", server_filename),
|
|
|
|
os.path.join("/etc/xdg", appname, server_filename),
|
2015-07-03 08:53:41 +00:00
|
|
|
os.path.join("/etc/xdg", appname + ".conf")]
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2015-06-09 14:25:36 +00:00
|
|
|
if self._files is None:
|
|
|
|
self._files = []
|
2016-09-30 15:34:28 +00:00
|
|
|
|
|
|
|
if self._main_config_file is None:
|
2019-04-01 08:53:39 +00:00
|
|
|
|
2019-05-19 10:59:00 +00:00
|
|
|
# TODO: migrate versioned config file from a previous version of GNS3 (for instance 2.2 -> 2.3) + support profiles
|
2019-04-05 10:44:31 +00:00
|
|
|
# migrate post version 2.2.0 config files if they exist
|
2019-04-01 08:53:39 +00:00
|
|
|
os.makedirs(versioned_user_dir, exist_ok=True)
|
|
|
|
try:
|
|
|
|
# migrate the server config file
|
|
|
|
old_server_config = os.path.join(legacy_user_dir, server_filename)
|
2019-04-14 13:39:55 +00:00
|
|
|
new_server_config = os.path.join(versioned_user_dir, server_filename)
|
|
|
|
if not os.path.exists(new_server_config) and os.path.exists(old_server_config):
|
2019-04-05 10:44:31 +00:00
|
|
|
shutil.copyfile(old_server_config, new_server_config)
|
|
|
|
|
2019-04-01 08:53:39 +00:00
|
|
|
# migrate the controller config file
|
|
|
|
old_controller_config = os.path.join(legacy_user_dir, controller_filename)
|
2019-04-14 13:39:55 +00:00
|
|
|
new_controller_config = os.path.join(versioned_user_dir, controller_filename)
|
|
|
|
if not os.path.exists(new_controller_config) and os.path.exists(old_controller_config):
|
|
|
|
shutil.copyfile(old_controller_config, os.path.join(versioned_user_dir, new_controller_config))
|
2019-04-01 08:53:39 +00:00
|
|
|
except OSError as e:
|
|
|
|
log.error("Cannot migrate old config files: {}".format(e))
|
|
|
|
|
|
|
|
self._main_config_file = os.path.join(versioned_user_dir, server_filename)
|
2016-09-30 15:34:28 +00:00
|
|
|
for file in self._files:
|
|
|
|
if os.path.exists(file):
|
|
|
|
self._main_config_file = file
|
|
|
|
break
|
|
|
|
|
2015-03-17 14:40:58 +00:00
|
|
|
self.clear()
|
|
|
|
self._watch_config_file()
|
|
|
|
|
2017-03-22 17:29:08 +00:00
|
|
|
def listen_for_config_changes(self, callback):
|
|
|
|
"""
|
|
|
|
Call the callback when the configuration file change
|
|
|
|
"""
|
|
|
|
self._watch_callback.append(callback)
|
|
|
|
|
2016-08-22 15:21:03 +00:00
|
|
|
@property
|
2016-09-07 15:44:51 +00:00
|
|
|
def profile(self):
|
2016-08-22 15:21:03 +00:00
|
|
|
"""
|
2016-09-07 15:44:51 +00:00
|
|
|
Settings profile
|
2016-08-22 15:21:03 +00:00
|
|
|
"""
|
2016-09-07 15:44:51 +00:00
|
|
|
return self._profile
|
2016-08-22 15:21:03 +00:00
|
|
|
|
2016-09-30 15:34:28 +00:00
|
|
|
@property
|
|
|
|
def config_dir(self):
|
2019-04-01 08:53:39 +00:00
|
|
|
|
2016-09-30 15:34:28 +00:00
|
|
|
return os.path.dirname(self._main_config_file)
|
|
|
|
|
2019-04-01 08:53:39 +00:00
|
|
|
@property
|
|
|
|
def controller_config(self):
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
controller_config_filename = "gns3_controller.ini"
|
|
|
|
else:
|
|
|
|
controller_config_filename = "gns3_controller.conf"
|
|
|
|
return os.path.join(self.config_dir, controller_config_filename)
|
|
|
|
|
2015-03-17 14:40:58 +00:00
|
|
|
def clear(self):
|
|
|
|
"""Restart with a clean config"""
|
2015-05-05 09:53:33 +00:00
|
|
|
self._config = configparser.RawConfigParser()
|
2015-03-17 14:40:58 +00:00
|
|
|
# Override config from command line even if we modify the config file and live reload it.
|
|
|
|
self._override_config = {}
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
self.read_config()
|
2015-03-17 09:21:52 +00:00
|
|
|
|
|
|
|
def _watch_config_file(self):
|
2016-06-10 15:51:19 +00:00
|
|
|
for file in self._files:
|
2018-01-08 11:07:15 +00:00
|
|
|
if os.path.exists(file):
|
|
|
|
self._watched_files[file] = FileWatcher(file, self._config_file_change)
|
2015-03-17 09:21:52 +00:00
|
|
|
|
2016-06-10 15:51:19 +00:00
|
|
|
def _config_file_change(self, path):
|
|
|
|
self.read_config()
|
|
|
|
for section in self._override_config:
|
|
|
|
self.set_section_config(section, self._override_config[section])
|
2017-03-22 17:29:08 +00:00
|
|
|
for callback in self._watch_callback:
|
|
|
|
callback()
|
2015-02-02 14:01:48 +00:00
|
|
|
|
2015-03-02 19:46:05 +00:00
|
|
|
def reload(self):
|
2015-02-02 14:01:48 +00:00
|
|
|
"""
|
2015-03-02 19:46:05 +00:00
|
|
|
Reload configuration
|
2015-02-02 14:01:48 +00:00
|
|
|
"""
|
|
|
|
|
2015-03-02 19:46:05 +00:00
|
|
|
self.read_config()
|
2015-02-02 14:08:46 +00:00
|
|
|
for section in self._override_config:
|
|
|
|
self.set_section_config(section, self._override_config[section])
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2015-02-28 02:35:31 +00:00
|
|
|
def get_config_files(self):
|
|
|
|
return self._watched_files
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
def read_config(self):
|
|
|
|
"""
|
|
|
|
Read the configuration files.
|
|
|
|
"""
|
|
|
|
|
2015-05-05 09:44:35 +00:00
|
|
|
try:
|
|
|
|
parsed_files = self._config.read(self._files, encoding="utf-8")
|
|
|
|
except configparser.Error as e:
|
|
|
|
log.error("Can't parse configuration file: %s", str(e))
|
|
|
|
return
|
2014-03-11 21:45:04 +00:00
|
|
|
if not parsed_files:
|
2015-02-02 09:49:46 +00:00
|
|
|
log.warning("No configuration file could be found or read")
|
2015-02-02 14:01:48 +00:00
|
|
|
else:
|
|
|
|
for file in parsed_files:
|
|
|
|
log.info("Load configuration file {}".format(file))
|
|
|
|
self._watched_files[file] = os.stat(file).st_mtime
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
def get_default_section(self):
|
|
|
|
"""
|
|
|
|
Get the default configuration section.
|
|
|
|
|
|
|
|
:returns: configparser section
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._config["DEFAULT"]
|
|
|
|
|
|
|
|
def get_section_config(self, section):
|
|
|
|
"""
|
|
|
|
Get a specific configuration section.
|
|
|
|
Returns the default section if none can be found.
|
|
|
|
|
|
|
|
:returns: configparser section
|
|
|
|
"""
|
|
|
|
|
2015-01-20 13:31:47 +00:00
|
|
|
if section not in self._config:
|
2014-03-11 21:45:04 +00:00
|
|
|
return self._config["DEFAULT"]
|
|
|
|
return self._config[section]
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
def set_section_config(self, section, content):
|
|
|
|
"""
|
|
|
|
Set a specific configuration section. It's not
|
|
|
|
dumped on the disk.
|
|
|
|
|
|
|
|
:param section: Section name
|
2015-01-23 20:01:23 +00:00
|
|
|
:param content: A dictionary with section content
|
2015-01-23 16:39:17 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-02 14:01:48 +00:00
|
|
|
if not self._config.has_section(section):
|
|
|
|
self._config.add_section(section)
|
|
|
|
for key in content:
|
2015-03-16 14:03:41 +00:00
|
|
|
if isinstance(content[key], bool):
|
|
|
|
content[key] = str(content[key]).lower()
|
2015-02-02 14:01:48 +00:00
|
|
|
self._config.set(section, key, content[key])
|
2015-02-02 14:08:46 +00:00
|
|
|
self._override_config[section] = content
|
2015-01-23 16:39:17 +00:00
|
|
|
|
2015-03-16 14:03:41 +00:00
|
|
|
def set(self, section, key, value):
|
|
|
|
"""
|
|
|
|
Set a config value.
|
|
|
|
It's not dumped on the disk.
|
|
|
|
|
|
|
|
If the section doesn't exists the section is created
|
|
|
|
"""
|
|
|
|
|
2015-03-17 14:40:58 +00:00
|
|
|
conf = self.get_section_config(section)
|
|
|
|
if isinstance(value, bool):
|
|
|
|
conf[key] = str(value)
|
|
|
|
else:
|
|
|
|
conf[key] = value
|
|
|
|
self.set_section_config(section, conf)
|
2015-03-16 14:03:41 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
@staticmethod
|
2016-08-22 15:21:03 +00:00
|
|
|
def instance(*args, **kwargs):
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
2015-03-13 00:44:05 +00:00
|
|
|
Singleton to return only one instance of Config.
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
:returns: instance of Config
|
|
|
|
"""
|
|
|
|
|
2015-02-13 10:15:11 +00:00
|
|
|
if not hasattr(Config, "_instance") or Config._instance is None:
|
2016-08-22 15:21:03 +00:00
|
|
|
Config._instance = Config(*args, **kwargs)
|
2014-03-11 21:45:04 +00:00
|
|
|
return Config._instance
|
2015-02-13 10:15:11 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reset():
|
|
|
|
"""
|
|
|
|
Reset singleton
|
|
|
|
"""
|
|
|
|
|
|
|
|
Config._instance = None
|