-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_example_docker.py
More file actions
183 lines (151 loc) · 6.93 KB
/
Copy pathdeploy_example_docker.py
File metadata and controls
183 lines (151 loc) · 6.93 KB
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
"""
Docker Image Wrapper Template
This template wraps a Docker image for deployment on Chutes.ai.
Use this when you have an existing Docker image to wrap as a Chute.
Usage:
1. Copy to deploy_<yourservice>.py
2. Update CHUTE_* variables for your service
3. Run route discovery: ./utils.sh --discover deploy_<yourservice>
4. Build and deploy: ./utils.sh --build deploy_<yourservice> [--local]
See also:
- vanilla_examples/deploy_example_sglang.py - For SGLang model serving
- vanilla_examples/deploy_example_imggen.py - For image generation models
"""
import os
from configparser import ConfigParser
from chutes.chute import Chute, NodeSelector
from tools.chute_wrappers import (
build_wrapper_image,
load_route_manifest,
register_passthrough_routes,
wait_for_services,
probe_services,
)
# =============================================================================
# Auth Configuration (auto-loaded from ~/.chutes/config.ini or environment)
# =============================================================================
chutes_config = ConfigParser()
chutes_config.read(os.path.expanduser("~/.chutes/config.ini"))
USERNAME = os.getenv("CHUTES_USERNAME") or chutes_config.get("auth", "username", fallback="chutes")
# =============================================================================
# Chute Configuration - CUSTOMIZE THESE FOR YOUR SERVICE
# =============================================================================
# Basic identification
CHUTE_NAME = "example-service"
CHUTE_TAG = "v0.1.0"
CHUTE_BASE_IMAGE = os.getenv("CHUTE_BASE_IMAGE", "your-registry/your-image:latest")
# Human-readable metadata
CHUTE_TAGLINE = "Example Service (customize this)"
CHUTE_DOC = """
### Example Service
Describe your service here. This appears in the Chutes.ai UI.
#### Endpoints
- GET /health - Health check
- POST /your-endpoint - Your endpoint description
"""
# Chute environment variables (passed to container during discovery and runtime)
# Add any env vars your base image needs
CHUTE_ENV = {
# "MODEL_NAME": "your-model",
# "WHISPER_MODEL": "large-v3-turbo",
}
# Static routes (always included, merged with discovered routes)
# Use this for services that don't expose OpenAPI specs (e.g., whisper.cpp server)
# See: https://github.com/ggml-org/whisper.cpp/tree/master/examples/server
CHUTE_STATIC_ROUTES = [
# {"path": "/inference", "method": "POST", "port": 8080, "target_path": "/inference"},
# {"path": "/load", "method": "GET", "port": 8080, "target_path": "/load"},
# {"path": "/v1/audio/transcriptions", "method": "POST", "port": 8080, "target_path": "/inference"},
]
# =============================================================================
# Resource Configuration - Adjust based on your service requirements
# =============================================================================
CHUTE_GPU_COUNT = int(os.getenv("CHUTE_GPU_COUNT", "1"))
CHUTE_MIN_VRAM_GB_PER_GPU = int(os.getenv("CHUTE_MIN_VRAM_GB_PER_GPU", "16"))
CHUTE_INCLUDE_GPU_TYPES = os.getenv(
"CHUTE_INCLUDE_GPU_TYPES",
"rtx4090,rtx3090,a100,a100_sxm,h100,h100_sxm"
).split(",")
CHUTE_SHUTDOWN_AFTER_SECONDS = int(os.getenv("CHUTE_SHUTDOWN_AFTER_SECONDS", "3600"))
CHUTE_CONCURRENCY = int(os.getenv("CHUTE_CONCURRENCY", "1"))
# =============================================================================
# Network Configuration
# =============================================================================
LOCAL_HOST = "127.0.0.1"
# Comma-separated list of ports your service exposes
SERVICE_PORTS = [int(p.strip()) for p in os.getenv("CHUTE_PORTS", "8080").split(",") if p.strip()]
if not SERVICE_PORTS:
raise RuntimeError("CHUTE_PORTS must specify at least one port")
DEFAULT_SERVICE_PORT = SERVICE_PORTS[0]
# Entrypoint script in the base image (if any)
ENTRYPOINT = os.getenv("CHUTE_ENTRYPOINT", "/usr/local/bin/docker-entrypoint.sh")
# =============================================================================
# Image Build Configuration
# =============================================================================
# build_wrapper_image sets up a Debian-based image with Chutes runtime deps.
# It extends your CHUTE_BASE_IMAGE with necessary tooling.
image = build_wrapper_image(
username=USERNAME,
name=CHUTE_NAME,
tag=CHUTE_TAG,
base_image=CHUTE_BASE_IMAGE,
)
# =============================================================================
# Chute Definition
# =============================================================================
chute = Chute(
username=USERNAME,
name=CHUTE_NAME,
tagline=CHUTE_TAGLINE,
readme=CHUTE_DOC,
image=image,
node_selector=NodeSelector(
gpu_count=CHUTE_GPU_COUNT,
min_vram_gb_per_gpu=CHUTE_MIN_VRAM_GB_PER_GPU,
include=CHUTE_INCLUDE_GPU_TYPES,
),
concurrency=CHUTE_CONCURRENCY,
allow_external_egress=True,
shutdown_after_seconds=CHUTE_SHUTDOWN_AFTER_SECONDS,
)
# Register routes from manifest (generated by route discovery)
# Static routes are merged with discovered routes (duplicates are skipped)
# If no manifest exists yet, run: ./utils.sh --discover <this_module_name>
register_passthrough_routes(chute, load_route_manifest(static_routes=CHUTE_STATIC_ROUTES), DEFAULT_SERVICE_PORT)
# =============================================================================
# Lifecycle Hooks
# =============================================================================
@chute.on_startup()
async def boot(self):
"""Wait for all service ports to be ready before accepting requests."""
await wait_for_services(SERVICE_PORTS, host=LOCAL_HOST, timeout=600)
# @chute.on_shutdown()
# async def shutdown(self):
# """Gracefully terminate services (optional)."""
# pass
# =============================================================================
# Health Check Endpoint
# =============================================================================
@chute.cord(public_api_path="/health", public_api_method="GET", method="GET")
async def health_check(self) -> dict:
"""Check if all services are healthy."""
errors = await probe_services(SERVICE_PORTS, host=LOCAL_HOST, timeout=5)
if errors:
return {"status": "unhealthy", "errors": errors}
return {"status": "healthy", "ports": SERVICE_PORTS}
# =============================================================================
# Local Testing
# =============================================================================
if __name__ == "__main__":
print(f"Chute: {chute.name}")
print(f"Image: {image.name}:{image.tag}")
print(f"Base Image: {CHUTE_BASE_IMAGE}")
print(f"Service Ports: {SERVICE_PORTS}")
print(f"GPU: {CHUTE_GPU_COUNT}x (min {CHUTE_MIN_VRAM_GB_PER_GPU}GB VRAM)")
print(f"Concurrency: {CHUTE_CONCURRENCY}")
print(f"\nEnvironment:")
for k, v in CHUTE_ENV.items():
print(f" {k}={v}")
print(f"\nCords:")
for cord in chute.cords:
print(f" {cord._public_api_method:6} {cord._public_api_path} -> port {cord._passthrough_port or 'N/A'}")