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

@@ -0,0 +1,70 @@
#!/bin/bash
source /opt/environment/.env
## This script installs `act_runner` onto the machine.
## Environment variables
# 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 (ex: 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

@@ -0,0 +1,58 @@
#!/bin/bash
source /opt/environment/.env
## This script triggers a CI/CD pipeline.
# It then restores a backup using `restore-backup.service` systemd service
## This script requires `restore-backup.service` in `/etc/systemd/system`
## Dependencies (in service.yaml `packages` section)
# - jq
# - curl
## Environment variables
# GITEA_SERVICE_APPLICATION_TOKEN: Gitea API token.
# GITEA_SERVICE_REPOSITORY: repository where the project resides.
# GITEA_INSTANCE_URL: Gitea url.
# SERVICE_DATABASE_CONTAINER_NAME: Container name for database.
# GITEA_WORKFLOW_NAME: deploy.yaml
curl -X POST -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/workflows/$GITEA_WORKFLOW_NAME/dispatches \
-d '{"ref": "main", "inputs": {"ref": "main"}}'
RUN_ID=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/runs \
| jq -r '.workflow_runs | sort_by(.created_at) | .[0].id')
while true; do
STATUS=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_REPOSITORY/actions/runs/$RUN_ID \
| jq -r '.status')
if [ "$STATUS" = "completed" ]; then
CONCLUSION=$(curl -s -H "Authorization: token $GITEA_SERVICE_APPLICATION_TOKEN" \
$GITEA_INSTANCE_URL/api/v1/repos/$GITEA_SERVICE_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}}' $SERVICE_DATABASE_CONTAINER_NAME-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