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

Docker setup and python 3 #31

Draft
wants to merge 91 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
23f2092
Initial commit, use docker-compose + make, use hackerapi, rename to j…
kpsuperplane May 31, 2019
9aabcc4
forgot the source whoops
kpsuperplane May 31, 2019
039057d
Update mds
kpsuperplane May 31, 2019
b5fee05
Fix rendering and counting bug, add returned status
kpsuperplane Jun 1, 2019
9cfa631
prep for deploy
kpsuperplane Jun 1, 2019
02bca4d
capital env vars
kpsuperplane Jun 1, 2019
e349b87
Use templater
kpsuperplane Jun 1, 2019
0416a02
nope back to envsubst
kpsuperplane Jun 1, 2019
7bcc944
Rename to template yaml
kpsuperplane Jun 1, 2019
b4254f1
Import os
kpsuperplane Jun 1, 2019
57a1764
Make resource requests a suggestion
kpsuperplane Jun 1, 2019
117ae14
Correct redirect uri, error state for login
kpsuperplane Jun 2, 2019
68a16c3
fix redirect uri
kpsuperplane Jun 2, 2019
2173b61
Use correct token name
kpsuperplane Jun 2, 2019
c0ba034
Remember to delete the cookie
kpsuperplane Jun 2, 2019
3149a6b
Add CSRF protection
kpsuperplane Jun 2, 2019
c0b7b0a
Fix csrf token
kpsuperplane Jun 2, 2019
d9a3851
fix csrf token typo
kpsuperplane Jun 2, 2019
10f22e9
Make csrf token lowercase
kpsuperplane Jun 2, 2019
34b5f58
Finally fix csrf token
kpsuperplane Jun 2, 2019
e9a5c5b
Basic theming
kpsuperplane Jun 7, 2019
91604b1
Allow people to fix their emails/phones if not set
kpsuperplane Jun 7, 2019
813d7d5
mobile-friendly everything
kpsuperplane Jul 8, 2019
50d3ebd
Improve desktop positioning of requests
kpsuperplane Jul 8, 2019
41370aa
Improve inventory styling
kpsuperplane Jul 8, 2019
5247ddf
top 40px
kpsuperplane Jul 8, 2019
eaecc90
revert temporary fake auth
kpsuperplane Jul 9, 2019
9ac4c07
forgot to comment out token
kpsuperplane Jul 9, 2019
9bea67b
Add support for MLH items
kpsuperplane Sep 1, 2019
f64ef85
Update deployment details
kpsuperplane Sep 1, 2019
cdd4c79
Add email notice
kpsuperplane Sep 1, 2019
28ef680
fix indentation
kpsuperplane Sep 1, 2019
4fe9318
fix secrets config
kpsuperplane Sep 1, 2019
779bc71
fix secrets again
kpsuperplane Sep 1, 2019
76b9dfb
Fix db secret
kpsuperplane Sep 1, 2019
13d703c
Change login path
kpsuperplane Sep 1, 2019
05ae8a9
Check user claims
kpsuperplane Sep 4, 2019
efc91c9
properly dispose cookie
kpsuperplane Sep 4, 2019
5d67813
get information from claim
kpsuperplane Sep 4, 2019
0d03d46
bugfix
kpsuperplane Sep 4, 2019
d65bddb
Never disable phone number
kpsuperplane Sep 4, 2019
7289be6
Update css
kpsuperplane Sep 5, 2019
96d9883
update notice
kpsuperplane Sep 10, 2019
1981ddd
Ensure hacker is checked in or confirmed
kpsuperplane Sep 10, 2019
31f45ce
Update copy
kpsuperplane Sep 13, 2019
2ee415b
Update deployment template
kpsuperplane Sep 14, 2019
33c164b
pool size 2
kpsuperplane Sep 14, 2019
1eb0e7d
pool size 2
kpsuperplane Sep 14, 2019
8cfb45d
Reduce resource allocation
kpsuperplane Sep 14, 2019
129644d
Remove socket support
kpsuperplane Sep 14, 2019
ecfdf42
Remove socket entirely
kpsuperplane Sep 14, 2019
4ab5cff
change dockerfile to run on port 8000
epicfaace Feb 4, 2020
4c9d7b1
run dev mode with right env vars
epicfaace Feb 4, 2020
fa0e526
redirect to login.treehacks.com on login
epicfaace Feb 4, 2020
9ca83ea
finish login integration
epicfaace Feb 4, 2020
ec8b5b0
fix cookie auth
epicfaace Feb 4, 2020
5c0069c
rename variables and things so it can be deployed on heroku
epicfaace Feb 4, 2020
1ef5dec
remove python 2.7 runtime
epicfaace Feb 4, 2020
743e909
remove cryptography and pycrypto requirements
epicfaace Feb 4, 2020
3b8dcb9
don't make SECRET env var required
epicfaace Feb 4, 2020
e304a65
don't make ENV variable required
epicfaace Feb 4, 2020
ec83369
use PORT instead of FLASK_RUN_PORT so it works on Heroku
epicfaace Feb 4, 2020
fe14242
upgrade psycopg2 to fix https://github.com/taigaio/taiga-back/issues/…
epicfaace Feb 4, 2020
f52de81
make SECRET required again (needed for sessions / csrf tokens)
epicfaace Feb 4, 2020
a5b7683
fix typo
epicfaace Feb 5, 2020
1ff2d61
wording changes
epicfaace Feb 5, 2020
f0e07f4
add slack and email methods
epicfaace Feb 9, 2020
f27fd0c
disable updating user, remove user phone and location
epicfaace Feb 9, 2020
93d1a94
auto-reply to received slack messages
epicfaace Feb 9, 2020
b38d0c4
add proper error handling for slack bot
epicfaace Feb 9, 2020
f81a301
remove slack rtm integration (not working)
epicfaace Feb 9, 2020
7a6e988
save user first_name and last_name in db
epicfaace Feb 9, 2020
9fedc40
fix: properly reload page when buttons are clicked
epicfaace Feb 9, 2020
6ef35c1
send email / slack upon approval
epicfaace Feb 9, 2020
cae9b9c
add "slack ..." button
epicfaace Feb 9, 2020
c5cfe78
add info about heroku deployment
epicfaace Feb 9, 2020
5ef8d29
add warning
epicfaace Feb 9, 2020
0cf1fa9
update sample env to include DEBUG and FORCE_SSL
epicfaace Feb 12, 2020
45cb8a9
update favicon and default image
epicfaace Feb 12, 2020
e74e027
add hardware_admin
epicfaace Feb 14, 2020
5ca31d5
rename cog -> hardwarecheckout
epicfaace Mar 13, 2020
1de9307
revert README
epicfaace Mar 13, 2020
0688bbc
revert more files
epicfaace Mar 13, 2020
c1519d5
revert more files
epicfaace Mar 13, 2020
fcc9643
revert static
epicfaace Mar 13, 2020
4ca9fdd
revert templates
epicfaace Mar 13, 2020
3bbc74c
update requirements.txt
epicfaace Mar 13, 2020
1e5498a
remove extra files
epicfaace Mar 13, 2020
d94b0d9
revert more, add tests
epicfaace Mar 13, 2020
790e11d
update vars
epicfaace Mar 13, 2020
3cfa65c
fix indent
epicfaace Mar 13, 2020
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ node_modules/
.DS_Store
__pycache__
.cache/
Cog
.vscode/
.env
33 changes: 12 additions & 21 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
# Development

