Ejemplo n.º 1
0
def post_install() -> None:
    if not os.geteuid() == 0:
        sys.exit('Script must be run as root')

    app = create_app(parse_options())
    config_path = os.path.join('/', 'etc', 'gitlab-tools', 'config.yml')

    configuration = {}
    if os.path.isfile(config_path):
        with open(config_path) as f:
            loaded_data = yaml.load(f)
            if isinstance(loaded_data, dict):
                configuration.update(loaded_data)

    if not configuration.get('USER') and OPTIONS['--user']:
        app.config['USER'] = configuration['USER'] = OPTIONS['--user']

    # Generate database and config if nothing is specified
    if 'SQLALCHEMY_DATABASE_URI' not in configuration or not configuration[
            'SQLALCHEMY_DATABASE_URI']:

        database_path = 'sqlite:///{}/gitlab-tools.db'.format(
            get_home_dir(app.config['USER']))

        configuration['SQLALCHEMY_DATABASE_URI'] = database_path

        # We need to set DB config to make stamp work
        app.config['SQLALCHEMY_DATABASE_URI'] = configuration[
            'SQLALCHEMY_DATABASE_URI']

        # Create empty database
        with app.app_context():
            db.create_all()

        with app.app_context():
            stamp()

        # Generate secret key
    if 'SECRET_KEY' not in configuration or not configuration['SECRET_KEY']:
        app.config['SECRET_KEY'] = configuration[
            'SECRET_KEY'] = random_password()

    # Set port and host
    if 'HOST' not in configuration or not configuration['HOST']:
        configuration['HOST'] = '0.0.0.0'

    if 'PORT' not in configuration or not configuration['PORT']:
        configuration['PORT'] = 80

    # Write new configuration
    with open(config_path, 'w') as f:
        yaml.dump(configuration,
                  f,
                  default_flow_style=False,
                  allow_unicode=True)
Ejemplo n.º 2
0
    def database_sqlite():
        print('SQLite configuration:')

        home_dir = get_home_dir(configuration['USER'])
        database_path_default = os.path.join(home_dir, 'gitlab-tools.db')
        connection_info = urllib.parse.urlparse(
            configuration.get('SQLALCHEMY_DATABASE_URI',
                              'sqlite:///{}'.format(database_path_default)))

        if connection_info.scheme == 'sqlite':
            database_path = os.path.join('/', connection_info.path.lstrip('/'))
        else:
            database_path = database_path_default

        database_location = input(
            'Location [{}]: '.format(database_path)) or database_path

        app.config['SQLALCHEMY_DATABASE_URI'] = configuration[
            'SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format(
                database_location)