#!/bin/bash # MongoDB Migration Web Interface # Serves migration progress at ROOT_URL/migration-progress using Node.js # Source settings source $SNAP/bin/wekan-read-settings # Set up Node.js environment like wekan-control export NODE_PATH=$SNAP/bin # Configuration MIGRATION_STATUS="${SNAP_COMMON}/mongodb-migration-status.json" MIGRATION_LOG="${SNAP_COMMON}/mongodb-migration-log.txt" MIGRATION_PROGRESS="${SNAP_COMMON}/mongodb-migration-progress.html" # Use same PORT as wekan-control, but add 1 to avoid conflicts MIGRATION_PORT=$((PORT + 1)) # Create Node.js HTTP server script create_node_server() { cat > "${SNAP_COMMON}/migration-web-server.js" << 'EOF' const http = require('http'); const fs = require('fs'); const path = require('path'); const PORT = process.env.MIGRATION_PORT || 8081; const SNAP_COMMON = process.env.SNAP_COMMON; const ROOT_URL = process.env.ROOT_URL || 'http://127.0.0.1'; const MIGRATION_STATUS = path.join(SNAP_COMMON, 'mongodb-migration-status.json'); const MIGRATION_LOG = path.join(SNAP_COMMON, 'mongodb-migration-log.txt'); function readFileSafe(filePath) { try { return fs.readFileSync(filePath, 'utf8'); } catch (error) { return null; } } function getMigrationStatus() { const statusContent = readFileSafe(MIGRATION_STATUS); if (!statusContent) { return null; } try { return JSON.parse(statusContent); } catch (error) { return null; } } function getMigrationLog() { const logContent = readFileSafe(MIGRATION_LOG); if (!logContent) { return 'No log available'; } const lines = logContent.split('\n'); return lines.slice(-20).join('\n'); } function generateHTML(status) { if (!status) { return ` MongoDB Migration Progress

MongoDB Migration

No migration in progress.

This page will refresh automatically every 5 seconds.

`; } const { status: statusValue, step, total_steps, percentage, description, timestamp } = status; const logContent = getMigrationLog(); return ` MongoDB Migration Progress

MongoDB Migration Progress

Migrating from MongoDB 3 to MongoDB 7

${percentage}%

Status: ${statusValue}

Progress: ${step} of ${total_steps} steps

Current Step: ${description}

Last Updated: ${timestamp}

Migration Log (Last 20 lines):

${logContent}

This page will refresh automatically every 5 seconds.
Migration URL: ${ROOT_URL}/migration-progress

`; } const server = http.createServer((req, res) => { if (req.url === '/migration-progress' || req.url === '/') { const status = getMigrationStatus(); const html = generateHTML(status); res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-cache', 'Connection': 'close' }); res.end(html); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); } }); server.listen(PORT, () => { console.log(`MongoDB Migration Web Server running on port ${PORT}`); }); // Handle graceful shutdown process.on('SIGTERM', () => { console.log('Received SIGTERM, shutting down gracefully'); server.close(() => { process.exit(0); }); }); process.on('SIGINT', () => { console.log('Received SIGINT, shutting down gracefully'); server.close(() => { process.exit(0); }); }); EOF } # Start the Node.js web server start_node_server() { echo "Starting MongoDB migration web server using Node.js..." echo "Migration server will be available at: ${ROOT_URL}/migration-progress" echo "Migration server port: ${MIGRATION_PORT}" # Create the Node.js server script create_node_server # Export environment variables for the Node.js process export MIGRATION_PORT export ROOT_URL # Start the server using Node.js from SNAP/bin $NODE_PATH/node "${SNAP_COMMON}/migration-web-server.js" } # Start the web server start_node_server