Monorepo for Aesthetic.Computer
aesthetic.computer
1#!/usr/bin/env fish
2# Storage Management Helper for DigitalOcean Spaces
3# Provides utilities for managing blob storage
4
5set -e
6
7set SCRIPT_DIR (dirname (status --current-filename))
8set VAULT_DIR (cd "$SCRIPT_DIR/../../../../aesthetic-computer-vault" && pwd)
9
10# Load configuration
11if test -f "$VAULT_DIR/at/deploy.env"
12 source "$VAULT_DIR/at/deploy.env"
13else
14 echo "✗ Vault config not found"
15 exit 1
16end
17
18# Configure s3cmd
19function setup_s3cmd
20 echo "Setting up s3cmd configuration..."
21
22 printf "[default]
23access_key = %s
24secret_key = %s
25host_base = %s
26host_bucket = %%(bucket)s.%s
27use_https = True
28" "$SPACES_KEY" "$SPACES_SECRET" (echo $SPACES_ENDPOINT | sed 's|https://||') (echo $SPACES_ENDPOINT | sed 's|https://||') > ~/.s3cfg
29
30 chmod 600 ~/.s3cfg
31 echo "✓ s3cmd configured"
32end
33
34# Check storage usage
35function check_usage
36 echo "╔════════════════════════════════════════╗"
37 echo "║ Storage Usage Report ║"
38 echo "╚════════════════════════════════════════╝"
39 echo ""
40
41 # Check if bucket exists
42 if not s3cmd ls | grep -q "$SPACES_BUCKET"
43 echo "Bucket '$SPACES_BUCKET' not found"
44 return 1
45 end
46
47 # Get size
48 set SIZE (s3cmd du s3://$SPACES_BUCKET | awk '{print $1}')
49 set SIZE_GB (math "$SIZE / 1024 / 1024 / 1024")
50
51 # Get object count
52 set COUNT (s3cmd ls -r s3://$SPACES_BUCKET | wc -l)
53
54 # Calculate cost
55 if test $SIZE_GB -gt 250
56 set COST (math "($SIZE_GB - 250) * 0.02 + 5")
57 else
58 set COST 5
59 end
60
61 echo "Bucket: $SPACES_BUCKET"
62 echo "Region: $SPACES_REGION"
63 echo ""
64 echo "Storage Used: "(printf "%.2f" $SIZE_GB)" GB"
65 echo "Objects: $COUNT"
66 echo "Estimated Cost: \$"(printf "%.2f" $COST)"/month"
67 echo ""
68
69 # Show breakdown
70 echo "Top directories by size:"
71 s3cmd du -r s3://$SPACES_BUCKET | sort -n -r | head -10
72end
73
74# List recent uploads
75function list_recent
76 set LIMIT 20
77 if test -n "$argv[1]"
78 set LIMIT $argv[1]
79 end
80
81 echo "Recent uploads (last $LIMIT):"
82 echo ""
83
84 s3cmd ls -r s3://$SPACES_BUCKET | tail -$LIMIT
85end
86
87# Test upload/download
88function test_storage
89 echo "Testing storage connectivity..."
90 echo ""
91
92 # Create test file
93 echo "test data $(date)" > /tmp/pds-storage-test.txt
94
95 # Upload
96 echo -n "Upload test... "
97 if s3cmd put /tmp/pds-storage-test.txt s3://$SPACES_BUCKET/test/ >/dev/null 2>&1
98 echo "✓"
99 else
100 echo "✗ FAILED"
101 return 1
102 end
103
104 # Download
105 echo -n "Download test... "
106 if s3cmd get s3://$SPACES_BUCKET/test/pds-storage-test.txt /tmp/pds-storage-test-dl.txt >/dev/null 2>&1
107 echo "✓"
108 else
109 echo "✗ FAILED"
110 return 1
111 end
112
113 # Verify
114 echo -n "Verify test... "
115 if diff /tmp/pds-storage-test.txt /tmp/pds-storage-test-dl.txt >/dev/null 2>&1
116 echo "✓"
117 else
118 echo "✗ FAILED"
119 return 1
120 end
121
122 # Cleanup
123 s3cmd del s3://$SPACES_BUCKET/test/pds-storage-test.txt >/dev/null 2>&1
124 rm /tmp/pds-storage-test*.txt
125
126 echo ""
127 echo "✓ All storage tests passed"
128end
129
130# Backup blobs
131function backup_blobs
132 set BACKUP_BUCKET $BACKUP_BUCKET
133 set TIMESTAMP (date +%Y%m%d-%H%M%S)
134
135 if test -z "$BACKUP_BUCKET"
136 echo "✗ BACKUP_BUCKET not configured"
137 return 1
138 end
139
140 echo "Backing up blobs..."
141 echo " Source: s3://$SPACES_BUCKET"
142 echo " Destination: s3://$BACKUP_BUCKET/backups/$TIMESTAMP/"
143 echo ""
144
145 s3cmd sync s3://$SPACES_BUCKET/ s3://$BACKUP_BUCKET/backups/$TIMESTAMP/ --progress
146
147 echo ""
148 echo "✓ Backup complete"
149end
150
151# Clean old test files
152function clean_test_files
153 echo "Cleaning test files..."
154
155 s3cmd ls -r s3://$SPACES_BUCKET | grep -i test | awk '{print $4}' | while read file
156 echo "Delete: $file"
157 s3cmd del "$file"
158 end
159
160 echo "✓ Cleanup complete"
161end
162
163# Show CDN info
164function show_cdn_info
165 echo "╔════════════════════════════════════════╗"
166 echo "║ CDN Information ║"
167 echo "╚════════════════════════════════════════╝"
168 echo ""
169
170 set CDN_ENDPOINT (echo $SPACES_ENDPOINT | sed 's/digitaloceanspaces/cdn.digitaloceanspaces/')
171
172 echo "Origin: $SPACES_ENDPOINT"
173 echo "CDN: $CDN_ENDPOINT"
174 echo ""
175 echo "CDN URL format:"
176 echo " $CDN_ENDPOINT/$SPACES_BUCKET/path/to/file"
177 echo ""
178 echo "✓ CDN is included with Spaces at no extra cost"
179end
180
181# Main command dispatcher
182function main
183 if test (count $argv) -eq 0
184 echo "Usage: fish storage-manager.fish <command>"
185 echo ""
186 echo "Commands:"
187 echo " setup - Configure s3cmd"
188 echo " usage - Show storage usage and cost"
189 echo " recent [N] - List N recent uploads (default: 20)"
190 echo " test - Test storage connectivity"
191 echo " backup - Backup blobs to backup bucket"
192 echo " clean - Remove test files"
193 echo " cdn - Show CDN information"
194 echo ""
195 return 0
196 end
197
198 switch $argv[1]
199 case setup
200 setup_s3cmd
201 case usage
202 check_usage
203 case recent
204 list_recent $argv[2..-1]
205 case test
206 test_storage
207 case backup
208 backup_blobs
209 case clean
210 clean_test_files
211 case cdn
212 show_cdn_info
213 case '*'
214 echo "Unknown command: $argv[1]"
215 echo "Run without arguments to see usage"
216 return 1
217 end
218end
219
220# Run main
221main $argv