setup_rc.py
123 lines
| 4.2 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2016-2023 RhodeCode GmbH | |||
r2007 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# 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 Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
r2341 | import logging | |||
r2007 | ||||
import click | ||||
r2341 | import pyramid.paster | |||
r2007 | ||||
r5325 | from rhodecode.lib.pyramid_utils import bootstrap | |||
from rhodecode.lib.config_utils import get_app_config | ||||
r2341 | from rhodecode.lib.db_manage import DbManage | |||
r5078 | from rhodecode.lib.utils2 import get_encryption_key | |||
r2341 | from rhodecode.model.db import Session | |||
log = logging.getLogger(__name__) | ||||
r2007 | ||||
@click.command() | ||||
@click.argument('ini_path', type=click.Path(exists=True)) | ||||
r2341 | @click.option( | |||
'--force-yes/--force-no', default=None, | ||||
help="Force yes/no to every question") | ||||
@click.option( | ||||
'--user', | ||||
default=None, | ||||
help='Initial super-admin username') | ||||
@click.option( | ||||
'--email', | ||||
default=None, | ||||
help='Initial super-admin email address.') | ||||
@click.option( | ||||
'--password', | ||||
default=None, | ||||
help='Initial super-admin password. Minimum 6 chars.') | ||||
@click.option( | ||||
'--api-key', | ||||
help='Initial API key for the admin user') | ||||
@click.option( | ||||
'--repos', | ||||
default=None, | ||||
help='Absolute path to storage location. This is storage for all ' | ||||
'existing and future repositories, and repository groups.') | ||||
@click.option( | ||||
'--public-access/--no-public-access', | ||||
default=None, | ||||
help='Enable public access on this installation. ' | ||||
'Default is public access enabled.') | ||||
r4618 | @click.option( | |||
'--skip-existing-db', | ||||
default=False, | ||||
is_flag=True, | ||||
help='Do not destroy and re-initialize the database if it already exist.') | ||||
@click.option( | ||||
'--apply-license-key', | ||||
default=False, | ||||
is_flag=True, | ||||
help='Get the license key from a license file or ENV and apply during DB creation.') | ||||
r2341 | def main(ini_path, force_yes, user, email, password, api_key, repos, | |||
r4618 | public_access, skip_existing_db, apply_license_key): | |||
r2341 | return command(ini_path, force_yes, user, email, password, api_key, | |||
r4618 | repos, public_access, skip_existing_db, apply_license_key) | |||
r2341 | ||||
def command(ini_path, force_yes, user, email, password, api_key, repos, | ||||
r4618 | public_access, skip_existing_db, apply_license_key): | |||
r2341 | # mapping of old parameters to new CLI from click | |||
options = dict( | ||||
username=user, | ||||
email=email, | ||||
password=password, | ||||
api_key=api_key, | ||||
repos_location=repos, | ||||
force_ask=force_yes, | ||||
public_access=public_access | ||||
) | ||||
r2007 | pyramid.paster.setup_logging(ini_path) | |||
r2341 | config = get_app_config(ini_path) | |||
db_uri = config['sqlalchemy.db1.url'] | ||||
r5078 | enc_key = get_encryption_key(config) | |||
r2341 | dbmanage = DbManage(log_sql=True, dbconf=db_uri, root='.', | |||
r5078 | tests=False, cli_args=options, enc_key=enc_key) | |||
r4618 | if skip_existing_db and dbmanage.db_exists(): | |||
return | ||||
r2341 | dbmanage.create_tables(override=True) | |||
dbmanage.set_db_version() | ||||
opts = dbmanage.config_prompt(None) | ||||
dbmanage.create_settings(opts) | ||||
dbmanage.create_default_user() | ||||
dbmanage.create_admin_and_prompt() | ||||
dbmanage.create_permissions() | ||||
dbmanage.populate_default_permissions() | ||||
r4618 | if apply_license_key: | |||
r5530 | from rhodecode.model.license import apply_license_from_file | |||
license_file_path = config.get('license.import_path') | ||||
if license_file_path: | ||||
apply_license_from_file(license_file_path, force=True) | ||||
r4618 | ||||
r2341 | Session().commit() | |||
r2651 | with bootstrap(ini_path, env={'RC_CMD_SETUP_RC': '1'}) as env: | |||
r2341 | msg = 'Successfully initialized database, schema and default data.' | |||
print() | ||||
print('*' * len(msg)) | ||||
print(msg.upper()) | ||||
print('*' * len(msg)) | ||||