add gateway automatic template and fefan vm

This commit is contained in:
2026-01-19 17:05:38 +01:00
parent d98c7b8bdb
commit f851ead7cd
56 changed files with 1243 additions and 82 deletions

View File

@@ -0,0 +1,24 @@
ACT_RUNNER_VERSION=0.2.13
ACT_RUNNER_LOCATION=/usr/local/bin
ACT_RUNNER_USER=act_runner
ENV_FILE_LOCATION=/opt/fefan/fefan.env
GITEA_INSTANCE_URL=https://gitea.aldon.fr
GITEA_RUNNER_REGISTRATION_TOKEN=<gitea-repository-runner-token>
GITEA_FEFAN_APPLICATION_TOKEN=<gitea-auth-token>
GITEA_FEFAN_REPOSITORY=Mop/fefan
NEXT_PUBLIC_CONTENT_URI=https://content.fefan.fr/api
NEXT_PUBLIC_IMG_URI=https://content.fefan.fr
NEXT_PUBLIC_ORIGIN=https://fefan.fr
POSTGRES_USER=strapi
POSTGRES_PASSWORD=password
STRAPI_APP_KEYS=
STRAPI_TOKEN_SALT=
STRAPI_ADMIN_JWT_SECRET=
STRAPI_TRANSFER_TOKEN_SALT=
STRAPI_JWT_SECRET=
USERNAME=fefan
FEFAN_BACKUPS_DIR=/backups/fefan
FEFAN_BACKUP_PREFIX=fefan-dump

View File

@@ -0,0 +1,92 @@
#cloud-config
hostname: ${hostname}
local-hostname: ${hostname}
fqdn: ${hostname}.${domain}
manage_etc_hosts: true
users:
- default
- name: ${hostname}
groups: sudo
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ${ssh_key}
disable_root: true
package_update: true
package_upgrade: false
packages:
- git
- nfs-common
- docker.io
- docker-compose
- curl
- jq
mounts:
- [ "192.168.1.12:/main/backups", "/backups", "nfs", "defaults,_netdev,x-systemd.requires=network-online.target", "0", "0" ]
write_files:
- path: /opt/fefan/env.sh
permissions: "0644"
content: |
${environment-setup-script}
- path: /opt/fefan/fefan.env
permissions: "0644"
content: |
${env-file-content}
- path: /usr/local/bin/restore-backup.sh
permissions: "0755"
content: |
${restore-backup-script}
- path: /etc/systemd/system/restore-backup.service
permissions: "0644"
content: |
${restore-backup-service}
- path: /usr/local/bin/backup.sh
permissions: "0755"
content: |
${create-backup-script}
- path: /etc/systemd/system/create-backup.timer
permissions: "0644"
content: |
${create-backup-timer}
- path: /etc/systemd/system/create-backup.service
permissions: "0644"
content: |
${create-backup-service}
- path: /etc/systemd/system/act_runner.service
permissions: "0644"
content: |
${act_runner-service}
- path: /opt/fefan/install-runner.sh
permissions: "0755"
content: |
${act_runner-install-script}
- path: /opt/fefan/install-fefan.sh
permissions: "0755"
content: |
${fefan-install-script}
runcmd:
# Backup setup
- mkdir -p /backups
- mount -t nfs ${proxmox_host_ip}:/main/backups /backups
- systemctl enable --now create-backup.timer
# Docker setup
- systemctl enable docker
- systemctl start docker
- usermod -aG docker ${hostname}
# Act_runner install
- /opt/fefan/install-runner.sh
- systemctl daemon-reload
- systemctl enable act_runner.service
- systemctl start act_runner.service
# Install fefan
- /opt/fefan/install-fefan.sh
final_message: |
Base system ready for ${hostname}

View File

@@ -0,0 +1,14 @@
#!/bin/bash
source /opt/fefan/env.sh
# FEFAN_BACKUPS_DIR=/backups/fefan
# FEFAN_BACKUP_PREFIX=fefan-dump
TIMESTAMP=$(date +'%Y-%m-%d_%H%M%S')
docker exec fefan-strapi-1 yarn strapi export -f export --no-encrypt
docker cp fefan-strapi-1:/app/export.tar.gz $FEFAN_BACKUPS_DIR/$FEFAN_BACKUP_PREFIX-$TIMESTAMP.tar.gz
docker exec fefan-strapi-1 rm /app/export.tar.gz
ls -1dt $FEFAN_BACKUPS_DIR/$FEFAN_BACKUP_PREFIX-*.tar.gz | tail -n +5 | xargs -r rm -f

