示例#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)
示例#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)
示例#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'],
            threaded=True)
示例#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)
示例#5
0
def celerybeat():
    setup_logging('celerybeat')
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = [
        'celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s',
        OPTIONS['--schedule']
    ]
    with app.app_context():
        return celery_main(celery_args)
示例#6
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))
示例#7
0
def celerybeat():
    options = parse_options()
    setup_logging('celerybeat',
                  logging.DEBUG if options.DEBUG else logging.WARNING)
    app = create_app(options, no_sql=True)
    Logging._setup = True
    celery_args = [
        'celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s',
        OPTIONS['--schedule']
    ]
    with app.app_context():
        return celery_main(celery_args)
示例#8
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', 'blacklist', '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)

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

        configuration[
            'SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/blacklist/blacklist.db'

        # 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)
示例#9
0
def migrations() -> None:
    app = create_app(parse_options())
    manager = Manager(app)
    manager.add_command('migrations', MigrateCommand)
    manager.run()
示例#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', 'blacklist', '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:')

        connection_info = urllib.parse.urlparse(
            configuration.get('SQLALCHEMY_DATABASE_URI',
                              'sqlite:////home/blacklist/blacklist.db'))

        if connection_info.scheme == 'sqlite':
            database_path = connection_info.path
        else:
            database_path = '/home/blacklist/blacklist.db'

        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',
                '{}://blacklist:[email protected]/blacklist'.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 = 'blacklist'
            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

    database_types = {
        0: {
            'name': 'Ignore',
            'default': True,
            'call': ignore
        },
        1: {
            'name': 'SQLite',
            'default': False,
            'call': database_sqlite
        },
        2: {
            'name': 'PostgreSQL',
            '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 [{}]: '.format(webserver_host)) or webserver_host
    configuration['PORT'] = input(
        'Port [{}]: '.format(webserver_port)) or webserver_port

    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()

            # Stamp database to lates migration
            stamp()

            # Create roles
            roles = {
                Role.GUEST: 'Guest',
                Role.ADMIN: 'Administrator',
                Role.CUSTOMER: 'Customer',
                Role.MAINTENANCE: 'Maintenance',
            }

            for role in roles:
                found_role = Role.query.filter_by(id=role).first()
                if not found_role:
                    found_role = Role()
                    found_role.id = role
                found_role.name = roles[role]
                db.session.add(found_role)
                db.session.commit()

            # Create admin user and set password
            admin_username = '******'
            admin = User.query.filter_by(username=admin_username).first()
            new_password = random_password()
            if not admin:
                admin = User()
            admin.set_password(new_password)
            admin.username = admin_username
            for role in Role.query.all():
                admin.roles.append(role)

            db.session.add(admin)
            db.session.commit()

            print('Database has been created, use this credentials to log-in:')
            print('Username: {}'.format(admin_username))
            print('Password: {}'.format(new_password))

    restart_services = input(
        'Restart services to load new configuration ? (y/n) [n]: ') or 'n'
    if restart_services == 'y':
        subprocess.call(['systemctl', 'restart', 'blacklist_celeryworker'])
        subprocess.call(['systemctl', 'restart', 'blacklist_celerybeat'])
        subprocess.call(['systemctl', 'restart', 'blacklist'])
示例#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)
示例#12
0
"""
Bootstrap for use in uwsgi and so
"""

from blacklist.application import create_app, get_config

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