Add common module managing common services and scripts (act_runner, create/restore backups)

Fix script environment variables
Add Fefan configuration
Fix gateway services file provisionning through ssh
This commit is contained in:
2026-01-20 15:42:17 +01:00
parent 152f09ac50
commit 1de2fe9ab4
53 changed files with 285 additions and 442 deletions

View File

@@ -1,9 +1,17 @@
GITEA_HOME="/var/lib/gitea"
GITEA_CONF="/var/lib/gitea/app.ini"
GITEA_USER="git"
GITEA_VERSION="1.25.3"
GITEA_BINARY="/usr/local/bin/gitea"
GITEA_SERVICE="/etc/systemd/system/gitea.service"
DB_NAME="giteadb"
DB_USER="gitea"
GITEA_BACKUPS_DIR="/backups/gitea"
# Environment files
ENV_FILE_LOCATION=/opt/environment/.env
# Application specifics
GITEA_HOME=/var/lib/gitea
GITEA_CONF=$GITEA_HOME/app.ini
GITEA_USER=git
GITEA_VERSION=1.25.3
GITEA_BINARY=/usr/local/bin/gitea
GITEA_SERVICE=/etc/systemd/system/gitea.service
DB_NAME=giteadb
DB_USER=gitea
# Backup specifics
SERVICE_BACKUPS_DIR=/backups/gitea
SERVICE_BACKUPS_PREFIX=gitea-dump
SERVICE_BACKUPS_EXTENSION=zip

View File

@@ -35,14 +35,10 @@ mounts:
- [ "192.168.1.12:/main/backups", "/backups", "nfs", "defaults,_netdev,x-systemd.requires=network-online.target", "0", "0" ]
write_files:
- path: /opt/gitea/gitea.env
- path: /opt/environment/.env
permissions: "0644"
content: |
${env-file-content}
- path: /opt/gitea/env.sh
permissions: "0644"
content: |
${environment-setup-script}
- path: /usr/local/bin/restore-backup.sh
permissions: "0755"
content: |

View File

@@ -1,10 +1,10 @@
#!/bin/bash
set -euo pipefail
source /opt/gitea/env.sh
source /opt/environment/.env
TIMESTAMP=$(date +'%Y-%m-%d_%H%M%S')
sudo -u "$GITEA_USER" gitea dump -c "$GITEA_HOME/app.ini" -f $GITEA_BACKUPS_DIR/gitea-dump-$TIMESTAMP.zip
sudo -u "$GITEA_USER" gitea dump -c "$GITEA_CONF" -f $SERVICE_BACKUPS_DIR/$SERVICE_BACKUPS_PREFIX-$TIMESTAMP.$SERVICE_BACKUPS_EXTENSION
ls -1dt $GITEA_BACKUPS_DIR/gitea-dump-*.zip | tail -n +5 | xargs -r rm -f
ls -1dt $SERVICE_BACKUPS_DIR/$SERVICE_BACKUPS_PREFIX-*.$SERVICE_BACKUPS_EXTENSION | tail -n +5 | xargs -r rm -f

View File

@@ -1,4 +0,0 @@
#!/bin/bash
set -a
[ -f /opt/gitea/gitea.env ] && source /opt/gitea/gitea.env
set +a

View File

@@ -1,7 +1,7 @@
#!/bin/bash
set -euo pipefail
source /opt/gitea/env.sh
source /opt/environment/.env
# Gitea user
if ! id -u $GITEA_USER >/dev/null 2>&1; then
@@ -41,15 +41,15 @@ CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';
CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';
EOF
if ls -1 "$GITEA_BACKUPS_DIR"/gitea-dump-*.zip >/dev/null 2>&1; then
if ls -1 "$SERVICE_BACKUPS_DIR"/$SERVICE_BACKUPS_PREFIX-*.$SERVICE_BACKUPS_EXTENSION >/dev/null 2>&1; then
echo "---- Backup found, restoring Gitea ----"
/usr/local/bin/restore-backup.sh
else
echo "---- No backup found in $GITEA_BACKUPS_DIR, skipping restore ----"
echo "---- No backup found in $SERVICE_BACKUPS_DIR, skipping restore ----"
fi
sudo chown -R $GITEA_USER:$GITEA_USER $GITEA_BACKUPS_DIR
sudo chmod -R 770 $GITEA_BACKUPS_DIR
sudo chown -R $GITEA_USER:$GITEA_USER $SERVICE_BACKUPS_DIR
sudo chmod -R 770 $SERVICE_BACKUPS_DIR
GITEA_SECRET_KEY=$("$GITEA_BINARY" generate secret SECRET_KEY)
GITEA_JWT_SECRET=$("$GITEA_BINARY" generate secret JWT_SECRET)
@@ -90,7 +90,7 @@ ENABLED=true
EOF
echo "---- Generated Gitea app.ini with secrets ----"
chown git:git $GITEA_CONF
chown $GITEA_USER:$GITEA_USER $GITEA_CONF
chmod 640 $GITEA_CONF
systemctl daemon-reload

