From 6b16d7a88f4f877bcf1fde5bf8b1987a083c6e5a Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 7 Nov 2026 16:14:13 -0400 Subject: [PATCH] fix: reject --startup-script for container and compose modes applyBuildMode only attaches the script for vm and k8s, so -m container and -m compose dropped -s/--startup-script with no error. validateBuildMode now rejects that combination the same way it rejects a missing image or compose file. --- pkg/cmd/gpucreate/gpucreate.go | 14 ++++-- pkg/cmd/gpucreate/gpucreate_test.go | 74 +++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/gpucreate/gpucreate.go b/pkg/cmd/gpucreate/gpucreate.go index 1cc9b9c4..fce2f70a 100644 --- a/pkg/cmd/gpucreate/gpucreate.go +++ b/pkg/cmd/gpucreate/gpucreate.go @@ -76,7 +76,8 @@ If type A fails for instance-2: Startup Scripts: You can attach a startup script that runs when the instance boots using the ---startup-script flag. The script can be provided as: +--startup-script flag. The script is supported for vm and k8s modes only. +It can be provided as: - An inline string: --startup-script 'pip install torch' - A file path (prefix with @): --startup-script @setup.sh - An absolute file path: --startup-script @/path/to/setup.sh` @@ -200,7 +201,7 @@ func NewCmdGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore) *cobra warnLaunchableFlagConflicts(cmd, t, launchableID) if launchableID == "" { - if err := validateBuildMode(mode, containerImage, composeFile); err != nil { + if err := validateBuildMode(mode, containerImage, composeFile, startupScript); err != nil { return err } } @@ -298,7 +299,7 @@ func registerCreateFlags(cmd *cobra.Command, name, instanceTypes *string, readSt cmd.Flags().IntVarP(parallel, "parallel", "p", 1, "Number of parallel creation attempts") cmd.Flags().BoolVarP(detached, "detached", "d", false, "Don't wait for instances to be ready") cmd.Flags().IntVar(timeout, "timeout", 300, "Timeout in seconds for each instance to become ready") - cmd.Flags().StringVarP(startupScript, "startup-script", "s", "", "Startup script to run on instance (string or @filepath)") + cmd.Flags().StringVarP(startupScript, "startup-script", "s", "", "Startup script to run on the instance for vm and k8s modes (string or @filepath)") cmd.Flags().BoolVar(dryRun, "dry-run", false, "Show matching instance types without creating anything") // Build mode flags @@ -1307,7 +1308,7 @@ func (c *createContext) createWorkspace(name string, spec InstanceSpec) (*entity return workspace, nil } -func validateBuildMode(mode, containerImage, composeFile string) error { +func validateBuildMode(mode, containerImage, composeFile, startupScript string) error { switch mode { case "vm", "k8s", "container", "compose": // valid @@ -1320,6 +1321,11 @@ func validateBuildMode(mode, containerImage, composeFile string) error { if mode == "compose" && composeFile == "" { return breverrors.NewValidationError("--compose-file is required for compose mode") } + // container and compose never attach the script (only vm and k8s set + // VMBuild.LifeCycleScriptAttr). Reject it here so the flag is not a no-op. + if startupScript != "" && (mode == "container" || mode == "compose") { + return breverrors.NewValidationError("--startup-script is only supported for vm and k8s modes") + } return nil } diff --git a/pkg/cmd/gpucreate/gpucreate_test.go b/pkg/cmd/gpucreate/gpucreate_test.go index 4fe40eb1..d3cbe969 100644 --- a/pkg/cmd/gpucreate/gpucreate_test.go +++ b/pkg/cmd/gpucreate/gpucreate_test.go @@ -1446,3 +1446,77 @@ func TestCreateInstancesWithType(t *testing.T) { }) } } + +// TestCreateRejectsStartupScriptForContainerAndCompose covers --startup-script +// with -m container or -m compose. Those modes never attach the script, so the +// command must fail instead of creating (or dry-running) as if it had. +func TestCreateRejectsStartupScriptForContainerAndCompose(t *testing.T) { + cases := []struct { + name string + args []string + wantErr string + }{ + { + name: "container", + args: []string{"my-inst", "--mode", "container", "--container-image", "nginx:latest", "--type", "g5.xlarge", "--startup-script", "echo hi", "--dry-run"}, + wantErr: "--startup-script is only supported for vm and k8s modes", + }, + { + name: "compose", + args: []string{"my-inst", "--mode", "compose", "--compose-file", "docker-compose.yml", "--type", "g5.xlarge", "--startup-script", "echo hi", "--dry-run"}, + wantErr: "--startup-script is only supported for vm and k8s modes", + }, + { + name: "compose script path is rejected before the file is read", + args: []string{"my-inst", "--mode", "compose", "--compose-file", "docker-compose.yml", "--type", "g5.xlarge", "--startup-script", "@setup.sh", "--dry-run"}, + wantErr: "--startup-script is only supported for vm and k8s modes", + }, + { + name: "vm still accepts a script", + args: []string{"my-inst", "--mode", "vm", "--type", "g5.xlarge", "--startup-script", "echo hi", "--dry-run"}, + }, + { + name: "container without a script still validates", + args: []string{"my-inst", "--mode", "container", "--container-image", "nginx:latest", "--type", "g5.xlarge", "--dry-run"}, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + mock := NewMockGPUCreateStore() + cmd := NewCmdGPUCreate(terminal.New(), mock) + cmd.SetArgs(tc.args) + + err := cmd.Execute() + if tc.wantErr == "" { + assert.NoError(t, err) + } else { + assert.ErrorContains(t, err, tc.wantErr) + } + assert.Empty(t, mock.CreatedWorkspaces) + }) + } +} + +func TestValidateBuildModeStartupScript(t *testing.T) { + err := validateBuildMode("container", "nginx:latest", "", "echo hi") + assert.ErrorContains(t, err, "--startup-script is only supported for vm and k8s modes") + + err = validateBuildMode("compose", "", "docker-compose.yml", "@setup.sh") + assert.ErrorContains(t, err, "--startup-script is only supported for vm and k8s modes") + + assert.NoError(t, validateBuildMode("vm", "", "", "echo hi")) + assert.NoError(t, validateBuildMode("k8s", "", "", "echo hi")) + assert.NoError(t, validateBuildMode("container", "nginx:latest", "", "")) + assert.NoError(t, validateBuildMode("compose", "", "docker-compose.yml", "")) + + // A missing image is still reported first, same as before this check. + err = validateBuildMode("container", "", "", "echo hi") + assert.ErrorContains(t, err, "--container-image is required for container mode") + + err = validateBuildMode("compose", "", "", "echo hi") + assert.ErrorContains(t, err, "--compose-file is required for compose mode") + + err = validateBuildMode("nope", "", "", "echo hi") + assert.ErrorContains(t, err, `invalid mode "nope"`) +}