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
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ ifdef env
ifeq ($(env),local-stack)
@echo 'export BREV_API_URL="http://localhost:8080"' >> brev
@echo 'export BREV_PUBLIC_API_URL="http://localhost:8081"' >> brev
@echo 'export BREV_GRPC_URL="localhost:8081"' >> brev
else
@echo 'export BREV_API_URL="https://bd.$(env).brev.nvidia.com"' >> brev
@echo 'export BREV_PUBLIC_API_URL="https://api.$(env).brev.nvidia.com"' >> brev
@echo 'export BREV_GRPC_URL="api.$(env).brev.nvidia.com:443"' >> brev
endif
@echo 'exec "$$(cd "$$(dirname "$$0")" && pwd)/brev-local" "$$@"' >> brev
@chmod +x brev
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/brevdev/brev-cli
go 1.25.0

require (
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20260915224432-6a5d0c858507.1
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20260915224432-6a5d0c858507.2
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20261002013339-6f4f902cdf2d.1
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20261002013339-6f4f902cdf2d.2
connectrpc.com/connect v1.21.0
github.com/NVIDIA/go-nvml v0.13.0-1
github.com/alessio/shellescape v1.4.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20260915224432-6a5d0c858507.1 h1:CFXTDUzEoMYQzJXAG+hnMO5Cs6h3zVq734k/VnyWuCM=
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20260915224432-6a5d0c858507.1/go.mod h1:dS34xJi+QXqWDdKwUHTBfzkcbQFNK3u4UH3F3L6izx0=
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20260915224432-6a5d0c858507.2 h1:CjGsO8kcUR0YcKzjKy/L5m2jIhnNW6VW6ArYliA2p6o=
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20260915224432-6a5d0c858507.2/go.mod h1:SBVLUYGc/4vgMlhOJL5SkwIrSh/FL9EEimlpI8zQSXo=
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20261002013339-6f4f902cdf2d.1 h1:6YxQ+ovFXTH0qhavCXrRAsa/+CgyJviIBBPvuar5fAU=
buf.build/gen/go/brevdev/devplane/connectrpc/go v1.21.0-20261002013339-6f4f902cdf2d.1/go.mod h1:0EoTlhUve4UaoJRDO8DXLA79x0HYGLnFchd8ITsIn/4=
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20261002013339-6f4f902cdf2d.2 h1:FoyCt/oxRt8ZFE4JmtAclKTlGOT7zFbyyhY/xeEmyjQ=
buf.build/gen/go/brevdev/devplane/protocolbuffers/go v1.36.12-20261002013339-6f4f902cdf2d.2/go.mod h1:SBVLUYGc/4vgMlhOJL5SkwIrSh/FL9EEimlpI8zQSXo=
buf.build/gen/go/brevdev/protoc-gen-gotag/protocolbuffers/go v1.36.12-20220906235457-8b4922735da5.2 h1:W5SVs96P8ZgLElYzL93rJu+S+XmkdGQSaVWzPvR5lvg=
buf.build/gen/go/brevdev/protoc-gen-gotag/protocolbuffers/go v1.36.12-20220906235457-8b4922735da5.2/go.mod h1:SacJAYqnICCQAsBA46cSA/hxhqhxYkiYzseucf6/fhQ=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/deregister/deregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/externalnode"
"github.com/brevdev/brev-cli/pkg/sshcert"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/sudo"
"github.com/brevdev/brev-cli/pkg/terminal"
)
Expand Down Expand Up @@ -141,7 +142,7 @@ type mockNodeClientFactory struct {
}

func (m mockNodeClientFactory) NewNodeClient(provider externalnode.TokenProvider, _ string) nodev1connect.ExternalNodeServiceClient {
return register.NewNodeServiceClient(provider, m.serverURL)
return store.NewDevPlaneClient(provider, m.serverURL).ExternalNodes
}

type mockSSHKeyRemover struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/enablessh/enablessh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/brevdev/brev-cli/pkg/cmd/register"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/externalnode"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
)