View File

@@ -1,7 +1,7 @@
#!/bin/bash
set -euo pipefail
source /opt/gitea/env.sh
source /opt/environment/.env
sudo -u postgres psql <<EOF
DO \$\$
@@ -16,7 +16,7 @@ END
\$\$;
EOF
LATEST_BACKUP=$(ls -1 $GITEA_BACKUPS_DIR/gitea-dump-*.zip 2>/dev/null | sort | tail -n1)
LATEST_BACKUP=$(ls -1 $SERVICE_BACKUPS_DIR/$SERVICE_BACKUPS_PREFIX-*.$SERVICE_BACKUPS_EXTENSION 2>/dev/null | sort | tail -n1)
if [ -n "$LATEST_BACKUP" ] && [ -f "$LATEST_BACKUP" ]; then
TMP_DIR=$(mktemp -d)

View File

@@ -1,9 +0,0 @@
[Unit]
Description=Run backup weekly
[Timer]
OnCalendar=Sun *-*-* 01:00:00
Persistent=true
[Install]
WantedBy=timers.target

View File

@@ -15,6 +15,7 @@ module "vm" {
ssh_public_key = var.ssh_public_key
proxmox_host_ip = var.proxmox_host_ip
cloudinit_config = templatefile(
"${path.module}/cloud-init/service.yaml",
{
@@ -22,12 +23,12 @@ module "vm" {
domain = var.domain
ssh_key = var.ssh_public_key
proxmox_host_ip = var.proxmox_host_ip
environment-setup-script = indent(6, file("${path.module}/lib/scripts/env.sh"))
restore-backup-script = indent(6, file("${path.module}/lib/scripts/restore-backup.sh"))
restore-backup-service = indent(6, file("${path.module}/lib/services/restore-backup.service"))
create-backup-script = indent(6, file("${path.module}/lib/scripts/create-backup.sh"))
create-backup-service = indent(6, file("${path.module}/lib/services/create-backup.service"))
create-backup-timer = indent(6, file("${path.module}/lib/services/create-backup.timer"))
create-backup-timer = indent(6, file("${path.module}/../common/services/create-backup.timer"))
install-gitea-script = indent(6, file("${path.module}/lib/scripts/install-gitea.sh"))
gitea-service = indent(6, file("${path.module}/lib/services/gitea.service"))

View File

@@ -1,22 +1,27 @@
variable "name" {
description = "Virtual Machine name"
type = string
}
variable "vm_id" {
description = "Virtual Machine id"
type = number
}
variable "node_name" {
description = "Proxmox node name"
type = string
default = "mop"
}
variable "cores" {
description = "Number of CPU cores for this virtual machine"
type = number
default = 2
}
variable "memory" {
description = "Memory RAM for this virtual machine"
type = number
default = 2048
}
@@ -28,35 +33,39 @@ variable "balloon" {
}
variable "template_id" {
description = "Virtual machine template ID"
type = number
}
variable "ssh_public_key" {
type = string
description = "Public SSH key for cloud-init user"
type = string
}
variable "hostname" {
description = "VM hostname"
description = "Virtual Machine hostname (<service-name>)"
type = string
default = "test"
}
variable "domain" {
description = "VM domain"
description = "Virtual Machine domain (example.fr)"
type = string
default = ""
}
variable "disk_size" {
description = "Disk size for the virtual machine"
type = number
default = 10
}
variable "proxmox_host_ip" {
description = "Proxmox host base ip"
type = string
}
variable "vm_ip_address" {
description = "Virtual machine ip"
type = string
}