Skip to content
Permalink
Browse files
Extract database out of project, add alembic config
  • Loading branch information
brogand93 committed Jun 16, 2014
1 parent e03a7c4 commit 841364e96df7f2bbdc634a1b4026b5cda083d7ee
Showing 16 changed files with 373 additions and 8 deletions.
@@ -0,0 +1,59 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = migrations

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

sqlalchemy.url = driver://user:pass@localhost/dbname


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
@@ -21,6 +21,7 @@
import sys

from flask import Flask
from gstack.core import db
from flask.ext.sqlalchemy import SQLAlchemy


@@ -35,28 +36,37 @@ def _load_config_file():

return config_file

def _load_database():
database_file = os.path.join(
os.path.expanduser('~'),
'.gstack/gstack.sqlite'
)

if not os.path.exists(database_file):
sys.exit('No database found, please run gstack-configure')

return 'sqlite:///' + database_file


def configure_app(settings=None):
app.config['DATA'] = os.path.abspath(os.path.dirname(__file__)) + '/data'

db.init_app(app)
database_uri = _load_database()

if settings:
app.config.from_object(settings)
else:
config_file = _load_config_file()
app.config.from_pyfile(config_file)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
os.path.join(app.config['DATA'], 'app.db')
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri


app = Flask(__name__)

db = SQLAlchemy(app)
publickey_storage = {}

from gstack.controllers import *


basedir = os.path.abspath(os.path.dirname(__file__))

db.create_all()
@@ -19,10 +19,14 @@

import os

from alembic import command
from alembic.config import Config as AlembicConfig


def main():
config_folder = _create_config_folder()
_create_config_file(config_folder)
_create_database()


def _create_config_folder():
@@ -69,3 +73,12 @@ def _create_config_file(config_folder):
config_file.write('CLOUDSTACK_PATH = \'%s\'\n' % cloudstack_path)

config_file.close()

def _create_database():
directory = os.path.join(os.path.dirname(__file__), '../migrations')
config = AlembicConfig(os.path.join(
directory,
'alembic.ini'
))
config.set_main_option('script_location', directory)
command.upgrade(config, 'head', sql=False, tag=None)
@@ -295,7 +295,6 @@ def addinstance(authorization, projectid, zone):
args = {}
args['name'] = data['name']
args['serviceoffering'] = data['machineType'].rsplit('/', 1)[1]
print args['serviceoffering']
args['template'] = data['disks'][0]['initializeParams']['sourceImage'].rsplit('/', 1)[1]
args['zone'] = zone

@@ -0,0 +1,49 @@
#!/usr/bin/env python
# encoding: utf-8
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class Service(object):
__model__ = None

def _isinstance(self, model, raise_error=True):
valid_type = isinstance(model, self.__model__)
if not valid_type and raise_error:
raise ValueError('%s is not of type %s' % (model, self.__model__))
return valid_type

def save(self, model):
self._isinstance(model)
db.session.add(model)
db.session.commit()
return model

def get(self, primarykey):
return self.__model__.query.get(primarykey)

def create(self, **kwargs):
return self.save(self.__model__(**kwargs))

def delete(self, model):
self._isinstance(model)
db.session.delete(model)
db.session.commit()
BIN -11 KB gstack/data/app.db
Binary file not shown.
@@ -33,7 +33,6 @@ def create_response(data):
def successful_response(**kwargs):
content = render_template(**kwargs)
response = make_response(content)
print response
response.headers['Content-Type'] = 'application/json'
return _create_response(response, '200')

@@ -21,7 +21,14 @@


class AccessToken(db.Model):
__tablename__ = 'accesstoken'
access_token = db.Column(db.String(100), primary_key=True, unique=True)
client_id = db.Column(db.String(100), unique=True)
expires_in = db.Column(db.Integer)
data = db.Column(db.String(500))

def __init__(self, access_token, client_id, expires_in, data):
self.access_token = access_token
self.client_id = client_id
self.expires_in = expires_in
self.data = data
@@ -21,5 +21,10 @@


class Client(db.Model):
__tablename__ = 'client'
client_id = db.Column(db.String(100), primary_key=True, unique=True)
client_secret = db.Column(db.String(100), unique=True)

def __init__(self, client_id, client_secret):
self.client_id = client_id
self.client_secret = client_secret
@@ -21,6 +21,12 @@


class RefreshToken(db.Model):
__tablename__ = 'refreshtoken'
refresh_token = db.Column(db.String(100), primary_key=True, unique=True)
client_id = db.Column(db.String(100), unique=True)
data = db.Column(db.String(500))

def __init__(self, refresh_token, client_id, data):
self.refresh_token = refresh_token
self.client_id = client_id
self.data = data
@@ -0,0 +1 @@
Generic single-database configuration.
@@ -0,0 +1,57 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = .

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
@@ -0,0 +1,77 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

import os
from gstack.core import db

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)


# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

database_file = os.path.join(
os.path.expanduser('~'),
'.gstack/gstack.sqlite'
)

config.set_main_option('sqlalchemy.url', 'sqlite:///' + database_file)


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(url=url, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()

def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(
connection=connection,
target_metadata=db.metadata
)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

0 comments on commit 841364e

Please sign in to comment.