File size: 6,323 Bytes
fff13d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Blaxel Remote Sandbox Deployment Script
# This script configures and deploys a sandbox directly to Blaxel,
# triggering a remote cloud build to save local disk space.

set -e  # Exit on error

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

# Configuration
SANDBOX_NAME="manim-sandbox"
# Note: Port 8080 is reserved by Blaxel Sandbox API, we don't need to list it in blaxel.toml ports
# but we need to know the Sandbox is of type "sandbox"

# Helper functions
print_header() {
    echo -e "\n${BLUE}================================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}================================================${NC}\n"
}

print_success() {
    echo -e "${GREEN}βœ“ $1${NC}"
}

print_error() {
    echo -e "${RED}βœ— $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠ $1${NC}"
}

print_info() {
    echo -e "${BLUE}β„Ή $1${NC}"
}

check_prerequisites() {
    print_header "Checking Prerequisites"

    # Check Blaxel CLI
    if ! command -v bl &> /dev/null; then
        print_error "Blaxel CLI is not installed."
        echo -e "Install with: ${YELLOW}curl -fsSL https://raw.githubusercontent.com/blaxel-ai/toolkit/main/install.sh | sh${NC}"
        exit 1
    fi
    print_success "Blaxel CLI is installed"

    # Check if Dockerfile exists
    if [ ! -f "Dockerfile.sandbox" ]; then
        print_error "Dockerfile.sandbox not found in current directory"
        exit 1
    fi
    print_success "Dockerfile.sandbox found"

    # Check Blaxel authentication
    print_info "Checking Blaxel authentication..."
    if ! bl workspaces &> /dev/null; then
        print_warning "Not logged in to Blaxel"
        echo -e "Please login with: ${YELLOW}bl login${NC}"
        exit 1
    fi
    print_success "Authenticated with Blaxel"
}

create_config() {
    print_header "Generating Blaxel Configuration"

    # Blaxel needs a blaxel.toml to know how to build and deploy this as a sandbox
    # We create it dynamically to ensure it matches your requirements.
    
    if [ -f "blaxel.toml" ]; then
        print_warning "blaxel.toml already exists. Backing up to blaxel.toml.bak"
        mv blaxel.toml blaxel.toml.bak
    fi

    print_info "Creating blaxel.toml..."
    
    cat << EOF > blaxel.toml
name = "${SANDBOX_NAME}"
type = "sandbox"
description = "Custom Manim + FFmpeg Sandbox"

[runtime]
memory = 4096  # 4GB RAM should be sufficient for rendering

# Note: We do not explicitly list port 8080 here as it is injected by the sandbox-api
# If you run a custom server (like a flask app) on another port (e.g., 3000), add it here.
EOF

    print_success "blaxel.toml created"
    
    # Check if we need to rename Dockerfile.sandbox to Dockerfile
    # Blaxel deployment typically looks for standard "Dockerfile"
    if [ -f "Dockerfile.sandbox" ] && [ ! -f "Dockerfile" ]; then
        print_info "Linking Dockerfile.sandbox to Dockerfile for deployment..."
        cp Dockerfile.sandbox Dockerfile
    fi
}

deploy_to_blaxel() {
    print_header "Deploying to Blaxel (Remote Build)"

    print_info "Starting deployment..."
    print_info "This will upload your context and build the image on Blaxel's infrastructure."
    print_info "This may take a few minutes..."

    # We run bl deploy. 
    if bl deploy; then
        print_success "Deployment and Remote Build successful"
    else
        print_error "Deployment failed"
        print_info "If this failed due to Docker missing locally, verify if your Blaxel CLI version supports pure remote builds."
        print_info "Alternative: Push this code to GitHub and connect the repo in the Blaxel Console."
        exit 1
    fi

    sleep 3
}

get_image_id() {
    print_header "Retrieving Image ID"

    print_info "Fetching sandbox details..."

    # Retrieve the image ID using bl CLI
    # We look for the sandbox we just named in blaxel.toml
    IMAGE_ID=$(bl get sandboxes ${SANDBOX_NAME} -ojson 2>/dev/null | grep -o '"image": *"[^"]*"' | cut -d'"' -f4 | head -n 1)

    if [ -z "$IMAGE_ID" ]; then
         # Fallback method if json parsing fails
         IMAGE_ID=$(bl get sandboxes ${SANDBOX_NAME} 2>/dev/null | grep "${SANDBOX_NAME}" | awk '{print $2}')
    fi

    if [ -n "$IMAGE_ID" ]; then
        print_success "Image ID retrieved: $IMAGE_ID"
        echo ""
        echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${GREEN}Your custom sandbox image ID is:${NC}"
        echo -e "${YELLOW}$IMAGE_ID${NC}"
        echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo ""

        # Update .env file
        if [ -f ".env" ]; then
            if grep -q "MANIM_SANDBOX_IMAGE" .env; then
                sed -i.bak "s|^MANIM_SANDBOX_IMAGE=.*|MANIM_SANDBOX_IMAGE=$IMAGE_ID|" .env
                rm .env.bak 2>/dev/null || true
                print_success ".env file updated"
            else
                echo "" >> .env
                echo "# Blaxel Custom Sandbox Image" >> .env
                echo "MANIM_SANDBOX_IMAGE=$IMAGE_ID" >> .env
                print_success ".env file updated"
            fi
        else
            echo "MANIM_SANDBOX_IMAGE=$IMAGE_ID" > .env
            print_success "Created .env file"
        fi
    else
        print_warning "Could not verify image ID automatically."
        echo "Run: bl get sandboxes"
    fi
}

main() {
    echo -e "${BLUE}"
    cat << "EOF"
    ╔═══════════════════════════════════════════════════╗
    β•‘   Blaxel Remote Build & Deploy                    β•‘
    β•‘   (Zero Local Storage Mode)                       β•‘
    β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
EOF
    echo -e "${NC}"

    check_prerequisites
    create_config
    deploy_to_blaxel
    get_image_id
    
    echo -e "\n${GREEN}Done! You can now use your sandbox.${NC}"
}

main "$@"