Remember previous config, use them as defaults - closes #6
Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/95fd9915 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/95fd9915 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/95fd9915 Branch: refs/heads/master Commit: 95fd991593e4cbb4d626cf7a3a4e034573f1ed75 Parents: 4061611 Author: BroganD1993 <darrenbro...@hotmail.com> Authored: Sat Jun 21 21:48:25 2014 +0100 Committer: BroganD1993 <darrenbro...@hotmail.com> Committed: Sat Jun 21 21:48:25 2014 +0100 ---------------------------------------------------------------------- gstack/__init__.py | 12 +++++++- gstack/configure.py | 77 +++++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/95fd9915/gstack/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/__init__.py b/gstack/__init__.py index 276ebbf..bc6b8ed 100644 --- a/gstack/__init__.py +++ b/gstack/__init__.py @@ -21,6 +21,8 @@ import os import sys from flask import Flask +from ConfigParser import SafeConfigParser + from gstack.core import db @@ -47,6 +49,14 @@ def _load_database(): return 'sqlite:///' + database_file +def _config_from_config_profile(config_file, profile='initial'): + config = SafeConfigParser() + config.read(config_file) + + for attribute in config.options(profile): + app.config[attribute.upper()] = config.get(profile, attribute) + + def configure_app(settings=None): app.config['DATA'] = os.path.abspath(os.path.dirname(__file__)) + '/data' @@ -57,7 +67,7 @@ def configure_app(settings=None): else: config_file = _load_config_file() database_uri = _load_database() - app.config.from_pyfile(config_file) + _config_from_config_profile(config_file) app.config['SQLALCHEMY_DATABASE_URI'] = database_uri http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/95fd9915/gstack/configure.py ---------------------------------------------------------------------- diff --git a/gstack/configure.py b/gstack/configure.py index e4d042b..82f3f02 100644 --- a/gstack/configure.py +++ b/gstack/configure.py @@ -18,6 +18,7 @@ import os +import ConfigParser from alembic import command from alembic.config import Config as AlembicConfig @@ -38,48 +39,64 @@ def _create_config_folder(): def _create_config_file(config_folder): - config_file = open(config_folder + '/gstack.conf', 'w+') + config_file_path = config_folder + '/gstack.conf' + config = _modify_config_profile(config_file_path) + config_file = open(config_file_path, 'w+') + config.write(config_file) - config_file.write('PATH = \'%s\'\n' % 'compute/v1/projects/') - gstack_address = raw_input('gstack bind address [0.0.0.0]: ') - if gstack_address == '': - gstack_address = '0.0.0.0' - config_file.write('GSTACK_BIND_ADDRESS = \'%s\'\n' % gstack_address) +def _modify_config_profile(config_file, profile='initial'): + config = ConfigParser.SafeConfigParser() + config.read(config_file) - gstack_port = raw_input('gstack bind port [5000]: ') - if gstack_port == '': - gstack_port = '5000' - config_file.write('GSTACK_PORT = \'%s\'\n' % gstack_port) + if not config.has_section(profile): + config.add_section(profile) - cloudstack_host = raw_input('Cloudstack host [localhost]: ') - if cloudstack_host == '': - cloudstack_host = 'localhost' - config_file.write('CLOUDSTACK_HOST = \'%s\'\n' % cloudstack_host) + config = _set_attribute_of_profile( + config, profile, 'gstack_bind_address', 'gstack bind address', 'localhost' + ) + config = _set_attribute_of_profile( + config, profile, 'gstack_port', 'gstack bind port', '5000' + ) + config = _set_attribute_of_profile( + config, profile, 'cloudstack_host', 'Cloudstack host', 'localhost' + ) + config = _set_attribute_of_profile( + config, profile, 'cloudstack_port', 'Cloudstack port', '8080' + ) + config = _set_attribute_of_profile( + config, profile, 'cloudstack_protocol', 'Cloudstack protocol', 'http' + ) + config = _set_attribute_of_profile( + config, profile, 'cloudstack_path', 'Cloudstack path', '/client/api' + ) - cloudstack_port = raw_input('Cloudstack port [8080]: ') - if cloudstack_port == '': - cloudstack_port = '8080' - config_file.write('CLOUDSTACK_PORT = \'%s\'\n' % cloudstack_port) + return config - cloudstack_protocol = raw_input('Cloudstack protocol [http]: ') - if cloudstack_protocol == '': - cloudstack_protocol = 'http' - config_file.write('CLOUDSTACK_PROTOCOL = \'%s\'\n' % cloudstack_protocol) - cloudstack_path = raw_input('Cloudstack path [/client/api]: ') - if cloudstack_path == '': - cloudstack_path = '/client/api' - config_file.write('CLOUDSTACK_PATH = \'%s\'\n' % cloudstack_path) +def _set_attribute_of_profile(config, profile, attribute, message, default): + if config.has_option(profile, attribute): + default = config.get(profile, attribute) - config_file.close() + attribute_value = _read_in_config_attribute_or_use_default(message, default) + + config.set(profile, attribute, attribute_value) + return config + + +def _read_in_config_attribute_or_use_default(message, default): + attribute = raw_input(message + ' [' + default + ']: ') + if attribute == '': + attribute = default + + return attribute def _create_database(): directory = os.path.join(os.path.dirname(__file__), '../migrations') - config = AlembicConfig(os.path.join( + database_config = AlembicConfig(os.path.join( directory, 'alembic.ini' )) - config.set_main_option('script_location', directory) - command.upgrade(config, 'head', sql=False, tag=None) + database_config.set_main_option('script_location', directory) + command.upgrade(database_config, 'head', sql=False, tag=None)