Skip to content

Commit 3a3cc12

Browse files
authored
Merge pull request #240 from piratar/adding-makefile
Adding a basic Makefile (and few other tidbits)
2 parents d1dae07 + 7762783 commit 3a3cc12

File tree

8 files changed

+260
-17
lines changed

8 files changed

+260
-17
lines changed

.gitignore

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
*.pyc
2-
# Generated by django compilemessages utility
1+
# This file enumerates various files/patterns to ignore in git.
2+
# Please keep this file tidy and neat
3+
4+
# Local environment file
5+
.env
6+
7+
# Django translation files
38
*.mo
4-
wasa2il/locale/wasa2il.pot
9+
10+
# Sqlite DB files
11+
*.sqlite
12+
13+
# Log files
14+
*.log
15+
16+
# Virtualenvs
17+
/.venv/
18+
/venv/
19+
20+
# Coverage
21+
/reports/*
22+
/!reports/index.html
23+
/.coverage
24+
/coverage.xml
25+
26+
# Cache files
27+
__pycache__/
28+
.sass-cache
29+
30+
# Editors/IDEs
531
*.swp
632
*.swo
33+
.idea
34+
*~
35+
36+
# Misc/unknowns
37+
# TODO: Trim this list down/figure out reason for ignoring
38+
wasa2il/locale/wasa2il.pot
739
pip-selfcheck.json
840
bin
941
include
1042
lib/python2.7
1143
lib/
1244
local
1345
share
14-
.idea
15-
wasa2il.sqlite
16-
*~
17-
venv/
18-
reports/*
19-
!reports/index.html
20-
.coverage
21-
.sass-cache
22-
.env
23-
*.sqlite
24-
coverage.xml
25-
errors.log

Makefile

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# NOTE: This is not the most perfect makefile.
3+
# There's a few things we could do to improve performance.
4+
# But since this is new, we can strive for clarity and simplicity here.
5+
# Could use many of these suggestions:
6+
# https://docs.cloudposse.com/reference/best-practices/make-best-practices/
7+
8+
9+
# On _some_ systems, make might not default to bash, which might produce
10+
# unexpected output/errors. It's assumed that all of the recipes here are written
11+
# in bash syntax.
12+
SHELL = /bin/bash
13+
.SHELLFLAGS = -e -u -o pipefail -c
14+
15+
16+
default: help
17+
18+
19+
# TODO: Add more descriptions to makefile targets
20+
.PHONY: help
21+
help:
22+
@echo -e "\nThese are some useful commands to work with this project."
23+
@echo -e "Please refer to the README.md for more details.\n"
24+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
25+
@echo -e "\nFor more information, please read the Makefile\n"
26+
27+
28+
.env:
29+
@echo "Missing '.env' file. Creating one using 'env.example' as a template"
30+
@cp env.example .env
31+
@echo "Please open the '.env' file and adjust to your local setup!"
32+
33+
34+
.venv: ## Creates a virtualenv at `./.venv`
35+
@python -m venv .venv
36+
37+
38+
.PHONY: setup
39+
setup: .env .venv requirements.txt.log requirements-mysql.txt.log requirements-postgresql.txt.log ## Sets up the project (installs dependencies etc.)
40+
41+
42+
requirements.txt.log: requirements.txt
43+
@pip install -r requirements.txt | tee .requirements.txt.tmp.log
44+
@mv .requirements.txt.tmp.log requirements.txt.log
45+
46+
requirements-mysql.txt.log: .env requirements-mysql.txt
47+
@(grep '^W2_DATABASE_ENGINE' .env | grep 'mysql' > /dev/null && pip install -r requirements-mysql.txt || echo "Not using MySQL") | tee .requirements-mysql.txt.tmp.log
48+
@mv .requirements-mysql.txt.tmp.log requirements-mysql.txt.log
49+
50+
requirements-postgresql.txt.log: .env requirements-postgresql.txt
51+
@(grep '^W2_DATABASE_ENGINE' .env | grep 'postgresql' > /dev/null && pip install -r requirements-postgresql.txt || echo "Not using PostgreSQL") | tee .requirements-postgresql.txt.tmp.log
52+
@mv .requirements-postgresql.txt.tmp.log requirements-postgresql.txt.log
53+
54+
55+
.PHONY: test
56+
test: setup ## Runs the unit tests
57+
@. .venv/bin/activate
58+
@./manage.py test
59+
60+
61+
.PHONY: migrate
62+
migrate: setup ## Runs the migrate Django management command
63+
@. .venv/bin/activate
64+
@./manage.py migrate
65+
66+
67+
.PHONY: load_fake_data
68+
load_fake_data: setup ## Loads up fake data using custom Django management command
69+
@. .venv/bin/activate
70+
@./manage.py load_fake_data --full --reset
71+
72+
.PHONY: run
73+
run: setup ## Runs the Django development server
74+
@. .venv/bin/activate
75+
@./manage.py runserver
76+
77+
78+
.PHONY: clean
79+
clean: ## Removes cached python files and virtualenv
80+
@echo "Deleting '__pycache__/' directories"
81+
@find . -name "__pycache__" -exec rm -rf {} \+
82+
@echo "Deleting the virtualenv ('./.venv/')"
83+
@rm -rf .venv
84+
85+
86+
.PHONY: docker/build
87+
docker/build: ## Builds a Docker image and tags
88+
docker build -t wasa2il .
89+

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ Wasa2il must be set up on a web server capable of running Django. Instructions o
4141

4242
`cp env.example .env`
4343

44+
1. Run `make help` to see what make commands are available.
45+
46+
47+
#### Using make
48+
49+
You can use `make` to do _most_ of the things you might need to. Behind the scenes
50+
much of what follows runs in a virtualenv under `./.venv`. It's encouraged to get
51+
to know how to use the virtualenv directly (see next section below).
52+
53+
1. Create a virtual environment: `make venv`
54+
1. Install dependencies etc: `make setup` (edit the created `.env` file!)
55+
1. Create the database: `make migrate`
56+
1. **Optional:** Create some fake data: `make load_fake_data`
57+
1. Run the development server: `make run`
4458

4559
#### Using virtualenv
4660

47-
1. Create a virtual environment
61+
1. Create a virtual environment (usually either kept under `.venv` or `venv`)
4862

4963
`python3 -m venv venv`
5064

@@ -84,6 +98,15 @@ If you have `docker-compose` installed, you need to:
8498
`docker-compose restart app`
8599

86100

101+
#### Vagrant (Virtual machines)
102+
103+
Three are a few examples of virtual machines setting up the project. These are found under the `vagrants/` directory.
104+
105+
These are mostly meant to make sure that we have solid examples of how to set up the project on a brand new computer, including necessary system packages.
106+
107+
So these can be used to debug system-level problems, and also to test out changes made at that level, as opposed to at the python package level.
108+
109+
87110
#### SASS / SCSS / CSS
88111
To watch and compile `.scss` automatically:
89112
`cd core/static/css` and `scss --watch application.scss`

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ commonmark==0.8.0
22
gunicorn
33
django-dotenv
44
dj_database_url
5-
django
5+
django==2.2
66
django-registration-redux
77
django-bootstrap-form
88
diff_match_patch

vagrants/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vagrant

vagrants/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Wasa2il on Vagrant machines
2+
===========================
3+
4+
This directory contains Vagrant virtual machine definitions that should be able
5+
to boot up and run the code without problems.
6+
7+
These are here to better make sure that the code runs in various environments, and
8+
to figure out which system packages may be required to get the project running.
9+
10+
All that should be needed is a working Vagrant setup locally (usually backed up by
11+
VirtualBox, or VMWare, or similar), and then you should be able to go into any of
12+
the subdirectories and run `vagrant up` and `vagrant ssh`, naviate to `/app` and run `make test` or `make run` for example.
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
Vagrant.configure("2") do |config|
5+
config.vm.box = "archlinux/archlinux"
6+
7+
# Sync the local project root folder into /app
8+
config.vm.synced_folder "../../", "/app", type: "rsync",
9+
rsync__exclude: [".git/", ".venv/", ".env", "*.log"]
10+
11+
# Enable provisioning with a shell script
12+
config.vm.provision "shell", privileged: false, inline: <<-SHELL
13+
14+
### apt-get update
15+
sudo pacman -S --noconfirm make
16+
sudo pacman -S --noconfirm which
17+
18+
# Decided use pyenv to run a particular python version
19+
sudo pacman -S --noconfirm patch # to build python
20+
sudo pacman -S --noconfirm gcc # to build python
21+
sudo pacman -S --noconfirm pyenv
22+
eval "$(pyenv init -)"
23+
echo '
24+
export PYENV_ROOT="$HOME/.pyenv"
25+
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
26+
eval "$(pyenv init -)"
27+
' > ~/.bashrc
28+
29+
# Install and set to version 3.8.10
30+
pyenv install 3.8.10
31+
pyenv local 3.8.10
32+
33+
# The env.template defaults to MySQL. Using MariaDB instead.
34+
sudo pacman -S --noconfirm mariadb
35+
sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
36+
sudo systemctl start mariadb.service
37+
38+
# Setup the database
39+
sudo mariadb -e "CREATE USER 'wasa2il'@'localhost' IDENTIFIED BY 'wasa2il';"
40+
sudo mariadb -e "GRANT ALL PRIVILEGES ON *.* TO 'wasa2il'@'localhost' WITH GRANT OPTION;"
41+
sudo mariadb -e "CREATE DATABASE wasa2il"
42+
43+
# Now that prereqs are setup, let's try the app itself!
44+
45+
cd /app
46+
make setup
47+
make test
48+
make migrate
49+
# NOTE: This script seems to be python 2.7 compatible, so disabling for now
50+
# make load_fake_data
51+
SHELL
52+
end

vagrants/ubuntu_focal64/Vagrantfile

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
Vagrant.configure("2") do |config|
5+
config.vm.box = "ubuntu/focal64"
6+
7+
# Sync the local project root folder into /app
8+
config.vm.synced_folder "../../", "/app", type: "rsync",
9+
rsync__exclude: [".git/", ".venv/", ".env", "*.log"]
10+
11+
# Enable provisioning with a shell script
12+
config.vm.provision "shell", inline: <<-SHELL
13+
14+
apt-get update
15+
apt-get install -y make
16+
17+
apt-get install -y python3-pip
18+
19+
# Seems to be needed because `ensurepip` wasn't installed. Not quite sure why.
20+
apt install python3.8-venv
21+
22+
# python3 is installed, but the `python` command does not exist.
23+
ln -s `which python3` /usr/bin/python
24+
25+
# The env.template defaults to MySQL
26+
apt-get install -y mysql-server
27+
28+
apt-get install -y libmysqlclient-dev # needed for mysql_config
29+
# Not needed as the project defaults to MySQL
30+
# apt-get install -y libpq-dev # needed for pg_config
31+
32+
# Setup the database
33+
mysql -e "CREATE USER 'wasa2il'@'localhost' IDENTIFIED BY 'wasa2il';"
34+
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'wasa2il'@'localhost' WITH GRANT OPTION;"
35+
mysql -e "CREATE DATABASE wasa2il"
36+
37+
# Now that prereqs are setup, let's try the app itself!
38+
39+
cd /app
40+
sudo -u vagrant make setup
41+
sudo -u vagrant make test
42+
sudo -u vagrant make migrate
43+
# NOTE: This script seems to be python 2.7 compatible, so disabling for now
44+
# sudo -u vagrant make load_fake_data
45+
SHELL
46+
end

0 commit comments

Comments
 (0)