View File

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

View File

@@ -0,0 +1,44 @@
#!/bin/bash
source /opt/fefan/env.sh
# trigger manually a CI/CD pipeline
curl -X POST -H "Authorization: token $GITEA_FEFAN_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_FEFAN_REPOSITORY/actions/workflows/deploy.yaml/dispatches \
-d '{"ref": "main", "inputs": {"ref": "main"}}'
RUN_ID=$(curl -s -H "Authorization: token $GITEA_FEFAN_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_FEFAN_REPOSITORY/actions/runs \
| jq -r '.workflow_runs | sort_by(.created_at) | .[0].id')
while true; do
STATUS=$(curl -s -H "Authorization: token $GITEA_FEFAN_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_FEFAN_REPOSITORY/actions/runs/$RUN_ID \
| jq -r '.status')
if [ "$STATUS" = "completed" ]; then
CONCLUSION=$(curl -s -H "Authorization: token $GITEA_FEFAN_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_FEFAN_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}}' fefan-db-1 2>/dev/null)" != "true" ]; do
echo "Waiting database container status"
sleep 5
done
systemctl start restore-backup.service
else
echo "Workflow failed or was cancelled, aborting."
exit 1
fi

View File

@@ -0,0 +1,69 @@
#!/bin/bash
source /opt/fefan/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/fefan/fefan.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 (fefan)
# REPOSITORY: repository on which service code is hosted (mop/fefan)
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

@@ -0,0 +1,14 @@
#!/bin/bash
source /opt/fefan/env.sh
# FEFAN_BACKUPS_DIR=/backups/fefan
# FEFAN_BACKUP_PREFIX=fefan-dump
LATEST_BACKUP=$(ls -1 $FEFAN_BACKUPS_DIR/$FEFAN_BACKUP_PREFIX-*.tar.gz 2>/dev/null | sort | tail -n1)
if [ -n "$LATEST_BACKUP" ] && [ -f "$LATEST_BACKUP" ]; then
docker cp $LATEST_BACKUP fefan-strapi-1:/app/${LATEST_BACKUP#"$FEFAN_BACKUPS_DIR"/}
docker exec fefan-strapi-1 yarn strapi import -f /app/${LATEST_BACKUP#"$FEFAN_BACKUPS_DIR"/} --force
docker exec fefan-strapi-1 rm /app/${LATEST_BACKUP#"$FEFAN_BACKUPS_DIR"/}
fi

View File

@@ -0,0 +1,16 @@
[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

@@ -0,0 +1,9 @@
[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

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

View File

@@ -0,0 +1,9 @@
[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

@@ -0,0 +1,37 @@
module "vm" {
source = "../../vm"
name = var.name
hostname = var.hostname
domain = var.domain
vm_id = var.vm_id
node_name = var.node_name
vm_ip_address = var.vm_ip_address
template_id = var.template_id
cores = var.cores
memory = var.memory
disk_size = var.disk_size
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"))
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"))
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"))
fefan-install-script = indent(6, file("${path.module}/lib/scripts/install-fefan.sh"))
env-file-content = indent(6, file("${path.module}/.env"))
}
)
}

View File

@@ -0,0 +1,18 @@
output "traefik_service" {
value = [
{
domain = "fefan.fr"
name = "fefan"
host = ""
ip = var.vm_ip_address
port = 3000
},
{
domain = "fefan.fr"
name = "content-fefan"
host = "content"
ip = var.vm_ip_address
port = 1337
}
]
}

View File

@@ -0,0 +1,62 @@
variable "name" {
type = string
}
variable "vm_id" {
type = number
}
variable "node_name" {
type = string
default = "mop"
}
variable "cores" {
type = number
default = 2
}
variable "memory" {
type = number
default = 2048
}
variable "balloon" {
description = "Minimum vm memory, using ballooning devide to reach Proxmox node memory target."
type = number
default = 1024
}
variable "template_id" {
type = number
}
variable "ssh_public_key" {
type = string
description = "Public SSH key for cloud-init user"
}
variable "hostname" {
description = "VM hostname"
type = string
default = "test"
}
variable "domain" {
description = "VM domain"
type = string
default = ""
}
variable "disk_size" {
type = number
default = 10
}
variable "proxmox_host_ip" {
type = string
}
variable "vm_ip_address" {
type = string
}