Skip to content

Commit dafb57d

Browse files
committed
Feature: Added script to automatically execute all commands.
1 parent 5120e06 commit dafb57d

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status.
4+
set -e
5+
6+
# --- Initial Setup & User Input ---
7+
echo "This script will clean unpinned IPFS objects and reclaim disk space."
8+
echo "--------------------------------------------------------------------"
9+
read -p "Enter your DATABASE_USERNAME (default: aleph): " DB_USER
10+
DB_USER=${DB_USER:-aleph}
11+
12+
read -s -p "Enter your DATABASE_PASSWORD: " DB_PASS
13+
echo
14+
if [ -z "$DB_PASS" ]; then
15+
echo "❌ Error: Database password cannot be empty."
16+
exit 1
17+
fi
18+
19+
# --- 1. Measure Initial Space Usage ---
20+
echo -e "\n📊 Checking initial disk space usage..."
21+
IPFS_CONTAINER=$(docker ps -a --format "{{.Names}}" | grep ipfs | head -n 1)
22+
if [ -z "$IPFS_CONTAINER" ]; then
23+
echo "❌ Error: Could not find the IPFS container."
24+
exit 1
25+
fi
26+
27+
# Find the volume name mounted to the IPFS container
28+
IPFS_VOLUME=$(docker inspect -f '{{range .Mounts}}{{if eq .Destination "/data/ipfs"}}{{.Name}}{{end}}{{end}}' "$IPFS_CONTAINER")
29+
if [ -z "$IPFS_VOLUME" ]; then
30+
echo "❌ Error: Could not find the IPFS data volume for container '$IPFS_CONTAINER'."
31+
exit 1
32+
fi
33+
34+
# Use 'docker system df -v' to find the volume's reported size
35+
# We grep for the volume name and get the last column containing the size
36+
INITIAL_SIZE_HR=$(docker system df -v | grep -A 9999 "VOLUME" | grep -w "$IPFS_VOLUME" | awk '{print $NF}')
37+
38+
if [ -z "$INITIAL_SIZE_HR" ]; then
39+
echo " - ⚠️ Warning: Could not determine initial size from 'docker system df'."
40+
INITIAL_SIZE_HR="N/A"
41+
fi
42+
echo " - IPFS Volume: $IPFS_VOLUME"
43+
echo " - Initial Size (from docker df): $INITIAL_SIZE_HR"
44+
45+
# --- 2. Download Files ---
46+
echo -e "\n⬇️ Downloading Dockerfile and cleaner script..."
47+
wget -q --show-progress -O cleaner.dockerfile "https://raw.githubusercontent.com/aleph-im/pyaleph/refs/heads/andres-feature-implement_experimental_ipfs_pin_cleaner/deployment/docker-build/cleaner.dockerfile"
48+
wget -q --show-progress -O ipfs_pin_cleaner.py "https://raw.githubusercontent.com/aleph-im/pyaleph/refs/heads/andres-feature-implement_experimental_ipfs_pin_cleaner/deployment/scripts/ipfs_pin_cleaner.py"
49+
50+
# --- 3. Build Docker Image ---
51+
echo -e "\n🛠️ Building 'ipfs-pin-cleaner' Docker image..."
52+
docker build -f cleaner.dockerfile -t ipfs-pin-cleaner . > /dev/null
53+
echo " - Image built successfully."
54+
55+
# --- 4. Stop Containers ---
56+
echo -e "\n🛑 Stopping non-essential containers..."
57+
docker-compose stop pyaleph pyaleph-api p2p-service rabbitmq redis
58+
59+
# --- 5. Get Network and IPFS Info ---
60+
echo -e "\n🔎 Identifying network and IPFS container details..."
61+
PYALEPH_NETWORK=$(docker network list --format "{{.Name}}" | grep pyaleph | head -n 1)
62+
IPFS_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$IPFS_CONTAINER")
63+
echo " - Network: $PYALEPH_NETWORK, IPFS IP: $IPFS_IP"
64+
65+
# --- 6. Run IPFS Pin Cleaner ---
66+
echo -e "\n🧹 Running the IPFS pin cleaner (this may take a while)..."
67+
docker run --rm --network "$PYALEPH_NETWORK" \
68+
-e DATABASE_DSN="postgres://${DB_USER}:${DB_PASS}@postgres:5432/aleph" \
69+
-e IPFS_API="/ip4/${IPFS_IP}/tcp/5001" \
70+
ipfs-pin-cleaner --unpin
71+
72+
# --- 7. Execute IPFS Garbage Collector ---
73+
echo -e "\n🗑️ Executing IPFS garbage collector..."
74+
docker exec -it "$IPFS_CONTAINER" ipfs repo gc
75+
76+
# --- 8. Measure Final Space ---
77+
echo -e "\n📊 Checking final disk space usage..."
78+
# A small sleep can give Docker's daemon time to update its disk usage stats
79+
sleep 5
80+
FINAL_SIZE_HR=$(docker system df -v | grep -A 9999 "VOLUME" | grep -w "$IPFS_VOLUME" | awk '{print $NF}')
81+
82+
if [ -z "$FINAL_SIZE_HR" ]; then
83+
echo " - ⚠️ Warning: Could not determine final size from 'docker system df'."
84+
FINAL_SIZE_HR="N/A"
85+
fi
86+
echo " - Final Size (from docker df): $FINAL_SIZE_HR"
87+
88+
# --- 9. Restart All Containers ---
89+
echo -e "\n🚀 Starting all services..."
90+
docker-compose up -d
91+
92+
# --- 10. Cleanup ---
93+
echo -e "\n✨ Cleaning up temporary files..."
94+
rm cleaner.dockerfile ipfs_pin_cleaner.py
95+
96+
# --- Final Summary ---
97+
echo -e "\n------------------- SUMMARY -------------------"
98+
echo -e "Initial size reported: \033[1;31m$INITIAL_SIZE_HR\033[0m"
99+
echo -e "Final size reported: \033[1;32m$FINAL_SIZE_HR\033[0m"
100+
echo -e "\nℹ️ Compare the values above to see the reclaimed space."
101+
echo "-----------------------------------------------"
102+
echo "✅ All tasks finished successfully!"

0 commit comments

Comments
 (0)