diff --git a/kallithea/lib/paster_commands/setup_db.py b/kallithea/bin/kallithea_cli_db.py copy from kallithea/lib/paster_commands/setup_db.py copy to kallithea/bin/kallithea_cli_db.py --- a/kallithea/lib/paster_commands/setup_db.py +++ b/kallithea/bin/kallithea_cli_db.py @@ -11,104 +11,68 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" -kallithea.lib.paster_commands.setup_db -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Databaset setup gearbox command for Kallithea -""" - - +import click +import kallithea.bin.kallithea_cli_base as cli_base import kallithea from kallithea.lib.db_manage import DbManage -from kallithea.lib.paster_commands.common import BasePasterCommand from kallithea.model.meta import Session - -# This is almost like SetupAppCommand ... but we have to pass options and it is -# thus simpler to drop websetup and reimplement everything -class Command(BasePasterCommand): - """Kallithea: Configure the database specified in the .ini file +@cli_base.register_command(config_file=True) +@click.option('--user', help='Username of administrator account.') +@click.option('--password', help='Password for administrator account.') +@click.option('--email', help='Email address of administrator account.') +@click.option('--repos', help='Absolute path to repositories location.') +@click.option('--force-yes', is_flag=True, help='Answer yes to every question.') +@click.option('--force-no', is_flag=True, help='Answer no to every question.') +@click.option('--public-access/--no-public-access', default=True, + help='Enable/disable public access on this installation (default: enable)') +def db_create(user, password, email, repos, force_yes, force_no, public_access): + """Initialize the database. - Setup Kallithea according to its configuration file. This is - the second part of a two-phase web application installation - process (the first phase is prepare-app). The setup process - consist of things like setting up databases and creating the admin user + Create all required tables in the database specified in the configuration + file. Create the administrator account. Set certain settings based on + values you provide. + + You can pass the answers to all questions as options to this command. """ + dbconf = kallithea.CONFIG['sqlalchemy.url'] - def get_description(self): - return self.__doc__.splitlines()[0] - - requires_db_session = False # only available after this command has been run - - def get_parser(self, prog_name): - parser = super(Command, self).get_parser(prog_name) + # force_ask should be True (yes), False (no), or None (ask) + if force_yes: + force_ask = True + elif force_no: + force_ask = False + else: + force_ask = None - parser.add_argument('--user', - action='store', - dest='username', - default=None, - help='Admin Username') - parser.add_argument('--email', - action='store', - dest='email', - default=None, - help='Admin Email') - parser.add_argument('--password', - action='store', - dest='password', - default=None, - help='Admin password min 6 chars') - parser.add_argument('--repos', - action='store', - dest='repos_location', - default=None, - help='Absolute path to repositories location') - parser.add_argument('--force-yes', - action='store_true', - dest='force_ask', - default=None, - help='Force yes to every question') - parser.add_argument('--force-no', - action='store_false', - dest='force_ask', - default=None, - help='Force no to every question') - parser.add_argument('--public-access', - action='store_true', - dest='public_access', - default=None, - help='Enable public access on this installation (default)') - parser.add_argument('--no-public-access', - action='store_false', - dest='public_access', - default=None, - help='Disable public access on this installation ') + cli_args = dict( + username=user, + password=password, + email=email, + repos_location=repos, + force_ask=force_ask, + public_access=public_access, + ) + dbmanage = DbManage(dbconf=dbconf, root=kallithea.CONFIG['here'], + tests=False, cli_args=cli_args) + dbmanage.create_tables(override=True) + opts = dbmanage.config_prompt(None) + dbmanage.create_settings(opts) + dbmanage.create_default_user() + dbmanage.admin_prompt() + dbmanage.create_permissions() + dbmanage.populate_default_permissions() + Session().commit() - return parser + # initial repository scan + kallithea.config.middleware.make_app_without_logging( + kallithea.CONFIG.global_conf, **kallithea.CONFIG.local_conf) + added, _ = kallithea.lib.utils.repo2db_mapper(kallithea.model.scm.ScmModel().repo_scan()) + if added: + click.echo('Initial repository scan: added following repositories:') + click.echo('\t%s' % '\n\t'.join(added)) + else: + click.echo('Initial repository scan: no repositories found.') - def take_action(self, opts): - dbconf = self.config['sqlalchemy.url'] - dbmanage = DbManage(dbconf=dbconf, root=self.config['here'], - tests=False, cli_args=vars(opts)) - dbmanage.create_tables(override=True) - opts = dbmanage.config_prompt(None) - dbmanage.create_settings(opts) - dbmanage.create_default_user() - dbmanage.admin_prompt() - dbmanage.create_permissions() - dbmanage.populate_default_permissions() - Session().commit() - - # initial repository scan - kallithea.config.middleware.make_app_without_logging( - self.config.global_conf, **self.config.local_conf) - added, _ = kallithea.lib.utils.repo2db_mapper(kallithea.model.scm.ScmModel().repo_scan()) - if added: - print 'Initial repository scan: added following repositories:' - print '\t','\n\t'.join(added) - else: - print 'Initial repository scan: no repositories found.' - - print 'Database set up successfully.' + click.echo('Database set up successfully.')