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
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 == "" || attr.Script != "" {
return nil
}
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
6 changes: 6 additions & 0 deletions pkg/cmd/gpucreate/gpucreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func (m *MockGPUCreateStore) GetLaunchable(launchableID string) (*store.Launchab
}, nil
}

func (m *MockGPUCreateStore) GetLaunchableLifeCycleScript(launchableID, scriptID string) (*store.LifeCycleScriptResponse, error) {
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
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