## Setup
- Set up a Python2
[virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/)
to manage Python dependencies
- Source your virtualenv
- Run `pip install -r requirements.txt` to install all dependencies
- Install [PostgreSQL](https://www.postgresql.org/download/) to run a database locally
- If you're using Mac, install *Postgres.app* from
[here](https://www.postgresql.org/download/)
- Set three environment variables:
- `DATABASE_URL` points to the URL of a development database,
which has to be set up using Postgres on your system. A sample `DATABASE_URL`
could look like `postgres://username:password@localhost/cog`.
- `QUILL` is the URL to your Quill instance for auth.
- `SECRET` needs to be the same JWT secret used in your Quill instance.
- Run `python initialize.py`
- This initializes the database - run it if you make any changes to the models and
are fine with overwriting data.
- Install Docker
- Install Docker Compose
- Copy `sample.env` to `.env` and enter in the proper values
- `make migrate` to initialize and set up the db

## Build
- If you need to rebuild (in case you change the Dockerfile), run `make build`

## Running
- Run `make run`
- The site will be visible at `localhost:8000`
- Run `make start`
- The site will be visible at `localhost:80`
- `make logs` for a live stream of logs.

## Tests
- Run `make test` to run all tests
## Destroying
- Run `make stop` to destroy
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:2
FROM python:3.6.3

ARG APP_PATH=/hardware-checkout
ARG APP_PATH=/hardwarecheckout

WORKDIR $APP_PATH

Expand All @@ -9,6 +9,6 @@ RUN pip install -r requirements.txt

ADD . $APP_PATH

EXPOSE 8000
CMD ["gunicorn", "--bind", ":8000", "-k", "geventwebsocket.gunicorn.workers.GeventWebSocketWorker", "hardwarecheckout:app"]

EXPOSE $FLASK_RUN_PORT
CMD ["python", "runserver.py"]
# CMD ["flask", "run"]
2 changes: 2 additions & 0 deletions HEROKU.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Deploying on Heroku
After deploying on Heroku, you should go into the console and then run `python initialize.py` to properly set up the database. (WARNING: doing this will clear all existing data in the database)
27 changes: 22 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
test:
python -m pytest tests/
build:
docker-compose build

sass:
cd hardwarecheckout/static && sass --watch sass/app.scss:css/app.css
start:
docker-compose up -d
@echo "hardwarecheckout listening on port 8000, postgres on 5432"
@echo "run 'make logs' to watch logs"

stop:
docker-compose down

PORT = 8000
run:
gunicorn --bind 0.0.0.0:$(PORT) -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker hardwarecheckout:app
# watch the logs from hardwarecheckout
logs:
docker-compose logs -f -t hardwarecheckout

# run all the migrations
migrate:
docker-compose run hardwarecheckout python initialize.py
# db/containers still running

test:
docker-compose run hardwarecheckout python initialize.py && python -m pytest tests/
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: python runserver.py $PORT
web: python runserver.py
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3"
services:
dev_db:
image: postgres
ports:
- 5432
environment:
- PG_PASSWORD=password
hardwarecheckout:
container_name: hardwarecheckout
image: hardwarecheckout:latest
build:
context: .
environment:
- QUILL=${QUILL}
- DATABASE_URL=${DATABASE_URL}
- SECRET=${SECRET}
- PORT=5000
ports:
- 127.0.0.1:5000:5000
depends_on:
- dev_db
volumes:
- .:/hardwarecheckout
2 changes: 1 addition & 1 deletion hardwarecheckout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from flask import Flask
from flask_socketio import SocketIO
from urlparse import urlsplit
from urllib.parse import urlsplit
from flaskext.markdown import Markdown

from hardwarecheckout.utils import display_date, deltatimeformat
Expand Down
17 changes: 9 additions & 8 deletions hardwarecheckout/controllers/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hardwarecheckout import app
from hardwarecheckout import socketio
from hardwarecheckout.models import db

from hardwarecheckout.config import EMAIL_SUBJECT
from hardwarecheckout.models.request import Request, RequestStatus
from hardwarecheckout.models.inventory_entry import InventoryEntry
from hardwarecheckout.models.inventory_entry import ItemType
Expand All @@ -10,6 +10,7 @@
from hardwarecheckout.models.request_item import RequestItem
from hardwarecheckout.models.socket import Socket


from hardwarecheckout.utils import requires_auth, requires_admin, verify_token
from sqlalchemy import event

Expand Down Expand Up @@ -38,10 +39,10 @@ def get_requests():
def request_submit():
"""Submits new request"""
if not (user.location and user.phone):
return jsonify(
success=False,
message="""Please fill out your <a href='/user'>user info</a> before
requesting items!"""
return jsonify(
success=False,
message="""Please fill out your <a href='/user'>user info</a> before
requesting items!"""
)
proposal = request.form.get('proposal', '')
requested_quantity = int(request.form.get('quantity', 1))
Expand Down Expand Up @@ -91,7 +92,7 @@ def request_submit():
message='Out of stock!'
)

for _ in xrange(requested_quantity):
for _ in range(requested_quantity):
item = RequestItem(
InventoryEntry.query.get(request.form['item_id']),
1)
Expand Down Expand Up @@ -148,7 +149,7 @@ def request_approve(id):
quantity = request_item.quantity

# get items of proper type
for _ in xrange(quantity):
for _ in range(quantity):
if entry.quantity < quantity:
return jsonify(
success=False,
Expand Down Expand Up @@ -184,7 +185,7 @@ def request_fulfill(id):
quantity = request_item.quantity

# get items of proper type
for _ in xrange(quantity):
for _ in range(quantity):
item = Item.query.filter_by(entry_id = entry.id, user = None).first()
if item == None:
return jsonify(
Expand Down
3 changes: 2 additions & 1 deletion hardwarecheckout/models/inventory_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, name, description, link, category, tags,
self.image_src = image

self.items = []
for i in xrange(int(qty)):
for i in range(int(qty)):
self.items.append(
Item(self, self.name + " " + str(i+1)))

Expand All @@ -61,6 +61,7 @@ def quantity(self):
.filter_by(status=hardwarecheckout.models.request.RequestStatus.APPROVED) \
.with_entities(func.sum(RequestItem.quantity)).scalar()
if not requests: requests = 0

return Item.query.filter_by(entry_id = self.id, user = None).count() - requests

@property
Expand Down
2 changes: 1 addition & 1 deletion hardwarecheckout/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from babel import dates

def gen_uuid():
return str(uuid.uuid4()).replace('-', '')
return str(uuid.uuid4()).replace('-', '').decode('utf-8')

def verify_token(token):
try:
Expand Down
7 changes: 3 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ cffi==1.10.0
chardet==3.0.4
click==6.7
configparser==3.5.0
cryptography==1.7.2
decorator==4.1.2
ecdsa==0.13
enum34==1.1.6
eventlet
eventlet==0.25.1
Flask==0.12.2
Flask-Admin==1.5.0
Flask-Markdown==0.3
Expand All @@ -37,7 +36,7 @@ Markdown==2.6.9
MarkupSafe==1.0
mccabe==0.6.1
phonenumbers==8.8.1
psycopg2==2.7.1
psycopg2==2.7.3.2
py==1.4.34
pyasn1==0.2.3
pycodestyle==2.3.1
Expand All @@ -60,4 +59,4 @@ Werkzeug==0.12.2
wrapt==1.10.11
WTForms==2.0
WTForms-Alchemy==0.15.0
WTForms-Components==0.10.3
WTForms-Components==0.10.3
8 changes: 3 additions & 5 deletions runserver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from hardwarecheckout import app, socketio
import sys
import os

if __name__ == '__main__':
try:
port = int(sys.argv[1])
except (IndexError, ValueError):
port = 5000
socketio.run(app, host='0.0.0.0', port=port)
port = int(os.getenv("PORT", "5000"))
socketio.run(app, host='0.0.0.0', port=port)
1 change: 0 additions & 1 deletion runtime.txt

This file was deleted.

4 changes: 4 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
QUILL=
DATABASE_URL=postgres://postgres:password@dev_db/postgres
SECRET=random123456
FORCE_SSL=False
4 changes: 2 additions & 2 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def user(app):
user = User(quill_id, '[email protected]', False)
db.session.add(user)
db.session.commit()
app.set_cookie('localhost:8000', 'jwt', token)
app.set_cookie('localhost:5000', 'jwt', token)

return user

Expand Down Expand Up @@ -145,7 +145,7 @@ def test_run_lottery(app, admin, item):
.join(Request).filter_by(status=RequestStatus.APPROVED).count() == 3

def test_run_all_lotteries(app, admin):
for _ in xrange(3):
for _ in range(3):
item = InventoryEntry('Item' + str(_), 'Wow lick my socks',
'http://test.co', 'Item', [], '', 3, item_type=ItemType.LOTTERY)
db.session.add(item)
Expand Down