Expand Down Expand Up @@ -222,7 +223,7 @@ func Test_RemoveAuthorizedKey_DoesNotRemoveOtherBrevKeys(t *testing.T) {
type mockNodeClientFactory struct{ serverURL string }

func (m mockNodeClientFactory) NewNodeClient(provider externalnode.TokenProvider, _ string) nodev1connect.ExternalNodeServiceClient {
return register.NewNodeServiceClient(provider, m.serverURL)
return store.NewDevPlaneClient(provider, m.serverURL).ExternalNodes
}

type mockEnableSSHStore struct {
Expand Down
118 changes: 89 additions & 29 deletions pkg/cmd/gpucreate/gpucreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package gpucreate

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand/v2"
Expand All @@ -20,11 +22,14 @@ import (
"time"
"unicode"

devplanev1 "buf.build/gen/go/brevdev/devplane/protocolbuffers/go/devplaneapi/v1"

"github.com/brevdev/brev-cli/pkg/auth"
"github.com/brevdev/brev-cli/pkg/cmd/gpusearch"
"github.com/brevdev/brev-cli/pkg/cmd/util"
"github.com/brevdev/brev-cli/pkg/config"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/environment"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/featureflag"
"github.com/brevdev/brev-cli/pkg/names"
Expand Down Expand Up @@ -109,7 +114,7 @@ type GPUCreateStore interface {
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetAuthTokens() (*entity.AuthTokens, error)
GetCurrentUser() (*entity.User, error)
GetWorkspace(workspaceID string) (*entity.Workspace, error)
GetEnvironment(ctx context.Context, environmentID string) (*devplanev1.Environment, error)
CreateWorkspace(organizationID string, options *store.CreateWorkspacesOptions) (*entity.Workspace, error)
DeleteWorkspace(workspaceID string) (*entity.Workspace, error)
GetAllInstanceTypesWithCloudCreds(orgID string) (*gpusearch.AllInstanceTypesResponse, error)
Expand Down Expand Up @@ -266,7 +271,7 @@ func NewCmdGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore) *cobra
return runDryRun(t, gpuCreateStore, opts.InstanceTypes, &filters)
}

return RunGPUCreate(t, gpuCreateStore, opts)
return RunGPUCreate(cmd.Context(), t, gpuCreateStore, opts)
},
}

Expand Down Expand Up @@ -1130,25 +1135,53 @@ func (c *createContext) cleanupExtraInstances(workspaces []*entity.Workspace) []
return workspaces[:c.opts.Count]
}

// waitForInstances waits for all instances to be ready
func (c *createContext) waitForInstances(workspaces []*entity.Workspace) {
// errReadyTimeout marks a pollUntilReady timeout. It is non-fatal: waiting is a
// convenience and the instance may still finish provisioning after the window.
var errReadyTimeout = errors.New("timeout waiting for instance to be ready")

type workspaceFailedError struct{ message string }

func (e *workspaceFailedError) Error() string { return e.message }

// waitForInstances blocks until each workspace is ready. It returns an error
// when any instance reaches a terminal failure so the command exits non-zero;
// timeouts and transient polling errors are reported but remain non-fatal.
func (c *createContext) waitForInstances(parentCtx context.Context, workspaces []*entity.Workspace) error {
if c.opts.Detached {
return
return nil
}

c.logf("\nWaiting for instance(s) to be ready...\n")
c.logf("You can safely ctrl+c to exit\n")

var failed []string
for _, ws := range workspaces {
err := c.pollUntilReady(ws.ID)
if err != nil {
if strings.Contains(err.Error(), "timeout waiting") {
c.logf(" %s: Timeout waiting for ready state\n", ws.Name)
} else {
c.logf(" %s: %s\n", ws.Name, c.colorize(err.Error(), c.t.Red))
}
}
err := c.pollUntilReady(parentCtx, ws.ID)
var failedErr *workspaceFailedError
switch {
case err == nil:
case errors.Is(err, errReadyTimeout):
c.logf(" %s: Timeout waiting for ready state\n", ws.Name)
case parentCtx.Err() != nil:
// The parent context expired or was canceled while waiting.
// Propagate instead of logging, or a dead parent deadline would
// still produce a success summary.
return err
case errors.As(err, &failedErr):
c.logf(" %s: %s\n", ws.Name, c.colorize(err.Error(), c.t.Red))
failed = append(failed, err.Error())
default:
c.logf(" %s: %s\n", ws.Name, c.colorize(err.Error(), c.t.Red))
}
}

if len(failed) > 0 {
return breverrors.NewValidationError(fmt.Sprintf(
"%d/%d instance(s) failed to become ready: %s",
len(failed), len(workspaces), strings.Join(failed, "; "),
))
}
return nil
}

// printSummary outputs the final creation summary
Expand All @@ -1172,8 +1205,9 @@ func (c *createContext) printSummary(workspaces []*entity.Workspace) {
}
}

