changeset 7414:3158cf0dafb7

cli: convert 'gearbox setup-db' into 'kallithea-cli db-create'
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Sun, 18 Nov 2018 20:02:17 +0100
parents 9de61c5b8694
children f9c8fec48185
files docs/contributing.rst docs/dev/dbmigrations.rst docs/installation_win.rst docs/installation_win_old.rst docs/setup.rst docs/usage/vcs_notes.rst kallithea/alembic/env.py kallithea/bin/kallithea_cli.py kallithea/bin/kallithea_cli_db.py kallithea/lib/paster_commands/setup_db.py kallithea/tests/scripts/create_rc.sh scripts/dbmigrate-test setup.py
diffstat 13 files changed, 94 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/docs/contributing.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/contributing.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -40,7 +40,7 @@
         npm install     # install dependencies - both tools and data
         npm run less    # for generating css from less
         kallithea-cli config-create my.ini
-        gearbox setup-db -c my.ini --user=user --email=user@example.com --password=password --repos=/tmp
+        kallithea-cli db-create -c my.ini --user=user --email=user@example.com --password=password --repos=/tmp
         gearbox serve -c my.ini --reload &
         firefox http://127.0.0.1:5000/
 
--- a/docs/dev/dbmigrations.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/dev/dbmigrations.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -30,7 +30,7 @@
    to test the changes::
 
     rm temp.db
-    gearbox setup-db -c temp.ini --repos=/var/repos --user=doe --email doe@example.com --password=123456 --no-public-access --force-yes
+    kallithea-cli db-create -c temp.ini --repos=/var/repos --user=doe --email doe@example.com --password=123456 --no-public-access --force-yes
     kallithea-cli repo-scan -c temp.ini
 
 5. Once satisfied with the schema changes, auto-generate a draft Alembic
--- a/docs/installation_win.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/installation_win.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -177,7 +177,7 @@
 
 For the sake of simplicity, run it with the default settings. After your edits (if any) in the previous command prompt, type::
 
-  gearbox setup-db -c my.ini
+  kallithea-cli db-create -c my.ini
 
 .. warning:: This time a *new* database will be installed. You must
              follow a different process to later :ref:`upgrade <upgrade>`
--- a/docs/installation_win_old.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/installation_win_old.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -215,7 +215,7 @@
 For the sake of simplicity lets run it with the default settings. After
 your edits (if any), in the previous Command Prompt, type::
 
- gearbox setup-db -c my.ini
+  kallithea-cli db-create -c my.ini
 
 .. warning:: This time a *new* database will be installed. You must
              follow a different process to later :ref:`upgrade <upgrade>`
--- a/docs/setup.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/setup.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -43,20 +43,20 @@
 PostgreSQL, SQLite and MySQL databases. Create the database by running
 the following command::
 
-    gearbox setup-db -c my.ini
+    kallithea-cli db-create -c my.ini
 
 This will prompt you for a "root" path. This "root" path is the location where
 Kallithea will store all of its repositories on the current machine. After
-entering this "root" path ``setup-db`` will also prompt you for a username
-and password for the initial admin account which ``setup-db`` sets
+entering this "root" path ``db-create`` will also prompt you for a username
+and password for the initial admin account which ``db-create`` sets
 up for you.
 
-The ``setup-db`` values can also be given on the command line.
+The ``db-create`` values can also be given on the command line.
 Example::
 
-    gearbox setup-db -c my.ini --user=nn --password=secret --email=nn@example.com --repos=/srv/repos
+    kallithea-cli db-create -c my.ini --user=nn --password=secret --email=nn@example.com --repos=/srv/repos
 
-The ``setup-db`` command will create all needed tables and an
+The ``db-create`` command will create all needed tables and an
 admin account. When choosing a root path you can either use a new
 empty location, or a location which already contains existing
 repositories. If you choose a location which contains existing
@@ -77,7 +77,7 @@
 - This command runs the Kallithea server. The web app should be available at
   http://127.0.0.1:5000. The IP address and port is configurable via the
   configuration file created in the previous step.
-- Log in to Kallithea using the admin account created when running ``setup-db``.
+- Log in to Kallithea using the admin account created when running ``db-create``.
 - The default permissions on each repository is read, and the owner is admin.
   Remember to update these if needed.
 - In the admin panel you can toggle LDAP, anonymous, and permissions
--- a/docs/usage/vcs_notes.rst	Sun Nov 18 20:02:17 2018 +0100
+++ b/docs/usage/vcs_notes.rst	Sun Nov 18 20:02:17 2018 +0100
@@ -44,7 +44,7 @@
 scan that location.
 
 All repositories are stored in a central location on the filesystem. This
