/ .forgejo / scripts / system-cleanup.sh
system-cleanup.sh
 1  #!/bin/bash
 2  # System-wide CI cleanup script
 3  # Run this periodically via cron on the CI server to clean up accumulated artifacts
 4  # Suggested cron: 0 2 * * * (daily at 2am)
 5  
 6  set -e
 7  
 8  echo "=== System-wide CI Cleanup $(date) ==="
 9  
10  WORKING_REPOS="/home/devops/working-repos"
11  MIN_FREE_GB=50  # Alert if less than this many GB free
12  
13  # Function to get current free space in GB
14  get_free_space_gb() {
15      df / | tail -1 | awk '{print int($4/1024/1024)}'
16  }
17  
18  # Initial disk usage
19  echo "Initial disk usage:"
20  df -h / | tail -1
21  
22  FREE_SPACE=$(get_free_space_gb)
23  echo "Free space: ${FREE_SPACE}GB"
24  
25  # Clean up Rust target directories older than 7 days
26  echo ""
27  echo "Cleaning Rust target directories (older than 7 days)..."
28  find "$WORKING_REPOS" -type d -name target -mtime +7 -exec bash -c '
29      for dir; do
30          size=$(du -sh "$dir" 2>/dev/null | cut -f1)
31          echo "Removing: $dir ($size)"
32          rm -rf "$dir"
33      done
34  ' bash {} +
35  
36  # Clean up node_modules older than 7 days
37  echo ""
38  echo "Cleaning node_modules (older than 7 days)..."
39  find "$WORKING_REPOS" -type d -name node_modules -mtime +7 -exec bash -c '
40      for dir; do
41          size=$(du -sh "$dir" 2>/dev/null | cut -f1)
42          echo "Removing: $dir ($size)"
43          rm -rf "$dir"
44      done
45  ' bash {} +
46  
47  # Clean up rustc temp files in /tmp
48  echo ""
49  echo "Cleaning rustc temp files in /tmp..."
50  find /tmp -maxdepth 1 -name "rustc*" -type d -mtime +0 -exec rm -rf {} + 2>/dev/null || true
51  
52  # Clean up old sync directories
53  echo ""
54  echo "Cleaning old sync directories in /tmp..."
55  find /tmp -maxdepth 1 -type d \( -name "*-sync" -o -name "*-rad-*" \) -mtime +1 -exec rm -rf {} + 2>/dev/null || true
56  
57  # Clean up Docker if installed
58  if command -v docker &> /dev/null; then
59      echo ""
60      echo "Cleaning Docker artifacts..."
61      docker system prune -af --volumes 2>/dev/null || echo "Docker cleanup failed"
62  fi
63  
64  # Clean up Python caches
65  echo ""
66  echo "Cleaning Python caches..."
67  find "$WORKING_REPOS" -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
68  find "$WORKING_REPOS" -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
69  
70  # Final disk usage
71  echo ""
72  echo "=== Cleanup Complete ==="
73  echo "Final disk usage:"
74  df -h / | tail -1
75  
76  NEW_FREE_SPACE=$(get_free_space_gb)
77  FREED=$((NEW_FREE_SPACE - FREE_SPACE))
78  echo "Freed: ${FREED}GB (${FREE_SPACE}GB → ${NEW_FREE_SPACE}GB)"
79  
80  # Alert if disk space is low
81  if [ "$NEW_FREE_SPACE" -lt "$MIN_FREE_GB" ]; then
82      echo "⚠️  WARNING: Low disk space! Only ${NEW_FREE_SPACE}GB free (threshold: ${MIN_FREE_GB}GB)"
83      exit 1
84  fi
85  
86  echo "✓ Cleanup successful"