Add environment variable support to instance options and command building

This commit is contained in:
2025-09-28 13:31:41 +02:00
parent 50e1355205
commit c984d95723
2 changed files with 35 additions and 2 deletions

View File

@@ -372,13 +372,23 @@ func (i *Process) buildCommand() (*exec.Cmd, error) {
return nil, err return nil, err
} }
// Build the environment variables
env := i.options.BuildEnvironment(backendConfig)
// Get the command to execute // Get the command to execute
cmd := i.options.GetCommand(backendConfig) command := i.options.GetCommand(backendConfig)
// Build command arguments // Build command arguments
args := i.options.BuildCommandArgs(backendConfig) args := i.options.BuildCommandArgs(backendConfig)
return exec.Command(cmd, args...), nil // Create the exec.Cmd
cmd := exec.CommandContext(i.ctx, command, args...)
cmd.Env = []string{}
for k, v := range env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
return cmd, nil
} }
// getBackendConfig resolves the backend configuration for the current instance // getBackendConfig resolves the backend configuration for the current instance

View File

@@ -9,6 +9,7 @@ import (
"llamactl/pkg/backends/vllm" "llamactl/pkg/backends/vllm"
"llamactl/pkg/config" "llamactl/pkg/config"
"log" "log"
"maps"
) )
type CreateInstanceOptions struct { type CreateInstanceOptions struct {
@@ -20,6 +21,8 @@ type CreateInstanceOptions struct {
OnDemandStart *bool `json:"on_demand_start,omitempty"` OnDemandStart *bool `json:"on_demand_start,omitempty"`
// Idle timeout // Idle timeout
IdleTimeout *int `json:"idle_timeout,omitempty"` // minutes IdleTimeout *int `json:"idle_timeout,omitempty"` // minutes
//Environment variables
Environment map[string]string `json:"environment,omitempty"`
BackendType backends.BackendType `json:"backend_type"` BackendType backends.BackendType `json:"backend_type"`
BackendOptions map[string]any `json:"backend_options,omitempty"` BackendOptions map[string]any `json:"backend_options,omitempty"`
@@ -240,3 +243,23 @@ func (c *CreateInstanceOptions) BuildCommandArgs(backendConfig *config.BackendSe
return args return args
} }
func (c *CreateInstanceOptions) BuildEnvironment(backendConfig *config.BackendSettings) map[string]string {
env := map[string]string{}
if backendConfig.Environment != nil {
maps.Copy(env, backendConfig.Environment)
}
if backendConfig.Docker != nil && backendConfig.Docker.Enabled && c.BackendType != backends.BackendTypeMlxLm {
if backendConfig.Docker.Environment != nil {
maps.Copy(env, backendConfig.Docker.Environment)
}
}
if c.Environment != nil {
maps.Copy(env, c.Environment)
}
return env
}