Example #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)
Example #2
0
def create_all() -> None:
    setup_logging('create_all')
    app = create_app(parse_options())
    log = logging.getLogger(__name__)
    with app.app_context():
        tables_before = set(db.engine.table_names())
        db.create_all()
        tables_after = set(db.engine.table_names())
    created_tables = tables_after - tables_before
    for table in created_tables:
        log.info('Created table: {}'.format(table))
Example #3
0
def setup() -> 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)

    def required_input(text):
        return input(text) or required_input(text)

    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)

    def database_mysql():
        print('MySQL configuration:')
        database_login('mysql')

    def database_postgresql():
        print('PostgreSQL configuration:')
        database_login('postgresql')

    def database_login(database_type):

        connection_info = urllib.parse.urlparse(
            configuration.get(
                'SQLALCHEMY_DATABASE_URI',
                '{}://gitlab-tools:[email protected]/gitlab-tools'.format(
                    database_type)))

        if connection_info.scheme == database_type:
            database_name = connection_info.path
            database_server = connection_info.netloc
            database_user = connection_info.username
            database_password = connection_info.password
        else:
            database_name = 'gitlab-tools'
            database_server = '127.0.0.1'
            database_user = '******'
            database_password = None

        database_server = input(
            'Server [{}]: '.format(database_server)) or database_server
        database_name = input(
            'Database [{}]: '.format(database_name)) or database_name
        database_user = input(
            'User [{}]: '.format(database_user)) or database_user
        if not database_password:
            database_password = required_input('Password (required):')
        else:
            database_password = input('Password [{}]: '.format(
                '*' * len(database_password))) or database_password

        app.config['SQLALCHEMY_DATABASE_URI'] = configuration[
            'SQLALCHEMY_DATABASE_URI'] = '{}://{}:{}@{}/{}'.format(
                database_type, database_user, database_password,
                database_server, database_name)

    def ignore():
        pass

    print('Default application user (must exists):')
    default_user = configuration.get('USER', app.config.get('USER'))
    app.config['USER'] = configuration['USER'] = input(
        '[{}]: '.format(default_user)) or default_user

    # Check if repository storage directory exists
    repository_storage_path = get_repository_storage(app.config['USER'])
    if not os.path.isdir(repository_storage_path):
        print('Creating {}'.format(repository_storage_path))
        os.mkdir(repository_storage_path)

    os.chown(repository_storage_path, get_user_id(app.config['USER']),
             get_user_group_id(app.config['USER']))

    # Check if ssh storage directory exists
    ssh_storage_path = get_ssh_storage(app.config['USER'])
    if not os.path.isdir(ssh_storage_path):
        print('Creating {}'.format(ssh_storage_path))
        os.mkdir(ssh_storage_path)

    os.chown(ssh_storage_path, get_user_id(app.config['USER']),
             get_user_group_id(app.config['USER']))

    database_types = {
        0: {
            'name': 'Ignore',
            'default': True,
            'call': ignore
        },
        1: {
            'name': 'SQLite',
            'default': False,
            'call': database_sqlite
        },
        2: {
            'name': 'PostgreSQL [Recommended]',
            'default': False,
            'call': database_postgresql
        },
        3: {
            'name': 'MySQL',
            'default': False,
            'call': database_mysql
        },
    }

    print('Choose database type you want to use:')
    db_type_default = None
    for db_type in database_types:
        if database_types[db_type]['default']:
            db_type_default = db_type
        print('{}) {}{}'.format(
            db_type, database_types[db_type]['name'],
            ' (default)' if database_types[db_type]['default'] else ''))

    database_type = int(
        input('Database type [{}]: '.format(db_type_default))
        or db_type_default)
    if database_type not in database_types:
        print('Invalid option selected')
        sys.exit(1)

    database_types[database_type]['call']()

    print('Webserver configuration:')
    webserver_host = configuration.get('HOST', '127.0.0.1')
    webserver_port = configuration.get('PORT', '80')
    configuration['HOST'] = input(
        'Host (for integrated web server - when used) [{}]: '.format(
            webserver_host)) or webserver_host
    configuration['PORT'] = input(
        'Port (for integrated web server - when used) [{}]: '.format(
            webserver_port)) or webserver_port
    server_name = '{}:{}'.format(configuration['HOST'], configuration['PORT'])
    configuration['SERVER_NAME'] = input(
        'Server name (on what domain or ip+port is this application available) [{}]: '
        .format(server_name)) or server_name

    print('Gitlab configuration:')
    default_gitlab_url = configuration.get('GITLAB_URL')
    default_gitlab_app_id = configuration.get('GITLAB_APP_ID')
    default_gitlab_app_secret = configuration.get('GITLAB_APP_SECRET')
    configuration['GITLAB_URL'] = input(
        'Gitlab URL [{}]:'.format(default_gitlab_url)) or default_gitlab_url

    configuration['GITLAB_APP_ID'] = input('Gitlab APP ID [{}]:'.format(
        default_gitlab_app_id)) or default_gitlab_app_id
    configuration['GITLAB_APP_SECRET'] = input(
        'Gitlab APP SECRET [{}]:'.format(
            default_gitlab_app_secret)) or default_gitlab_app_secret

    print('Save new configuration ?')

    for item in configuration:
        print('{}: {}'.format(item, configuration[item]))

    save_configuration = input('Save ? (y/n) [y]: ') or 'y'
    if save_configuration == 'y':
        # Write new configuration
        with open(config_path, 'w') as f:
            yaml.dump(configuration,
                      f,
                      default_flow_style=False,
                      allow_unicode=True)

        print('Configuration saved.')

    recreate_database = input('Recreate database ? (y/n) [n]: ') or 'n'
    if recreate_database == 'y':
        # Create empty database
        with app.app_context():

            # Create tables
            db.create_all()

            # Since we are running this script as root,
            # make sure that SQlite database (if used) is writable by application user
            database_configuration_info = urllib.parse.urlparse(
                configuration.get('SQLALCHEMY_DATABASE_URI'))

            if database_configuration_info.scheme == 'sqlite':
                os.chown(database_configuration_info.path,
                         get_user_id(app.config['USER']),
                         get_user_group_id(app.config['USER']))

            # Stamp database to lates migration
            stamp()

            print('Database has been created')

    restart_services = input(
        'Restart services to load new configuration ? (y/n) [n]: ') or 'n'
    if restart_services == 'y':
        subprocess.call(['systemctl', 'restart', 'gitlab-tools_celeryworker'])
        subprocess.call(['systemctl', 'restart', 'gitlab-tools_celerybeat'])
        subprocess.call(['systemctl', 'restart', 'gitlab-tools'])