-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmain.go
More file actions
380 lines (342 loc) · 10.5 KB
/
main.go
File metadata and controls
380 lines (342 loc) · 10.5 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/docker/model-runner/pkg/envconfig"
"github.com/docker/model-runner/pkg/inference"
"github.com/docker/model-runner/pkg/inference/backends/llamacpp"
"github.com/docker/model-runner/pkg/inference/backends/sglang"
"github.com/docker/model-runner/pkg/inference/config"
"github.com/docker/model-runner/pkg/inference/models"
"github.com/docker/model-runner/pkg/logging"
dmrlogs "github.com/docker/model-runner/pkg/logs"
"github.com/docker/model-runner/pkg/metrics"
"github.com/docker/model-runner/pkg/routing"
modeltls "github.com/docker/model-runner/pkg/tls"
)
// initLogger creates the application logger based on LOG_LEVEL env var.
func initLogger() *slog.Logger {
return logging.NewLogger(envconfig.LogLevel())
}
var log = initLogger()
// exitFunc is used for Fatal-like exits; overridden in tests.
var exitFunc = func(code int) { os.Exit(code) }
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
sockName := envconfig.SocketPath()
modelPath, err := envconfig.ModelsPath()
if err != nil {
log.Error("Failed to get models path", "error", err)
exitFunc(1)
}
if envconfig.DisableServerUpdate() {
llamacpp.ShouldUpdateServerLock.Lock()
llamacpp.ShouldUpdateServer = false
llamacpp.ShouldUpdateServerLock.Unlock()
}
if v := envconfig.LlamaServerVersion(); v != "" {
llamacpp.SetDesiredServerVersion(v)
}
llamaServerPath := envconfig.LlamaServerPath()
vllmServerPath := envconfig.VLLMServerPath()
sglangServerPath := envconfig.SGLangServerPath()
mlxServerPath := envconfig.MLXServerPath()
diffusersServerPath := envconfig.DiffusersServerPath()
vllmMetalServerPath := envconfig.VLLMMetalServerPath()
// Create a proxy-aware HTTP transport
// Use a safe type assertion with fallback, and explicitly set Proxy to http.ProxyFromEnvironment
var baseTransport *http.Transport
if t, ok := http.DefaultTransport.(*http.Transport); ok {
baseTransport = t.Clone()
} else {
baseTransport = &http.Transport{}
}
baseTransport.Proxy = http.ProxyFromEnvironment
log.Info("LLAMA_SERVER_PATH", "path", llamaServerPath)
if vllmServerPath != "" {
log.Info("VLLM_SERVER_PATH", "path", vllmServerPath)
}
if sglangServerPath != "" {
log.Info("SGLANG_SERVER_PATH", "path", sglangServerPath)
}
if mlxServerPath != "" {
log.Info("MLX_SERVER_PATH", "path", mlxServerPath)
}
if diffusersServerPath != "" {
log.Info("DIFFUSERS_SERVER_PATH", "path", diffusersServerPath)
}
if vllmMetalServerPath != "" {
log.Info("VLLM_METAL_SERVER_PATH", "path", vllmMetalServerPath)
}
// Create llama.cpp configuration from environment variables
llamaCppConfig, err := createLlamaCppConfigFromEnv()
if err != nil {
log.Error("invalid LLAMA_ARGS", "error", err)
exitFunc(1)
return
}
updatedServerPath := func() string {
wd, _ := os.Getwd()
d := filepath.Join(wd, "updated-inference", "bin")
_ = os.MkdirAll(d, 0o755)
return d
}()
svc, err := routing.NewService(routing.ServiceConfig{
Log: log,
ClientConfig: models.ClientConfig{
StoreRootPath: modelPath,
Logger: log.With("component", "model-manager"),
Transport: baseTransport,
},
Backends: append(
routing.DefaultBackendDefs(routing.BackendsConfig{
Log: log,
LlamaCppVendoredPath: llamaServerPath,
LlamaCppUpdatedPath: updatedServerPath,
LlamaCppConfig: llamaCppConfig,
IncludeMLX: true,
MLXPath: mlxServerPath,
IncludeVLLM: includeVLLM,
VLLMPath: vllmServerPath,
VLLMMetalPath: vllmMetalServerPath,
IncludeDiffusers: true,
DiffusersPath: diffusersServerPath,
}),
routing.BackendDef{Name: sglang.Name, Init: func(mm *models.Manager) (inference.Backend, error) {
return sglang.New(log, mm, log.With("component", sglang.Name), nil, sglangServerPath)
}},
),
OnBackendError: func(name string, err error) {
log.Error("unable to initialize backend", "backend", name, "error", err)
exitFunc(1)
},
DefaultBackendName: llamacpp.Name,
HTTPClient: http.DefaultClient,
MetricsTracker: metrics.NewTracker(
http.DefaultClient,
log.With("component", "metrics"),
"",
false,
),
AllowedOrigins: envconfig.AllowedOrigins(),
IncludeResponsesAPI: true,
ExtraRoutes: func(r *routing.NormalizedServeMux, s *routing.Service) {
// Root handler – only catches exact "/" requests
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Docker Model Runner is running"))
})
// Version endpoint
r.HandleFunc("/version", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(map[string]string{"version": Version}); err != nil {
log.Warn("failed to write version response", "error", err)
}
})
// Logs endpoint (Docker Desktop mode only).
if logDir := envconfig.LogDir(); logDir != "" {
r.HandleFunc(
"GET /logs",
dmrlogs.NewHTTPHandler(logDir),
)
log.Info("Logs endpoint enabled at /logs", "dir", logDir)
}
// Metrics endpoint
if !envconfig.DisableMetrics() {
metricsHandler := metrics.NewAggregatedMetricsHandler(
log.With("component", "metrics"),
s.SchedulerHTTP,
)
r.Handle("/metrics", metricsHandler)
log.Info("Metrics endpoint enabled at /metrics")
} else {
log.Info("Metrics endpoint disabled")
}
},
})
if err != nil {
log.Error("failed to initialize service", "error", err)
exitFunc(1)
}
server := &http.Server{
Handler: svc.Router,
ReadHeaderTimeout: 10 * time.Second,
}
serverErrors := make(chan error, 1)
// TLS server (optional)
var tlsServer *http.Server
tlsServerErrors := make(chan error, 1)
// Check if we should use TCP port instead of Unix socket
tcpPort := envconfig.TCPPort()
if tcpPort != "" {
// Use TCP port
addr := ":" + tcpPort
log.Info("Listening on TCP port", "port", tcpPort)
server.Addr = addr
go func() {
serverErrors <- server.ListenAndServe()
}()
} else {
// Use Unix socket
if err := os.Remove(sockName); err != nil {
if !os.IsNotExist(err) {
log.Error("Failed to remove existing socket", "error", err)
exitFunc(1)
}
}
ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: sockName, Net: "unix"})
if err != nil {
log.Error("Failed to listen on socket", "error", err)
exitFunc(1)
}
go func() {
serverErrors <- server.Serve(ln)
}()
}
// Start TLS server if enabled
if envconfig.TLSEnabled() {
tlsPort := envconfig.TLSPort()
// Get certificate paths
certPath := envconfig.TLSCert()
keyPath := envconfig.TLSKey()
// Auto-generate certificates if not provided and auto-cert is not disabled
if certPath == "" || keyPath == "" {
if envconfig.TLSAutoCert(true) {
log.Info("Auto-generating TLS certificates...")
var err error
certPath, keyPath, err = modeltls.EnsureCertificates("", "")
if err != nil {
log.Error("Failed to ensure TLS certificates", "error", err)
exitFunc(1)
}
log.Info("Using TLS certificate", "cert", certPath)
log.Info("Using TLS key", "key", keyPath)
} else {
log.Error("TLS enabled but no certificate provided and auto-cert is disabled")
exitFunc(1)
}
}
// Load TLS configuration
tlsConfig, err := modeltls.LoadTLSConfig(certPath, keyPath)
if err != nil {
log.Error("Failed to load TLS configuration", "error", err)
exitFunc(1)
}
tlsServer = &http.Server{
Addr: ":" + tlsPort,
Handler: svc.Router,
TLSConfig: tlsConfig,
ReadHeaderTimeout: 10 * time.Second,
}
log.Info("Listening on TLS port", "port", tlsPort)
go func() {
// Use ListenAndServeTLS with empty strings since TLSConfig already has the certs
ln, err := tls.Listen("tcp", tlsServer.Addr, tlsConfig)
if err != nil {
tlsServerErrors <- err
return
}
tlsServerErrors <- tlsServer.Serve(ln)
}()
}
schedulerErrors := make(chan error, 1)
go func() {
schedulerErrors <- svc.Scheduler.Run(ctx)
}()
var tlsServerErrorsChan <-chan error
if envconfig.TLSEnabled() {
tlsServerErrorsChan = tlsServerErrors
} else {
// Use a nil channel which will block forever when TLS is disabled
tlsServerErrorsChan = nil
}
select {
case err := <-serverErrors:
if err != nil {
log.Error("Server error", "error", err)
}
case err := <-tlsServerErrorsChan:
if err != nil {
log.Error("TLS server error", "error", err)
}
case <-ctx.Done():
log.Info("Shutdown signal received")
log.Info("Shutting down the server")
if err := server.Close(); err != nil {
log.Error("Server shutdown error", "error", err)
}
if tlsServer != nil {
log.Info("Shutting down the TLS server")
if err := tlsServer.Close(); err != nil {
log.Error("TLS server shutdown error", "error", err)
}
}
log.Info("Waiting for the scheduler to stop")
if err := <-schedulerErrors; err != nil {
log.Error("Scheduler error", "error", err)
}
}
log.Info("Docker Model Runner stopped")
}
// createLlamaCppConfigFromEnv creates a LlamaCppConfig from environment variables.
// Returns nil config (use defaults) when LLAMA_ARGS is unset, or an error if
// the args contain disallowed flags.
func createLlamaCppConfigFromEnv() (config.BackendConfig, error) {
argsStr := envconfig.LlamaArgs()
if argsStr == "" {
return nil, nil
}
args := splitArgs(argsStr)
disallowedArgs := map[string]struct{}{
"--model": {},
"--host": {},
"--embeddings": {},
"--mmproj": {},
}
for _, arg := range args {
if _, found := disallowedArgs[arg]; found {
return nil, fmt.Errorf("LLAMA_ARGS cannot override %s, which is controlled by the model runner", arg)
}
}
log.Info("Using custom llama.cpp arguments", "args", args)
return &llamacpp.Config{Args: args}, nil
}
// splitArgs splits a string into arguments, respecting quoted arguments
func splitArgs(s string) []string {
var args []string
var currentArg strings.Builder
inQuotes := false
for _, r := range s {
switch {
case r == '"' || r == '\'':
inQuotes = !inQuotes
case r == ' ' && !inQuotes:
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
currentArg.Reset()
}
default:
currentArg.WriteRune(r)
}
}
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
}
return args
}