Refactor configuration structure to replace DataConfig with instance-specific directories and auto-creation options

This commit is contained in:
2025-08-02 19:10:40 +02:00
parent 7935f19cc1
commit dd6ffa548c
6 changed files with 53 additions and 51 deletions

View File

@@ -24,15 +24,16 @@ func main() {
} }
// Create the data directory if it doesn't exist // Create the data directory if it doesn't exist
if config.Data.AutoCreate { if config.Instances.AutoCreateDirs {
if err := os.MkdirAll(config.Data.Directory, 0755); err != nil { if err := os.MkdirAll(config.Instances.ConfigDir, 0755); err != nil {
fmt.Printf("Error creating data directory: %v\n", err) fmt.Printf("Error creating config directory %s: %v\n", config.Instances.ConfigDir, err)
fmt.Println("Persisting data will not be possible.") 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 // Initialize the instance manager

View File

@@ -15,7 +15,6 @@ type Config 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"`
Data DataConfig `yaml:"data"`
} }
// ServerConfig contains HTTP server configuration // ServerConfig contains HTTP server configuration
@@ -33,22 +32,22 @@ type ServerConfig struct {
EnableSwagger bool `yaml:"enable_swagger"` 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 // InstancesConfig contains instance management configuration
type InstancesConfig struct { type InstancesConfig struct {
// Port range for instances (e.g., 8000,9000) // Port range for instances (e.g., 8000,9000)
PortRange [2]int `yaml:"port_range"` PortRange [2]int `yaml:"port_range"`
// Directory where instance logs will be stored // Directory where all llamactl data will be stored (instances.json, logs, etc.)
LogDirectory string `yaml:"log_directory"` 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 // Maximum number of instances that can be created
MaxInstances int `yaml:"max_instances"` MaxInstances int `yaml:"max_instances"`
@@ -95,13 +94,12 @@ func LoadConfig(configPath string) (Config, error) {
AllowedOrigins: []string{"*"}, // Default to allow all origins AllowedOrigins: []string{"*"}, // Default to allow all origins
EnableSwagger: false, EnableSwagger: false,
}, },
Data: DataConfig{
Directory: getDefaultDataDirectory(),
AutoCreate: true,
},
Instances: InstancesConfig{ Instances: InstancesConfig{
PortRange: [2]int{8000, 9000}, 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 MaxInstances: -1, // -1 means unlimited
LlamaExecutable: "llama-server", LlamaExecutable: "llama-server",
DefaultAutoRestart: true, DefaultAutoRestart: true,
@@ -173,11 +171,17 @@ func loadEnvVars(cfg *Config) {
// Data config // Data config
if dataDir := os.Getenv("LLAMACTL_DATA_DIRECTORY"); dataDir != "" { 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 autoCreate := os.Getenv("LLAMACTL_AUTO_CREATE_DATA_DIR"); autoCreate != "" {
if b, err := strconv.ParseBool(autoCreate); err == nil { 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 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 maxInstances := os.Getenv("LLAMACTL_MAX_INSTANCES"); maxInstances != "" {
if m, err := strconv.Atoi(maxInstances); err == nil { if m, err := strconv.Atoi(maxInstances); err == nil {
cfg.Instances.MaxInstances = m cfg.Instances.MaxInstances = m

View File

@@ -22,17 +22,17 @@ func TestLoadConfig_Defaults(t *testing.T) {
if cfg.Server.Port != 8080 { if cfg.Server.Port != 8080 {
t.Errorf("Expected default port to be 8080, got %d", cfg.Server.Port) t.Errorf("Expected default port to be 8080, got %d", cfg.Server.Port)
} }
if cfg.Data.Directory != "/var/lib/llamactl" { if cfg.Instances.ConfigDir != "/var/lib/llamactl/instances" {
t.Errorf("Expected default data directory '/var/lib/llamactl', got %q", cfg.Data.Directory) t.Errorf("Expected default instances directory '/var/lib/llamactl/instances', got %q", cfg.Instances.ConfigDir)
} }
if !cfg.Data.AutoCreate { if !cfg.Instances.AutoCreateDirs {
t.Error("Expected default data auto-create to be true") t.Error("Expected default instances auto-create to be true")
} }
if cfg.Instances.PortRange != [2]int{8000, 9000} { if cfg.Instances.PortRange != [2]int{8000, 9000} {
t.Errorf("Expected default port range [8000, 9000], got %v", cfg.Instances.PortRange) t.Errorf("Expected default port range [8000, 9000], got %v", cfg.Instances.PortRange)
} }
if cfg.Instances.LogDirectory != "/tmp/llamactl" { if cfg.Instances.LogDir != "/tmp/llamactl/logs" {
t.Errorf("Expected default log directory '/tmp/llamactl', got %q", cfg.Instances.LogDirectory) t.Errorf("Expected default logs directory '/tmp/llamactl/logs', got %q", cfg.Instances.LogDir)
} }
if cfg.Instances.MaxInstances != -1 { if cfg.Instances.MaxInstances != -1 {
t.Errorf("Expected default max instances -1, got %d", cfg.Instances.MaxInstances) 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} { if cfg.Instances.PortRange != [2]int{7000, 8000} {
t.Errorf("Expected port range [7000, 8000], got %v", cfg.Instances.PortRange) t.Errorf("Expected port range [7000, 8000], got %v", cfg.Instances.PortRange)
} }
if cfg.Instances.LogDirectory != "/custom/logs" { if cfg.Instances.LogDir != "/custom/logs" {
t.Errorf("Expected log directory '/custom/logs', got %q", cfg.Instances.LogDirectory) t.Errorf("Expected logs directory '/custom/logs', got %q", cfg.Instances.LogDir)
} }
if cfg.Instances.MaxInstances != 5 { if cfg.Instances.MaxInstances != 5 {
t.Errorf("Expected max instances 5, got %d", cfg.Instances.MaxInstances) 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} { if cfg.Instances.PortRange != [2]int{5000, 6000} {
t.Errorf("Expected port range [5000, 6000], got %v", cfg.Instances.PortRange) t.Errorf("Expected port range [5000, 6000], got %v", cfg.Instances.PortRange)
} }
if cfg.Instances.LogDirectory != "/env/logs" { if cfg.Instances.LogDir != "/env/logs" {
t.Errorf("Expected log directory '/env/logs', got %q", cfg.Instances.LogDirectory) t.Errorf("Expected logs directory '/env/logs', got %q", cfg.Instances.LogDir)
} }
if cfg.Instances.MaxInstances != 20 { if cfg.Instances.MaxInstances != 20 {
t.Errorf("Expected max instances 20, got %d", cfg.Instances.MaxInstances) t.Errorf("Expected max instances 20, got %d", cfg.Instances.MaxInstances)

View File

@@ -149,7 +149,7 @@ func NewInstance(name string, globalSettings *InstancesConfig, options *CreateIn
// Apply defaults // Apply defaults
applyDefaultOptions(optionsCopy, globalSettings) applyDefaultOptions(optionsCopy, globalSettings)
// Create the instance logger // Create the instance logger
logger := NewInstanceLogger(name, globalSettings.LogDirectory) logger := NewInstanceLogger(name, globalSettings.LogDir)
return &Instance{ return &Instance{
Name: name, Name: name,

View File

@@ -9,7 +9,7 @@ import (
func TestNewInstance(t *testing.T) { func TestNewInstance(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -54,7 +54,7 @@ func TestNewInstance(t *testing.T) {
func TestNewInstance_WithRestartOptions(t *testing.T) { func TestNewInstance_WithRestartOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -91,7 +91,7 @@ func TestNewInstance_WithRestartOptions(t *testing.T) {
func TestNewInstance_ValidationAndDefaults(t *testing.T) { func TestNewInstance_ValidationAndDefaults(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -123,7 +123,7 @@ func TestNewInstance_ValidationAndDefaults(t *testing.T) {
func TestSetOptions(t *testing.T) { func TestSetOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -164,7 +164,7 @@ func TestSetOptions(t *testing.T) {
func TestSetOptions_NilOptions(t *testing.T) { func TestSetOptions_NilOptions(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -191,7 +191,7 @@ func TestSetOptions_NilOptions(t *testing.T) {
func TestGetProxy(t *testing.T) { func TestGetProxy(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
} }
options := &llamactl.CreateInstanceOptions{ options := &llamactl.CreateInstanceOptions{
@@ -224,7 +224,7 @@ func TestGetProxy(t *testing.T) {
func TestMarshalJSON(t *testing.T) { func TestMarshalJSON(t *testing.T) {
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
DefaultAutoRestart: true, DefaultAutoRestart: true,
DefaultMaxRestarts: 3, DefaultMaxRestarts: 3,
DefaultRestartDelay: 5, DefaultRestartDelay: 5,
@@ -406,7 +406,7 @@ func TestCreateInstanceOptionsValidation(t *testing.T) {
} }
globalSettings := &llamactl.InstancesConfig{ globalSettings := &llamactl.InstancesConfig{
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
} }
for _, tt := range tests { for _, tt := range tests {

View File

@@ -10,7 +10,7 @@ import (
func TestNewInstanceManager(t *testing.T) { func TestNewInstanceManager(t *testing.T) {
config := llamactl.InstancesConfig{ config := llamactl.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
MaxInstances: 5, MaxInstances: 5,
LlamaExecutable: "llama-server", LlamaExecutable: "llama-server",
DefaultAutoRestart: true, DefaultAutoRestart: true,
@@ -490,7 +490,7 @@ func TestUpdateInstance_NotFound(t *testing.T) {
func createTestManager() llamactl.InstanceManager { func createTestManager() llamactl.InstanceManager {
config := llamactl.InstancesConfig{ config := llamactl.InstancesConfig{
PortRange: [2]int{8000, 9000}, PortRange: [2]int{8000, 9000},
LogDirectory: "/tmp/test", LogDir: "/tmp/test",
MaxInstances: 10, MaxInstances: 10,
LlamaExecutable: "llama-server", LlamaExecutable: "llama-server",
DefaultAutoRestart: true, DefaultAutoRestart: true,