Spaces:
Sleeping
Sleeping
File size: 3,446 Bytes
085912f 31ab0c0 085912f 31ab0c0 085912f 31ab0c0 085912f eee4731 085912f 31ab0c0 085912f |
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 |
#!/bin/bash
set -e
echo "Starting VS Code with Hugging Face persistence..."
# Check if HF_TOKEN and HF_DATASET are set
if [ -z "$HF_TOKEN" ] || [ -z "$HF_DATASET" ]; then
echo "Warning: HF_TOKEN or HF_DATASET not set. Running without persistence."
exec code-server --bind-addr 0.0.0.0:7860 --auth password /home/coder/workspace
exit 0
fi
# Login to Hugging Face using Python
echo "Logging in to Hugging Face..."
python3 -c "from huggingface_hub import login; login(token='$HF_TOKEN', add_to_git_credential=True)"
# Directories to persist
CONFIG_DIR="/home/coder/.config/code-server"
LOCAL_DIR="/home/coder/.local/share/code-server"
WORKSPACE_DIR="/home/coder/workspace"
BACKUP_DIR="/tmp/hf_backup"
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Function to download from HF dataset
download_from_hf() {
echo "Downloading persisted data from Hugging Face dataset: $HF_DATASET"
# Try to download the backup archive using Python
python3 << 'PYTHON_SCRIPT'
import os
from huggingface_hub import hf_hub_download
import tarfile
try:
backup_dir = "/tmp/hf_backup"
os.makedirs(backup_dir, exist_ok=True)
# Download backup file
file_path = hf_hub_download(
repo_id=os.environ["HF_DATASET"],
filename="backup.tar.gz",
repo_type="dataset",
local_dir=backup_dir
)
print(f"Found existing backup at {file_path}, restoring...")
# Extract the backup
with tarfile.open(file_path, "r:gz") as tar:
tar.extractall("/home/coder")
print("Restoration complete!")
except Exception as e:
print(f"No existing backup found or restoration failed: {e}")
print("Starting fresh.")
PYTHON_SCRIPT
}
# Function to upload to HF dataset
upload_to_hf() {
echo "Saving data to Hugging Face dataset..."
# Create backup archive
cd /home/coder
tar -czf "$BACKUP_DIR/backup.tar.gz" \
--exclude='*.log' \
--exclude='*.sock' \
--exclude='CachedData' \
--exclude='Cache' \
--exclude='GPUCache' \
.config/code-server \
.local/share/code-server \
.continue \
workspace 2>/dev/null || true
# Upload to HF dataset using Python
python3 << 'PYTHON_SCRIPT'
import os
from huggingface_hub import HfApi
try:
api = HfApi()
backup_file = "/tmp/hf_backup/backup.tar.gz"
# Create repo if it doesn't exist and upload
api.create_repo(
repo_id=os.environ["HF_DATASET"],
repo_type="dataset",
private=True,
exist_ok=True
)
api.upload_file(
path_or_fileobj=backup_file,
path_in_repo="backup.tar.gz",
repo_id=os.environ["HF_DATASET"],
repo_type="dataset"
)
print("Upload successful!")
except Exception as e:
print(f"Upload failed: {e}")
print("Continuing anyway...")
PYTHON_SCRIPT
}
# Setup trap to save on exit
trap 'upload_to_hf' EXIT SIGTERM SIGINT
# Download existing data
download_from_hf
# Start background sync every 5 minutes
(
while true; do
sleep 300
upload_to_hf
done
) &
SYNC_PID=$!
echo "Starting code-server..."
# Start code-server
code-server --bind-addr 0.0.0.0:7860 --auth password /home/coder/workspace &
CODE_SERVER_PID=$!
# Wait for code-server to exit
wait $CODE_SERVER_PID
# Kill background sync
kill $SYNC_PID 2>/dev/null || true
# Final upload
upload_to_hf
echo "Shutdown complete."
|