From dd6ffa548c4c5b5678dba246df291959be5a4e58 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 2 Aug 2025 19:10:40 +0200 Subject: [PATCH] Refactor configuration structure to replace DataConfig with instance-specific directories and auto-creation options --- cmd/server/main.go | 17 +++++++++-------- pkg/config.go | 45 ++++++++++++++++++++++---------------------- pkg/config_test.go | 20 ++++++++++---------- pkg/instance.go | 2 +- pkg/instance_test.go | 16 ++++++++-------- pkg/manager_test.go | 4 ++-- 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 4cd0d19..ec0e2d1 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -24,15 +24,16 @@ func main() { } // Create the data directory if it doesn't exist - if config.Data.AutoCreate { - if err := os.MkdirAll(config.Data.Directory, 0755); err != nil { - fmt.Printf("Error creating data directory: %v\n", err) - fmt.Println("Persisting data will not be possible.") + if config.Instances.AutoCreateDirs { + if err := os.MkdirAll(config.Instances.ConfigDir, 0755); err != nil { + fmt.Printf("Error creating config directory %s: %v\n", config.Instances.ConfigDir, err) + fmt.Println("Persistence will not be available.") + } + + if err := os.MkdirAll(config.Instances.LogDir, 0755); err != nil { + fmt.Printf("Error creating log directory %s: %v\n", config.Instances.LogDir, err) + fmt.Println("Instance logs will not be available.") } - } - if err := os.MkdirAll(config.Instances.LogDirectory, 0755); err != nil { - fmt.Printf("Error creating log directory: %v\n", err) - fmt.Println("Persisting instance logs will not be possible.") } // Initialize the instance manager diff --git a/pkg/config.go b/pkg/config.go index 4e94927..305dc18 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -15,7 +15,6 @@ type Config struct { Server ServerConfig `yaml:"server"` Instances InstancesConfig `yaml:"instances"` Auth AuthConfig `yaml:"auth"` - Data DataConfig `yaml:"data"` } // ServerConfig contains HTTP server configuration @@ -33,22 +32,22 @@ type ServerConfig struct { EnableSwagger bool `yaml:"enable_swagger"` } -// DataConfig contains data storage configuration -type DataConfig struct { - // Directory where all llamactl data will be stored (instances.json, logs, etc.) - Directory string `yaml:"directory"` - - // Automatically create the data directory if it doesn't exist - AutoCreate bool `yaml:"auto_create"` -} - // InstancesConfig contains instance management configuration type InstancesConfig struct { // Port range for instances (e.g., 8000,9000) PortRange [2]int `yaml:"port_range"` - // Directory where instance logs will be stored - LogDirectory string `yaml:"log_directory"` + // Directory where all llamactl data will be stored (instances.json, logs, etc.) + DataDir string `yaml:"data_dir"` + + // Instance config directory override + ConfigDir string `yaml:"config_dir"` + + // Logs directory override + LogDir string `yaml:"logs_dir"` + + // Automatically create the data directory if it doesn't exist + AutoCreateDirs bool `yaml:"auto_create_dirs"` // Maximum number of instances that can be created MaxInstances int `yaml:"max_instances"` @@ -95,13 +94,12 @@ func LoadConfig(configPath string) (Config, error) { AllowedOrigins: []string{"*"}, // Default to allow all origins EnableSwagger: false, }, - Data: DataConfig{ - Directory: getDefaultDataDirectory(), - AutoCreate: true, - }, Instances: InstancesConfig{ PortRange: [2]int{8000, 9000}, - LogDirectory: filepath.Join(getDefaultDataDirectory(), "logs"), + DataDir: getDefaultDataDirectory(), + ConfigDir: filepath.Join(getDefaultDataDirectory(), "instances"), + LogDir: filepath.Join(getDefaultDataDirectory(), "logs"), + AutoCreateDirs: true, MaxInstances: -1, // -1 means unlimited LlamaExecutable: "llama-server", DefaultAutoRestart: true, @@ -173,11 +171,17 @@ func loadEnvVars(cfg *Config) { // Data config if dataDir := os.Getenv("LLAMACTL_DATA_DIRECTORY"); dataDir != "" { - cfg.Data.Directory = dataDir + cfg.Instances.DataDir = dataDir + } + if instancesDir := os.Getenv("LLAMACTL_INSTANCES_DIRECTORY"); instancesDir != "" { + cfg.Instances.ConfigDir = instancesDir + } + if logsDir := os.Getenv("LLAMACTL_LOGS_DIRECTORY"); logsDir != "" { + cfg.Instances.LogDir = logsDir } if autoCreate := os.Getenv("LLAMACTL_AUTO_CREATE_DATA_DIR"); autoCreate != "" { if b, err := strconv.ParseBool(autoCreate); err == nil { - cfg.Data.AutoCreate = b + cfg.Instances.AutoCreateDirs = b } } @@ -187,9 +191,6 @@ func loadEnvVars(cfg *Config) { cfg.Instances.PortRange = ports } } - if logDir := os.Getenv("LLAMACTL_LOG_DIR"); logDir != "" { - cfg.Instances.LogDirectory = logDir - } if maxInstances := os.Getenv("LLAMACTL_MAX_INSTANCES"); maxInstances != "" { if m, err := strconv.Atoi(maxInstances); err == nil { cfg.Instances.MaxInstances = m diff --git a/pkg/config_test.go b/pkg/config_test.go index c5de26e..afb1a1c 100644 --- a/pkg/config_test.go +++ b/pkg/config_test.go @@ -22,17 +22,17 @@ func TestLoadConfig_Defaults(t *testing.T) { if cfg.Server.Port != 8080 { t.Errorf("Expected default port to be 8080, got %d", cfg.Server.Port) } - if cfg.Data.Directory != "/var/lib/llamactl" { - t.Errorf("Expected default data directory '/var/lib/llamactl', got %q", cfg.Data.Directory) + if cfg.Instances.ConfigDir != "/var/lib/llamactl/instances" { + t.Errorf("Expected default instances directory '/var/lib/llamactl/instances', got %q", cfg.Instances.ConfigDir) } - if !cfg.Data.AutoCreate { - t.Error("Expected default data auto-create to be true") + if !cfg.Instances.AutoCreateDirs { + t.Error("Expected default instances auto-create to be true") } if cfg.Instances.PortRange != [2]int{8000, 9000} { t.Errorf("Expected default port range [8000, 9000], got %v", cfg.Instances.PortRange) } - if cfg.Instances.LogDirectory != "/tmp/llamactl" { - t.Errorf("Expected default log directory '/tmp/llamactl', got %q", cfg.Instances.LogDirectory) + if cfg.Instances.LogDir != "/tmp/llamactl/logs" { + t.Errorf("Expected default logs directory '/tmp/llamactl/logs', got %q", cfg.Instances.LogDir) } if cfg.Instances.MaxInstances != -1 { t.Errorf("Expected default max instances -1, got %d", cfg.Instances.MaxInstances) @@ -90,8 +90,8 @@ instances: if cfg.Instances.PortRange != [2]int{7000, 8000} { t.Errorf("Expected port range [7000, 8000], got %v", cfg.Instances.PortRange) } - if cfg.Instances.LogDirectory != "/custom/logs" { - t.Errorf("Expected log directory '/custom/logs', got %q", cfg.Instances.LogDirectory) + if cfg.Instances.LogDir != "/custom/logs" { + t.Errorf("Expected logs directory '/custom/logs', got %q", cfg.Instances.LogDir) } if cfg.Instances.MaxInstances != 5 { t.Errorf("Expected max instances 5, got %d", cfg.Instances.MaxInstances) @@ -145,8 +145,8 @@ func TestLoadConfig_EnvironmentOverrides(t *testing.T) { if cfg.Instances.PortRange != [2]int{5000, 6000} { t.Errorf("Expected port range [5000, 6000], got %v", cfg.Instances.PortRange) } - if cfg.Instances.LogDirectory != "/env/logs" { - t.Errorf("Expected log directory '/env/logs', got %q", cfg.Instances.LogDirectory) + if cfg.Instances.LogDir != "/env/logs" { + t.Errorf("Expected logs directory '/env/logs', got %q", cfg.Instances.LogDir) } if cfg.Instances.MaxInstances != 20 { t.Errorf("Expected max instances 20, got %d", cfg.Instances.MaxInstances) diff --git a/pkg/instance.go b/pkg/instance.go index 0f6025a..0d239b0 100644 --- a/pkg/instance.go +++ b/pkg/instance.go @@ -149,7 +149,7 @@ func NewInstance(name string, globalSettings *InstancesConfig, options *CreateIn // Apply defaults applyDefaultOptions(optionsCopy, globalSettings) // Create the instance logger - logger := NewInstanceLogger(name, globalSettings.LogDirectory) + logger := NewInstanceLogger(name, globalSettings.LogDir) return &Instance{ Name: name, diff --git a/pkg/instance_test.go b/pkg/instance_test.go index 194645e..1d05e21 100644 --- a/pkg/instance_test.go +++ b/pkg/instance_test.go @@ -9,7 +9,7 @@ import ( func TestNewInstance(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -54,7 +54,7 @@ func TestNewInstance(t *testing.T) { func TestNewInstance_WithRestartOptions(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -91,7 +91,7 @@ func TestNewInstance_WithRestartOptions(t *testing.T) { func TestNewInstance_ValidationAndDefaults(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -123,7 +123,7 @@ func TestNewInstance_ValidationAndDefaults(t *testing.T) { func TestSetOptions(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -164,7 +164,7 @@ func TestSetOptions(t *testing.T) { func TestSetOptions_NilOptions(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -191,7 +191,7 @@ func TestSetOptions_NilOptions(t *testing.T) { func TestGetProxy(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", } options := &llamactl.CreateInstanceOptions{ @@ -224,7 +224,7 @@ func TestGetProxy(t *testing.T) { func TestMarshalJSON(t *testing.T) { globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", DefaultAutoRestart: true, DefaultMaxRestarts: 3, DefaultRestartDelay: 5, @@ -406,7 +406,7 @@ func TestCreateInstanceOptionsValidation(t *testing.T) { } globalSettings := &llamactl.InstancesConfig{ - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", } for _, tt := range tests { diff --git a/pkg/manager_test.go b/pkg/manager_test.go index 5a5cfa8..06c634e 100644 --- a/pkg/manager_test.go +++ b/pkg/manager_test.go @@ -10,7 +10,7 @@ import ( func TestNewInstanceManager(t *testing.T) { config := llamactl.InstancesConfig{ PortRange: [2]int{8000, 9000}, - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", MaxInstances: 5, LlamaExecutable: "llama-server", DefaultAutoRestart: true, @@ -490,7 +490,7 @@ func TestUpdateInstance_NotFound(t *testing.T) { func createTestManager() llamactl.InstanceManager { config := llamactl.InstancesConfig{ PortRange: [2]int{8000, 9000}, - LogDirectory: "/tmp/test", + LogDir: "/tmp/test", MaxInstances: 10, LlamaExecutable: "llama-server", DefaultAutoRestart: true,