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
# Configuration
ALM_CI_NAME="CI"
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_DATA="$HOST_BASE/data"
HOST_CONF="$HOST_BASE/conf"
HOST_LOGS="$HOST_BASE/logs"
BIN_PATH="/opt/gbo/bin"
CONTAINER_NAME="${PARAM_TENANT}-alm-ci"
mkdir -p "$HOST_DATA" "$HOST_CONF" "$HOST_LOGS"
chmod -R 750 "$HOST_BASE"
# Create host directories
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
sleep 15
# Launch container
if ! lxc launch "$CONTAINER_IMAGE" "$CONTAINER_NAME"; then
echo "Failed to launch container"
exit 1
fi
# Add directory mappings before installation
lxc config device add "${PARAM_TENANT}-alm-ci" almdata disk source="$HOST_DATA" path=/opt/gbo/data
lxc config device add "${PARAM_TENANT}-alm-ci" almconf disk source="$HOST_CONF" path=/opt/gbo/conf
lxc config device add "${PARAM_TENANT}-alm-ci" almlogs disk source="$HOST_LOGS" path=/opt/gbo/logs
# Wait for container to be ready
for i in {1..10}; do
if lxc exec "$CONTAINER_NAME" -- bash -c "true"; then
break
fi
sleep 3
done
lxc exec "${PARAM_TENANT}-alm-ci" -- bash -c "
apt-get update && apt-get install -y wget
# Add directory mappings
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
wget -O ${BIN_PATH}/forgejo-runner https://code.forgejo.org/forgejo/runner/releases/download/v6.3.1/forgejo-runner-6.3.1-linux-amd64
chmod +x ${BIN_PATH}/forgejo-runner
# Container setup
lxc exec "$CONTAINER_NAME" -- bash -c "
set -e
${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}\"
# Update and install dependencies
apt-get update && apt-get install -y wget || { echo 'Package installation failed'; exit 1; }
# 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
[Unit]
Description=ALM CI Runner
@ -42,20 +71,16 @@ After=network.target
Type=simple
User=root
Group=root
WorkingDirectory=/opt/gbo/data
ExecStart=${BIN_PATH}/forgejo-runner daemon
WorkingDirectory=$BIN_PATH
ExecStart=$BIN_PATH/forgejo-runner daemon
Restart=always
StandardOutput=append:/opt/gbo/logs/stdout.log
StandardError=append:/opt/gbo/logs/stderr.log
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable alm-ci
systemctl start alm-ci
# Enable and start service
systemctl daemon-reload || { echo 'daemon-reload failed'; exit 1; }
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