/ scripts / diagnose-stack.sh
diagnose-stack.sh
  1  #!/bin/bash
  2  set -e
  3  
  4  # Colors for output
  5  RED='\033[0;31m'
  6  GREEN='\033[0;32m'
  7  YELLOW='\033[1;33m'
  8  NC='\033[0m' # No Color
  9  
 10  echo "================================="
 11  echo "🩺 FOLD STACK COMPREHENSIVE DIAGNOSTICS"
 12  echo "================================="
 13  echo "📅 Date: $(date)"
 14  echo ""
 15  
 16  # Helper function to print section headers
 17  print_section() {
 18      echo "---------------------------------"
 19      echo "📌 $1"
 20      echo "---------------------------------"
 21  }
 22  
 23  # Helper function to print success
 24  print_success() {
 25      echo -e "${GREEN}✅ $1${NC}"
 26  }
 27  
 28  # Helper function to print warning
 29  print_warning() {
 30      echo -e "${YELLOW}⚠️  $1${NC}"
 31  }
 32  
 33  # Helper function to print error
 34  print_error() {
 35      echo -e "${RED}❌ $1${NC}"
 36  }
 37  
 38  # 1. Check Current Directory
 39  print_section "Current Directory"
 40  echo "📁 Current Directory: $(pwd)"
 41  if [[ "$(pwd)" != *"/fieldwork/fold-stack" ]]; then
 42      print_error "You are not in the expected fold-stack directory. Please run this script from ~/fieldwork/fold-stack."
 43      exit 1
 44  fi
 45  print_success "Directory check passed."
 46  
 47  # 2. Check Docker Compose File
 48  print_section "Docker Compose File Check: docker-compose.dev.yml"
 49  if [ -f "docker-compose.dev.yml" ] && grep -q "^services:" docker-compose.dev.yml; then
 50      print_success "docker-compose.dev.yml exists and looks valid."
 51  else
 52      print_error "docker-compose.dev.yml is missing or invalid — check formatting."
 53      exit 1
 54  fi
 55  
 56  # 3. Check Environment Variables (.env.dev)
 57  print_section "Environment Variables (.env.dev)"
 58  if [ -f ".env.dev" ]; then
 59      cat .env.dev | grep -v '^#' || print_warning "No environment variables found in .env.dev."
 60  else
 61      print_warning ".env.dev not found."
 62  fi
 63  
 64  # 4. Check Containers Status
 65  print_section "Containers Status"
 66  docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" || print_error "Failed to list Docker containers. Is Docker running?"
 67  
 68  # 5. Check Port Bindings
 69  print_section "Port Bindings"
 70  docker compose -f docker-compose.dev.yml port ghost 2368 || print_error "Ghost not exposing port 2368"
 71  docker compose -f docker-compose.dev.yml port forgejo 3000 || print_error "Forgejo not exposing port 3000"
 72  docker compose -f docker-compose.dev.yml port trilium 8080 || print_error "Trilium not exposing port 8080"
 73  docker compose -f docker-compose.dev.yml port hedgedoc 3000 || print_error "HedgeDoc not exposing port 3000"
 74  docker compose -f docker-compose.dev.yml port mailhog 8025 || print_error "MailHog not exposing port 8025"
 75  docker compose -f docker-compose.dev.yml port nextcloud 80 || print_error "Nextcloud not exposing port 80"
 76  docker compose -f docker-compose.dev.yml port overleaf 80 || print_warning "Overleaf CE not exposing port 80 (run ./scripts/enable-overleaf.sh to start)"
 77  
 78  # 6. Check Logs for Each Service
 79  print_section "Forgejo Logs (last 50 lines)"
 80  docker logs forgejo_dev --tail=50 2>&1 || print_warning "Forgejo container not found."
 81  
 82  print_section "Ghost Logs (last 20 lines)"
 83  docker logs ghost_dev --tail=20 2>&1 || print_warning "Ghost container not found."
 84  
 85  print_section "Trilium Logs (last 20 lines)"
 86  docker logs trilium_dev --tail=20 2>&1 || print_warning "Trilium container not found."
 87  
 88  print_section "HedgeDoc Logs (last 20 lines)"
 89  docker logs hedgedoc_dev --tail=20 2>&1 || print_warning "HedgeDoc container not found."
 90  
 91  print_section "MailHog Logs (last 20 lines)"
 92  docker logs mailhog_dev --tail=20 2>&1 || print_warning "MailHog container not found."
 93  
 94  print_section "Nextcloud Logs (last 20 lines)"
 95  docker logs nextcloud_dev --tail=20 2>&1 || print_warning "Nextcloud container not found."
 96  
 97  print_section "Rclone Logs (last 20 lines)"
 98  docker logs rclone_dev --tail=20 2>&1 || print_warning "Rclone container not found."
 99  
