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,20 +1,32 @@
# Environment files
ENV_FILE_LOCATION=/opt/environment/.env
# gitea act_runner
ACT_RUNNER_VERSION=0.2.13
ACT_RUNNER_LOCATION=/usr/local/bin
ACT_RUNNER_USER=act_runner
ENV_FILE_LOCATION=/opt/bookshelf/bookshelf.env
GITEA_INSTANCE_URL=https://gitea.aldon.fr
GITEA_RUNNER_REGISTRATION_TOKEN=<gitea-repository-runner-token>
GITEA_BOOKSHELF_APPLICATION_TOKEN=<gitea-auth-token>
GITEA_BOOKSHELF_REPOSITORY=mop/bookshelf
USERNAME=bookshelf
BOOKSHELF_BACKUPS_DIR=/backups/bookshelf
BOOKSHELF_BACKUP_PREFIX=bookshelf-dump
# gitea instance
GITEA_INSTANCE_URL=https://gitea.aldon.fr
GITEA_SERVICE_APPLICATION_TOKEN=<gitea-auth-token>
GITEA_SERVICE_REPOSITORY=mop/bookshelf
GITEA_WORKFLOW_NAME=deploy.yaml
# Applicaiton specifics
MARIADB_USER=bookshelf
MARIADB_PASSWORD=<mariadb-password>
MARIADB_DATABASE=Biblio
MARIADB_ROOT_PASSWORD=<mariadb-root-password>
SERVICE_SECRET_KEY=<bookshelf-secret-key>
SERVICE_ORIGIN=https://bookshelf.aldon.fr
SERVICE_ROOT_FQDN=https://bookshelf.aldon.fr/api
SERVICE_ROOT_FQDN=https://bookshelf.aldon.fr/api
SERVICE_DATABASE_CONTAINER_NAME=bookshelf-database-1
# VM
USERNAME=bookshelf
# Backup specifics
SERVICE_BACKUPS_DIR=/backups/bookshelf
SERVICE_BACKUPS_PREFIX=bookshelf-dump
SERVICE_BACKUPS_EXTENSION=sql

View File

@@ -30,11 +30,7 @@ mounts:
- [ "192.168.1.12:/main/backups", "/backups", "nfs", "defaults,_netdev,x-systemd.requires=network-online.target", "0", "0" ]
write_files:
- path: /opt/bookshelf/env.sh
permissions: "0644"
content: |
${environment-setup-script}
- path: /opt/bookshelf/bookshelf.env
- path: /opt/environment/.env
permissions: "0644"
content: |
${env-file-content}
@@ -66,10 +62,10 @@ write_files:
permissions: "0755"
content: |
${act_runner-install-script}
- path: /opt/bookshelf/install-bookshelf.sh
- path: /opt/bookshelf/install-service.sh
permissions: "0755"
content: |
${bookshelf-install-script}
${service-install-script}
runcmd:
# Backup setup
@@ -86,7 +82,7 @@ runcmd:
- systemctl enable act_runner.service
- systemctl start act_runner.service
# Bookshelf install
- /opt/bookshelf/install-bookshelf.sh
- /opt/bookshelf/install-service.sh
final_message: |

View File

@@ -1,8 +1,8 @@
#!/bin/bash
source /opt/bookshelf/env.sh
source /opt/environment/.env
TIMESTAMP=$(date +'%Y-%m-%d_%H%M%S')
docker exec bookshelf-database-1 mariadb-dump --all-databases -u root -p"$MARIADB_ROOT_PASSWORD" > $BOOKSHELF_BACKUPS_DIR/bookshelf-dump-$TIMESTAMP.sql
docker exec bookshelf-database-1 mariadb-dump --all-databases -u root -p"$MARIADB_ROOT_PASSWORD" > $SERVICE_BACKUPS_DIR/$SERVICE_BACKUPS_PREFIX-$TIMESTAMP.$SERVICE_BACKUPS_EXTENSION
ls -1dt $BOOKSHELF_BACKUPS_DIR/$BOOKSHELF_BACKUP_PREFIX-*.sql | 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/bookshelf/bookshelf.env ] && source /opt/bookshelf/bookshelf.env
set +a

View File

