Skip to content

Commit 8b56e7b

Browse files
committed
fix(cli): ssh: auto-update workspace
1 parent 059e533 commit 8b56e7b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cli/ssh.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"net/http"
910
"net/url"
1011
"os"
1112
"os/exec"
@@ -575,13 +576,27 @@ func getWorkspaceAndAgent(ctx context.Context, inv *clibase.Invocation, client *
575576
codersdk.WorkspaceStatusStopped,
576577
)
577578
}
578-
// startWorkspace based on the last build parameters.
579+
580+
// Start workspace based on the last build parameters.
581+
// It's possible for a workspace build to fail due to the template requiring starting
582+
// workspaces with the active version.
579583
_, _ = fmt.Fprintf(inv.Stderr, "Workspace was stopped, starting workspace to allow connecting to %q...\n", workspace.Name)
580-
build, err := startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceStart)
584+
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceStart)
585+
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
586+
_, _ = fmt.Fprintln(inv.Stdout, "Unable to start the workspace with template version from last build. The auto-update policy may require you to restart with the current active template version.")
587+
_, err = startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceUpdate)
588+
if err != nil {
589+
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("start workspace with active template version: %w", err)
590+
}
591+
} else if err != nil {
592+
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("start workspace with current template version: %w", err)
593+
}
594+
595+
// Refresh workspace state so that `outdated`, `build`,`template_*` fields are up-to-date.
596+
workspace, err = namedWorkspace(ctx, client, workspaceParts[0])
581597
if err != nil {
582-
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("unable to start workspace: %w", err)
598+
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
583599
}
584-
workspace.LatestBuild = build
585600
}
586601
if workspace.LatestBuild.Job.CompletedAt == nil {
587602
err := cliui.WorkspaceBuild(ctx, inv.Stderr, client, workspace.LatestBuild.ID)

cli/start.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (r *RootCmd) start() *clibase.Cmd {
4949
// It's possible for a workspace build to fail due to the template requiring starting
5050
// workspaces with the active version.
5151
if cerr, ok := codersdk.AsError(err); ok && cerr.StatusCode() == http.StatusForbidden {
52-
_, _ = fmt.Fprintln(inv.Stdout, "Failed to restart with the template version from your last build. Policy may require you to restart with the current active template version.")
52+
_, _ = fmt.Fprintln(inv.Stdout, "Unable to start with the template version from your last build. Policy may require you to restart with the current active template version.")
5353
build, err = startWorkspace(inv, client, workspace, parameterFlags, WorkspaceUpdate)
5454
if err != nil {
5555
return xerrors.Errorf("start workspace with active template version: %w", err)
@@ -79,6 +79,7 @@ func (r *RootCmd) start() *clibase.Cmd {
7979

8080
func buildWorkspaceStartRequest(inv *clibase.Invocation, client *codersdk.Client, workspace codersdk.Workspace, parameterFlags workspaceParameterFlags, action WorkspaceCLIAction) (codersdk.CreateWorkspaceBuildRequest, error) {
8181
version := workspace.LatestBuild.TemplateVersionID
82+
8283
if workspace.AutomaticUpdates == codersdk.AutomaticUpdatesAlways || action == WorkspaceUpdate {
8384
version = workspace.TemplateActiveVersionID
8485
if version != workspace.LatestBuild.TemplateVersionID {

0 commit comments

Comments
 (0)