Skip to content

Commit f97a9ea

Browse files
committed
add ansible setup
1 parent f997a37 commit f97a9ea

File tree

11 files changed

+491
-7
lines changed

11 files changed

+491
-7
lines changed

.envrc.example

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ venv/
44

55
# environment
66
.envrc
7-
.direnv/
8-
.pyenv/
9-
postgres-data/
107

118
# generated static
129
/static/
1310

1411
# testing
1512
.coverage
1613
htmlcov/
14+
15+
# database
16+
db.sqlite3

ansible/.envrc.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# inventory.yaml
2+
3+
# Server IP and user with ssh access
4+
export ANSIBLE_HOST=
5+
export ANSIBLE_USER=
6+
7+
8+
# vars.yaml
9+
10+
# Show exceptions and tracebacks on errors
11+
export DEBUG=1
12+
13+
# Used by Django to encrypt session login cookies
14+
export SECRET_KEY=xxx
15+
16+
# SMTP credentials
17+
export EMAIL_HOST_USER=
18+
export EMAIL_HOST_PASSWORD=

ansible/ansible.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[defaults]
2+
inventory = inventory.yaml
3+
pipelining = True

ansible/illich.caddy.j2

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
collection.mataroa.blog {
2+
route {
3+
file_server /static/* {
4+
root /var/www/illich
5+
}
6+
reverse_proxy 127.0.0.1:5006
7+
}
8+
encode zstd gzip
9+
log {
10+
output stdout
11+
format console
12+
}
13+
}

ansible/illich.service.j2

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[Unit]
2+
Description=illich
3+
After=network.target
4+
5+
[Service]
6+
Type=simple
7+
User=deploy
8+
Group=www-data
9+
WorkingDirectory=/var/www/illich
10+
ExecStart=/var/www/illich/.venv/bin/gunicorn -b 127.0.0.1:5006 -w 4 illich.wsgi
11+
ExecReload=/bin/kill -HUP $MAINPID
12+
Environment="DEBUG={{ debug }}"
13+
Environment="SECRET_KEY={{ secret_key }}"
14+
Environment="EMAIL_HOST_USER={{ email_host_user }}"
15+
Environment="EMAIL_HOST_PASSWORD={{ email_host_password }}"
16+
TimeoutSec=15
17+
Restart=always
18+
19+
[Install]
20+
WantedBy=multi-user.target

ansible/inventory.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
virtualmachines:
2+
hosts:
3+
brick:
4+
ansible_host: "{{ lookup('env', 'ANSIBLE_HOST') }}"
5+
ansible_user: "{{ lookup('env', 'ANSIBLE_USER') }}"

ansible/playbook.yaml

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
- hosts: virtualmachines
3+
vars_files:
4+
- vars.yaml
5+
become: yes
6+
tasks:
7+
# smoke test and essential dependencies
8+
- name: ping
9+
ansible.builtin.ping:
10+
- name: essentials
11+
ansible.builtin.apt:
12+
update_cache: yes
13+
name:
14+
- gcc
15+
- git
16+
- rclone
17+
- vim
18+
state: present
19+
20+
# caddy
21+
- name: add caddy key
22+
ansible.builtin.apt_key:
23+
id: 65760C51EDEA2017CEA2CA15155B6D79CA56EA34
24+
url: https://dl.cloudsmith.io/public/caddy/stable/gpg.key
25+
keyring: /etc/apt/trusted.gpg.d/caddy-stable.gpg
26+
state: present
27+
- name: add caddy deb repository
28+
ansible.builtin.apt_repository:
29+
repo: deb [signed-by=/etc/apt/trusted.gpg.d/caddy-stable.gpg] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main
30+
- name: add caddy deb-src repository
31+
ansible.builtin.apt_repository:
32+
repo: deb [signed-by=/etc/apt/trusted.gpg.d/caddy-stable.gpg] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main
33+
- name: install caddy
34+
ansible.builtin.apt:
35+
update_cache: yes
36+
name: caddy
37+
- name: caddyfile
38+
ansible.builtin.template:
39+
src: illich.caddy.j2
40+
dest: /etc/caddy/illich.caddy
41+
owner: root
42+
group: root
43+
mode: '0644'
44+
45+
# deploy user and directory
46+
- name: www directory
47+
ansible.builtin.file:
48+
path: /var/www
49+
state: directory
50+
mode: '0755'
51+
- name: create user
52+
ansible.builtin.user:
53+
name: deploy
54+
password: ""
55+
shell: /bin/bash
56+
groups:
57+
- sudo
58+
- www-data
59+
append: yes
60+
createhome: yes
61+
skeleton: '/etc/skel'
62+
generate_ssh_key: yes
63+
ssh_key_type: 'ed25519'
64+
- name: www ownership
65+
ansible.builtin.file:
66+
path: /var/www
67+
owner: deploy
68+
group: www-data
69+
recurse: yes
70+
71+
# uv
72+
- name: uv
73+
ansible.builtin.shell:
74+
cmd: curl -LsSf https://astral.sh/uv/0.9.17/install.sh | sh
75+
become_user: deploy
76+
77+
# repository
78+
- name: clone
79+
ansible.builtin.git:
80+
repo: https://github.com/mataroablog/illich
81+
dest: /var/www/illich
82+
version: main
83+
accept_hostkey: true
84+
become_user: deploy
85+
86+
# systemd
87+
- name: systemd template
88+
ansible.builtin.template:
89+
src: illich.service.j2
90+
dest: /etc/systemd/system/illich.service
91+
owner: root
92+
group: root
93+
mode: '0644'
94+
- name: systemd reload
95+
ansible.builtin.systemd:
96+
daemon_reload: true
97+
- name: systemd enable
98+
ansible.builtin.systemd:
99+
name: illich
100+
enabled: yes
101+
- name: systemd start
102+
ansible.builtin.systemd:
103+
name: illich
104+
state: restarted
105+
106+
# deployment specific
107+
- name: collectstatic
108+
ansible.builtin.shell:
109+
cmd: |
110+
source $HOME/.local/bin/env
111+
uv run manage.py collectstatic --no-input
112+
chdir: /var/www/illich
113+
args:
114+
executable: /bin/bash
115+
become_user: deploy
116+
- name: migrations
117+
ansible.builtin.shell:
118+
cmd: |
119+
source $HOME/.local/bin/env
120+
uv run manage.py migrate --no-input
121+
chdir: /var/www/illich
122+
args:
123+
executable: /bin/bash
124+
become_user: deploy
125+
- name: gunicorn restart
126+
ansible.builtin.systemd:
127+
name: illich
128+
state: restarted
129+
- name: caddy restart
130+
ansible.builtin.systemd:
131+
name: caddy
132+
state: restarted

ansible/vars.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
debug: "{{ lookup('env', 'DEBUG') }}"
3+
secret_key: "{{ lookup('env', 'SECRET_KEY') }}"
4+
email_host_user: "{{ lookup('env', 'EMAIL_HOST_USER') }}"
5+
email_host_password: "{{ lookup('env', 'EMAIL_HOST_PASSWORD') }}"

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description = "mataroa collection engine"
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
8+
"ansible>=13.1.0",
89
"django>=6.0",
910
"psycopg[binary]>=3.3.2",
1011
]

0 commit comments

Comments
 (0)