Skip to content
Merged
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
24 changes: 24 additions & 0 deletions pkg/cmd/gpucreate/gpucreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type GPUCreateStore interface {
DeleteWorkspace(workspaceID string) (*entity.Workspace, error)
GetAllInstanceTypesWithWorkspaceGroups(orgID string) (*gpusearch.AllInstanceTypesResponse, error)
GetLaunchable(launchableID string) (*store.LaunchableResponse, error)
GetLaunchableLifeCycleScript(launchableID, scriptID string) (*store.LifeCycleScriptResponse, error)
RedeemCouponCode(organizationID string, code string) (*store.RedeemCouponCodeResponse, error)
}

Expand Down Expand Up @@ -394,6 +395,11 @@ func fetchAndDisplayLaunchable(gpuCreateStore GPUCreateStore, t *terminal.Termin
return nil, fmt.Errorf("failed to fetch launchable %q: %w", launchableID, err)
}

// Inline the script body; the launchable GET only returns its id.
if err := inlineLaunchableLifeCycleScript(gpuCreateStore, launchableID, info); err != nil {
return nil, err
}

t.Vprintf("Deploying launchable: %q\n", info.Name)
if info.Description != "" {
t.Vprintf("Description: %s\n", info.Description)
Expand All @@ -410,6 +416,24 @@ func fetchAndDisplayLaunchable(gpuCreateStore GPUCreateStore, t *terminal.Termin
return info, nil
}

func inlineLaunchableLifeCycleScript(gpuCreateStore GPUCreateStore, launchableID string, info *store.LaunchableResponse) error {
if info == nil || info.BuildRequest.VMBuild == nil {
return nil
}
attr := info.BuildRequest.VMBuild.LifeCycleScriptAttr
if attr == nil || attr.ID == "" {
return nil
Comment thread
harshsharmanv marked this conversation as resolved.
}
resp, err := gpuCreateStore.GetLaunchableLifeCycleScript(launchableID, attr.ID)
if err != nil {
return fmt.Errorf("failed to fetch lifecycle script %q for launchable %q: %w", attr.ID, launchableID, err)
}
if resp != nil && resp.Attrs != nil {
attr.Script = resp.Attrs.Script
}
return nil
}

func launchableBuildModeName(info *store.LaunchableResponse) string {
switch {
case info.BuildRequest.CustomContainer != nil:
Expand Down
98 changes: 90 additions & 8 deletions pkg/cmd/gpucreate/gpucreate_test.go
Comment thread
harshsharmanv marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (

// MockGPUCreateStore is a mock implementation of GPUCreateStore for testing
type MockGPUCreateStore struct {
User *entity.User
Org *entity.Organization
Workspaces map[string]*entity.Workspace
CreateError error
CreateErrorTypes map[string]error // Errors for specific instance types
DeleteError error
CreatedWorkspaces []*entity.Workspace
DeletedWorkspaceIDs []string
User *entity.User
Org *entity.Organization
Workspaces map[string]*entity.Workspace
CreateError error
CreateErrorTypes map[string]error // Errors for specific instance types
DeleteError error
CreatedWorkspaces []*entity.Workspace
DeletedWorkspaceIDs []string
FetchedLifeCycleScriptIDs []string
}

func NewMockGPUCreateStore() *MockGPUCreateStore {
Expand Down Expand Up @@ -110,6 +111,13 @@ func (m *MockGPUCreateStore) GetLaunchable(launchableID string) (*store.Launchab
}, nil
}

func (m *MockGPUCreateStore) GetLaunchableLifeCycleScript(launchableID, scriptID string) (*store.LifeCycleScriptResponse, error) {
m.FetchedLifeCycleScriptIDs = append(m.FetchedLifeCycleScriptIDs, scriptID)
return &store.LifeCycleScriptResponse{
Attrs: &store.LifeCycleScriptAttr{ID: scriptID, Script: "echo mock-script"},
}, nil
}

func (m *MockGPUCreateStore) RedeemCouponCode(organizationID string, code string) (*store.RedeemCouponCodeResponse, error) {
return &store.RedeemCouponCodeResponse{}, nil
}
Expand Down Expand Up @@ -665,3 +673,77 @@ func TestPollUntilReadyReportsWorkspaceFailureMessage(t *testing.T) {

assert.ErrorContains(t, err, "instance test failed: unexpected end of JSON input")
}

func TestInlineLaunchableLifeCycleScript(t *testing.T) {
t.Run("fetches and inlines lifecycle script body", func(t *testing.T) {
mockStore := NewMockGPUCreateStore()
info := &store.LaunchableResponse{
BuildRequest: store.LaunchableBuildRequest{
VMBuild: &store.VMBuild{
LifeCycleScriptAttr: &store.LifeCycleScriptAttr{ID: "ls-abc"},
},
},
}

err := inlineLaunchableLifeCycleScript(mockStore, "env-abc", info)

assert.NoError(t, err)
assert.Equal(t, []string{"ls-abc"}, mockStore.FetchedLifeCycleScriptIDs)
assert.Equal(t, "echo mock-script", info.BuildRequest.VMBuild.LifeCycleScriptAttr.Script)
})

t.Run("skips fetch when info is nil", func(t *testing.T) {
mockStore := NewMockGPUCreateStore()

err := inlineLaunchableLifeCycleScript(mockStore, "env-abc", nil)

assert.NoError(t, err)
assert.Empty(t, mockStore.FetchedLifeCycleScriptIDs)
})

t.Run("container build skips lifecycle script fetch", func(t *testing.T) {
mockStore := NewMockGPUCreateStore()
info := &store.LaunchableResponse{
BuildRequest: store.LaunchableBuildRequest{
CustomContainer: &store.CustomContainer{ContainerURL: "nvcr.io/nvidia/test:latest"},
},
}

err := inlineLaunchableLifeCycleScript(mockStore, "env-abc", info)

assert.NoError(t, err)
assert.Empty(t, mockStore.FetchedLifeCycleScriptIDs)
})

t.Run("skips fetch when launchable has no lifecycle script", func(t *testing.T) {
mockStore := NewMockGPUCreateStore()
info := &store.LaunchableResponse{
BuildRequest: store.LaunchableBuildRequest{
VMBuild: &store.VMBuild{ForceJupyterInstall: true},
},
}

err := inlineLaunchableLifeCycleScript(mockStore, "env-abc", info)

assert.NoError(t, err)
assert.Empty(t, mockStore.FetchedLifeCycleScriptIDs)
assert.Nil(t, info.BuildRequest.VMBuild.LifeCycleScriptAttr)
})

t.Run("skips fetch when script ID is empty", func(t *testing.T) {
mockStore := NewMockGPUCreateStore()
info := &store.LaunchableResponse{
BuildRequest: store.LaunchableBuildRequest{
VMBuild: &store.VMBuild{
LifeCycleScriptAttr: &store.LifeCycleScriptAttr{Name: "stale"},
},
},
}

err := inlineLaunchableLifeCycleScript(mockStore, "env-abc", info)

assert.NoError(t, err)
assert.Empty(t, mockStore.FetchedLifeCycleScriptIDs)
assert.Equal(t, "", info.BuildRequest.VMBuild.LifeCycleScriptAttr.Script)
})
}
23 changes: 23 additions & 0 deletions pkg/store/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,29 @@ func (s AuthHTTPStore) GetLaunchable(launchableID string) (*LaunchableResponse,
return &result, nil
}

// LifeCycleScriptResponse holds a lifecycle script with the script body populated.
type LifeCycleScriptResponse struct {
Attrs *LifeCycleScriptAttr `json:"attrs"`
}

// GetLaunchableLifeCycleScript fetches the full lifecycle script for a launchable.
func (s AuthHTTPStore) GetLaunchableLifeCycleScript(launchableID, scriptID string) (*LifeCycleScriptResponse, error) {
var result LifeCycleScriptResponse
res, err := s.authHTTPClient.restyClient.R().
SetHeader("Content-Type", "application/json").
SetQueryParam("envId", launchableID).
SetQueryParam("scriptId", scriptID).
SetResult(&result).
Get("api/launchable/lifecycle-script")
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if res.IsError() {
return nil, NewHTTPResponseError(res)
}
return &result, nil
}

type GetWorkspacesOptions struct {
UserID string
Name string
Expand Down
Loading