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/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Reads the configuration file and store the settings for the server & modules.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import configparser
|
2015-02-02 14:01:48 +00:00
|
|
|
import asyncio
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2014-10-28 17:01:17 +00:00
|
|
|
CLOUD_SERVER = 'CLOUD_SERVER'
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
class Config(object):
|
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
|
|
|
|
2015-02-08 21:44:56 +00:00
|
|
|
:params files: Array of configuration files (optional)
|
2014-03-11 21:45:04 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-02 14:01:48 +00:00
|
|
|
def __init__(self, files=None):
|
|
|
|
|
|
|
|
self._files = files
|
2015-02-02 14:08:46 +00:00
|
|
|
|
|
|
|
# Monitor configuration files for changes
|
2015-02-02 14:01:48 +00:00
|
|
|
self._watched_files = {}
|
2014-03-11 21:45:04 +00:00
|
|
|
|
2015-02-08 21:44:56 +00:00
|
|
|
# Override config from command line even if we modify the config file and live reload it.
|
2015-02-02 14:08:46 +00:00
|
|
|
self._override_config = {}
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
|
2015-02-02 14:01:48 +00:00
|
|
|
appname = "GNS3"
|
|
|
|
|
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%")
|
2015-03-10 17:00:32 +00:00
|
|
|
filename = "gns3_server.ini"
|
2015-02-02 14:01:48 +00:00
|
|
|
if self._files is None:
|
|
|
|
self._files = [os.path.join(appdata, appname, filename),
|
|
|
|
os.path.join(appdata, appname + ".ini"),
|
|
|
|
os.path.join(common_appdata, appname, filename),
|
|
|
|
os.path.join(common_appdata, appname + ".ini"),
|
2015-03-05 22:15:06 +00:00
|
|
|
filename]
|
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
|
|
|
|
# 5: server.conf in the current working directory
|
|
|
|
|
2015-02-02 20:13:56 +00:00
|
|
|
if sys.platform.startswith("darwin"):
|
|
|
|
appname = "gns3.net"
|
|
|
|
else:
|
|
|
|
appname = "GNS3"
|
2014-03-11 21:45:04 +00:00
|
|
|
home = os.path.expanduser("~")
|
2015-03-10 17:00:32 +00:00
|
|
|
filename = "gns3_server.conf"
|
2015-02-02 14:01:48 +00:00
|
|
|
if self._files is None:
|
|
|
|
self._files = [os.path.join(home, ".config", appname, filename),
|
|
|
|
os.path.join(home, ".config", appname + ".conf"),
|
|
|
|
os.path.join("/etc/xdg", appname, filename),
|
|
|
|
os.path.join("/etc/xdg", appname + ".conf"),
|
2015-03-05 22:15:06 +00:00
|
|
|
filename]
|
2014-03-11 21:45:04 +00:00
|
|
|
|
|
|
|
self._config = configparser.ConfigParser()
|
|
|
|
self.read_config()
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parsed_files = self._config.read(self._files)
|
|
|
|
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:
|
|
|
|
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
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
@staticmethod
|
2015-02-13 16:34:22 +00:00
|
|
|
def instance(files=None):
|
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
|
|
|
|
2015-02-12 02:21:34 +00:00
|
|
|
:params files: Array of configuration files (optional)
|
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:
|
|
|
|
Config._instance = Config(files)
|
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
|