From c984d95723d57a470fa4890d84c19fb33df9ad01 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 28 Sep 2025 13:31:41 +0200 Subject: [PATCH] Add environment variable support to instance options and command building --- pkg/instance/lifecycle.go | 14 ++++++++++++-- pkg/instance/options.go | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pkg/instance/lifecycle.go b/pkg/instance/lifecycle.go index 9f7243a..eec9689 100644 --- a/pkg/instance/lifecycle.go +++ b/pkg/instance/lifecycle.go @@ -372,13 +372,23 @@ func (i *Process) buildCommand() (*exec.Cmd, error) { return nil, err } + // Build the environment variables + env := i.options.BuildEnvironment(backendConfig) + // Get the command to execute - cmd := i.options.GetCommand(backendConfig) + command := i.options.GetCommand(backendConfig) // Build command arguments 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 diff --git a/pkg/instance/options.go b/pkg/instance/options.go index e776e05..62181dd 100644 --- a/pkg/instance/options.go +++ b/pkg/instance/options.go @@ -9,6 +9,7 @@ import ( "llamactl/pkg/backends/vllm" "llamactl/pkg/config" "log" + "maps" ) type CreateInstanceOptions struct { @@ -20,6 +21,8 @@ type CreateInstanceOptions struct { OnDemandStart *bool `json:"on_demand_start,omitempty"` // Idle timeout IdleTimeout *int `json:"idle_timeout,omitempty"` // minutes + //Environment variables + Environment map[string]string `json:"environment,omitempty"` BackendType backends.BackendType `json:"backend_type"` BackendOptions map[string]any `json:"backend_options,omitempty"` @@ -240,3 +243,23 @@ func (c *CreateInstanceOptions) BuildCommandArgs(backendConfig *config.BackendSe 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 +}