Exemple #1
0
def check_database():
    app.logger.info('Checking database "{}"'.format(
        app.config['SQLALCHEMY_DATABASE_URI'], ))

    try:
        # Can we connect to the DB
        db.engine.execute('select 1')
    except sqlalchemy.exc.OperationalError:
        app.logger.error("Cannot connect to the database, please check the "
                         "database and configuration.")
        raise SystemExit(1)

    try:
        with app.app_context():
            current_head = alembic.script_directory.get_current_head()
            current_rev = alembic.migration_context.get_current_revision()
            app.logger.debug('Current revision: {}'.format(current_rev))
            app.logger.debug('Current head: {}'.format(current_head))
            if current_head is None:
                raise OrloStartupError('No alembic revisions, this is a bug')
            elif current_rev is None:
                app.logger.info('Database not configured, calling alembic '
                                'upgrade')
                try:
                    alembic.upgrade()
                except sqlalchemy.exc.ProgrammingError as e:
                    # if this occurs on upgrade from None, it's probably because
                    # tables already existed
                    app.logger.error("Database migration failed: {}".format(
                        e.message))
                    if e.message.endswith(
                            'relation "platform" already exists\n'):
                        app.logger.warning(
                            "This error is expected when installing an "
                            "alembic-enabled build for the first time, "
                            "continuing")
                        stamp_initial_revision()
                    else:
                        raise
            elif current_head != current_rev:
                app.logger.info('New database revision available, calling '
                                'alembic upgrade')
                alembic.upgrade()
    except sqlalchemy.exc.OperationalError:
        if 'sqlite' in app.config['SQLALCHEMY_DATABASE_URI']:
            app.logger.warning('Database is not configured, creating tables')
            db.create_all()
            stamp_initial_revision()
    except alembic_exc.CommandError:
        app.logger.error(
            'Alembic raised an exception, please check the state of the '
            'database, and that there aren\'t any extra files in '
            '/opt/venvs/orlo/local/lib/python2.7/site-packages/orlo/'
            'migrations. Exception was:\n{}'.format(traceback.format_exc()))
        raise SystemExit(1)
    finally:
        alembic.migration_context.connection.close()

    app.logger.info('Database is configured')
Exemple #2
0
def setup_database(args):
    from orlo.orm import db
    from orlo.config import config

    if config.get('db', 'uri') == 'sqlite://':
        print("Warning: setting up in-memory database, this is "
              "probably not what you want!\n"
              "Please configure db:uri in /etc/orlo/orlo.ini")
    db.create_all()
Exemple #3
0
 def setUp(self):
     db.create_all()
     self.mockldap.start()
     self.ldapobj = self.mockldap['ldap://localhost/']
     self.orig_security_enabled = orlo.config.get('security', 'enabled')
     self.orig_security_secret_key = orlo.config.set('security', 'secret_key')
     self.orig_security_ldap_server = orlo.config.set('security', 'ldap_server')
     self.orig_security_ldap_port = orlo.config.set('security', 'ldap_port')
     self.orig_security_user_base_dn = orlo.config.set('security', 'user_base_dn')
     orlo.config.set('security', 'enabled', 'true')
     orlo.config.set('security', 'secret_key', 'It does not matter how slowly you go so long as you do not stop')
     orlo.config.set('security', 'ldap_server', 'localhost')
     orlo.config.set('security', 'ldap_port', '389')
     orlo.config.set('security', 'user_base_dn', 'ou=people,ou=example,o=test')
Exemple #4
0
 def setUp(self):
     db.create_all()
     self.mockldap.start()
     self.ldapobj = self.mockldap['ldap://localhost/']
     self.orig_security_enabled = orlo.config.get('security', 'enabled')
     self.orig_security_secret_key = orlo.config.set(
         'security', 'secret_key')
     self.orig_security_ldap_server = orlo.config.set(
         'security', 'ldap_server')
     self.orig_security_ldap_port = orlo.config.set('security', 'ldap_port')
     self.orig_security_user_base_dn = orlo.config.set(
         'security', 'user_base_dn')
     orlo.config.set('security', 'enabled', 'true')
     orlo.config.set(
         'security', 'secret_key', 'It does not matter how '
         'slowly you go so long as '
         'you do not stop')
     orlo.config.set('security', 'ldap_server', 'localhost')
     orlo.config.set('security', 'ldap_port', '389')
     orlo.config.set('security', 'user_base_dn',
                     'ou=people,ou=example,o=test')
 def setUp(self):
     db.create_all()
     self._import_doc()
Exemple #6
0
 def setUp(self):
     db.get_engine(self.app).dispose()
     db.create_all()
     db.session.begin_nested()
Exemple #7
0
 def setUp(self):
     db.create_all()
Exemple #8
0
 def setUp(self):
     # super(DeployTest, self).setUp()
     db.create_all()
     rid = self._create_release()
     pid = self._create_package(rid)
     self.release = db.session.query(Release).first()
 def setUp(self):
     db.create_all()
     self._import_doc()
Exemple #10
0
 def setUp(self):
     db.create_all()
     db.session.begin_nested()
Exemple #11
0
    db.session.commit()
    return (jsonify({'username': user.username}), 201,
            {'Location': url_for('get_user', id=user.id, _external=True)})


@app.route('/auth/users/<int:id>')
def get_user(id):
    user = User.query.get(id)
    if not user:
        abort(400)
    return jsonify({'username': user.username})


@app.route('/auth/token')
@auth.login_required
def get_auth_token():
    token = g.user.generate_auth_token(600)
    return jsonify({'token': token.decode('ascii'), 'duration': 600})


@app.route('/auth/resource')
@auth.login_required
def get_resource():
    return jsonify({'data': 'Hello, %s!' % g.user.username})


if __name__ == '__main__':
    if not os.path.exists('db.sqlite'):
        db.create_all()
    app.run(debug=True)
 def setUp(self):
     db.get_engine(self.app).dispose()
     db.create_all()
     db.session.begin_nested()
Exemple #13
0
 def setUp(self):
     # db.engine.dispose()
     db.create_all()
     db.session.begin_nested()
Exemple #14
0
 def setUp(self):
     db.create_all()
     db.session.begin_nested()