100  print_section "Pandoc Logs (last 20 lines)"
101  docker logs pandoc_dev --tail=20 2>&1 || print_warning "Pandoc container not found."
102  
103  print_section "Typst Logs (last 20 lines)"
104  docker logs typst_dev --tail=20 2>&1 || print_warning "Typst container not found."
105  
106  print_section "Overleaf Logs (last 20 lines)"
107  docker logs overleaf_dev --tail=20 2>&1 || print_warning "Overleaf CE container not found (run ./scripts/enable-overleaf.sh to start)."
108  
109  print_section "Overleaf Mongo Logs (last 20 lines)"
110  docker logs overleaf_mongo_dev --tail=20 2>&1 || print_warning "Overleaf Mongo container not found (run ./scripts/enable-overleaf.sh to start)."
111  
112  print_section "Overleaf Redis Logs (last 20 lines)"
113  docker logs overleaf_redis_dev --tail=20 2>&1 || print_warning "Overleaf Redis container not found (run ./scripts/enable-overleaf.sh to start)."
114  
115  print_section "Git-Sync Logs (last 20 lines)"
116  docker logs git_sync_dev --tail=20 2>&1 || print_warning "Git-Sync container not found."
117  
118  # 7. Check Volume Permissions and Contents
119  print_section "Forgejo Volume Permissions"
120  ls -ld ./volumes/forgejo || print_error "Missing volumes/forgejo"
121  ls -la ./volumes/forgejo || print_warning "Forgejo volume contents not accessible"
122  
123  print_section "Trilium Volume Permissions"
124  ls -ld ./volumes/trilium || print_error "Missing volumes/trilium"
125  ls -la ./volumes/trilium || print_warning "Trilium volume contents not accessible"
126  
127  print_section "HedgeDoc Volume Permissions"
128  ls -ld ./volumes/hedgedoc/uploads || print_error "Missing volumes/hedgedoc/uploads"
129  ls -la ./volumes/hedgedoc/uploads || print_warning "HedgeDoc volume contents not accessible"
130  
131  print_section "Nextcloud Volume Permissions"
132  ls -ld ./volumes/nextcloud/html || print_error "Missing volumes/nextcloud/html"
133  ls -la ./volumes/nextcloud/html || print_warning "Nextcloud html volume contents not accessible"
134  ls -ld ./volumes/nextcloud/data || print_error "Missing volumes/nextcloud/data"
135  ls -la ./volumes/nextcloud/data || print_warning "Nextcloud data volume contents not accessible"
136  
137  print_section "Scrolls Volume Permissions (Pandoc/Typst/Overleaf)"
138  ls -ld ./volumes/scrolls || print_error "Missing volumes/scrolls"
139  ls -la ./volumes/scrolls || print_warning "Scrolls volume contents not accessible"
140  
141  print_section "Trilium Backup Volume Permissions"
142  ls -ld ./volumes/trilium-backup || print_warning "Missing volumes/trilium-backup (needed for Web3.storage sync)"
143  ls -la ./volumes/trilium-backup || print_warning "Trilium backup volume contents not accessible"
144  
145  print_section "Overleaf Volume Permissions"
146  ls -ld ./volumes/overleaf || print_warning "Missing volumes/overleaf (needed for Overleaf CE persistence)"
147  ls -la ./volumes/overleaf || print_warning "Overleaf volume contents not accessible"
148  
149  print_section "Git Repositories Volume Permissions"
150  ls -ld ./volumes/repos || print_error "Missing volumes/repos (needed for Git-Sync)"
151  ls -la ./volumes/repos || print_warning "Git repositories volume contents not accessible"
152  
153  print_section "Logs Volume Permissions"
154  ls -ld ./volumes/logs || print_error "Missing volumes/logs (needed for logging)"
155  ls -la ./volumes/logs || print_warning "Logs volume contents not accessible"
156  
157  # 8. Check Entrypoint Script for Forgejo
158  print_section "Forgejo Entrypoint Script Check (forgejo-entrypoint.sh)"
159  head -n 10 scripts/forgejo-entrypoint.sh 2>/dev/null || print_warning "Missing forgejo-entrypoint.sh script"
160  
161  # 9. Check Rclone Configuration
162  print_section "Rclone Configuration Check"
163  if [ -f "./config/rclone/rclone.conf" ]; then
164      print_success "Rclone config file found."
165      echo "Configured remotes:"
166      rclone listremotes --config ./config/rclone/rclone.conf || print_error "Failed to list Rclone remotes."
167  else
168      print_error "Rclone config file (./config/rclone/rclone.conf) not found."
169  fi
170  
171  # 10. Test Rclone Connectivity
172  print_section "Rclone Connectivity Test"
173  if [ -f "./config/rclone/rclone.conf" ]; then
174      echo "Testing Google Drive (gdrive)..."
175      rclone lsd gdrive: --config ./config/rclone/rclone.conf 2>&1 | grep -q "fold-stack" && print_success "Google Drive connectivity test passed." || print_error "Google Drive connectivity test failed."
176  
177      echo "Testing Internet Archive (ia)..."
178      rclone lsd ia: --config ./config/rclone/rclone.conf 2>&1 | grep -q "fold-stack-scrolls" && print_success "Internet Archive connectivity test passed." || print_error "Internet Archive connectivity test failed."
179  
180      echo "Testing Web3.storage (web3)..."
181      rclone lsd web3: --config ./config/rclone/rclone.conf 2>&1 | grep -q "fold-stack-trilium" && print_success "Web3.storage connectivity test passed." || print_error "Web3.storage connectivity test failed."
182  else
183      print_warning "Skipping Rclone connectivity test due to missing config file."
184  fi
185  
186  # 11. Test Rclone Sync by Adding a Test File
187  print_section "Rclone Sync Test"
188  TEST_FILE="./volumes/scrolls/diagnostic-test-$(date +%s).scroll"
189  echo "Test file for diagnostics" > "$TEST_FILE"
190  echo "Created test file: $TEST_FILE"
191  echo "Waiting for Rclone to detect and sync (up to 30 seconds)..."
192  sleep 30
193  docker logs rclone_dev --tail=10 2>&1 | grep -q "diagnostic-test" && print_success "Rclone sync test passed: Test file detected in logs." || print_warning "Rclone sync test failed: Test file not detected in logs."
194  
195  # 12. Check Disk Space
196  print_section "Disk Space Check"
197  df -h / || print_error "Failed to check disk space."
198  
199  # 13. Check Network Interfaces
200  print_section "Network Interfaces"
201  if command -v ifconfig >/dev/null; then
202      ifconfig -a || print_error "Failed to check network interfaces."
203  else
204      print_warning "ifconfig not found. Install net-tools to check network interfaces:"
205      echo "  sudo apt install net-tools"
206  fi
207  
208  # 14. Summary of Findings
209  print_section "Summary of Findings"
210  echo "Check the above output for any errors (❌) or warnings (⚠️)."
211  echo "Common issues and fixes:"
212  echo "- If a container is not running, restart the stack: ./scripts/down-dev.sh && ./scripts/up-dev.sh"
213  echo "- If Rclone connectivity fails, reconfigure remotes: rclone config"
214  echo "- If volumes are inaccessible, fix permissions: chmod -R 775 ./volumes && chown -R 1000:1000 ./volumes"
215  echo "- If ports are not exposed, check for conflicts: netstat -tuln | grep <port>"
216  
217  echo ""
218  echo "================================="
219  echo "✅ Diagnostics Completed"
220  echo "================================="
221  echo "If issues persist, share the output with support or run:"
222  echo "  docker compose logs -f [service]"