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

[233] Add lambda handler and deployment steps #306

Merged
merged 5 commits into from
Sep 20, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ env:
- secure: "JggIiR6ZzXMoK7uCxMgB4nkPzWl7aYXTvIUlkjDdt0OHHySMAEv1D6m45npjCYTqSXwiEuNSplRCMvK72Br1PfWl6hu3d+Nr6DxDTjVu0Xs2jW55h5+A+VNS/Df7Rb2cQ7Ei9e+KI2yoD5oF4ayp62P0RXu060f/9jVnZNFHmR0yJ97eZLcSuY2nmxfQXK/ADnhKIOg0wk2MEnKPlVjzfFBFhvM7h+fmWGaD02QqBfijfnbH6vRdEWICvdkY3eE0Ah7fnETEYMbGVdzGFBouFx66BRKcBFZPbkRtfDNbkIjiClQjWa/HpeXjNxSfgdVrse826qBUN2FrUMR1lxUq4lzSJqNBEQq12if19RFaI12grLo7zfgDIUTOCgXtR+9hGrFredweE7E+q4SmmeFKrzI3fElnEq3PjCzOMMYkF0u3Fvhm9yncZ52SiZYovF4ws6FNxlNud6t6u4jgvphr6fUAHq94g/lLfPgxM/LEIfCaXYHAQ1bKH9MGdqP8zTmQC0OQ2QcpUFl961fiUowIawGrUnkddQGB1AsVlIYehMhVYIhvV7DI9X9ZpYbbADS9WKy2FTzD2KezuJrmZHUumTVT22ZMGai0wP/deLaNEklT6p5qgid4d+ifSvOwaBSTghqpktE6/DbkcgAx4I8o8RkGsl6kucdy8USCRYTHWAE="

install:
- pip install awscli
- nvm install 10.13.0
- (cd frontend && yarn install)
- pip install -r backend/requirements.txt
- pip install awscli
- (cd backend && pip install -r requirements.txt)


script:
Expand Down
2 changes: 1 addition & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DevelopmentConfig(Config):
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
DB_HOSTNAME = os.environ.get("DB_HOST")
DB_PORT = 5432
DB_PORT = os.environ.get("DB_PORT") or 5432
DB_NAME = os.environ.get("DB_NAME")