// RunGPUCreate executes the GPU create with retry logic
func RunGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore, opts GPUCreateOptions) error {
// RunGPUCreate executes the GPU create with retry logic. parentCtx carries the
// caller's deadline into the readiness polls.
func RunGPUCreate(parentCtx context.Context, t *terminal.Terminal, gpuCreateStore GPUCreateStore, opts GPUCreateOptions) error {
ctx, err := newCreateContext(t, gpuCreateStore, opts)
if err != nil {
return err
Expand Down Expand Up @@ -1230,7 +1264,9 @@ func RunGPUCreate(t *terminal.Terminal, gpuCreateStore GPUCreateStore, opts GPUC
}

successfulWorkspaces = ctx.cleanupExtraInstances(successfulWorkspaces)
ctx.waitForInstances(successfulWorkspaces)
if err := ctx.waitForInstances(parentCtx, successfulWorkspaces); err != nil {
return err
}
ctx.printSummary(successfulWorkspaces)

return nil
Expand Down Expand Up @@ -1609,31 +1645,55 @@ func resolveWorkspaceUserOptions(options *store.CreateWorkspacesOptions, user *e
}

// pollUntilReady waits for a workspace to reach the running state
func (c *createContext) pollUntilReady(wsID string) error {
func (c *createContext) pollUntilReady(parentCtx context.Context, wsID string) error {
deadline := time.Now().Add(c.opts.Timeout)

for time.Now().Before(deadline) {
ws, err := c.store.GetWorkspace(wsID)
pollCtx, cancel := context.WithDeadline(parentCtx, deadline)
defer cancel()

for {
// An expired parent deadline (or cancellation) must propagate as an
// error, not be classified as this instance's nonfatal readiness
// timeout — the caller's context ran out, not the per-instance wait.
if err := parentCtx.Err(); err != nil {
return breverrors.WrapAndTrace(err)
}
if err := pollCtx.Err(); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return errReadyTimeout
}
return breverrors.WrapAndTrace(err)
}

env, err := c.store.GetEnvironment(pollCtx, wsID)
if err != nil {
return breverrors.WrapAndTrace(err)
}

if ws.Status == entity.Running {
c.logf(" %s: %s\n", ws.Name, c.colorize("Ready", c.t.Green))
name := wsID

switch {
case env.GetStatus() == devplanev1.EnvironmentStatus_ENVIRONMENT_STATUS_RUNNING:
c.logf(" %s: %s\n", name, c.colorize("Ready", c.t.Green))
return nil
case environment.IsFailed(env):
return &workspaceFailedError{message: instanceFailureMessage(name, environment.FailureMessage(env))}
}

if ws.Status == entity.Failure {
if ws.StatusMessage != "" {
return breverrors.NewValidationError(fmt.Sprintf("instance %s failed: %s", ws.Name, ws.StatusMessage))
}
return breverrors.NewValidationError(fmt.Sprintf("instance %s failed", ws.Name))
// Wait for the next poll interval; cancellation and deadline are
// handled by the pollCtx check at the top of the loop.
select {
case <-time.After(5 * time.Second):
case <-pollCtx.Done():
}

time.Sleep(5 * time.Second)
}
}

return breverrors.NewValidationError("timeout waiting for instance to be ready")
func instanceFailureMessage(name, statusMessage string) string {
if statusMessage != "" {
return fmt.Sprintf("instance %s failed: %s", name, statusMessage)
}
return fmt.Sprintf("instance %s failed", name)
}

// displayConnectBreadCrumb shows connection instructions
Expand Down
Loading
Loading