Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/cmd/gpucreate/gpucreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
74 changes: 74 additions & 0 deletions pkg/cmd/gpucreate/gpucreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`)
}