Changeset - 304aae43194c
[Not reviewed]
default
0 1 0
Mads Kiilerich - 9 years ago 2017-04-14 21:30:42
mads@kiilerich.com
gearbox: fix setup-db failure when db is created for the first time (Issue #272)

The port to TG2 in e1ab82613133 replaced pylonsconfig.init_app for gearbox
commands with middleware setup code. That introduced reading of the database
for commands that don't need a database - for example the command for creating
databases ... which thus failed.

To fix that, move the middleware setup with database access so it only is run
for gearbox command that requires a db session. There is apparently no need for
a direct replacement for pylonsconfig.init_app .
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/paster_commands/common.py
Show inline comments
 
@@ -26,71 +26,71 @@ Original author and date, and relevant c
 
"""
 

	
 
import os
 
import sys
 
import logging.config
 

	
 
import paste.deploy
 
import gearbox.command
 
from tg import config
 

	
 
import kallithea.config.middleware
 
import kallithea.model.base
 
import kallithea.lib.utils2
 
import kallithea.lib.utils
 

	
 

	
 
def ask_ok(prompt, retries=4, complaint='Yes or no please!'):
 
    while True:
 
        ok = raw_input(prompt)
 
        if ok in ('y', 'ye', 'yes'):
 
            return True
 
        if ok in ('n', 'no', 'nop', 'nope'):
 
            return False
 
        retries = retries - 1
 
        if retries < 0:
 
            raise IOError
 
        print complaint
 

	
 

	
 
class BasePasterCommand(gearbox.command.Command):
 
    """
 
    Abstract Base Class for gearbox commands.
 
    """
 

	
 
    # override to control how much get_parser and run should do:
 
    takes_config_file = True
 
    requires_db_session = True
 

	
 
    def run(self, args):
 
        """
 
        Overrides Command.run
 

	
 
        If needed by the command, read config file and initialize database before running.
 
        """
 
        if self.takes_config_file:
 
            path_to_ini_file = os.path.realpath(args.config_file)
 
            conf = paste.deploy.appconfig('config:' + path_to_ini_file)
 
            logging.config.fileConfig(path_to_ini_file)
 
            kallithea.config.middleware.make_app_without_logging(conf.global_conf, **conf.local_conf)
 

	
 
            if self.requires_db_session:
 
                kallithea.config.middleware.make_app_without_logging(conf.global_conf, **conf.local_conf)
 
                kallithea.lib.utils.setup_cache_regions(config)
 
                engine = kallithea.lib.utils2.engine_from_config(config, 'sqlalchemy.')
 
                kallithea.model.base.init_model(engine)
 

	
 
        return super(BasePasterCommand, self).run(args)
 

	
 
    def get_parser(self, prog_name):
 
        parser = super(BasePasterCommand, self).get_parser(prog_name)
 

	
 
        if self.takes_config_file:
 
            parser.add_argument("-c", "--config",
 
                help='Kallithea .ini file with configuration of database etc',
 
                dest='config_file', required=True)
 

	
 
        return parser
 

	
 
    def error(self, msg, exitcode=1):
 
        """Write error message and exit"""
 
        sys.stderr.write('%s\n' % msg)
 
        raise SystemExit(exitcode)
0 comments (0 inline, 0 general)