Ejemplo n.º 1
0
def list_routes() -> None:
    output = []
    app = create_app(parse_options())
    app.config['SERVER_NAME'] = 'example.com'
    with app.app_context():
        for rule in app.url_map.iter_rules():

            integer_replaces = {}
            options = {}
            integer = 0
            for arg in rule.arguments:
                options[arg] = str(integer)
                integer_replaces[str(integer)] = "[{0}]".format(arg)
                integer = +1

            methods = ','.join(rule.methods)
            url = flask.url_for(rule.endpoint, **options)
            for integer_replace in integer_replaces:
                url = url.replace(integer_replace,
                                  integer_replaces[integer_replace])
            line = urllib.parse.unquote("{:50s} {:20s} {}".format(
                rule.endpoint, methods, url))
            output.append(line)

        for line in sorted(output):
            print(line)
Ejemplo n.º 2
0
def celerydev():
    setup_logging('celerydev')
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True  # Disable Celery from setting up logging, already done in setup_logging().
    celery_args = [
        'celery', 'worker', '-B', '-s', '/tmp/celery.db', '--concurrency=5'
    ]
    with app.app_context():
        return celery_main(celery_args)
Ejemplo n.º 3
0
def server() -> None:
    options = parse_options()
    setup_logging('server',
                  logging.DEBUG if options.DEBUG else logging.WARNING)
    app = create_app(options)
    log_messages(app)
    app.run(host=app.config['HOST'],
            port=int(app.config['PORT']),
            debug=app.config['DEBUG'])
Ejemplo n.º 4
0
def celeryworker():
    setup_logging('celeryworker{}'.format(OPTIONS['--name']))
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = [
        'celery', 'worker', '-n', OPTIONS['--name'], '-C', '--autoscale=10,1',
        '--without-gossip'
    ]
    with app.app_context():
        return celery_main(celery_args)
Ejemplo n.º 5
0
def celerybeat():
    setup_logging('celerybeat')
    app = create_app(parse_options())
    Logging._setup = True
    celery_args = [
        'celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s',
        OPTIONS['--schedule']
    ]
    with app.app_context():
        return celery_main(celery_args)
Ejemplo n.º 6
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.º 7
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))
Ejemplo n.º 8
0
def celerybeat():
    options = parse_options()
    setup_logging('celerybeat',
                  logging.DEBUG if options.DEBUG else logging.WARNING)
    app = create_app(options)
    Logging._setup = True
    celery_args = [
        'celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s',
        OPTIONS['--schedule']
    ]
    with app.app_context():
        return celery_main(celery_args)
Ejemplo n.º 9
0
def migrations() -> None:
    app = create_app(parse_options())
    manager = Manager(app)
    manager.add_command('migrations', MigrateCommand)
    manager.run()
Ejemplo n.º 10
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'])
Ejemplo n.º 11
0
def shell() -> None:
    setup_logging('shell')
    app = create_app(parse_options())
    app.app_context().push()
    Shell(make_context=lambda: dict(app=app, db=db)).run(no_ipython=False,
                                                         no_bpython=False)
Ejemplo n.º 12
0
"""
Bootstrap for use in uwsgi and so
"""

from gitlab_tools.application import create_app, get_config

config = get_config('gitlab_tools.config.Production')
app = create_app(config)