Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions deploy/bbledger-autodeploy.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# /etc/systemd/system/bbledger-autodeploy.service — continuous deployment:
# pull :latest and restart the bot only when CI has published a new image.
# Pull-based on purpose: CI never gets credentials for the server.
[Unit]
Description=bbledger: restart the bot when a new image is published
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
# $$ keeps systemd from eating the shell's $; a failed pull (offline, GHCR
# down) fails the unit visibly and the timer simply tries again
ExecStart=/bin/sh -ec '\
docker pull -q ghcr.io/schroedingberg/bbledger:latest >/dev/null; \
new=$$(docker image inspect -f "{{.Id}}" ghcr.io/schroedingberg/bbledger:latest); \
cur=$$(docker inspect -f "{{.Image}}" bbledger-bot 2>/dev/null || true); \
[ "$$new" = "$$cur" ] || systemctl restart bbledger-bot'
10 changes: 10 additions & 0 deletions deploy/bbledger-autodeploy.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# /etc/systemd/system/bbledger-autodeploy.timer
[Unit]
Description=Check for a new bbledger image every 5 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=5min

[Install]
WantedBy=timers.target
10 changes: 6 additions & 4 deletions infra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ Security → SSH keys) — every project key is installed on the server.
3. Send `12,30 Test` in the Telegram group; expect the ✓ — and the entry
commit appearing in the data repo moments later.

Redeploying app versions never touches infra: CI pushes a new image and
`ssh root@<ip> systemctl restart bbledger-bot` picks it up (IP is in the
apply output). `destroy` is safe for the ledger: every entry is pushed to
the data repo, and the next `apply` resumes from the clone.
Redeploying app versions never touches infra: merging to main releases a
new image, and the server's `bbledger-autodeploy.timer` pulls it and
restarts the bot within ~5 minutes (pull-based on purpose — CI holds no
server credentials). `bb deploy` / `bb restart` cover the impatient case.
`destroy` is safe for the ledger: every entry is pushed to the data repo,
and the next `apply` resumes from the clone.
8 changes: 7 additions & 1 deletion infra/cloud-init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ write_files:
- path: /etc/systemd/system/bbledger-push.path
content: |
${indent(6, push_path)}
- path: /etc/systemd/system/bbledger-autodeploy.service
content: |
${indent(6, autodeploy_unit)}
- path: /etc/systemd/system/bbledger-autodeploy.timer
content: |
${indent(6, autodeploy_timer)}

runcmd:
- systemctl enable --now docker
Expand All @@ -48,4 +54,4 @@ runcmd:
echo '${ghcr_token}' | docker login ghcr.io -u '${ghcr_user}' --password-stdin
fi
- systemctl daemon-reload
- systemctl enable --now bbledger-bot.service bbledger-summary.timer bbledger-push.path
- systemctl enable --now bbledger-bot.service bbledger-summary.timer bbledger-push.path bbledger-autodeploy.timer
22 changes: 12 additions & 10 deletions infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ resource "hcloud_server" "bot" {
firewall_ids = [hcloud_firewall.ssh_only.id]

user_data = templatefile("${path.module}/cloud-init.yaml", {
bot_token = var.bot_token
data_repo = var.data_repo
data_deploy_key = var.data_deploy_key
ghcr_user = var.ghcr_user
ghcr_token = var.ghcr_token
bot_unit = file("${path.module}/../deploy/bbledger-bot.service")
summary_unit = file("${path.module}/../deploy/bbledger-summary.service")
summary_timer = file("${path.module}/../deploy/bbledger-summary.timer")
push_unit = file("${path.module}/../deploy/bbledger-push.service")
push_path = file("${path.module}/../deploy/bbledger-push.path")
bot_token = var.bot_token
data_repo = var.data_repo
data_deploy_key = var.data_deploy_key
ghcr_user = var.ghcr_user
ghcr_token = var.ghcr_token
bot_unit = file("${path.module}/../deploy/bbledger-bot.service")
summary_unit = file("${path.module}/../deploy/bbledger-summary.service")
summary_timer = file("${path.module}/../deploy/bbledger-summary.timer")
push_unit = file("${path.module}/../deploy/bbledger-push.service")
push_path = file("${path.module}/../deploy/bbledger-push.path")
autodeploy_unit = file("${path.module}/../deploy/bbledger-autodeploy.service")
autodeploy_timer = file("${path.module}/../deploy/bbledger-autodeploy.timer")
})

# an edited cloud-init must not silently rebuild a running VM — recreate
Expand Down
Loading