Files
gitea-migration/phase2_gitea_fedora.sh
S dc08375ad0 fix: address multiple bugs from code review
- teardown_all.sh: replace `yes |` pipeline with `< <(yes)` process
  substitution to avoid SIGPIPE (exit 141) false failures under pipefail
- phase6_teardown.sh: extract push mirror `.id` instead of `.remote_name`
  to match the DELETE /push_mirrors/{id} API contract
- phase5_migrate_pipelines.sh: expand sed regex from `[a-z_]*` to
  `[a-z_.]*` to handle nested GitHub contexts like
  `github.event.pull_request.number`
- lib/common.sh: render_template now requires explicit variable list to
  prevent envsubst from eating Nginx variables ($host, $proxy_add_...)
- backup scripts: remove MacBook relay, use direct Unraid↔Fedora SCP;
  fix dump path to write to /data/ (mounted volume) instead of /tmp/
  (container-only); add unzip -t integrity verification
- preflight.sh: add --skip-port-checks flag for resuming with
  --start-from (ports already bound by earlier phases)
- run_all.sh: update run_step to pass extra args; use --skip-port-checks
  when --start-from > 1
- post-checks (phase4/7/9): wrap API calls in helper functions with
  >/dev/null redirection instead of passing -o /dev/null as API data
- phase8: replace GitHub archiving with [MIRROR] description marking
  and disable wiki/projects/Pages (archived repos reject push mirrors)
- restore_to_primary.sh: add require_vars for Fedora SSH variables

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 20:18:35 -05:00

