|
6 | 6 | "errors"
|
7 | 7 | "fmt"
|
8 | 8 | "io"
|
| 9 | + "net/http" |
9 | 10 | "net/url"
|
10 | 11 | "os"
|
11 | 12 | "os/exec"
|
@@ -575,13 +576,27 @@ func getWorkspaceAndAgent(ctx context.Context, inv *clibase.Invocation, client *
|
575 | 576 | codersdk.WorkspaceStatusStopped,
|
576 | 577 | )
|
577 | 578 | }
|
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. |
579 | 583 | _, _ = 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]) |
581 | 597 | if err != nil {
|
582 |
| - return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("unable to start workspace: %w", err) |
| 598 | + return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err |
583 | 599 | }
|
584 |
| - workspace.LatestBuild = build |
585 | 600 | }
|
586 | 601 | if workspace.LatestBuild.Job.CompletedAt == nil {
|
587 | 602 | err := cliui.WorkspaceBuild(ctx, inv.Stderr, client, workspace.LatestBuild.ID)
|
|
0 commit comments