feat(gb-infra): Refactor ALM CI script for improved container setup and error handling

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-06-01 19:37:33 -03:00
parent 94abb1b02f
commit 90605456f2

View file

@ -1,38 +1,67 @@
#!/bin/bash #!/bin/bash
# Configuration
ALM_CI_NAME="CI" ALM_CI_NAME="CI"
ALM_CI_LABELS="gbo" ALM_CI_LABELS="gbo"
FORGEJO_RUNNER_VERSION="v6.3.1"
FORGEJO_RUNNER_BINARY="forgejo-runner-6.3.1-linux-amd64"
CONTAINER_IMAGE="images:debian/12"
# Paths
HOST_BASE="/opt/gbo/tenants/$PARAM_TENANT/alm-ci" HOST_BASE="/opt/gbo/tenants/$PARAM_TENANT/alm-ci"
HOST_DATA="$HOST_BASE/data" HOST_DATA="$HOST_BASE/data"
HOST_CONF="$HOST_BASE/conf" HOST_CONF="$HOST_BASE/conf"
HOST_LOGS="$HOST_BASE/logs" HOST_LOGS="$HOST_BASE/logs"
BIN_PATH="/opt/gbo/bin" BIN_PATH="/opt/gbo/bin"
CONTAINER_NAME="${PARAM_TENANT}-alm-ci"
mkdir -p "$HOST_DATA" "$HOST_CONF" "$HOST_LOGS" # Create host directories
chmod -R 750 "$HOST_BASE" mkdir -p "$HOST_DATA" "$HOST_CONF" "$HOST_LOGS" || exit 1
chmod -R 750 "$HOST_BASE" || exit 1
lxc launch images:debian/12 "${PARAM_TENANT}-alm-ci" -c security.privileged=true # Launch container
sleep 15 if ! lxc launch "$CONTAINER_IMAGE" "$CONTAINER_NAME"; then
echo "Failed to launch container"
exit 1
fi
# Add directory mappings before installation # Wait for container to be ready
lxc config device add "${PARAM_TENANT}-alm-ci" almdata disk source="$HOST_DATA" path=/opt/gbo/data for i in {1..10}; do
lxc config device add "${PARAM_TENANT}-alm-ci" almconf disk source="$HOST_CONF" path=/opt/gbo/conf if lxc exec "$CONTAINER_NAME" -- bash -c "true"; then
lxc config device add "${PARAM_TENANT}-alm-ci" almlogs disk source="$HOST_LOGS" path=/opt/gbo/logs break
fi
sleep 3
done
lxc exec "${PARAM_TENANT}-alm-ci" -- bash -c " # Add directory mappings
apt-get update && apt-get install -y wget lxc config device add "$CONTAINER_NAME" almdata disk source="$HOST_DATA" path=/opt/gbo/data || exit 1
lxc config device add "$CONTAINER_NAME" almconf disk source="$HOST_CONF" path=/opt/gbo/conf || exit 1
lxc config device add "$CONTAINER_NAME" almlogs disk source="$HOST_LOGS" path=/opt/gbo/logs || exit 1
mkdir -p ${BIN_PATH} /opt/gbo/data /opt/gbo/conf /opt/gbo/logs # Container setup
wget -O ${BIN_PATH}/forgejo-runner https://code.forgejo.org/forgejo/runner/releases/download/v6.3.1/forgejo-runner-6.3.1-linux-amd64 lxc exec "$CONTAINER_NAME" -- bash -c "
chmod +x ${BIN_PATH}/forgejo-runner set -e
${BIN_PATH}/forgejo-runner register --no-interactive \ # Update and install dependencies
--name \"${ALM_CI_NAME}\" \ apt-get update && apt-get install -y wget || { echo 'Package installation failed'; exit 1; }
--instance \"${PARAM_ALM_CI_INSTANCE}\" \
--token \"${PARAM_ALM_CI_TOKEN}\" \
--labels \"${ALM_CI_LABELS}\"
# Create directories
mkdir -p \"$BIN_PATH\" /opt/gbo/data /opt/gbo/conf /opt/gbo/logs || { echo 'Directory creation failed'; exit 1; }
# Download and install forgejo-runner
wget -O \"$BIN_PATH/forgejo-runner\" \"https://code.forgejo.org/forgejo/runner/releases/download/$FORGEJO_RUNNER_VERSION/$FORGEJO_RUNNER_BINARY\" || { echo 'Download failed'; exit 1; }
chmod +x \"$BIN_PATH/forgejo-runner\" || { echo 'chmod failed'; exit 1; }
cd \"$BIN_PATH\"
# Register runner
\"$BIN_PATH/forgejo-runner\" register --no-interactive \\
--name \"$ALM_CI_NAME\" \\
--instance \"$PARAM_ALM_CI_INSTANCE\" \\
--token \"$PARAM_ALM_CI_TOKEN\" \\
--labels \"$ALM_CI_LABELS\" || { echo 'Runner registration failed'; exit 1; }
# Create systemd service
cat > /etc/systemd/system/alm-ci.service <<EOF cat > /etc/systemd/system/alm-ci.service <<EOF
[Unit] [Unit]
Description=ALM CI Runner Description=ALM CI Runner
@ -42,20 +71,16 @@ After=network.target
Type=simple Type=simple
User=root User=root
Group=root Group=root
WorkingDirectory=/opt/gbo/data WorkingDirectory=$BIN_PATH
ExecStart=${BIN_PATH}/forgejo-runner daemon ExecStart=$BIN_PATH/forgejo-runner daemon
Restart=always Restart=always
StandardOutput=append:/opt/gbo/logs/stdout.log
StandardError=append:/opt/gbo/logs/stderr.log
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
systemctl daemon-reload # Enable and start service
systemctl enable alm-ci systemctl daemon-reload || { echo 'daemon-reload failed'; exit 1; }
systemctl start alm-ci systemctl enable alm-ci || { echo 'enable service failed'; exit 1; }
systemctl start alm-ci || { echo 'start service failed'; exit 1; }
" "
# Fix permissions on host
chown -R 100000:100000 "$HOST_BASE" # Using default LXC mapping for root