@@ -1,49 +0,0 @@
#!/bin/bash
source /opt/bookshelf/env.sh
# trigger manually a CI/CD pipeline
curl -X POST -H "Authorization: token $GITEA_BOOKSHELF_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_BOOKSHELF_REPOSITORY/actions/workflows/deploy.yaml/dispatches \
-d '{"ref": "main", "inputs": {"ref": "main"}}'
RUN_ID=$(curl -s -H "Authorization: token $GITEA_BOOKSHELF_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_BOOKSHELF_REPOSITORY/actions/runs \
| jq -r '.workflow_runs | sort_by(.created_at) | .[0].id')
while true; do
STATUS=$(curl -s -H "Authorization: token $GITEA_BOOKSHELF_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_BOOKSHELF_REPOSITORY/actions/runs/$RUN_ID \
| jq -r '.status')
if [ "$STATUS" = "completed" ]; then
CONCLUSION=$(curl -s -H "Authorization: token $GITEA_BOOKSHELF_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_BOOKSHELF_REPOSITORY/actions/runs/$RUN_ID \
| jq -r '.conclusion')
echo "Workflow finished with status: $CONCLUSION"
break
fi
echo "Waiting 10 seconds..."
sleep 10
done
if [ "$CONCLUSION" = "success" ]; then
echo "Launching command..."
while [ "$(docker inspect -f '{{.State.Running}}' bookshelf-database-1 2>/dev/null)" != "true" ]; do
echo "Waiting database container status"
sleep 5
done
until docker exec bookshelf-database-1 sh -c "mariadb -u root -p$MARIADB_ROOT_PASSWORD -e 'SELECT 1;' >/dev/null 2>&1"; do
echo "Waitin mariadb to accept connections"
sleep 5
done
echo "Restoring backup"
systemctl start restore-backup.service
else
echo "Workflow failed or was cancelled, aborting."
exit 1
fi

View File

@@ -1,69 +0,0 @@
#!/bin/bash
source /opt/bookshelf/env.sh
## .env should define
# ACT_RUNNER_USER: act_runner username (act_runner)
# ACT_RUNNER_LOCATION: act_runner binary location (/usr/local/bin)
# ACT_RUNNER_VERSION: act_runner version (0.2.13)
# ENV_FILE_LOCATION: .env file location on vm (/opt/bookshelf/bookshelf.env)
# GITEA_INSTANCE_URL: url of the gitea instance (https://gitea.aldon.fr)
# GITEA_RUNNER_REGISTRATION_TOKEN: registration token for gitea runner (repository scope)
# USERNAME: username of the vm (bookshelf)
# REPOSITORY: repository on which service code is hosted (mop/bookshelf)
if ! id -u $ACT_RUNNER_USER >/dev/null 2>&1; then
adduser \
--system \
--shell /bin/bash \
--gecos 'Action runner user' \
--ingroup docker\
--disabled-password \
--home /home/$ACT_RUNNER_USER \
$ACT_RUNNER_USER
fi
wget -O $ACT_RUNNER_LOCATION/act_runner https://dl.gitea.com/act_runner/$ACT_RUNNER_VERSION/act_runner-$ACT_RUNNER_VERSION-linux-amd64
chmod +x $ACT_RUNNER_LOCATION/act_runner
cat <<EOF > /home/$ACT_RUNNER_USER/config.yaml
log:
level: info
runner:
file: .runner
capacity: 1
timeout: 3h
shutdown_timeout: 0s
insecure: false
fetch_timeout: 5s
env_file: $ENV_FILE_LOCATION
fetch_interval: 2s
github_mirror: ''
labels:
- 'ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest'
- 'ubuntu-22.04:docker://docker.gitea.com/runner-images:ubuntu-22.04'
- 'ubuntu-20.04:docker://docker.gitea.com/runner-images:ubuntu-20.04'
cache:
enabled: true
dir: ""
host: ""
port: 0
external_server: ""
container:
network: ""
privileged: false
options:
workdir_parent:
valid_volumes: []
docker_host: ""
force_pull: true
force_rebuild: false
require_docker: false
docker_timeout: 0s
host:
workdir_parent:
EOF
cd /home/act_runner
sudo -u $ACT_RUNNER_USER act_runner register --no-interactive --instance $GITEA_INSTANCE_URL --token $GITEA_RUNNER_REGISTRATION_TOKEN --name $USERNAME --labels $USERNAME
chown -R $ACT_RUNNER_USER:docker /home/$ACT_RUNNER_USER

View File

@@ -1,8 +1,8 @@
#!/bin/bash
source /opt/bookshelf/env.sh
source /opt/environment/.env
LATEST_BACKUP=$(ls -1 $BOOKSHELF_BACKUPS_DIR/$BOOKSHELF_BACKUP_PREFIX-*.sql 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
cat $LATEST_BACKUP | docker exec -i bookshelf-database-1 mariadb -u root -p"$MARIADB_ROOT_PASSWORD" -D $MARIADB_DATABASE

View File

@@ -1,16 +0,0 @@
[Unit]
Description=Gitea Actions runner
Documentation=https://gitea.com/gitea/act_runner
After=docker.service
[Service]
ExecStart=/usr/local/bin/act_runner daemon --config /home/act_runner/config.yaml
ExecReload=/bin/kill -s HUP $MAINPID
WorkingDirectory=/home/act_runner
TimeoutSec=0
RestartSec=10
Restart=always
User=act_runner
[Install]
WantedBy=multi-user.target

View File

@@ -1,9 +0,0 @@
[Unit]
Description=Backup Service
Wants=network.target
After=network.target docker.service
[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/backup.sh

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

@@ -1,9 +0,0 @@
[Unit]
Description=Restore latest backup
After=network.target
Requires=docker.service
[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/restore-backup.sh

View File

@@ -15,22 +15,24 @@ 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",
{
hostname = var.hostname
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"))
proxmox_host_ip = var.proxmox_host_ip
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"))
restore-backup-service = indent(6, file("${path.module}/../common/services/docker/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"))
act_runner-service = indent(6, file("${path.module}/lib/services/act_runner.service"))
act_runner-install-script = indent(6, file("${path.module}/lib/scripts/install-runner.sh"))
bookshelf-install-script = indent(6, file("${path.module}/lib/scripts/install-bookshelf.sh"))
create-backup-service = indent(6, file("${path.module}}/../common/services/docker/create-backup.service"))
create-backup-timer = indent(6, file("${path.module}/../common/services/create-backup.timer"))
act_runner-service = indent(6, file("${path.module}/../common/services/act_runner.service"))
act_runner-install-script = indent(6, file("${path.module}/../common/scripts/install-runner.sh"))
service-install-script = indent(6, file("${path.module}/../common/scripts/install-service-ci.sh"))
env-file-content = indent(6, file("${path.module}/.env"))
}

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
}