mnemo_cards/tools/deploy/web-app/setup-forgejo-domain.sh
2025-11-16 16:54:50 +03:00

143 lines
4.8 KiB
Bash
Executable file

#!/bin/bash
# Setup script for Forgejo domain (code.mnemo-cards.online)
# Usage: ./setup-forgejo-domain.sh
set -e
# Load configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.sh"
echo "🚀 Setting up Forgejo domain: code.mnemo-cards.online..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
# Upload nginx config for Forgejo
print_status "Uploading Forgejo nginx configuration..."
scp "$SCRIPT_DIR/forgejo-nginx.conf" "$SERVER_USER@$SERVER_IP:/tmp/forgejo-nginx.conf"
print_status "Setting up Forgejo domain on server..."
# Execute setup commands on server
ssh "$SERVER_USER@$SERVER_IP" << EOF
set -e
echo "Setting up Forgejo domain configuration..."
# Install SSL certificate for code.mnemo-cards.online
if [ ! -d "/etc/letsencrypt/live/code.mnemo-cards.online" ]; then
echo "🔐 Getting Let's Encrypt SSL certificate for code.mnemo-cards.online..."
# Stop nginx temporarily for certificate issuance
systemctl stop nginx 2>/dev/null || true
if certbot certonly --standalone -d code.mnemo-cards.online --non-interactive --agree-tos --email admin@memo-cards.online; then
echo "✅ Let's Encrypt certificate obtained successfully for code.mnemo-cards.online!"
else
echo "❌ Failed to get Let's Encrypt certificate. Generating self-signed certificate..."
if [ ! -f "/etc/ssl/certs/nginx-selfsigned-forgejo.crt" ]; then
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/nginx-selfsigned-forgejo.key \
-out /etc/ssl/certs/nginx-selfsigned-forgejo.crt \
-subj "/C=RU/ST=Moscow/L=Moscow/O=MnemoCards/OU=IT/CN=code.mnemo-cards.online"
fi
fi
# Start nginx again
systemctl start nginx 2>/dev/null || true
else
echo "✅ Let's Encrypt certificate already exists for code.mnemo-cards.online"
fi
# Configure nginx for Forgejo
echo "Configuring nginx for Forgejo..."
# Copy Forgejo nginx configuration
cp /tmp/forgejo-nginx.conf /etc/nginx/sites-available/forgejo
# Enable Forgejo site
ln -sf /etc/nginx/sites-available/forgejo /etc/nginx/sites-enabled/forgejo
# Test nginx configuration
nginx -t
# Restart nginx
systemctl restart nginx
systemctl enable nginx
# Setup cron for certificate renewal (if not already configured)
if ! crontab -l | grep -q "code.mnemo-cards.online"; then
echo "Setting up cron job for Forgejo certificate renewal..."
(crontab -l ; echo "0 12 * * * certbot renew --quiet --cert-name code.mnemo-cards.online") | crontab -
fi
# Update Forgejo ROOT_URL configuration
echo "Updating Forgejo ROOT_URL configuration..."
# Find Forgejo app.ini file
FORGEJO_CONFIG=""
for config_path in "/etc/forgejo/app.ini" "/home/forgejo/custom/conf/app.ini" "/var/lib/forgejo/custom/conf/app.ini" "/opt/forgejo/custom/conf/app.ini"; do
if [ -f "$config_path" ]; then
FORGEJO_CONFIG="$config_path"
echo "Found Forgejo config at: $FORGEJO_CONFIG"
break
fi
done
if [ -n "$FORGEJO_CONFIG" ]; then
# Backup original config
cp "$FORGEJO_CONFIG" "${FORGEJO_CONFIG}.backup.$(date +%Y%m%d_%H%M%S)"
# Update ROOT_URL
sed -i 's|ROOT_URL.*=.*|ROOT_URL = https://code.mnemo-cards.online|' "$FORGEJO_CONFIG"
echo "Updated ROOT_URL in Forgejo configuration"
# Restart Forgejo service
if systemctl is-active --quiet forgejo 2>/dev/null; then
echo "Restarting Forgejo service..."
systemctl restart forgejo
elif systemctl is-active --quiet gitea 2>/dev/null; then
echo "Restarting Gitea service..."
systemctl restart gitea
else
echo "Warning: Could not find active Forgejo/Gitea service to restart"
fi
else
echo "Warning: Could not find Forgejo app.ini configuration file"
echo "Please manually update ROOT_URL to https://code.mnemo-cards.online in your Forgejo config"
fi
echo "Forgejo domain setup completed successfully!"
echo "Forgejo is now available at: https://code.mnemo-cards.online"
echo "Access by IP address is blocked"
EOF
print_success "Forgejo domain setup completed successfully! 🎉"
print_success "Forgejo is now available at: https://code.mnemo-cards.online"
print_info "Make sure DNS is configured: code.mnemo-cards.online -> $SERVER_IP"