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

Setup ansible for deployment #60

Merged
merged 30 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4e50fdc
add pricing page
sirodoht Jul 14, 2024
1d08e67
replace uwsgi with gunicorn
sirodoht Mar 30, 2024
ea2ba85
rename requirements dev
sirodoht Mar 30, 2024
713b04c
remove uwsgi from gitignore
sirodoht Mar 30, 2024
9f019a0
add ansible configurations
sirodoht Mar 30, 2024
86c4c41
change host key name on ansible inventory
sirodoht Mar 30, 2024
727fc1d
add postgres setup on ansible
sirodoht Mar 30, 2024
a4d3b24
fix database env vars for ansible
sirodoht Mar 30, 2024
873f926
fix database url on migrations ansible task
sirodoht Mar 30, 2024
cc9205d
change ansible environment configuration
sirodoht Mar 30, 2024
6c38c14
add caddy on playbook ansible
sirodoht Mar 30, 2024
1945333
update vars on playbook ansible
sirodoht Mar 30, 2024
0669958
add caddyfile config ansible template
sirodoht Mar 30, 2024
0b58e3e
enable environment configured domain settings
sirodoht Jun 30, 2024
28fcd74
remove uwsgi related docs
sirodoht Jul 14, 2024
2af33b8
remove deploy script
sirodoht Mar 30, 2024
a4693d0
add new variables on ansible envrc example
sirodoht Mar 30, 2024
ff31312
change readme with updated deploy docs
sirodoht Jun 30, 2024
ad5d7c2
fix dockerfile dev requirements
sirodoht Mar 30, 2024
f975b0d
fix and update github ci
sirodoht Mar 30, 2024
d9061f6
add localdev mode env variable
sirodoht Mar 30, 2024
b870673
switch to getenv on settings
sirodoht Jun 30, 2024
15f3b20
switch to ruff
sirodoht Jun 30, 2024
13de15e
ruff check fix
sirodoht Jun 30, 2024
da0dcc9
ruff check fix unsafe
sirodoht Jun 30, 2024
549b240
fix raise from exception ruff check errors
sirodoht Jun 30, 2024
ecba2ed
change localdev variable name
sirodoht Jun 30, 2024
f06f356
write new deployment documentation
sirodoht Jun 30, 2024
2152d8e
add cronjobs document
sirodoht Jun 30, 2024
caf2ec5
change ci lint to ruff
sirodoht Jul 14, 2024
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
25 changes: 22 additions & 3 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Exceptions and tracebacks on errors
# 1: show
# 0: don't show
export DEBUG=1

# Stop real emails and turn https off
# 1: stop and off
# 0: do not stop and on
export LOCALDEV=1

# Session cookies secret
export SECRET_KEY=some-secret-key
export DATABASE_URL=postgres://mataroa:db-password@db:5432/mataroa
export EMAIL_HOST_USER=smtp-user
export EMAIL_HOST_PASSWORD=smtp-password

# Database connection
export DATABASE_URL=postgres://mataroa:xxx@localhost:5432/mataroa

# SMTP credentials
export EMAIL_HOST_USER=
export EMAIL_HOST_PASSWORD=

# Stripe payments details
export STRIPE_API_KEY=
export STRIPE_PUBLIC_KEY=
export STRIPE_PRICE_ID=
8 changes: 4 additions & 4 deletions .github/workflows/django-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -43,6 +43,6 @@ jobs:
- name: Lint
run: |
touch .envrc
pip install -r requirements_dev.txt
pip install -r requirements.dev.txt
pip install -r requirements.txt
make lint
ruff check .
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ postgres-data/
.coverage
htmlcov/

# uwsgi
uwsgi.ini
uwsgi-log.txt
mataroa.pid

