Skip to content

Commit 921cdb7

Browse files
authored
Merge pull request #135 from ucladevx/kat-eb-deployment
Configure Backend for EB Deployment
2 parents 55aca86 + da9152c commit 921cdb7

33 files changed

+70
-281845
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Mappening
2-
id_rsa_mappening.pem
32
scraping/data/
43
src/mappening/api/*/*.json
54
src/mappening/api/*.json

Makefile

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,30 @@
11
# Include Environment Variables from .env file
22
include .env
33

4-
ECR_REPO=698514710897.dkr.ecr.us-west-1.amazonaws.com
5-
BASE_NAME=mappening/base
6-
APP_NAME=mappening/backend
7-
DEV_NAME=mappening/dev
8-
PROD_DOCKERFILE=./src/Dockerfile
9-
BASE_DOCKERFILE=./src/Base-Dockerfile
10-
DEV_DOCKER_COMPOSE=./docker-compose-dev.yml
11-
12-
########################## AWS / PRODUCTION ##########################
13-
14-
# Authenticate Docker client
15-
ecr-login:
16-
$(shell aws ecr get-login --no-include-email --region us-west-1)
17-
18-
# Build backend image
19-
build:
20-
docker build ./src -t $(APP_NAME)
21-
22-
# Login, build, and push latest image to AWS
23-
push: ecr-login build
24-
docker tag $(APP_NAME):latest $(ECR_REPO)/$(APP_NAME):latest
25-
docker push $(ECR_REPO)/$(APP_NAME):latest
26-
27-
############################# AWS / DEV ##############################
28-
29-
# Build backend image for dora
30-
build-dora:
31-
docker build ./src -t $(DEV_NAME):dora
32-
33-
# Login, build, and push latest image to AWS for dev testing
34-
dora: ecr-login build-dora
35-
docker tag $(DEV_NAME):dora $(ECR_REPO)/$(DEV_NAME):dora
36-
docker push $(ECR_REPO)/$(DEV_NAME):dora
37-
384
################## LOCAL DEVELOPMENT (Backend Only) ##################
395

40-
# Build backend image
6+
# Build backend image. Must be built before dev work (and only once unless changed)
417
build-base:
428
docker build ./src -t $(BASE_NAME) -f $(BASE_DOCKERFILE)
439

44-
# Build backend image
45-
build-dev:
46-
docker build ./src -t $(APP_NAME) -f $(DEV_DOCKERFILE)
47-
48-
# Build backend image
49-
build-prod:
50-
docker build ./src -t $(APP_NAME) -f $(PROD_DOCKERFILE)
51-
52-
# Build and run backend image
53-
dev: build-dev
54-
docker run --rm --name backend-dev -v $(shell pwd)/src:/app -p "5000:5000" -it $(APP_NAME)
55-
56-
prod: build-prod
57-
docker run --rm --name backend-dev -v $(shell pwd)/src:/app -p "5000:5000" -it $(APP_NAME)
58-
59-
# Stop running containers
60-
stop:
61-
-docker ps | tail -n +2 | cut -d ' ' -f 1 | xargs docker kill
62-
63-
################## IN PROGRESS: POSTGRES ##################
64-
65-
up-dev:
10+
# Run backend in dev mode with local Postgres database
11+
dev:
6612
docker-compose -f $(DEV_DOCKER_COMPOSE) up --build
6713

68-
up-prod:
14+
# Run backend in prod mode with AWS Postgres database
15+
prod:
6916
docker-compose up --build
7017

7118
# Stops the stack. Can also Ctrl+C in the same terminal window stack was run.
72-
down:
19+
stop:
7320
docker-compose down
7421

22+
# Kill any running containers
23+
kill:
24+
-docker ps | tail -n +2 | cut -d ' ' -f 1 | xargs docker kill
25+
26+
################## LOCAL POSTGRES DATABASE ##################
27+
7528
# Sets CONTAINER_ID variable with ID of postgres container.
7629
# := means CONTAINER_ID will only be set if output is non-empty.
7730
# -q option for quiet output with only the container ID.
@@ -86,7 +39,7 @@ endif
8639

8740
# Connects to psql shell of Postgres container when running `dev` target.
8841
pg: check-id
89-
docker exec -ti $(CONTAINER_ID) psql -U $(POSTGRES_USER)
42+
docker exec -ti $(CONTAINER_ID) psql -U $(POSTGRES_USER) -d $(POSTGRES_DB)
9043

9144
# Zip current db data.
9245
zip:

README.md

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,30 @@
11
# Mappening-Backend
22

3-
## Overview
4-
A single platform for events across campus. Mappening helps raise awareness of events by aggregating event information from various sources of advertising.
5-
63
## Documentation
74
Hosted on GitHub Pages at http://ucladevx.com/Mappening-Backend/
85

9-
## Built With
10-
- Python 2.7.14
11-
- Flask (Port 5000): Web microframework for Python
12-
- mLab: Database-as-a-Service cloud-hosted MongoDB
13-
- Pymongo: Database Connector between MongoDB and Flask
14-
- Facebook Graph API + Login/OAuth for getting event information and user authentication
15-
- Google Maps/Places API Web Service for getting place information
16-
- AWS EC2/Elastic Container Service for deployment
17-
- Sphinx to automatically generate documentation
18-
- Tkinter, Selenium, Google chromedriver for internal tools
19-
- Beautiful Soup for web scraping
20-
216
## Setting Up the Environment
22-
- Download [Docker](https://www.docker.com) and [Docker-Compose](https://github.com/docker/compose/releases) release 1.16.1.
23-
- Run the Docker daemon
24-
- Clone this repository
25-
- `git clone https://github.com/ucladevx/Mappening-Backend.git`
26-
- For Mac users, install Xcode developer tools `xcode-select --install`
27-
- Get the `.env` file which contains sensitive information from a dev and add it to src/mappening/utils/
7+
- Follow instructions in main [Mappening](https://github.com/ucladevx/Mappening) repository
8+
- Get the `.env` file which contains sensitive information from a dev and add it to `src/mappening/utils/`
9+
- Build the base image that contains all dependencies that are fairly static but take a while to install
10+
- `make build-base`
2811
- NOTE: the database connection doesn't seem to work over UCLA_WEB wifis, a more secure connection is needed (UCLA_WIFI)
2912

30-
#### For AWS Access
31-
32-
- Install [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)
33-
- Configure CLI with `aws configure`
34-
- Requires AWS secret info (only for PM/TL)
35-
- NOTE: Computer local time must be accurate lol @Hakan
36-
37-
## How to Push Image to AWS ECS
38-
- Enter the repository
39-
- `cd Mappening-Backend`
40-
- Login, build, and push image to AWS
41-
- For production deployment: `make push`
42-
- For dev site deployment: `make dora`
43-
- NOTE: for Windows/Linux users that must use `sudo` with Docker commands, use the `docker login` command generated by `make ecr-login` and run it with `sudo` before running above commands
44-
45-
4613
## How to Run Backend Locally
47-
- Build and run container
14+
15+
- Build and run container using local Postgres database
4816
- `make dev`
49-
- Navigate to `localhost`
50-
- Access flask api directly at port 5000
17+
- To use production database (not recommended for local development):
18+
- `make prod`
19+
- Navigate to [http://localhost](http://localhost)
20+
- Access flask api directly at port [5000](http://localhost:5000/)
5121
- Use `localhost:5000/api/<insert_api_route_here>`
5222
- e.g. `localhost:5000/api/events`
5323
- Stop running with `CTRL+C` or `make stop` in a separate terminal window
5424

55-
## More Info
56-
- Check out the [frontend](https://github.com/ucladevx/Mappening-Frontend) repository
57-
- Check out the [deployment](https://github.com/ucladevx/Mappening-Deployment) repository
58-
- Contains instructions for local development and production
59-
6025
## Repo Breakdown
26+
27+
- Database config/data in `/database`
6128
- Backend source code in `src/`
6229
- tkinter GUIs in `tkinter/`
6330
- Autodocumentation in `docs/`

database/create.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CREATE DATABASE devxdb;
2-
\c devxdb;
1+
CREATE DATABASE mapdb;
2+
\c mapdb;
33

44
-- List all tables and types
55

database/postgres.zip

-3.42 KB
Binary file not shown.

docker-compose-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ services:
1818
- ${API_SERVER_PORT}:${API_SERVER_PORT}
1919
command: bash -c 'while !</dev/tcp/postgres/5432; do sleep 2; done; python3 app.py'
2020
environment:
21-
- POSTGRES_URL=${POSTGRES_URL}
21+
- ENV=development
2222
depends_on:
2323
- postgres

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ services:
55
build: ./src
66
ports:
77
- ${API_SERVER_PORT}:${API_SERVER_PORT}
8-
command: python3 app.py -p
98
environment:
10-
- POSTGRES_URL=${AWS_PG_URL}
9+
- ENV=production

src/Base-Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ RUN set -ex ;\
2828
RUN pip3 install git+https://github.com/mitsuhiko/flask-oauth
2929
RUN pip3 install numpy scipy gensim
3030
RUN pip3 install Cython
31-
RUN pip3 install scikit-learn
31+
RUN pip3 install scikit-learn==0.20.3
3232
RUN pip3 install pandas
3333

3434
# https://gist.github.com/orenitamar/f29fb15db3b0d13178c1c4dd611adce2

src/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ COPY . .
1010
RUN ls -l
1111

1212
EXPOSE 5000
13-
CMD ["python3", "app.py"]
13+
CMD ["python3", "-u", "app.py", "-p"]

src/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,7 @@ def thread_scheduler(args):
6565
print("\n~~~~~~~~~~~~~~~~~~~\n~~~ IN DEV MODE ~~~\n~~~~~~~~~~~~~~~~~~~\n")
6666
app.run(host='0.0.0.0', debug=True)
6767
else:
68+
print("\n~~~~~~~~~~~~~~~~~~~~\n~~~ IN PROD MODE ~~~\n~~~~~~~~~~~~~~~~~~~~\n")
69+
# TODO: Breaks EB deployment. cron jobs?
6870
thread_scheduler(args)
6971
app.run(host='0.0.0.0', debug=False)

0 commit comments

Comments
 (0)