157 lines
7.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# phase2_gitea_fedora.sh — Deploy Gitea on Fedora (backup instance)
# Identical to Phase 1 except: targets Fedora, uses BACKUP vars, NO org created.
# Produces: Running Gitea on Fedora, admin user, backup API token in .env
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
load_env
require_vars FEDORA_IP FEDORA_SSH_USER FEDORA_SSH_PORT \
FEDORA_GITEA_PORT FEDORA_GITEA_SSH_PORT FEDORA_GITEA_DATA_PATH \
GITEA_ADMIN_USER GITEA_ADMIN_PASSWORD GITEA_ADMIN_EMAIL \
GITEA_INSTANCE_NAME GITEA_DB_TYPE GITEA_VERSION \
GITEA_BACKUP_INTERNAL_URL
phase_header 2 "Gitea on Fedora (Backup)"
# Alias for template rendering — same template as Phase 1, different values
DATA_PATH="$FEDORA_GITEA_DATA_PATH"
# ---------------------------------------------------------------------------
# Step 1: Create data directories on Fedora
# ---------------------------------------------------------------------------
log_step 1 "Creating data directories on Fedora..."
if ssh_exec FEDORA "test -d '${DATA_PATH}/data'"; then
log_info "Data directory already exists — skipping"
else
ssh_exec FEDORA "mkdir -p '${DATA_PATH}/data' '${DATA_PATH}/config'"
log_success "Data directories created"
fi
# ---------------------------------------------------------------------------
# Step 2: Render + SCP docker-compose file
# Uses the same template as Phase 1 but with Fedora-specific port/path vars.
# ---------------------------------------------------------------------------
log_step 2 "Deploying docker-compose.yml..."
if ssh_exec FEDORA "test -f '${DATA_PATH}/docker-compose.yml'"; then
log_info "docker-compose.yml already exists — skipping"
else
TMPFILE=$(mktemp)
export DATA_PATH GITEA_PORT="${FEDORA_GITEA_PORT}" GITEA_SSH_PORT="${FEDORA_GITEA_SSH_PORT}"
render_template "${SCRIPT_DIR}/templates/docker-compose-gitea.yml.tpl" "$TMPFILE" \
'${GITEA_VERSION} ${DATA_PATH} ${GITEA_PORT} ${GITEA_SSH_PORT}'
scp_to FEDORA "$TMPFILE" "${DATA_PATH}/docker-compose.yml"
rm -f "$TMPFILE"
log_success "docker-compose.yml deployed"
fi
# ---------------------------------------------------------------------------
# Step 3: Render + SCP app.ini
# Uses GITEA_BACKUP_INTERNAL_URL as the ROOT_URL for the Fedora instance.
# The domain is derived from the backup URL since Fedora doesn't need a
# public-facing domain — it's accessed internally for mirrors.
# ---------------------------------------------------------------------------
log_step 3 "Deploying app.ini..."
if ssh_exec FEDORA "test -f '${DATA_PATH}/config/app.ini'"; then
log_info "app.ini already exists — skipping"
else
TMPFILE=$(mktemp)
# Generate a unique secret key for the Fedora instance (different from Unraid)
GITEA_SECRET_KEY=$(openssl rand -hex 32)
export GITEA_SECRET_KEY
# Override GITEA_DOMAIN for the backup instance — use the IP:port since
# the Fedora instance doesn't have a public domain
GITEA_DOMAIN="${FEDORA_IP}:${FEDORA_GITEA_PORT}"
export GITEA_DOMAIN
render_template "${SCRIPT_DIR}/templates/app.ini.tpl" "$TMPFILE" \
'${GITEA_DOMAIN} ${GITEA_DB_TYPE} ${GITEA_SECRET_KEY}'
scp_to FEDORA "$TMPFILE" "${DATA_PATH}/config/app.ini"
rm -f "$TMPFILE"
log_success "app.ini deployed"
fi
# ---------------------------------------------------------------------------
# Step 4: Start Gitea container
# ---------------------------------------------------------------------------
log_step 4 "Starting Gitea container..."
CONTAINER_STATUS=$(ssh_exec FEDORA "docker ps --filter name=gitea --format '{{.Status}}'" 2>/dev/null || true)
if [[ "$CONTAINER_STATUS" == *"Up"* ]]; then
log_info "Gitea container already running — skipping"
else
# Try "docker compose" plugin first, fall back to standalone
ssh_exec FEDORA "cd '${DATA_PATH}' && docker compose up -d 2>/dev/null || docker-compose up -d"
log_success "Gitea container started"
fi
# ---------------------------------------------------------------------------
# Step 5: Wait for Gitea to be ready
# ---------------------------------------------------------------------------
log_step 5 "Waiting for Gitea to be ready..."
wait_for_http "${GITEA_BACKUP_INTERNAL_URL}/api/v1/version" 120
# ---------------------------------------------------------------------------
# Step 6: Create admin user (same creds as primary — shared credentials)
# ---------------------------------------------------------------------------
log_step 6 "Creating admin user..."
if curl -sf -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" "${GITEA_BACKUP_INTERNAL_URL}/api/v1/user" -o /dev/null 2>/dev/null; then
log_info "Admin user already exists — skipping"
else
# Create via CLI inside container, same pattern as Phase 1
ssh_exec FEDORA "docker exec gitea gitea admin user create \
--username '${GITEA_ADMIN_USER}' \
--password '${GITEA_ADMIN_PASSWORD}' \
--email '${GITEA_ADMIN_EMAIL}' \
--admin \
--must-change-password=false" || true
if curl -sf -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" "${GITEA_BACKUP_INTERNAL_URL}/api/v1/user" -o /dev/null 2>/dev/null; then
log_success "Admin user created"
else
log_error "Failed to create admin user on Fedora"
exit 1
fi
fi
# ---------------------------------------------------------------------------
# Step 7: Generate API token and save to .env as GITEA_BACKUP_ADMIN_TOKEN
# ---------------------------------------------------------------------------
log_step 7 "Generating backup API token..."
if [[ -n "${GITEA_BACKUP_ADMIN_TOKEN:-}" ]]; then
# Verify existing token works
if curl -sf -H "Authorization: token ${GITEA_BACKUP_ADMIN_TOKEN}" "${GITEA_BACKUP_INTERNAL_URL}/api/v1/user" -o /dev/null 2>/dev/null; then
log_info "Backup API token already exists and is valid — skipping"
else
log_warn "Existing backup token is invalid — generating new one"
GITEA_BACKUP_ADMIN_TOKEN=""
fi
fi
if [[ -z "${GITEA_BACKUP_ADMIN_TOKEN:-}" ]]; then
# Generate via basic auth — token is only returned once in the .sha1 field
TOKEN_RESPONSE=$(curl -sf -u "${GITEA_ADMIN_USER}:${GITEA_ADMIN_PASSWORD}" \
-X POST \
-H "Content-Type: application/json" \
-d '{"name":"migration-token","scopes":["all"]}' \
"${GITEA_BACKUP_INTERNAL_URL}/api/v1/users/${GITEA_ADMIN_USER}/tokens")
GITEA_BACKUP_ADMIN_TOKEN=$(printf '%s' "$TOKEN_RESPONSE" | jq -r '.sha1')
if [[ -z "$GITEA_BACKUP_ADMIN_TOKEN" ]] || [[ "$GITEA_BACKUP_ADMIN_TOKEN" == "null" ]]; then
log_error "Failed to generate backup API token"
exit 1
fi
save_env_var "GITEA_BACKUP_ADMIN_TOKEN" "$GITEA_BACKUP_ADMIN_TOKEN"
log_success "Backup API token generated and saved to .env"
fi
# NOTE: No organization is created on the Fedora instance.
# Mirror repos will be stored under the admin user's namespace.
log_success "Phase 2 complete — Gitea backup instance is running on Fedora"