Skip to content

Commit

Permalink
🐘 Change to use postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
dankolbman committed Jan 31, 2018
1 parent 1602f38 commit 8de9d82
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jobs:
# specify the version you desire here
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
- image: circleci/python:3.6.1
- image: postgres:9.5
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=test

working_directory: ~/repo

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ADD . /app
EXPOSE 80
ENV FLASK_APP "manage.py"
ENV FLASK_CONFIG "production"
RUN ["flask", "db", "init"]
RUN ["flask", "db", "migrate"]
#RUN ["flask", "db", "init"]
RUN ["flask", "db", "upgrade"]
RUN ["flask", "db", "migrate"]
CMD ["gunicorn", "-b", ":80", "--access-logfile", "-", "manage:app", "--threads", "4"]
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ pip install -r requirements.txt
# Configure the flask application
export FLASK_APP=manage
# Setup the database
flask db init
docker run --name dataservice-pg -p 5432:5432 -d postgres:9.5
docker exec dataservice-pg psql -U postgres -c "CREATE DATABASE dev;"
flask db upgrade
# Run the flask web application
flask run
```

### Database

The example environment above uses docker to run a Postgres instance,
however, a local install of Postgres may be used just as easily.

The API should now be available at `localhost:5000/`.

## Documentation
Expand Down Expand Up @@ -58,6 +64,9 @@ Unit tests and pep8 linting is run via `flask test`
```
# Install test dependencies
pip install -r dev-requirements.txt
# Setup test database
docker run --name dataservice-pg -p 5432:5432 -d postgres
docker exec dataservice-pg psql -U postgres -c "CREATE DATABASE dev;"
# Run tests
flask test
```
Expand Down
39 changes: 24 additions & 15 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,43 @@ def init_app(app):
class DevelopmentConfig(Config):
DEBUG = True
SSL_DISABLE = True
SQLALCHEMY_DATABASE_URI = os.environ.get("DEV_DATABASE_URL") or \
"sqlite:///" + os.path.join(basedir, "data-dev.sqlite")
SQLALCHEMY_TRACK_MODIFICATIONS = True


class TestingConfig(Config):
SERVER_NAME = "localhost"
TESTING = True
WTF_CSRF_ENABLED = False
SQLALCHEMY_DATABASE_URI = os.environ.get("TEST_DATABASE_URL") or \
"sqlite:///" + os.path.join(basedir, "data-test.sqlite")
SQLALCHEMY_DATABASE_URI = 'postgres://postgres@localhost:5432/test'
SQLALCHEMY_TRACK_MODIFICATIONS = True


class ProductionConfig(Config):
# Should use postgres
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or \
"sqlite:///" + os.path.join(basedir, "data.sqlite")
@staticmethod
def init_app(app):
import hvac

@classmethod
def init_app(cls, app):
Config.init_app(app)
vault_url = os.environ.get('VAULT_URL', 'https//vault/')
# Role to authenticate with
iam_role = os.environ.get('IAM_ROLE', 'PostgresRole')
# Path for the postgres secret in vault
pg_secret = os.environ.get('DB_SECRET', 'secret/postgres')

# email errors to the administrators
import logging
from logging.handlers import SMTPHandler
credentials = None
secure = None
# Retrieve postgres secrets
client = hvac.Client(url=vault_url)
client.auth_iam(iam_role)
secrets = client.read(pg_secret)
client.logout()

pg_host = os.environ.get('PG_HOST', 'localhost')
pg_name = os.environ.get('PG_NAME', 'prod')
pg_user = secrets['data']['username']
pg_pass = secrets['data']['password']

connection_str = 'postgres://{}:{}@{}:{}/{}'.format(
pg_user, pg_pass, pg_host, pg_port, pg_name)

app.config['SQLALCHEMY_DATABASE_URI'] = connection_str


class UnixConfig(ProductionConfig):
Expand Down
1 change: 1 addition & 0 deletions dataservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def create_app(config_name):
app = Flask(__name__)
app.url_map.strict_slashes = False
app.config.from_object(config[config_name])
config[config_name].init_app(app)

# Register Flask extensions
register_extensions(app)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ gunicorn==19.7.1
flask-marshmallow==0.8.0
marshmallow==2.15.0
marshmallow-sqlalchemy==0.13.2
psycopg2==2.7.3.2
-e git+git@github.com:dankolbman/hvac.git#egg=hvac
10 changes: 7 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
from dataservice import create_app
from dataservice.extensions import db

@pytest.yield_fixture
def app():
return create_app('testing')

@pytest.fixture
def client():
app = create_app('testing')
@pytest.yield_fixture
def client(app):
app_context = app.app_context()
app_context.push()
db.create_all()
yield app.test_client()
# Need to make sure we close all connections so pg won't lock tables
db.session.close()
db.drop_all()

0 comments on commit 8de9d82

Please sign in to comment.