-location is specified during installation (via ``setup-db``) and can be reviewed
+location is specified during installation (via ``db-create``) and can be reviewed
 at *Admin > Settings > VCS > Location of repositories*. Repository groups
 (defined in *Admin > Repository Groups*) are represented by a directory in that
 repository location. Repositories of the repository group are nested under that
--- a/kallithea/alembic/env.py	Sun Nov 18 20:02:17 2018 +0100
+++ b/kallithea/alembic/env.py	Sun Nov 18 20:02:17 2018 +0100
@@ -40,7 +40,7 @@
 
 # Setup Python loggers based on the config file provided to the alembic
 # command. If we're being invoked via the Alembic API (presumably for
-# stamping during "gearbox setup-db"), config_file_name is not available,
+# stamping during "kallithea-cli db-create"), config_file_name is not available,
 # and loggers are assumed to already have been configured.
 if config.config_file_name:
     fileConfig(config.config_file_name, disable_existing_loggers=False)
--- a/kallithea/bin/kallithea_cli.py	Sun Nov 18 20:02:17 2018 +0100
+++ b/kallithea/bin/kallithea_cli.py	Sun Nov 18 20:02:17 2018 +0100
@@ -17,6 +17,7 @@
 
 # import commands (they will add themselves to the 'cli' object)
 import kallithea.bin.kallithea_cli_config
+import kallithea.bin.kallithea_cli_db
 import kallithea.bin.kallithea_cli_iis
 import kallithea.bin.kallithea_cli_ishell
 import kallithea.bin.kallithea_cli_repo
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/bin/kallithea_cli_db.py	Sun Nov 18 20:02:17 2018 +0100
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+import click
+import kallithea.bin.kallithea_cli_base as cli_base
+
+import kallithea
+from kallithea.lib.db_manage import DbManage
+from kallithea.model.meta import Session
+
+@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.
+
+    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']
+
+    # 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
+
+    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()
+
+    # 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.')
+
+    click.echo('Database set up successfully.')
--- a/kallithea/lib/paster_commands/setup_db.py	Sun Nov 18 20:02:17 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-# -*- coding: utf-8 -*-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-"""
-kallithea.lib.paster_commands.setup_db
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Databaset setup gearbox command for Kallithea
-"""
-
-
-
-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
-
-    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
-    """
-
-    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)
-
-        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 ')
-
-        return parser
-
-    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.'
--- a/kallithea/tests/scripts/create_rc.sh	Sun Nov 18 20:02:17 2018 +0100
+++ b/kallithea/tests/scripts/create_rc.sh	Sun Nov 18 20:02:17 2018 +0100
@@ -1,7 +1,7 @@
 #!/bin/sh
 psql -U postgres -h localhost -c 'drop database if exists kallithea;'
 psql -U postgres -h localhost -c 'create database kallithea;'
-gearbox setup-db -c server.ini --force-yes --user=username --password=qweqwe --email=username@example.com --repos=/home/username/repos --no-public-access
+kallithea-cli db-create -c server.ini --force-yes --user=username --password=qweqwe --email=username@example.com --repos=/home/username/repos --no-public-access
 API_KEY=`psql -R " " -A -U postgres -h localhost -c "select api_key from users where admin=TRUE" -d kallithea | awk '{print $2}'`
 echo "run those after running server"
 gearbox serve -c server.ini --pid-file=server.pid --daemon
--- a/scripts/dbmigrate-test	Sun Nov 18 20:02:17 2018 +0100
+++ b/scripts/dbmigrate-test	Sun Nov 18 20:02:17 2018 +0100
@@ -104,7 +104,7 @@
     cd "$temp/from"
     . "$temp/from-env/bin/activate"
     announce "Initializing database..."
-    quiet_if_ok gearbox setup-db -c "$config_file" --repos="$temp/repos" --user=doe --email=doe@example.com --password=123456 --no-public-access --force-yes
+    quiet_if_ok kallithea-cli db-create -c "$config_file" --repos="$temp/repos" --user=doe --email=doe@example.com --password=123456 --no-public-access --force-yes
     alembic -c "$config_file" current -v
 )
 
--- a/setup.py	Sun Nov 18 20:02:17 2018 +0100
+++ b/setup.py	Sun Nov 18 20:02:17 2018 +0100
@@ -162,7 +162,6 @@
     celeryd=kallithea.lib.paster_commands.celeryd:Command
     make-index=kallithea.lib.paster_commands.make_index:Command
     make-rcext=kallithea.lib.paster_commands.make_rcextensions:Command
-    setup-db=kallithea.lib.paster_commands.setup_db:Command
     upgrade-db=kallithea.lib.dbmigrate:UpgradeDb
     """,
 )