[PATCH 12 of 17 v3] cli: convert 'gearbox setup-db' into 'kallithea-cli db-create'

Thomas De Schampheleire patrickdepinguin at gmail.com
Thu Oct 18 20:49:35 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1539720694 -7200
#      Tue Oct 16 22:11:34 2018 +0200
# Node ID afaa88076a9e6fb9df4a676b0232f0a3c810fe3d
# Parent  714cacbf3266d092c7cce3c20e83ae6525d32f9a
cli: convert 'gearbox setup-db' into 'kallithea-cli db-create'

diff --git a/docs/contributing.rst b/docs/contributing.rst
--- a/docs/contributing.rst
+++ b/docs/contributing.rst
@@ -40,7 +40,7 @@ To get started with Kallithea developmen
         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 at example.com --password=password --repos=/tmp
+        kallithea-cli db-create --user=user --email=user at example.com --password=password --repos=/tmp my.ini
         gearbox serve -c my.ini --reload &
         firefox http://127.0.0.1:5000/
 
diff --git a/docs/dev/dbmigrations.rst b/docs/dev/dbmigrations.rst
--- a/docs/dev/dbmigrations.rst
+++ b/docs/dev/dbmigrations.rst
@@ -30,7 +30,7 @@ 4. After every database schema change, r
    to test the changes::
 
     rm temp.db
-    gearbox setup-db -c temp.ini --repos=/var/repos --user=doe --email doe at example.com --password=123456 --no-public-access --force-yes
+    kallithea-cli db-create --repos=/var/repos --user=doe --email doe at example.com --password=123456 --no-public-access --force-yes temp.ini
     kallithea-cli repo-scan temp.ini
 
 5. Once satisfied with the schema changes, auto-generate a draft Alembic
diff --git a/docs/installation_win.rst b/docs/installation_win.rst
--- a/docs/installation_win.rst
+++ b/docs/installation_win.rst
@@ -177,7 +177,7 @@ differences between Unix and Windows.
 
 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 my.ini
 
 .. warning:: This time a *new* database will be installed. You must
              follow a different process to later :ref:`upgrade <upgrade>`
diff --git a/docs/installation_win_old.rst b/docs/installation_win_old.rst
--- a/docs/installation_win_old.rst
+++ b/docs/installation_win_old.rst
@@ -215,7 +215,7 @@ character differences between Unix and W
 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 my.ini
 
 .. warning:: This time a *new* database will be installed. You must
              follow a different process to later :ref:`upgrade <upgrade>`
diff --git a/docs/setup.rst b/docs/setup.rst
--- a/docs/setup.rst
+++ b/docs/setup.rst
@@ -43,20 +43,20 @@ configuration file to use this other dat
 PostgreSQL, SQLite and MySQL databases. Create the database by running
 the following command::
 
-    gearbox setup-db -c my.ini
+    kallithea-cli db-create 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 at example.com --repos=/srv/repos
+    kallithea-cli db-create --user=nn --password=secret --email=nn at example.com --repos=/srv/repos my.ini
 
-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 @@ You are now ready to use Kallithea. To r
 - 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
diff --git a/docs/usage/vcs_notes.rst b/docs/usage/vcs_notes.rst
--- a/docs/usage/vcs_notes.rst
+++ b/docs/usage/vcs_notes.rst
@@ -44,7 +44,7 @@ repositories in the desired hierarchy on
 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
diff --git a/kallithea/alembic/env.py b/kallithea/alembic/env.py
--- a/kallithea/alembic/env.py
+++ b/kallithea/alembic/env.py
@@ -40,7 +40,7 @@ logging.getLogger('alembic').setLevel(lo
 
 # 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)
diff --git a/kallithea/bin/kallithea_cli.py b/kallithea/bin/kallithea_cli.py
--- a/kallithea/bin/kallithea_cli.py
+++ b/kallithea/bin/kallithea_cli.py
@@ -16,6 +16,7 @@
 from kallithea.bin.kallithea_cli_base import cli
 
 # import commands (they will add themselves to the 'cli' object)
+import kallithea.bin.kallithea_cli_db
 import kallithea.bin.kallithea_cli_cache
 import kallithea.bin.kallithea_cli_celery
 import kallithea.bin.kallithea_cli_config
diff --git a/kallithea/lib/paster_commands/setup_db.py b/kallithea/bin/kallithea_cli_db.py
rename from kallithea/lib/paster_commands/setup_db.py
rename 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,70 @@
 #
 # 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 click
+from kallithea.bin.kallithea_cli_base import cli
+import kallithea.bin.kallithea_cli_util as cli_util
 
 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
+ at cli.command()
+ at cli_util.auto_setup_app(config_only=True)
+ at click.option('--user', help='Username of administrator account.')
+ at click.option('--password', help='Password for administrator account.')
+ at click.option('--email', help='Email address of administrator account.')
+ at click.option('--repos', help='Absolute path to repositories location.')
+ at click.option('--force-yes', is_flag=True, help='Answer yes to every question.')
+ at click.option('--force-no', is_flag=True, help='Answer no to every question.')
+ at click.option('--public-access/--no-public-access', default=True,
+        help='Enable/disable public access on this installation (default: enable)')
+def db_create(config_file, 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.')
diff --git a/kallithea/lib/db_manage.py b/kallithea/lib/db_manage.py
--- a/kallithea/lib/db_manage.py
+++ b/kallithea/lib/db_manage.py
@@ -77,7 +77,7 @@ class DbManage(object):
         force_ask = self.cli_args.get('force_ask')
         if force_ask is not None:
             return force_ask
-        from kallithea.lib.paster_commands.common import ask_ok
+        from kallithea.bin.kallithea_cli_util import ask_ok
         return ask_ok(msg)
 
     def init_db(self, SESSION=None):
diff --git a/kallithea/tests/scripts/create_rc.sh b/kallithea/tests/scripts/create_rc.sh
--- a/kallithea/tests/scripts/create_rc.sh
+++ b/kallithea/tests/scripts/create_rc.sh
@@ -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 at example.com --repos=/home/username/repos --no-public-access
+kallithea-cli db-create --force-yes --user=username --password=qweqwe --email=username at example.com --repos=/home/username/repos --no-public-access server.ini
 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
diff --git a/scripts/dbmigrate-test b/scripts/dbmigrate-test
--- a/scripts/dbmigrate-test
+++ b/scripts/dbmigrate-test
@@ -104,7 +104,7 @@ install_kallithea "$temp/from" "$from_re
     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 at example.com --password=123456 --no-public-access --force-yes
+    quiet_if_ok kallithea-cli db-create --repos="$temp/repos" --user=doe --email=doe at example.com --password=123456 --no-public-access --force-yes "$config_file"
     alembic -c "$config_file" current -v
 )
 
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -160,7 +160,6 @@ setuptools.setup(
     [gearbox.commands]
     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
     """,
 )


More information about the kallithea-general mailing list