File size: 10,286 Bytes
fd357f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/bash

# DTO ARCHIVE PROTOCOL: Upload-then-Delete Workflow
# Automated process to upload assets to HF/Xet, verify, then safely delete local copies
# Prometheus - Head of Data Migration & Transfer Operations

set -euo pipefail

# Load configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DTO_ROOT="$(dirname "$SCRIPT_DIR")"
ENV_FILE="$DTO_ROOT/.env"

if [ -f "$ENV_FILE" ]; then
    # Source the .env file to load all variables
    set -a
    source "$ENV_FILE"
    set +a
    echo "βœ… Loaded .env file from $ENV_FILE"
else
    echo "❌ .env file not found at $ENV_FILE"
    exit 1
fi

# Configuration
PROTOCOL_LOG="/tmp/archive_protocol_$(date +%Y%m%d_%H%M%S).log"
BACKUP_DIR="/tmp/archive_backup_$(date +%Y%m%d_%H%M%S)"
UPLOAD_MANIFEST="$BACKUP_DIR/upload_manifest.txt"
DELETE_MANIFEST="$BACKUP_DIR/delete_manifest.txt"

# Target repositories
REPO_MODELS="$HF_REPO_MODELS"
REPO_DATASETS="$HF_REPO_DATASETS"
REPO_ARTIFACTS="$HF_REPO_ARTIFACTS"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging functions
log() {
    echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$PROTOCOL_LOG"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" | tee -a "$PROTOCOL_LOG" >&2
}

warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$PROTOCOL_LOG"
}

info() {
    echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$PROTOCOL_LOG"
}

# Phase 1: Discovery & Inventory
discovery_phase() {
    log "=== PHASE 1: DISCOVERY & INVENTORY ==="
    mkdir -p "$BACKUP_DIR"
    
    # Discover space optimization opportunities
    log "Discovering space optimization opportunities..."
    
    # 1. Experiments directory
    log "Inventory: /data/experiments/"
    find /data/experiments -type f \( -name "*.safetensors" -o -name "*.pt" -o -name "*.bin" \) > "$BACKUP_DIR/experiments_files.txt"
    find /data/experiments -type f \( -name "*.parquet" -o -name "*.jsonl" -o -name "*.csv" \) >> "$BACKUP_DIR/experiments_files.txt"
    
    # 2. Data directory components (exclude cache and problematic directories)
    log "Inventory: /data/data/ workspace (excluding cache)"
    find /data/data/workspace -type f -size +100M ! -path "*/.cache/*" ! -path "*/.local/*" 2>/dev/null > "$BACKUP_DIR/data_files.txt" || true
    
    # Create combined manifest
    cat "$BACKUP_DIR/experiments_files.txt" "$BACKUP_DIR/data_files.txt" | sort -u > "$UPLOAD_MANIFEST"
    
    local total_files=$(wc -l < "$UPLOAD_MANIFEST")
    local total_size=$(while read -r f; do du -b "$f" 2>/dev/null | cut -f1; done < "$UPLOAD_MANIFEST" | awk '{sum += $1} END {print sum}')
    
    log "Discovered $total_files files for potential archiving"
    log "Total size: $(numfmt --to=iec $total_size)"
    
    # Show top 5 largest files
    log "Top 5 largest files:"
    while read -r f; do du -h "$f" 2>/dev/null | cut -f1 | xargs -I {} echo "   {} - $f"; done < "$UPLOAD_MANIFEST" | sort -rh | head -5 | tee -a "$PROTOCOL_LOG"
}

