mongodb-migration-web 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. # MongoDB Migration Web Interface
  3. # Serves migration progress at ROOT_URL/migration-progress
  4. # Source settings
  5. source $SNAP/bin/wekan-read-settings
  6. # Configuration
  7. MIGRATION_STATUS="${SNAP_COMMON}/mongodb-migration-status.json"
  8. MIGRATION_LOG="${SNAP_COMMON}/mongodb-migration-log.txt"
  9. MIGRATION_PROGRESS="${SNAP_COMMON}/mongodb-migration-progress.html"
  10. PORT="${MIGRATION_WEB_PORT:-8081}"
  11. # Create a simple HTTP server using netcat and bash
  12. serve_migration_progress() {
  13. while true; do
  14. {
  15. echo "HTTP/1.1 200 OK"
  16. echo "Content-Type: text/html; charset=utf-8"
  17. echo "Cache-Control: no-cache"
  18. echo "Connection: close"
  19. echo ""
  20. # Generate HTML page
  21. if [ -f "$MIGRATION_STATUS" ]; then
  22. local status=$(jq -r '.status' "$MIGRATION_STATUS" 2>/dev/null || echo "unknown")
  23. local step=$(jq -r '.step' "$MIGRATION_STATUS" 2>/dev/null || echo "0")
  24. local total_steps=$(jq -r '.total_steps' "$MIGRATION_STATUS" 2>/dev/null || echo "0")
  25. local percentage=$(jq -r '.percentage' "$MIGRATION_STATUS" 2>/dev/null || echo "0")
  26. local description=$(jq -r '.description' "$MIGRATION_STATUS" 2>/dev/null || echo "Unknown")
  27. local timestamp=$(jq -r '.timestamp' "$MIGRATION_STATUS" 2>/dev/null || echo "Unknown")
  28. cat << EOF
  29. <!DOCTYPE html>
  30. <html>
  31. <head>
  32. <title>MongoDB Migration Progress</title>
  33. <meta http-equiv="refresh" content="5">
  34. <style>
  35. body {
  36. font-family: Arial, sans-serif;
  37. margin: 40px;
  38. background-color: #f5f5f5;
  39. }
  40. .container {
  41. max-width: 800px;
  42. margin: 0 auto;
  43. background: white;
  44. padding: 30px;
  45. border-radius: 10px;
  46. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  47. }
  48. .progress-bar {
  49. width: 100%;
  50. background-color: #e0e0e0;
  51. border-radius: 10px;
  52. overflow: hidden;
  53. margin: 20px 0;
  54. }
  55. .progress-fill {
  56. height: 40px;
  57. background: linear-gradient(90deg, #4CAF50, #45a049);
  58. border-radius: 10px;
  59. width: ${percentage}%;
  60. transition: width 0.3s ease;
  61. display: flex;
  62. align-items: center;
  63. justify-content: center;
  64. color: white;
  65. font-weight: bold;
  66. }
  67. .status {
  68. margin: 20px 0;
  69. padding: 20px;
  70. background-color: #f9f9f9;
  71. border-radius: 5px;
  72. }
  73. .error { color: #d32f2f; }
  74. .success { color: #388e3c; }
  75. .warning { color: #f57c00; }
  76. .info { color: #1976d2; }
  77. .log-container {
  78. margin-top: 30px;
  79. max-height: 300px;
  80. overflow-y: auto;
  81. background-color: #f5f5f5;
  82. padding: 15px;
  83. border-radius: 5px;
  84. font-family: monospace;
  85. font-size: 12px;
  86. }
  87. .header {
  88. text-align: center;
  89. margin-bottom: 30px;
  90. }
  91. .status-indicator {
  92. display: inline-block;
  93. width: 12px;
  94. height: 12px;
  95. border-radius: 50%;
  96. margin-right: 8px;
  97. }
  98. .status-running { background-color: #ff9800; }
  99. .status-completed { background-color: #4caf50; }
  100. .status-error { background-color: #f44336; }
  101. .status-unknown { background-color: #9e9e9e; }
  102. </style>
  103. </head>
  104. <body>
  105. <div class="container">
  106. <div class="header">
  107. <h1>MongoDB Migration Progress</h1>
  108. <p>Migrating from MongoDB 3 to MongoDB 7</p>
  109. </div>
  110. <div class="progress-bar">
  111. <div class="progress-fill">${percentage}%</div>
  112. </div>
  113. <div class="status">
  114. <p><span class="status-indicator status-${status}"></span><strong>Status:</strong> ${status}</p>
  115. <p><strong>Progress:</strong> $step of $total_steps steps</p>
  116. <p><strong>Current Step:</strong> $description</p>
  117. <p><strong>Last Updated:</strong> $timestamp</p>
  118. </div>
  119. <div class="log-container">
  120. <h3>Migration Log (Last 20 lines):</h3>
  121. <pre>$(tail -20 "$MIGRATION_LOG" 2>/dev/null || echo "No log available")</pre>
  122. </div>
  123. <p style="text-align: center; margin-top: 30px; color: #666;">
  124. <em>This page will refresh automatically every 5 seconds.</em><br>
  125. <em>Migration URL: ${ROOT_URL:-http://localhost:8080}/migration-progress</em>
  126. </p>
  127. </div>
  128. </body>
  129. </html>
  130. EOF
  131. else
  132. cat << EOF
  133. <!DOCTYPE html>
  134. <html>
  135. <head>
  136. <title>MongoDB Migration Progress</title>
  137. <meta http-equiv="refresh" content="5">
  138. <style>
  139. body { font-family: Arial, sans-serif; margin: 40px; }
  140. .container { max-width: 800px; margin: 0 auto; text-align: center; }
  141. </style>
  142. </head>
  143. <body>
  144. <div class="container">
  145. <h1>MongoDB Migration</h1>
  146. <p>No migration in progress.</p>
  147. <p><em>This page will refresh automatically every 5 seconds.</em></p>
  148. </div>
  149. </body>
  150. </html>
  151. EOF
  152. fi
  153. } | nc -l -p "$PORT" -q 1
  154. done
  155. }
  156. # Start the web server
  157. serve_migration_progress