From 8e0bbe901eec580bc081dad84eb23fd4cac9b140 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 16 Nov 2025 23:06:26 +0300 Subject: [PATCH] o2 --- tools/deploy/web-app/config.sh | 80 +++++++++++++++++++++++++++++++++- tools/deploy/web-app/deploy.sh | 20 +++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/tools/deploy/web-app/config.sh b/tools/deploy/web-app/config.sh index f8cc48a..937bd61 100644 --- a/tools/deploy/web-app/config.sh +++ b/tools/deploy/web-app/config.sh @@ -171,7 +171,11 @@ check_build_directory() { # Function to build Flutter web app for production build_flutter_app() { print_status "Building Flutter web app for production..." - flutter build $FLUTTER_BUILD_TARGET $FLUTTER_BUILD_MODE --dart-define=API_BASE_URL=$API_BASE_URL --dart2js-optimization=O2 + # Use -O2 optimization and disable wasm to reduce memory usage during compilation + flutter build $FLUTTER_BUILD_TARGET $FLUTTER_BUILD_MODE \ + --dart-define=API_BASE_URL=$API_BASE_URL \ + --dart2js-optimization=O2 \ + --no-wasm if [ $? -ne 0 ]; then print_error "Flutter build failed" @@ -217,6 +221,78 @@ restart_nginx() { print_success "Nginx restarted successfully" } +# Function to setup swap if needed +setup_swap() { + print_status "Checking swap configuration..." + + # Check current swap + CURRENT_SWAP=$(free -m | awk '/^Swap:/ {print $2}') + RECOMMENDED_SWAP=2048 # 2GB + + if [ "$CURRENT_SWAP" -lt "$RECOMMENDED_SWAP" ]; then + print_warning "Current swap is ${CURRENT_SWAP}MB, recommended is ${RECOMMENDED_SWAP}MB" + print_status "Setting up temporary swap file..." + + # Create swap file if it doesn't exist + if [ ! -f /swapfile ]; then + dd if=/dev/zero of=/swapfile bs=1M count=$RECOMMENDED_SWAP status=progress + chmod 600 /swapfile + mkswap /swapfile + swapon /swapfile + + # Optimize swap usage (don't add to fstab - it's temporary) + sysctl vm.swappiness=10 + + print_success "Temporary swap file created and activated (${RECOMMENDED_SWAP}MB)" + # Mark that we created it for cleanup + touch /tmp/.swap_created_by_deploy + else + # Swap exists, activate it if not active + if ! swapon --show | grep -q '/swapfile'; then + swapon /swapfile + print_info "Existing swap file activated" + else + print_info "Swap file already active" + fi + fi + else + print_success "Swap is already configured (${CURRENT_SWAP}MB)" + fi +} + +# Function to remove temporary swap after deployment +remove_swap() { + print_status "Checking if temporary swap needs cleanup..." + + # Only remove swap if we created it during this deployment + if [ -f /tmp/.swap_created_by_deploy ]; then + print_status "Removing temporary swap file..." + + # Deactivate swap + if swapon --show | grep -q '/swapfile'; then + swapoff /swapfile + print_info "Swap deactivated" + fi + + # Remove swap file + if [ -f /swapfile ]; then + rm -f /swapfile + print_success "Temporary swap file removed" + fi + + # Remove fstab entry if exists + if grep -q '/swapfile' /etc/fstab 2>/dev/null; then + sed -i '/\/swapfile/d' /etc/fstab + print_info "Removed swap entry from fstab" + fi + + # Remove marker file + rm -f /tmp/.swap_created_by_deploy + else + print_info "No temporary swap to remove (swap was pre-existing or already cleaned)" + fi +} + # ============================================================================= # EXPORT ALL VARIABLES # ============================================================================= @@ -224,7 +300,7 @@ restart_nginx() { # Make sure all variables are exported export -f print_status print_warning print_error print_success print_info export -f check_project_root check_build_directory build_flutter_app -export -f create_backup set_permissions test_nginx restart_nginx +export -f create_backup set_permissions test_nginx restart_nginx setup_swap remove_swap print_info "Configuration loaded successfully" print_info "Server: $SERVER_USER@$SERVER_IP" diff --git a/tools/deploy/web-app/deploy.sh b/tools/deploy/web-app/deploy.sh index e23cc07..026126a 100755 --- a/tools/deploy/web-app/deploy.sh +++ b/tools/deploy/web-app/deploy.sh @@ -30,6 +30,23 @@ print_status "Deploying on server..." ssh "$SERVER_USER@$SERVER_IP" << EOF set -e + # Setup swap if needed (helps with memory-intensive operations) + $(declare -f setup_swap) + $(declare -f remove_swap) + $(declare -f print_status) + $(declare -f print_warning) + $(declare -f print_success) + $(declare -f print_info) + RED='$RED' + GREEN='$GREEN' + YELLOW='$YELLOW' + BLUE='$BLUE' + NC='$NC' + setup_swap + + # Trap to ensure swap cleanup on exit + trap 'remove_swap' EXIT + # Create web directory if it doesn't exist mkdir -p $WEB_ROOT @@ -98,6 +115,9 @@ ssh "$SERVER_USER@$SERVER_IP" << EOF ufw allow $FIREWALL_ALLOW_SSH ufw --force enable + # Clean up temporary swap + remove_swap + echo "Deployment completed successfully!" echo "Application is available at: https://memo-cards.online" EOF