SQLALCHEMY_DATABASE_URI = (
Expand Down
4 changes: 1 addition & 3 deletions backend/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/bash

export FLASK_APP=app.py
export FLASK_ENV=development
wait-for-it ${DB_HOST}:5432
wait-for-it "${DB_HOST}:${DB_PORT:-5432}"
flask db migrate
flask db upgrade
flask run --host=0.0.0.0
62 changes: 62 additions & 0 deletions backend/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
import sys
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr

from flask_migrate import init, stamp, migrate, upgrade
from serverless_wsgi import handle_request

from backend.app import create_app


def api(event, context):
event["headers"]["Host"] = os.environ["BASE_URL"]
print("REQUEST: ", event)
return handle_request(create_app(), event, context)


def migration(event, context):
out, err = wrap_io(migrate)

if "Please use the 'init' command" in err:
wrap_io(init)
out, err = wrap_io(migrate)

if "Target database is not up to date" in err:
wrap_io(stamp)
wrap_io(migrate)
out, err = wrap_io(upgrade)

return {"stdout": out, "stderr": err}


# TODO: rewrite as contextmanager?
def wrap_io(fn: callable) -> (str, str):
out = StringIO()
err = StringIO()

# noinspection PyBroadException
try:
with redirect_stderr(err):
with redirect_stdout(out):
with create_app().app_context():
print(f"Running {fn.__name__}")
fn()

# catch-all case for potential SystemExit(1)
except BaseException:
pass

out = out.getvalue()
err = err.getvalue()

# print captured outputs
sys.stdout.write(f"stdout: {out}")
sys.stderr.write(f"stderr: {err}")

# a cheap state caching trick, but does the job
wrap_io._out = getattr(wrap_io, "_out", "") + out
wrap_io._err = getattr(wrap_io, "_err", "") + err
out, err = wrap_io._out, wrap_io._err

return out, err
5 changes: 3 additions & 2 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Flask==1.0.2
Flask-SQLAlchemy==2.4.0
Flask-SQLAlchemy==2.4.4
Flask-Migrate==2.3.0
psycopg2==2.8.3
psycopg2-binary==2.8.5
pytest==5.0.1
marshmallow==3.3.0
flask-jwt-extended==3.14.0
Expand All @@ -12,3 +12,4 @@ tqdm==2.2.3
flask-restful==0.3.7
pytest-cov==2.8.1
black==19.10b0
serverless_wsgi==1.7.5
8 changes: 6 additions & 2 deletions backend/resources/contact.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from http import HTTPStatus

from flask import request
Expand All @@ -18,11 +20,13 @@ def post(self):
except ValidationError as err:
return {"message": err.messages}, HTTPStatus.BAD_REQUEST
name, phone, content = new_msg["name"], new_msg["phone"], new_msg["content"]
base_url = os.environ.get("BASE_URL", "codeforpoznan.pl")

msg = Message(
subject=f"Email z cfp_v3 od {name}",
subject=f"Email z {base_url} od {name}",
sender=new_msg["email"],
reply_to=new_msg["email"],
recipients=["[email protected]"],
recipients=[f"hello@{base_url}"],
)
msg.body = f"Nowa wiadomość od {name}, nr tel: {phone} \nTreść:\n {content}"
mail.send(msg)
Expand Down
55 changes: 46 additions & 9 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
#!/usr/bin/env bash

# build and push frontend
pushd frontend
yarn run build
aws s3 sync dist s3://codeforpoznan-public/codeforpoznan.pl_v3
aws cloudfront create-invalidation --paths "/*" --distribution-id E6PZCV3N5WWJ8
popd

# build and push backend
pip install --quiet -r backend/requirements.txt --target packages
echo "build and push frontend"
(cd frontend && yarn run build && cp -r dist ../public)
aws s3 sync --delete public s3://codeforpoznan-public/dev_codeforpoznan_pl_v3
aws cloudfront create-invalidation --paths "/*" --distribution-id E6PZCV3N5WWJ8

echo "bundle application"
pip install -r backend/requirements.txt --target packages
(cd packages && zip -qgr9 ../lambda.zip .)
ln -s backend/migrations migrations
zip --symlinks -qgr9 lambda.zip backend/ migrations/

echo "upload lambdas"
aws s3 cp lambda.zip s3://codeforpoznan-lambdas/dev_codeforpoznan_pl_v3_serverless_api.zip
aws s3 cp lambda.zip s3://codeforpoznan-lambdas/dev_codeforpoznan_pl_v3_migration.zip

echo "refresh lambdas"
aws lambda update-function-code \
--function-name dev_codeforpoznan_pl_v3_serverless_api \
--s3-bucket codeforpoznan-lambdas \
--s3-key dev_codeforpoznan_pl_v3_serverless_api.zip \
--region eu-west-1 \
| jq 'del(.Environment, .VpcConfig, .Role, .FunctionArn)' \

aws lambda update-function-code \
--function-name dev_codeforpoznan_pl_v3_migration \
--s3-bucket codeforpoznan-lambdas \
--s3-key dev_codeforpoznan_pl_v3_migration.zip \
--region eu-west-1 \
| jq 'del(.Environment, .VpcConfig, .Role, .FunctionArn)' \

echo "run migrations"
aws lambda invoke \
--function-name dev_codeforpoznan_pl_v3_migration \
--region eu-west-1 \
response.json \
> request.json \

echo "show migration output"
jq -s add ./*.json | jq -re '
if .FunctionError then
.FunctionError, .errorMessage, false
else
.stdout, .stderr
end'

exit $?
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
DB_USER: cfp_v3
DB_PASSWORD: cfp_v3
SECRET_KEY: codeforpoznan
FLASK_ENV: development
FLASK_APP: app.py

db:
container_name: cfp_v3_db
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import App from './App.vue';
Vue.use(Vuelidate);

Vue.config.productionTip = false;
axios.defaults.baseURL = 'http://0.0.0.0:5000/';
axios.defaults.baseURL = '/';

const token = localStorage.getItem('token');

Expand Down