# docker
docker-postgres-data/
docker-compose.override.yml
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ RUN apt-get update && \
&& rm -rf /var/lib/apt/lists/*

COPY requirements.txt /code/
COPY requirements_dev.txt /code/
RUN pip install -U pip && pip install -Ur /code/requirements.txt && pip install -Ur /code/requirements_dev.txt
COPY requirements.dev.txt /code/
RUN pip install -U pip && pip install -Ur /code/requirements.txt && pip install -Ur /code/requirements.dev.txt

WORKDIR /code
COPY . /code/
Expand Down
34 changes: 0 additions & 34 deletions Makefile

This file was deleted.

50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ volume, located in the root of the project.
```
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements_dev.txt
pip install -r requirements.dev.txt
pip install -r requirements.txt
```

Expand Down Expand Up @@ -197,44 +197,48 @@ python manage.py test
For coverage, run:

```sh
make cov
coverage run --source='.' --omit '.venv/*' manage.py test
coverage report -m
```

## Code linting & formatting

The following tools are used for code linting and formatting:
We use [ruff](https://github.com/astral-sh/ruff) for Python code formatting and linting.

* [black](https://github.com/psf/black) for code formatting
* [isort](https://github.com/pycqa/isort) for imports order consistency
* [flake8](https://gitlab.com/pycqa/flake8) for code linting
* [shellcheck](https://github.com/koalaman/shellcheck) for shell scripts
To format:

To use:
```sh
ruff format
```

To lint:

```sh
make format
make lint
ruff check
ruff check --fix
```

## Python dependencies

We use [pip-tools](https://github.com/jazzband/pip-tools) to manage our Python dependencies:

```sh
pip-compile -U requirements.in
pip install --upgrade pip
pip install -r requirements.txt
```

## Deployment

See the [Deployment](./docs/deployment.md) document for an overview on steps
required to deploy a mataroa instance.

See the [Server Playbook](./docs/server-playbook.md) document for a detailed
run through of setting up a mataroa instance on an Ubuntu 22.04 LTS system
using [uWSGI](https://uwsgi.readthedocs.io/en/latest/) and
[Caddy](https://caddyserver.com/).

See the [Server Migration](./docs/server-migration.md) document for a guide on
how to migrate servers.

### Useful Commands

To reload the uWSGI process:
To reload the gunicorn process:

```sh
sudo systemctl reload mataroa.uwsgi
sudo systemctl reload mataroa
```

To reload Caddy:
Expand All @@ -243,10 +247,10 @@ To reload Caddy:
systemctl restart caddy # root only
```

uWSGI logs:
gunicorn logs:

```sh
journalctl -fb -u mataroa.uwsgi
journalctl -fb -u mataroa
```

Caddy logs:
Expand All @@ -259,7 +263,7 @@ Get an overview with systemd status:

```sh
systemctl status caddy
systemctl status mataroa.uwsgi
systemctl status mataroa
```

## Backup
Expand Down
39 changes: 39 additions & 0 deletions ansible/.envrc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# inventory.yaml

# Server IP and user with ssh access
export ANSIBLE_HOST=
export ANSIBLE_USER=root


# vars.yaml

# Domain name and email for Caddy
export DOMAIN=mataroa.blog
export EMAIL=admin@mataroa.blog

# Show exceptions and tracebacks on errors
# 1: show
# 0: don't show
export DEBUG=1

# Stop real emails and turn https off
# 1: stop and off
# 0: do not stop and on
export LOCALDEV=1

# Session cookies secret
export SECRET_KEY=some-secret-key

# Database connection
export DATABASE_URL=postgres://mataroa:xxx@localhost:5432/mataroa
export POSTGRES_USERNAME=mataroa
export POSTGRES_PASSWORD=xxx

# SMTP credentials
export EMAIL_HOST_USER=
export EMAIL_HOST_PASSWORD=

# Stripe payments details
export STRIPE_API_KEY=
export STRIPE_PUBLIC_KEY=
export STRIPE_PRICE_ID=
14 changes: 14 additions & 0 deletions ansible/Caddyfile.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ domain }} {
route {
file_server /static/* {
root /var/www/mataroa
}
reverse_proxy 127.0.0.1:5000
}

tls {{ email }} {
on_demand
}

encode zstd gzip
}
3 changes: 3 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[defaults]
inventory = inventory.yaml
pipelining = True
5 changes: 5 additions & 0 deletions ansible/inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
virtualmachines:
hosts:
main:
ansible_host: "{{ lookup('env', 'ANSIBLE_HOST') }}"
ansible_user: "{{ lookup('env', 'ANSIBLE_USER') }}"
27 changes: 27 additions & 0 deletions ansible/mataroa.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[Unit]
Description=mataroa
After=network.target

[Service]
Type=simple
User=deploy
Group=www-data
WorkingDirectory=/var/www/mataroa
ExecStart=/var/www/mataroa/.venv/bin/gunicorn -b 127.0.0.1:5000 -w 4 mataroa.wsgi
ExecReload=/bin/kill -HUP $MAINPID
Environment="DOMAIN={{ domain }}"
Environment="EMAIL={{ email }}"
Environment="DEBUG={{ debug }}"
Environment="LOCALDEV={{ localdev }}"
Environment="SECRET_KEY={{ secret_key }}"
Environment="DATABASE_URL={{ database_url }}"
Environment="EMAIL_HOST_USER={{ email_host_user }}"
Environment="EMAIL_HOST_PASSWORD={{ email_host_password }}"
Environment="STRIPE_API_KEY={{ stripe_api_key }}"
Environment="STRIPE_PUBLIC_KEY={{ stripe_public_key }}"
Environment="STRIPE_PRICE_ID={{ stripe_price_id }}"
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target
Loading
Loading