# Phase 2: Upload to HF/Xet
upload_phase() {
    log "=== PHASE 2: UPLOAD TO HF/XET ==="
    
    local uploaded_count=0
    local skipped_count=0
    local failed_count=0
    
    # Create upload tracking file
    echo "# Archive Protocol Upload Log - $(date)" > "$BACKUP_DIR/upload_results.txt"
    
    while read -r file_path; do
        if [ ! -f "$file_path" ]; then
            warning "File not found: $file_path"
            skipped_count=$((skipped_count + 1))
            continue
        fi
        
        # Determine repository based on file type
        local repo="$REPO_ARTIFACTS"
        local filename=$(basename "$file_path")
        
        if [[ "$filename" == *.safetensors ]] || [[ "$filename" == *.pt ]] || [[ "$filename" == *.bin ]]; then
            repo="$REPO_MODELS"
        elif [[ "$filename" == *.parquet ]] || [[ "$filename" == *.jsonl ]] || [[ "$filename" == *.csv ]]; then
            repo="$REPO_DATASETS"
        fi
        
        # Create relative path for repository
        local relative_path=""
        if [[ "$file_path" == /data/experiments/* ]]; then
            relative_path="experiments/$(echo "$file_path" | sed 's|/data/experiments/||')"
        elif [[ "$file_path" == /data/data/* ]]; then
            relative_path="data/$(echo "$file_path" | sed 's|/data/data/||')"
        else
            relative_path="other/$(basename "$file_path")"
        fi
        
        log "Uploading: $relative_path β†’ $repo"
        
        # Debug: Check if environment variables are set
        if [ -z "$HF_TOKEN" ]; then
            error "HF_TOKEN is not set"
            exit 1
        fi
        
        # Upload using our client with token passed directly
        if python3 -c "
import os
from integrations.huggingface_client import HuggingFaceClient

# Use token directly instead of environment variable
client = HuggingFaceClient(token='$HF_TOKEN')
if not client.is_authenticated():
    print('NOT_AUTHENTICATED')
    exit(1)

success = client.upload_artifact('$file_path', '$relative_path', '$repo')
if success:
    print('SUCCESS')
else:
    print('FAILED')
    exit(1)
" 2>> "$PROTOCOL_LOG"; then
            echo "βœ… SUCCESS: $file_path β†’ $repo/$relative_path" | tee -a "$BACKUP_DIR/upload_results.txt"
            echo "$file_path" >> "$DELETE_MANIFEST"
            uploaded_count=$((uploaded_count + 1))
        else
            error "FAILED: $file_path"
            echo "❌ FAILED: $file_path" >> "$BACKUP_DIR/upload_results.txt"
            failed_count=$((failed_count + 1))
        fi
        
    done < "$UPLOAD_MANIFEST"
    
    log "Upload phase completed:"
    log "   βœ… Uploaded: $uploaded_count files"
    log "   ⚠️  Skipped: $skipped_count files"
    log "   ❌ Failed: $failed_count files"
    
    if [ $uploaded_count -eq 0 ]; then
        error "No files were uploaded successfully"
        exit 1
    fi
}

# Phase 3: Verification
verification_phase() {
    log "=== PHASE 3: VERIFICATION ==="
    
    log "Verifying uploads on HF repositories..."
    
    python3 -c "
import os
from huggingface_hub import HfApi

api = HfApi(token='$HF_TOKEN')

repos = ['$REPO_MODELS', '$REPO_DATASETS', '$REPO_ARTIFACTS']

for repo in repos:
    try:
        info = api.repo_info(repo)
        print(f'βœ… {repo}: {len(info.siblings)} files, {info.size} bytes')
        
        # List files if repository has content
        if info.siblings:
            for file in info.siblings[:5]:  # Show first 5 files
                print(f'   - {file.rfilename} ({file.size} bytes)')
        
    except Exception as e:
        print(f'❌ {repo}: {e}')
" | tee -a "$PROTOCOL_LOG"
    
    log "Verification completed"
}

# Phase 4: Safe Deletion (Optional)
deletion_phase() {
    log "=== PHASE 4: SAFE DELETION (OPTIONAL) ==="
    
    if [ ! -f "$DELETE_MANIFEST" ] || [ ! -s "$DELETE_MANIFEST" ]; then
        warning "No files marked for deletion - skipping deletion phase"
        return
    fi
    
    local delete_count=$(wc -l < "$DELETE_MANIFEST")
    local total_size=0
    
    log "Found $delete_count files ready for safe deletion"
    
    # Calculate total space to be freed
    while read -r file; do
        if [ -f "$file" ]; then
            size=$(du -b "$file" 2>/dev/null | cut -f1 || echo 0)
            total_size=$((total_size + size))
        fi
    done < "$DELETE_MANIFEST"
    
    log "Total space to free: $(numfmt --to=iec $total_size)"
    
    # Confirm deletion
    read -p "Do you want to proceed with deletion? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        log "Deletion cancelled by user"
        return
    fi
    
    # Perform safe deletion
    local deleted_count=0
    local failed_count=0
    
    while read -r file_path; do
        if [ -f "$file_path" ]; then
            if rm -v "$file_path" 2>/dev/null; then
                echo "βœ… DELETED: $file_path" | tee -a "$PROTOCOL_LOG"
                deleted_count=$((deleted_count + 1))
            else
                error "FAILED to delete: $file_path"
                failed_count=$((failed_count + 1))
            fi
        else
            warning "File already gone: $file_path"
        fi
    done < "$DELETE_MANIFEST"
    
    log "Deletion completed:"
    log "   βœ… Deleted: $deleted_count files"
    log "   ❌ Failed: $failed_count files"
    
    # Show disk space after deletion
    log "Disk space after deletion:"
    df -h /data | tee -a "$PROTOCOL_LOG"
}

# Phase 5: Cleanup & Report
cleanup_phase() {
    log "=== PHASE 5: CLEANUP & REPORT ==="
    
    # Create summary report
    local total_uploaded=$(grep -c "βœ… SUCCESS" "$BACKUP_DIR/upload_results.txt" 2>/dev/null || echo 0)
    local total_failed=$(grep -c "❌ FAILED" "$BACKUP_DIR/upload_results.txt" 2>/dev/null || echo 0)
    
    cat > "$BACKUP_DIR/summary_report.md" << EOF
# DTO Archive Protocol Summary

## Execution Details
- **Timestamp**: $(date)
- **Protocol Log**: $PROTOCOL_LOG
- **Backup Directory**: $BACKUP_DIR

## Results
- **Files Discovered**: $(wc -l < "$UPLOAD_MANIFEST" 2>/dev/null || echo 0)
- **Successfully Uploaded**: $total_uploaded
- **Upload Failures**: $total_failed
- **Files Deleted**: $(wc -l < "$DELETE_MANIFEST" 2>/dev/null || echo 0)

## Repository Status
- **Models Repository**: $REPO_MODELS
- **Datasets Repository**: $REPO_DATASETS
- **Artifacts Repository**: $REPO_ARTIFACTS

## Next Steps
1. Verify uploads on Hugging Face Hub
2. Monitor repository storage usage
3. Run regular archive protocols for new data
4. Review failed uploads for manual intervention

EOF
    
    log "Summary report created: $BACKUP_DIR/summary_report.md"
    log "Protocol completed successfully!"
}

# Main execution
main() {
    log "πŸš€ STARTING DTO ARCHIVE PROTOCOL"
    log "Timestamp: $(date)"
    log "Protocol log: $PROTOCOL_LOG"
    log "Backup directory: $BACKUP_DIR"
    
    # Execute phases
    discovery_phase
    upload_phase
    verification_phase
    
    # Only proceed to deletion if explicitly requested
    if [ "${1:-}" = "--delete" ]; then
        deletion_phase
    else
        log "Skipping deletion phase (use --delete to enable)"
    fi
    
    cleanup_phase
    
    log "βœ… ARCHIVE PROTOCOL COMPLETED SUCCESSFULLY"
}

# Run main function
main "$@"