diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml index 1fc47b4..ce47286 100644 --- a/ansible/group_vars/all.yml +++ b/ansible/group_vars/all.yml @@ -1,19 +1,26 @@ $ANSIBLE_VAULT;1.1;AES256 -35323132643564396232333034306638623934636533313039653861353465303732373131666161 -6238303664356561663936333662666163323932653836360a333237396332636339303535646533 -37356633353263633163646436343462626638666639346662383831616134643863363261636563 -3866653936373538350a643666316365616235303036613066383030656166386166643661336534 -32653662393136653133373033356130623833343831306562626662376536303237306233303638 -30356538373964386164366430316465633464646132643931373232313666626535333666663466 -32633537333133333838656234646537326339363633646636313234636532376133303963306638 -32303734633462396330626363386530386465366565306233326430663164623236663061386330 -63616562303337303933353734623535373939313363356431366264643531383761396230373338 -38386364363063316163306365656533373034623734346432353230633837646266636231363264 -33353937656130656335313763363762386663386236376532636265363163656337646562346632 -32363761316134373363616464623030343137373338643532666138376139616431323436313063 -62363137633732643830653665663333333963653532616531353064303563383334393239656465 -31333332373265316336643762386432383432366130303135333362343030333537323865643036 -65646639386437656365613134393936363132313639393966313162323437373236316239666631 -35333961333435613139353235356637663235616261373165323031393264363965333435316331 -30373238316235613937663966356438663961633336366436306235303135633362303737636338 -3062666462303436613761613065623536356462616565366433 +61363761383764636162396639656633356361363761373834663965303637626262383834336265 +3834363165356137623130313562636366616433316666330a316535643536333638383038313838 +31333133643763303530316535633934666437373834663137613337323234646265386361343763 +6632373238346164650a386438373065656361623032646566613437323137353063343438393566 +33646230653366343634623363366134616364373163323433613032633665613732616634363762 +66346438323130396534623631326339616634663261363061363136306334386361353834333534 +39336638623939323133353362323034356132623161393932386161626638313663623733353232 +35656237666464646131376466353339363932666535633738333035353536306630323038353531 +32313432316233653436333632326231386235643237643833326163373362306338626332393434 +38633032623438636265636131666638663532393537663763633961633663633434393933663461 +38333333393437373636346231306237656665393331363465353635393530396565333039366137 +37333239663563376631333162373738343234336538646537353464303763633763626132346238 +34326631643165393937643131323539616532663366366533386633663765303764653939396564 +32383861346534633364656539316130373035303039373033363338643237393565616562393665 +39363235313535306531656535623134373665386536623836666362306133343437303132653138 +30396138343262613761636138616565346236353936396431336337306262383034623464366335 +65643966326365353636343637613035633564666436623636613239633335633064383034393035 +31363134393861623762646561656632653635653436363666643230383732363734653439373965 +30623537353738643939393734303135316364376465653964623561346434653939376465663334 +30643132663532613366393262656166323236633730613037383030393264626534313862376366 +36303462386561316339363165396638636531333431613138613134393234666638376235333763 +63373366353530613736663531336331376265303464343761333236626130313332333564616231 +31333665316432386435663436373862333031373738323064316136373464653437633462663836 +33313338343533373832353063626439363663383563666133353866653362343734376566316466 +3632 diff --git a/ansible/hosts.ini b/ansible/hosts.ini index 118cd50..c0652a7 100644 --- a/ansible/hosts.ini +++ b/ansible/hosts.ini @@ -11,3 +11,4 @@ srib-radio azuracast-srib beszel kanidm +vt-booking diff --git a/ansible/playbook_infra.yml b/ansible/playbook_infra.yml index 71ca108..67d349f 100644 --- a/ansible/playbook_infra.yml +++ b/ansible/playbook_infra.yml @@ -19,3 +19,13 @@ roles: - kanidm become: true + +- name: VT Booking host config + hosts: vt-booking + pre_tasks: + - name: Install nginx + ansible.builtin.include_role: + name: nginxinc.nginx + roles: + - vt_booking + become: true diff --git a/ansible/requirements.yml b/ansible/requirements.yml index 6fe23b1..f05c421 100644 --- a/ansible/requirements.yml +++ b/ansible/requirements.yml @@ -4,6 +4,8 @@ collections: version: "2.2.2" - name: community.beszel version: "2.0.0" + - name: community.docker + version: "5.4.0" roles: - name: nginxinc.nginx version: "0.26.0" diff --git a/ansible/roles/common/tasks/main.yml b/ansible/roles/common/tasks/main.yml index b6f4b21..8637381 100644 --- a/ansible/roles/common/tasks/main.yml +++ b/ansible/roles/common/tasks/main.yml @@ -28,5 +28,4 @@ - tmux - dnsutils - qemu-guest-agent - - podman become: true diff --git a/ansible/roles/vt_booking/defaults/main.yml b/ansible/roles/vt_booking/defaults/main.yml new file mode 100644 index 0000000..7804f28 --- /dev/null +++ b/ansible/roles/vt_booking/defaults/main.yml @@ -0,0 +1,17 @@ +--- +# Domenet. Bytter dere senere, endrer du bare denne +vt_booking_domain: booking.fribyte.no + +# Hvor app-filene ligger på serveren +vt_booking_dir: /opt/vt-booking + +# Hvilken versjon av imagene: latest, eller en commit-SHA +vt_booking_image_tag: latest + +# Porten appens egen nginx lytter på, bare tilgjengelig fra serveren selv +vt_booking_http_port: 8080 + +# Fast adresseområde for Docker, så appens nettverk alltid får gateway 172.31.0.1 +vt_booking_docker_bip: 172.30.0.1/24 +vt_booking_docker_pool: 172.31.0.0/16 +vt_booking_trusted_proxy: 172.31.0.1 diff --git a/ansible/roles/vt_booking/files/Makefile b/ansible/roles/vt_booking/files/Makefile new file mode 100644 index 0000000..f2ccae5 --- /dev/null +++ b/ansible/roles/vt_booking/files/Makefile @@ -0,0 +1,70 @@ +# Common tasks for the room booking system. Run `make help` for a list. +# Requires Docker, Java 21 and Node 22 (only Docker for `make up`). + +FRONTEND := frontend +BACKEND := backend +COMPOSE := docker compose +PROD := docker compose -f compose.prod.yaml +APP_PORT ?= 8088 +export APP_PORT + +.PHONY: help dev test up down reset-db bootstrap-admin prod-up prod-down prod-logs prod-update prod-pull prod-bootstrap-admin + +help: ## Show this list + @grep -E '^[a-z0-9-]+:.*## ' $(MAKEFILE_LIST) | awk -F ':.*## ' '{printf " make %-22s %s\n", $$1, $$2}' + +$(FRONTEND)/node_modules: $(FRONTEND)/package-lock.json + cd $(FRONTEND) && npm ci + @touch $@ + +dev: $(FRONTEND)/node_modules ## PostgreSQL in Docker, Spring Boot and React locally with hot reload + $(COMPOSE) up -d --wait db + @echo "Starting backend (:8080) and frontend (http://localhost:5173)." + @echo "Log in with your own account (first time: 'make bootstrap-admin' in another terminal). Ctrl+C stops both." + @# `exec` makes each job the real process, so the trap's kill reaches it. + @trap 'trap - INT TERM EXIT; kill $$(jobs -p) 2>/dev/null; wait' INT TERM EXIT; \ + (cd $(BACKEND) && exec ./mvnw -q spring-boot:run -Dspring-boot.run.profiles=dev) & \ + (cd $(FRONTEND) && exec npm run dev -- --strictPort) & \ + wait + +test: $(FRONTEND)/node_modules ## Frontend and backend tests (backend needs Docker for Testcontainers) + cd $(FRONTEND) && npm test + cd $(BACKEND) && ./mvnw -q test + +up: ## Build and run the complete application in Docker on http://localhost:8088 (APP_PORT to change) + $(COMPOSE) up -d --build --wait + @echo "Running on http://localhost:$(APP_PORT). First time: create an administrator with 'make bootstrap-admin'. Stop with 'make down'." + +bootstrap-admin: ## Create the first system administrator (prompts for email and password; refuses if one exists) + $(COMPOSE) run --rm --build backend bootstrap-admin + +down: ## Stop the containers (data is kept) + $(COMPOSE) down + +reset-db: ## Delete the development database (asks first) + @printf "Delete all data in the development database? [y/N] "; read answer; \ + case "$$answer" in \ + [yY]*) $(COMPOSE) down -v && echo "Database deleted. It is recreated empty on the next 'make dev' or 'make up'; then run 'make bootstrap-admin'." ;; \ + *) echo "Cancelled." ;; \ + esac + +# ── Production: compose.prod.yaml with settings in .env (see docs/DEPLOYMENT.md) ── + +prod-up: ## Production: start in the background (builds the images the first time) + $(PROD) up -d --wait + +prod-down: ## Production: stop the containers (the database is kept) + $(PROD) down + +prod-logs: ## Production: follow the logs of all services + $(PROD) logs -f --tail=100 + +prod-update: ## Production: rebuild the images from this checkout and restart (back up first) + $(PROD) up -d --build --wait + +prod-pull: ## Production: pull the images built by CI (IMAGE_TAG in .env) and restart (back up first) + $(PROD) pull backend frontend + $(PROD) up -d --no-build --wait + +prod-bootstrap-admin: ## Production: create the first system administrator (interactive) + $(PROD) run --rm backend bootstrap-admin diff --git a/ansible/roles/vt_booking/files/compose.prod.yaml b/ansible/roles/vt_booking/files/compose.prod.yaml new file mode 100644 index 0000000..354afe9 --- /dev/null +++ b/ansible/roles/vt_booking/files/compose.prod.yaml @@ -0,0 +1,66 @@ +# Production: PostgreSQL, the Spring Boot API and Nginx (the built React app), behind the host's +# HTTPS reverse proxy. Settings and secrets come from .env (copy .env.example). Use the +# `make prod-*` commands, and see docs/DEPLOYMENT.md. For development, use compose.yaml instead. +name: vt-booking-prod + +services: + db: + image: postgres:18-alpine + environment: + POSTGRES_DB: booking + POSTGRES_USER: booking + POSTGRES_PASSWORD: ${DB_PASSWORD:?Set DB_PASSWORD in .env} + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD-SHELL", "pg_isready -U booking -d booking"] + interval: 10s + timeout: 5s + retries: 10 + restart: unless-stopped + # No ports: only the API reaches the database. + + backend: + # Pulled from GHCR (built by .github/workflows/images.yml), or built locally with `make prod-update`. + image: ghcr.io/fribyte-code/vt-booking-backend:${IMAGE_TAG:-latest} + build: backend + environment: + # No SPRING_PROFILES_ACTIVE: the dev profile must never run in production. + DB_URL: jdbc:postgresql://db:5432/booking + DB_USER: booking + DB_PASSWORD: ${DB_PASSWORD:?Set DB_PASSWORD in .env} + depends_on: + db: + condition: service_healthy + healthcheck: + test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://localhost:8080/api/auth/csrf"] + interval: 10s + timeout: 5s + start_period: 60s + retries: 6 + restart: unless-stopped + # No ports: only Nginx reaches the API. Never publish 8080 (see docs/DEPLOYMENT.md). + + frontend: + image: ghcr.io/fribyte-code/vt-booking-frontend:${IMAGE_TAG:-latest} + build: frontend + environment: + # The reverse proxy in front of Nginx, as Nginx sees it, and the header with the client address. + TRUSTED_PROXY: ${TRUSTED_PROXY:-127.0.0.1} + REAL_IP_HEADER: ${REAL_IP_HEADER:-X-Forwarded-For} + volumes: + - ./frontend/real-ip.conf.template:/etc/nginx/templates/real-ip.conf.template:ro + ports: + - "${HTTP_BIND:-127.0.0.1}:${HTTP_PORT:-8080}:80" + depends_on: + backend: + condition: service_healthy + healthcheck: + test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://localhost/"] + interval: 10s + timeout: 5s + retries: 6 + restart: unless-stopped + +volumes: + pgdata: diff --git a/ansible/roles/vt_booking/files/real-ip.conf.template b/ansible/roles/vt_booking/files/real-ip.conf.template new file mode 100644 index 0000000..acaa747 --- /dev/null +++ b/ansible/roles/vt_booking/files/real-ip.conf.template @@ -0,0 +1,7 @@ +# Mounted by compose.prod.yaml. At start, the Nginx image fills in the variables (from .env) and +# writes the result to /etc/nginx/conf.d/real-ip.conf. Requests from TRUSTED_PROXY, the reverse +# proxy in front of Nginx, carry the client address in REAL_IP_HEADER; from anywhere else the +# header is ignored. The default, 127.0.0.1, trusts no proxy. +set_real_ip_from ${TRUSTED_PROXY}; +real_ip_header ${REAL_IP_HEADER}; +real_ip_recursive on; diff --git a/ansible/roles/vt_booking/handlers/main.yml b/ansible/roles/vt_booking/handlers/main.yml new file mode 100644 index 0000000..3d8ff5d --- /dev/null +++ b/ansible/roles/vt_booking/handlers/main.yml @@ -0,0 +1,29 @@ +--- +- name: Restart docker + ansible.builtin.systemd_service: + name: docker + state: restarted + become: true + +# Same as `make prod-pull` in the app repository +- name: Pull vt-booking images + ansible.builtin.command: + cmd: docker compose -f compose.prod.yaml pull backend frontend + chdir: "{{ vt_booking_dir }}" + changed_when: true + listen: Deploy vt-booking + become: true + +- name: Start vt-booking + ansible.builtin.command: + cmd: docker compose -f compose.prod.yaml up -d --no-build --wait + chdir: "{{ vt_booking_dir }}" + changed_when: true + listen: Deploy vt-booking + become: true + +- name: Restart nginx + ansible.builtin.systemd_service: + name: nginx + state: restarted + become: true diff --git a/ansible/roles/vt_booking/tasks/main.yml b/ansible/roles/vt_booking/tasks/main.yml new file mode 100644 index 0000000..2140996 --- /dev/null +++ b/ansible/roles/vt_booking/tasks/main.yml @@ -0,0 +1,143 @@ +--- +- name: Create the Docker config directory + ansible.builtin.file: + path: /etc/docker + state: directory + mode: "0755" + become: true + +- name: Write Docker daemon config + ansible.builtin.template: + src: templates/daemon.json.j2 + dest: /etc/docker/daemon.json + mode: "0644" + notify: Restart docker + become: true + +- name: Add the Docker apt repository + ansible.builtin.deb822_repository: + name: docker + types: deb + uris: https://download.docker.com/linux/debian + suites: "{{ ansible_facts['distribution_release'] }}" + components: stable + signed_by: https://download.docker.com/linux/debian/gpg + become: true + +- name: Install Docker, nginx ACME module and make + ansible.builtin.apt: + pkg: + - docker-ce + - docker-ce-cli + - containerd.io + - docker-compose-plugin + - nginx-module-acme + - make + - cron + state: present + update_cache: true + become: true + +- name: Start Docker on boot + ansible.builtin.systemd_service: + name: docker + state: started + enabled: true + become: true + +- name: Check that the database password is set + ansible.builtin.assert: + that: + - vt_booking_db_password is defined + - vt_booking_db_password | length >= 32 + fail_msg: Set vt_booking_db_password in group_vars/all.yml (openssl rand -hex 32) + quiet: true + +- name: Create the app directories + ansible.builtin.file: + path: "{{ item }}" + state: directory + mode: "0755" + loop: + - "{{ vt_booking_dir }}" + - "{{ vt_booking_dir }}/frontend" + become: true + +- name: Copy the app's compose file, Makefile and real-ip template + ansible.builtin.copy: + src: "files/{{ item.src }}" + dest: "{{ vt_booking_dir }}/{{ item.dest }}" + mode: "0644" + loop: + - { src: compose.prod.yaml, dest: compose.prod.yaml } + - { src: Makefile, dest: Makefile } + - { src: real-ip.conf.template, dest: frontend/real-ip.conf.template } + notify: Deploy vt-booking + become: true + +- name: Write the app's .env + ansible.builtin.template: + src: templates/env.j2 + dest: "{{ vt_booking_dir }}/.env" + mode: "0600" + notify: Deploy vt-booking + become: true + +- name: Write nginx config + ansible.builtin.template: + src: templates/nginx.conf.j2 + dest: /etc/nginx/nginx.conf + mode: "0644" + validate: /usr/sbin/nginx -t -c %s + notify: Restart nginx + become: true + +- name: Create the backup directory + ansible.builtin.file: + path: /var/backups/vt-booking + state: directory + mode: "0700" + become: true + +- name: Write the backup script + ansible.builtin.template: + src: templates/vt-booking-backup.sh.j2 + dest: /usr/local/sbin/vt-booking-backup + mode: "0755" + become: true + +- name: Back up the database every night + ansible.builtin.cron: + name: vt-booking backup + minute: "15" + hour: "3" + job: /usr/local/sbin/vt-booking-backup + user: root + become: true + +- name: Deploy pending changes + ansible.builtin.meta: flush_handlers + +- name: Start vt-booking stack + community.docker.docker_compose_v2: + project_src: "{{ vt_booking_dir }}" + files: + - compose.prod.yaml + state: present + pull: missing + build: never + wait: true + become: true + +- name: Read the gateway of the app's Docker network + ansible.builtin.command: + cmd: docker network inspect vt-booking-prod_default --format {% raw %}'{{ range .IPAM.Config }}{{ .Gateway }}{{ end }}'{% endraw %} + register: vt_booking_gateway + changed_when: false + become: true + +- name: Check that the gateway is the trusted proxy + ansible.builtin.assert: + that: vt_booking_gateway.stdout == vt_booking_trusted_proxy + fail_msg: "Gateway is {{ vt_booking_gateway.stdout }}, TRUSTED_PROXY is {{ vt_booking_trusted_proxy }}" + quiet: true diff --git a/ansible/roles/vt_booking/templates/daemon.json.j2 b/ansible/roles/vt_booking/templates/daemon.json.j2 new file mode 100644 index 0000000..59f8330 --- /dev/null +++ b/ansible/roles/vt_booking/templates/daemon.json.j2 @@ -0,0 +1,6 @@ +{ + "bip": "{{ vt_booking_docker_bip }}", + "default-address-pools": [ + { "base": "{{ vt_booking_docker_pool }}", "size": 24 } + ] +} \ No newline at end of file diff --git a/ansible/roles/vt_booking/templates/env.j2 b/ansible/roles/vt_booking/templates/env.j2 new file mode 100644 index 0000000..9ca3f99 --- /dev/null +++ b/ansible/roles/vt_booking/templates/env.j2 @@ -0,0 +1,14 @@ +# {{ ansible_managed }} +# Settings for compose.prod.yaml. See .env.example and docs/DEPLOYMENT.md in the app repository. + +DB_PASSWORD={{ vt_booking_db_password }} + +# Only the nginx on this host connects to the app +HTTP_BIND=127.0.0.1 +HTTP_PORT={{ vt_booking_http_port }} + +# The host nginx, as the app's nginx sees it: the gateway of the compose network +TRUSTED_PROXY={{ vt_booking_trusted_proxy }} +REAL_IP_HEADER=X-Forwarded-For + +IMAGE_TAG={{ vt_booking_image_tag }} \ No newline at end of file diff --git a/ansible/roles/vt_booking/templates/nginx.conf.j2 b/ansible/roles/vt_booking/templates/nginx.conf.j2 new file mode 100644 index 0000000..514f37e --- /dev/null +++ b/ansible/roles/vt_booking/templates/nginx.conf.j2 @@ -0,0 +1,71 @@ +# {{ ansible_managed }} +load_module modules/ngx_http_acme_module.so; + +user nginx nginx; +worker_processes auto; +worker_rlimit_nofile 2048; + +pid /run/nginx.pid; +error_log /var/log/nginx/error.log notice; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + gzip on; + keepalive_timeout 10; + server_tokens off; + + resolver 1.1.1.1; + + ### For ssl cert + acme_issuer letsencrypt { + uri https://acme-v02.api.letsencrypt.org/directory; + state_path /var/cache/nginx/acme-letsencrypt; + + accept_terms_of_service; + } + + ### General listener on port 80 is required to process ACME HTTP-01 challenges + server { + listen 80 default_server; + listen [::]:80 default_server; + + ### Redirect to https + return 301 https://$host$request_uri; + } + + server { + listen 443 ssl; + listen [::]:443 ssl; + + server_name {{ vt_booking_domain }}; + + ### SSL + acme_certificate letsencrypt; + ssl_certificate $acme_certificate; + ssl_certificate_key $acme_certificate_key; + ssl_certificate_cache max=2; + + ### The app's own nginx (frontend container), which passes /api on to the backend + location / { + proxy_pass http://127.0.0.1:{{ vt_booking_http_port }}; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + } + } +} \ No newline at end of file diff --git a/ansible/roles/vt_booking/templates/vt-booking-backup.sh.j2 b/ansible/roles/vt_booking/templates/vt-booking-backup.sh.j2 new file mode 100644 index 0000000..875226f --- /dev/null +++ b/ansible/roles/vt_booking/templates/vt-booking-backup.sh.j2 @@ -0,0 +1,14 @@ +#!/bin/sh +# {{ ansible_managed }} +# Nightly dump of the VT Booking database, as in docs/DEPLOYMENT.md in the app repository. +set -eu + +cd {{ vt_booking_dir }} +file=/var/backups/vt-booking/vt-booking-$(date +%F).dump + +# Write to a temporary file first, so a failed dump never replaces a good one +docker compose -f compose.prod.yaml exec -T db pg_dump -U booking -Fc booking > "$file.tmp" +mv "$file.tmp" "$file" + +# Keep 14 days +find /var/backups/vt-booking -name 'vt-booking-*.dump' -mtime +14 -delete \ No newline at end of file diff --git a/edit_group_variables.sh b/edit_group_variables.sh index f1d49b6..aee9c48 100755 --- a/edit_group_variables.sh +++ b/edit_group_variables.sh @@ -1,10 +1,10 @@ -#! /usr/bin/bash +#! /usr/bin/env bash cd ansible echo "Opening group vars in editor" -ansible-vault edit group_vars/all.yml --vault-password-file .ansible_vault_key +env EDITOR=nano ansible-vault edit group_vars/all.yml --vault-password-file .ansible_vault_key if [ $? -ne 0 ]; then echo "Error in writing group vars file, have you created a vault password file?" diff --git a/local_run_config.sh b/local_run_config.sh index b1d9217..b51ad18 100755 --- a/local_run_config.sh +++ b/local_run_config.sh @@ -1,4 +1,4 @@ -#! /usr/bin/bash +#! /usr/bin/env bash cd ansible diff --git a/run_config.sh b/run_config.sh index a3e764d..f71d5e9 100755 --- a/run_config.sh +++ b/run_config.sh @@ -1,4 +1,4 @@ -#! /usr/bin/bash +#! /usr/bin/env bash pushd ansible @@ -14,4 +14,4 @@ fi echo "Switching to friByte tailnet" tailscale switch headscale.fribyte.no -ansible-playbook playbook_infra.yml --become-password-file .ansible_sudo_password --vault-password-file .ansible_vault_key +ansible-playbook playbook_infra.yml --vault-password-file .ansible_vault_key diff --git a/setup_ansible.sh b/setup_ansible.sh index bc26e3c..d3b0c8a 100755 --- a/setup_ansible.sh +++ b/setup_ansible.sh @@ -1,4 +1,4 @@ -#! /usr/bin/bash +#! /usr/bin/env bash pushd ansible