Merge branch 'profil_support' into 2.0

This commit is contained in:
Julien Duponchelle 2016-08-23 10:06:40 +02:00
commit ab38edf7ca
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
3 changed files with 48 additions and 12 deletions

View File

@ -30,18 +30,19 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class Config(object): class Config:
""" """
Configuration file management using configparser. Configuration file management using configparser.
:params files: Array of configuration files (optional) :param files: Array of configuration files (optional)
:params config_directory: Path of the configuration directory. If None default OS directory :param profil: Profil settings (default use standard settings file)
""" """
def __init__(self, files=None, config_directory=None): def __init__(self, files=None, profil=None):
self._files = files self._files = files
self._profil = profil
# Monitor configuration files for changes # Monitor configuration files for changes
self._watched_files = {} self._watched_files = {}
@ -61,10 +62,16 @@ class Config(object):
appdata = os.path.expandvars("%APPDATA%") appdata = os.path.expandvars("%APPDATA%")
common_appdata = os.path.expandvars("%COMMON_APPDATA%") common_appdata = os.path.expandvars("%COMMON_APPDATA%")
if self._profil:
user_dir = os.path.join(appdata, appname, "profiles", self._profil)
else:
user_dir = os.path.join(appdata, appname)
filename = "gns3_server.ini" filename = "gns3_server.ini"
if self._files is None: if self._files is None:
self._files = [os.path.join(os.getcwd(), filename), self._files = [os.path.join(os.getcwd(), filename),
os.path.join(appdata, appname, filename), os.path.join(user_dir, filename),
os.path.join(appdata, appname + ".ini"), os.path.join(appdata, appname + ".ini"),
os.path.join(common_appdata, appname, filename), os.path.join(common_appdata, appname, filename),
os.path.join(common_appdata, appname + ".ini")] os.path.join(common_appdata, appname + ".ini")]
@ -80,9 +87,15 @@ class Config(object):
appname = "GNS3" appname = "GNS3"
home = os.path.expanduser("~") home = os.path.expanduser("~")
filename = "gns3_server.conf" filename = "gns3_server.conf"
if self._profil:
user_dir = os.path.join(home, ".config", appname, "profiles", self._profil)
else:
user_dir = os.path.join(home, ".config", appname)
if self._files is None: if self._files is None:
self._files = [os.path.join(os.getcwd(), filename), self._files = [os.path.join(os.getcwd(), filename),
os.path.join(home, ".config", appname, filename), os.path.join(user_dir, filename),
os.path.join(home, ".config", appname + ".conf"), os.path.join(home, ".config", appname + ".conf"),
os.path.join("/etc/gns3", filename), os.path.join("/etc/gns3", filename),
os.path.join("/etc/xdg", appname, filename), os.path.join("/etc/xdg", appname, filename),
@ -93,6 +106,13 @@ class Config(object):
self.clear() self.clear()
self._watch_config_file() self._watch_config_file()
@property
def profil(self):
"""
Settings profil
"""
return self._profil
def clear(self): def clear(self):
"""Restart with a clean config""" """Restart with a clean config"""
self._config = configparser.RawConfigParser() self._config = configparser.RawConfigParser()
@ -193,16 +213,15 @@ class Config(object):
self.set_section_config(section, conf) self.set_section_config(section, conf)
@staticmethod @staticmethod
def instance(files=None): def instance(*args, **kwargs):
""" """
Singleton to return only one instance of Config. Singleton to return only one instance of Config.
:params files: Array of configuration files (optional)
:returns: instance of Config :returns: instance of Config
""" """
if not hasattr(Config, "_instance") or Config._instance is None: if not hasattr(Config, "_instance") or Config._instance is None:
Config._instance = Config(files) Config._instance = Config(*args, **kwargs)
return Config._instance return Config._instance
@staticmethod @staticmethod

View File

@ -48,9 +48,23 @@ class Controller:
config_path = os.path.join(os.path.expandvars("%APPDATA%"), "GNS3") config_path = os.path.join(os.path.expandvars("%APPDATA%"), "GNS3")
else: else:
config_path = os.path.join(os.path.expanduser("~"), ".config", "GNS3") config_path = os.path.join(os.path.expanduser("~"), ".config", "GNS3")
self._config_file = os.path.join(config_path, "gns3_controller.conf")
server_config = Config.instance().get_section_config("Server") server_config = Config.instance().get_section_config("Server")
if Config.instance().profil:
config_path = os.path.join(config_path, "profiles", Config.instance().profil)
self._config_file = os.path.join(config_path, "gns3_controller.conf")
log.info("Load controller configuration file {}".format(self._config_file))
if server_config.getboolean("local", False) is True:
self._computes["local"] = Compute(compute_id="local",
controller=self,
protocol=server_config.get("protocol", "http"),
host=server_config.get("host", "localhost"),
port=server_config.getint("port", 3080),
user=server_config.get("user", ""),
password=server_config.get("password", ""))
self._computes["local"] = Compute(compute_id="local", self._computes["local"] = Compute(compute_id="local",
controller=self, controller=self,
protocol=server_config.get("protocol", "http"), protocol=server_config.get("protocol", "http"),

View File

@ -106,10 +106,13 @@ def parse_arguments(argv):
parser.add_argument("--log", help="send output to logfile instead of console") parser.add_argument("--log", help="send output to logfile instead of console")
parser.add_argument("--daemon", action="store_true", help="start as a daemon") parser.add_argument("--daemon", action="store_true", help="start as a daemon")
parser.add_argument("--pid", help="store process pid") parser.add_argument("--pid", help="store process pid")
parser.add_argument("--profil", help="Settings profil (blank will use default settings files)")
args = parser.parse_args(argv) args = parser.parse_args(argv)
if args.config: if args.config:
Config.instance(files=[args.config]) Config.instance(files=[args.config], profil=args.profil)
else:
Config.instance(profil=args.profil)
config = Config.instance().get_section_config("Server") config = Config.instance().get_section_config("Server")
defaults = { defaults = {
@ -126,7 +129,7 @@ def parse_arguments(argv):
"quiet": config.getboolean("quiet", False), "quiet": config.getboolean("quiet", False),
"debug": config.getboolean("debug", False), "debug": config.getboolean("debug", False),
"logfile": config.getboolean("logfile", ""), "logfile": config.getboolean("logfile", ""),
"server_discovery": config.getboolean("server_discovery", False), "server_discovery": config.getboolean("server_discovery", False)
} }
parser.set_defaults(**defaults) parser.set_defaults(**defaults)