From 6895b400192b0fe5343d89f8a92cd7e7eef23a07 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Tue, 28 Aug 2012 15:32:11 -0400 Subject: Bcfg2.settings: Fixed config file reading: * If -W is given on the command line, the config file given by -W is read. * If /etc/bcfg2-web.conf does not exist, and -C was given on the command line, the config file given by -C is read. * If /etc/bcfg2-web.conf does not exist and -C was not given on the command line, the default /etc/bcfg2.conf is read. * If /etc/bcfg2-web.conf exists, and neither -W nor -C was given on the command line, then /etc/bcfg2-web.conf is read. This will produce errors if: * A bogus (non-existent, unreadable) file was given by -W; or * A bogus file was given by -C and /etc/bcfg2-web.conf does not exist; or * No -W was given and /etc/bcfg2-web.conf exists but is unreadable; or * No -W was given, /etc/bcfg2-web.conf does not exist, and /etc/bcfg2.conf is bogus. --- src/lib/Bcfg2/Server/Admin/Syncdb.py | 9 +-------- src/lib/Bcfg2/Server/Core.py | 3 +-- src/lib/Bcfg2/settings.py | 34 ++++++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/lib/Bcfg2/Server/Admin/Syncdb.py b/src/lib/Bcfg2/Server/Admin/Syncdb.py index bff232b05..1eb953e2a 100644 --- a/src/lib/Bcfg2/Server/Admin/Syncdb.py +++ b/src/lib/Bcfg2/Server/Admin/Syncdb.py @@ -8,8 +8,7 @@ class Syncdb(Bcfg2.Server.Admin.Mode): __shorthelp__ = ("Sync the Django ORM with the configured database") __longhelp__ = __shorthelp__ + "\n\nbcfg2-admin syncdb" __usage__ = "bcfg2-admin syncdb" - options = {'web_configfile': Bcfg2.Options.WEB_CFILE, - 'repo': Bcfg2.Options.SERVER_REPOSITORY} + options = {'configfile': Bcfg2.Options.WEB_CFILE} def __call__(self, args): import Bcfg2.Server.Admin @@ -19,12 +18,6 @@ class Syncdb(Bcfg2.Server.Admin.Mode): self.opts = Bcfg2.Options.OptionParser(self.options) self.opts.parse(args) - # we have to set up the django environment before we import - # the syncdb command, but we have to wait to set up the - # environment until we've read the config, which has to wait - # until we've parsed options. it's a windy, twisting road. - Bcfg2.settings.read_config(cfile=self.opts['web_configfile'], - repo=self.opts['repo']) setup_environ(Bcfg2.settings) import Bcfg2.Server.models Bcfg2.Server.models.load_models(cfile=self.opts['configfile']) diff --git a/src/lib/Bcfg2/Server/Core.py b/src/lib/Bcfg2/Server/Core.py index 749e4b7d8..dc29f45eb 100644 --- a/src/lib/Bcfg2/Server/Core.py +++ b/src/lib/Bcfg2/Server/Core.py @@ -103,8 +103,7 @@ class BaseCore(object): # generate Django ORM settings. this must be done _before_ we # load plugins - Bcfg2.settings.read_config(cfile=self.setup['web_configfile'], - repo=self.datastore) + Bcfg2.settings.read_config(repo=self.datastore) self._database_available = False # verify our database schema diff --git a/src/lib/Bcfg2/settings.py b/src/lib/Bcfg2/settings.py index 05e85bb9a..032e08a2c 100644 --- a/src/lib/Bcfg2/settings.py +++ b/src/lib/Bcfg2/settings.py @@ -27,20 +27,38 @@ MEDIA_URL = '/site_media' # default config file is /etc/bcfg2-web.conf, UNLESS /etc/bcfg2.conf # exists AND /etc/bcfg2-web.conf does not exist. -DEFAULT_CONFIG = Bcfg2.Options.WEB_CFILE.default -if (not os.path.exists(Bcfg2.Options.WEB_CFILE.default) and - os.path.exists(Bcfg2.Options.CFILE.default)): - DEFAULT_CONFIG = Bcfg2.Options.CFILE.default +def _default_config(): + optinfo = dict(cfile=Bcfg2.Options.CFILE, + web_cfile=Bcfg2.Options.WEB_CFILE) + setup = Bcfg2.Options.OptionParser(optinfo, quiet=True) + setup.parse(sys.argv[1:], do_getopt=False) + if (not os.path.exists(setup['web_cfile']) and + os.path.exists(setup['cfile'])): + return setup['cfile'] + else: + return setup['web_cfile'] + +DEFAULT_CONFIG = _default_config() def read_config(cfile=DEFAULT_CONFIG, repo=None, quiet=False): global DATABASE_ENGINE, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, \ DATABASE_HOST, DATABASE_PORT, DEBUG, TEMPLATE_DEBUG, TIME_ZONE, \ MEDIA_URL + if not os.path.exists(cfile) and os.path.exists(DEFAULT_CONFIG): + print("%s does not exist, using %s for database configuration" % + (cfile, DEFAULT_CONFIG)) + cfile = DEFAULT_CONFIG + optinfo = Bcfg2.Options.DATABASE_COMMON_OPTIONS optinfo['repo'] = Bcfg2.Options.SERVER_REPOSITORY - setup = Bcfg2.Options.OptionParser(optinfo, quiet=quiet) - setup.parse([Bcfg2.Options.WEB_CFILE.cmd, cfile]) + # when setting a different config file, it has to be set in either + # sys.argv or in the OptionSet() constructor AS WELL AS the argv + # that's passed to setup.parse() + argv = [Bcfg2.Options.CFILE.cmd, cfile, + Bcfg2.Options.WEB_CFILE.cmd, cfile] + setup = Bcfg2.Options.OptionParser(optinfo, argv=argv, quiet=quiet) + setup.parse(argv) if repo is None: repo = setup['repo'] @@ -76,8 +94,8 @@ def read_config(cfile=DEFAULT_CONFIG, repo=None, quiet=False): MEDIA_URL = '/site_media' -# initialize settings from /etc/bcfg2.conf, or set up basic defaults. -# this lets manage.py work in all cases +# initialize settings from /etc/bcfg2-web.conf or /etc/bcfg2.conf, or +# set up basic defaults. this lets manage.py work in all cases read_config(quiet=True) ADMINS = (('Root', 'root')) -- cgit v1.2.3-1-g7c22