Skip to content

Commit

Permalink
Deploy (#2)
Browse files Browse the repository at this point in the history
* add heroku deployment

* fix python version

* fix python version

* fix python version

* fix python version

* fix python version

* fix python version

* store certs in heroku

* fix release command

* fix release command

* fix release command

* fix release command

* cleanup

* deploy

* python version
  • Loading branch information
Ivan-Feofanov authored Feb 21, 2021
1 parent abc8fb2 commit 66fd6d5
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 118 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
release:
types: [created]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Cloning repo
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Push to dokku
uses: dokku/github-action@master
env:
APP_NAME: kafka-python-example
HOST: ovz2.feofanov.mydem.vps.myjino.ru
PORT: 49279
with:
git_remote_url: 'ssh://dokku@$HOST:$PORT/$APP_NAME'
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.8 ]
python-version: [ 3.8.8 ]

# Service containers to run with `runner-job`
services:
# Label used to access the service container
postgres:
image: postgres:13
env:
POSTGRES_DB: test
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432

steps:
Expand Down
1 change: 1 addition & 0 deletions .prospector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pylint:
- unused-argument
- too-few-public-methods
- too-many-arguments
- not-a-mapping
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: uvicorn main:app --host=0.0.0.0 --port=${PORT:-5000}
receiver: python receiver.py
62 changes: 40 additions & 22 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@


class AuthEnum(str, Enum):
off = 'off'
ssl = 'ssl'
basic = 'basic'
OFF = 'OFF'
SSL = 'SSL'
SASL = 'SASL_SSL'


class Settings(BaseSettings):
root_dir: str = PROJECT_DIR
database_url: PostgresDsn
kafka_url: str = None
kafka_topic: str = None
Expand All @@ -27,35 +26,54 @@ class Settings(BaseSettings):
kafka_auth_ca: str = None
kafka_auth_cert: str = None
kafka_auth_pkey: str = None
kafka_auth_username: str = None
kafka_auth_password: str = None
kafka: Dict = None

class Config:
env_file = ".env"

@validator('kafka_url', 'kafka_topic', 'kafka_consumer_group', pre=True)
@classmethod
@validator('kafka_url', 'kafka_topic', 'kafka_consumer_group', pre=True)
def pass_in_test_env(cls, value, values, field):
if os.environ.get('ENVIRONMENT') != 'test' and value is None:
raise ValueError(f'Config var {field.name} is required')
return value


@lru_cache
def get_settings():
settings = Settings()

settings.kafka = dict(bootstrap_servers=settings.kafka_url)

# TODO: add password auth
if settings.kafka_auth == 'ssl' and all([settings.kafka_auth_ca,
settings.kafka_auth_cert,
settings.kafka_auth_pkey]):
settings.kafka['security_protocol'] = 'SSL'
settings.kafka['ssl_cafile'] = os.path.join(
settings.root_dir, settings.kafka_auth_ca)
settings.kafka['ssl_certfile'] = os.path.join(
settings.root_dir, settings.kafka_auth_cert)
settings.kafka['ssl_keyfile'] = os.path.join(
settings.root_dir, settings.kafka_auth_pkey)
def set_kafka_auth(settings: Settings) -> Settings:
settings.kafka = dict(
bootstrap_servers=settings.kafka_url,
)

if settings.kafka_auth == AuthEnum.SASL and all([
settings.kafka_auth_username,
settings.kafka_auth_password]):

settings.kafka.update({
'security_protocol': AuthEnum.SASL,
'sasl_mechanism': 'PLAIN',
'sasl_plain_username': settings.kafka_auth_username,
'sasl_plain_password': settings.kafka_auth_password,
# 'ssl_cafile': os.path.join(PROJECT_DIR, settings.kafka_auth_ca)
})

elif settings.kafka_auth == AuthEnum.SSL and all([
settings.kafka_auth_ca,
settings.kafka_auth_cert,
settings.kafka_auth_pkey]):

settings.kafka.update({
'security_protocol': AuthEnum.SSL,
'ssl_certfile': os.path.join(PROJECT_DIR,
settings.kafka_auth_cert),
'ssl_keyfile': os.path.join(PROJECT_DIR, settings.kafka_auth_pkey),
'ssl_cafile': os.path.join(PROJECT_DIR, settings.kafka_auth_ca)
})

return settings


@lru_cache
def get_settings() -> Settings:
return set_kafka_auth(Settings())
13 changes: 8 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


# Dependencies
@lru_cache
# One session per request
def get_db():
session = SessionLocal()
try:
Expand All @@ -28,16 +28,19 @@ def get_db():
session.close()


# One Kafka producer for all requests
@lru_cache
def get_kafka():
producer = KafkaProducer(
value_serializer=msgpack.packb,
**settings.kafka
)
try:
yield producer
finally:
producer.close()
return producer


@app.on_event("shutdown")
def shutdown_event(producer: KafkaProducer = Depends(get_kafka)):
producer.close()


@app.post("/messages/")
Expand Down
Loading

0 comments on commit 66fd6d5

Please sign in to comment.