Skip to content
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

Merged
merged 21 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .coveragerc
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.egg-info
.cache
*.pyc
htmlcov
.coverage
*/wheelhouse
*.sql
33 changes: 33 additions & 0 deletions Makefile
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
24 changes: 23 additions & 1 deletion README.md
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.
19 changes: 19 additions & 0 deletions circle.yml
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
41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: "2"
services:
rabbit:
container_name: nameko-example-rabbitmq
image: rabbitmq:3.6-management
ports:
- "15673:15672" # Exposing RabbitMQ web management on different port for convenience
restart: always

postgres:
container_name: nameko-example-postgres
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: nameko-example-orders
image: nameko/nameko-example-orders:latest
depends_on:
- rabbit
- postgres
ports:
- "8001:8000"
links:
- "rabbit:nameko-example-rabbitmq"
- "postgres:nameko-example-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: "5672"
RABBIT_MANAGEMENT_PORT: "15672"
14 changes: 14 additions & 0 deletions docker/docker.base
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
23 changes: 23 additions & 0 deletions docker/docker.build
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 .
9 changes: 9 additions & 0 deletions orders/Makefile
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 .;
65 changes: 65 additions & 0 deletions orders/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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

# 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
1 change: 1 addition & 0 deletions orders/alembic/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
84 changes: 84 additions & 0 deletions orders/alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import with_statement
import os
from alembic import context
from sqlalchemy import create_engine
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 = create_engine(get_url())

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()
24 changes: 24 additions & 0 deletions orders/alembic/script.py.mako
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"}