-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Orders #1
Orders #1
Changes from 17 commits
c1f9fed
5aeae14
1ee0aac
ab7626b
21a366a
a764a58
99d9420
fc3d2d8
3b8a069
3407f57
21dd931
6aa3e05
561f0be
3cbdb51
8c50602
c5f9ffe
44f8837
4411a5d
5642aba
7fcffc7
02e4563
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[run] | ||
concurrency = eventlet | ||
|
||
omit = | ||
*/test/* | ||
*/setup.py | ||
|
||
source = orders | ||
|
||
[html] | ||
directory = htmlcov |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.egg-info | ||
.cache | ||
*.pyc | ||
htmlcov | ||
.coverage | ||
*/wheelhouse | ||
*.sql |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
HTMLCOV_DIR ?= htmlcov | ||
|
||
IMAGES := orders | ||
|
||
# test | ||
|
||
coverage-html: | ||
coverage html -d $(HTMLCOV_DIR) --fail-under 100 | ||
|
||
coverage-report: | ||
coverage report -m | ||
|
||
test: | ||
flake8 orders | ||
coverage run -m pytest */test $(ARGS) | ||
|
||
coverage: test coverage-report coverage-html | ||
|
||
# docker | ||
|
||
build-example-base: | ||
docker build -t nameko-example-base -f docker/docker.base .; | ||
|
||
build-wheel-builder: build-example-base | ||
docker build -t nameko-example-builder -f docker/docker.build .; | ||
|
||
run-wheel-builder: build-wheel-builder | ||
for image in $(IMAGES) ; do make -C $$image run-wheel-builder; done | ||
|
||
build-images: run-wheel-builder | ||
for image in $(IMAGES) ; do make -C $$image build-image; done | ||
|
||
build: build-images |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
# Nameko Examples | ||
|
||
Work in progress | ||
[![CircleCI](https://circleci.com/gh/nameko/nameko-examples/tree/orders.svg?style=svg)](https://circleci.com/gh/nameko/nameko-examples/tree/orders) | ||
|
||
TODO: Overview of the project | ||
|
||
# Prerequisites | ||
|
||
* Python 3 | ||
* [Docker](https://www.docker.com/) | ||
* [Docker Compose](https://docs.docker.com/compose/) | ||
|
||
# Run tests | ||
|
||
Ensure RabbitMQ is running. | ||
|
||
`$ make coverage` | ||
|
||
# Run docker compose | ||
|
||
Quickest way to try out examples is to run them with [Docker Compose](https://docs.docker.com/compose/) | ||
|
||
`$ docker-compose up` | ||
|
||
Docker images for [RabbitMQ](https://hub.docker.com/_/rabbitmq/), [PostgreSQL](https://hub.docker.com/_/postgres/) and [Redis](https://hub.docker.com/_/redis/) will be automatically downloaded and their containers linked to our service containers. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
machine: | ||
python: | ||
version: 3.4.3 | ||
services: | ||
- docker | ||
- rabbitmq-server | ||
pre: | ||
- sudo rabbitmq-plugins enable rabbitmq_management | ||
|
||
dependencies: | ||
pre: | ||
- pip install -U pip wheel setuptools | ||
- cd orders; pip install -U .[dev] | ||
|
||
test: | ||
override: | ||
- HTMLCOV_DIR=$CIRCLE_ARTIFACTS make coverage | ||
post: | ||
- make build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
version: "2" | ||
services: | ||
rabbit: | ||
container_name: rabbitmq | ||
image: rabbitmq:3.6-management | ||
ports: | ||
- "15673:15672" # Exposing RabbitMQ web management on different port for convenience | ||
restart: always | ||
|
||
postgres: | ||
container_name: postgres_db | ||
image: postgres | ||
ports: | ||
- "5433:5432" # Exposing Postgres on different port for convenience | ||
environment: | ||
POSTGRES_DB: "orders" | ||
POSTGRES_PASSWORD: "password" | ||
POSTGRES_USER: "postgres" | ||
restart: always | ||
|
||
orders: | ||
container_name: orders | ||
image: nameko/nameko-example-orders:latest | ||
depends_on: | ||
- rabbit | ||
- postgres | ||
ports: | ||
- "8001:8000" | ||
links: | ||
- "rabbit:rabbit" | ||
- "postgres:postgres" | ||
environment: | ||
DB_PASSWORD: "password" | ||
DB_USER: "postgres" | ||
DB_HOST: "postgres" | ||
DB_NAME: "orders" | ||
RABBIT_PASSWORD: "guest" | ||
RABBIT_USER: "guest" | ||
RABBIT_HOST: "rabbit" | ||
RABBIT_PORT: "15672" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be 5672 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there is a problem here: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM debian:jessie | ||
|
||
RUN apt-get update && \ | ||
apt-get install -qyy \ | ||
-o APT::Install-Recommends=false -o APT::Install-Suggests=false \ | ||
python3 python-pip ca-certificates libpq-dev python-psycopg2 curl && \ | ||
cd /usr/local/bin && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | ||
|
||
RUN pip install virtualenv | ||
|
||
RUN virtualenv -p python3 /appenv | ||
RUN . /appenv/bin/activate; pip install -U pip |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM nameko-example-base | ||
|
||
RUN apt-get update && \ | ||
apt-get install -qyy \ | ||
-o APT::Install-Recommends=false -o APT::Install-Suggests=false \ | ||
build-essential python3-dev && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | ||
|
||
RUN . /appenv/bin/activate; \ | ||
pip install wheel | ||
|
||
|
||
ENV WHEELHOUSE=/wheelhouse | ||
ENV PIP_WHEEL_DIR=/wheelhouse | ||
ENV PIP_FIND_LINKS=/wheelhouse | ||
|
||
VOLUME /wheelhouse | ||
VOLUME /application | ||
|
||
CMD . /appenv/bin/activate; \ | ||
cd /application; \ | ||
pip wheel . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# docker | ||
|
||
run-wheel-builder: | ||
docker run --rm \ | ||
-v "$$(pwd)":/application -v "$$(pwd)"/wheelhouse:/wheelhouse \ | ||
nameko-example-builder; | ||
|
||
build-image: | ||
docker build -t nameko/nameko-example-orders:latest -f docker.run .; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# path to migration scripts | ||
script_location = alembic | ||
|
||
# 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 | ||
|
||
# version location specification; this defaults | ||
# to alembic/versions. When using multiple version | ||
# directories, initial revisions must be specified with --version-path | ||
# version_locations = %(here)s/bar %(here)s/bat alembic/versions | ||
|
||
# the output encoding used when revision files | ||
# are written from script.py.mako | ||
# output_encoding = utf-8 | ||
|
||
sqlalchemy.url = postgresql://postgres:password@postgres/orders | ||
|
||
|
||
# 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.stdout,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generic single-database configuration. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from __future__ import with_statement | ||
import os | ||
from alembic import context | ||
from sqlalchemy import engine_from_config, pool | ||
from logging.config import fileConfig | ||
|
||
from orders.models import DeclarativeBase | ||
|
||
|
||
# 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) | ||
|
||
# add your model's MetaData object here | ||
# for 'autogenerate' support | ||
# from myapp import mymodel | ||
# target_metadata = mymodel.Base.metadata | ||
target_metadata = DeclarativeBase.metadata | ||
|
||
# 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. | ||
|
||
|
||
def get_url(): | ||
return ( | ||
"postgresql://{db_user}:{db_pass}@{db_host}:" | ||
"{db_port}/{db_name}" | ||
).format( | ||
db_user=os.getenv("DB_USER", "postgres"), | ||
db_pass=os.getenv("DB_PASSWORD", "password"), | ||
db_host=os.getenv("DB_HOST", "localhost"), | ||
db_port=os.getenv("DB_PORT", "5432"), | ||
db_name=os.getenv("DB_NAME", "orders"), | ||
) | ||
|
||
|
||
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 = get_url() | ||
context.configure( | ||
url=url, target_metadata=target_metadata, literal_binds=True) | ||
|
||
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. | ||
|
||
""" | ||
connectable = engine_from_config( | ||
config.get_section(config.config_ini_section), | ||
prefix='sqlalchemy.', | ||
poolclass=pool.NullPool) | ||
|
||
with connectable.connect() as connection: | ||
context.configure( | ||
connection=connection, | ||
target_metadata=target_metadata | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
branch_labels = ${repr(branch_labels)} | ||
depends_on = ${repr(depends_on)} | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
${imports if imports else ""} | ||
|
||
def upgrade(): | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These generic container names (
rabbitmq
,postgres_db
,orders
) could clash with other existing containers running in the docker machine. Since these are just used for the nameko example, we could prepend some kind of prefix to them to make more explicit what they are used for (likenameko-example-...
). What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the same. I'll add more explicit prefixes.