- Notifications
You must be signed in to change notification settings - Fork928
feat: restart stopped workspaces on ssh command#11050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
55bec81
f422f47
0d12948
2c6a501
dc363a4
1c6669d
d87dd79
6bab0e9
e0e9de5
ae7de26
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -14,6 +14,7 @@ import ( | ||
"sync" | ||
"time" | ||
"github.com/coder/retry" | ||
"github.com/gen2brain/beeep" | ||
"github.com/gofrs/flock" | ||
"github.com/google/uuid" | ||
@@ -34,7 +35,6 @@ import ( | ||
"github.com/coder/coder/v2/coderd/util/ptr" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/cryptorand" | ||
) | ||
var ( | ||
@@ -44,15 +44,16 @@ var ( | ||
func (r *RootCmd) ssh() *clibase.Cmd { | ||
var ( | ||
stdio bool | ||
forwardAgent bool | ||
forwardGPG bool | ||
identityAgent string | ||
wsPollInterval time.Duration | ||
waitEnum string | ||
noWait bool | ||
logDirPath string | ||
remoteForward string | ||
disableAutostart bool | ||
) | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
@@ -143,7 +144,7 @@ func (r *RootCmd) ssh() *clibase.Cmd { | ||
} | ||
} | ||
workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, inv, client,!disableAutostart,codersdk.Me, inv.Args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
@@ -459,6 +460,7 @@ func (r *RootCmd) ssh() *clibase.Cmd { | ||
FlagShorthand: "R", | ||
Value: clibase.StringOf(&remoteForward), | ||
}, | ||
sshDisableAutostartOption(clibase.BoolOf(&disableAutostart)), | ||
} | ||
return cmd | ||
} | ||
@@ -530,9 +532,9 @@ startWatchLoop: | ||
} | ||
// getWorkspaceAgent returns the workspace and agent selected using either the | ||
// `<workspace>[.<agent>]` syntax via `in`. | ||
//If autoStart is true, the workspace will be started if it is not already running. | ||
func getWorkspaceAndAgent(ctx context.Context, inv *clibase.Invocation, client *codersdk.Client,autostart bool,userID string, in string) (codersdk.Workspace, codersdk.WorkspaceAgent, error) { //nolint:revive | ||
var ( | ||
workspace codersdk.Workspace | ||
workspaceParts = strings.Split(in, ".") | ||
@@ -545,7 +547,35 @@ func getWorkspaceAndAgent(ctx context.Context, inv *clibase.Invocation, client * | ||
} | ||
if workspace.LatestBuild.Transition != codersdk.WorkspaceTransitionStart { | ||
if !autostart { | ||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.New("workspace must be in start transition to ssh") | ||
} | ||
// Autostart the workspace for the user. | ||
// For some failure modes, return a better message. | ||
if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionDelete { | ||
// Any sort of deleting status, we should reject with a nicer error. | ||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q is deleted", workspace.Name) | ||
} | ||
if workspace.LatestBuild.Job.Status == codersdk.ProvisionerJobFailed { | ||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, | ||
xerrors.Errorf("workspace %q is in failed state, unable to autostart the workspace", workspace.Name) | ||
} | ||
// The workspace needs to be stopped before we can start it. | ||
// It cannot be in any pending or failed state. | ||
if workspace.LatestBuild.Status != codersdk.WorkspaceStatusStopped { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. A case that I would personally like for to work is that I can ssh into a stopping workspace and have it wait until the stop is complete and then either issue the start or wait for a start to complete. No action necessary for this PR, though. Just writing this down as a food for thought. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Things could be improved to handle more cases, but that felt a bit overkill atm. | ||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, | ||
xerrors.Errorf("workspace must be in start transition to ssh, was unable to autostart as the last build job is %q, expected %q", | ||
workspace.LatestBuild.Status, | ||
codersdk.WorkspaceStatusStopped, | ||
) | ||
} | ||
// startWorkspace based on the last build parameters. | ||
_, _ = fmt.Fprintf(inv.Stderr, "Workspace was stopped, starting workspace to allow connecting to %q...\n", workspace.Name) | ||
build, err := startWorkspace(inv, client, workspace, workspaceParameterFlags{}, WorkspaceStart) | ||
if err != nil { | ||
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, xerrors.Errorf("unable to start workspace: %w", err) | ||
} | ||
workspace.LatestBuild = build | ||
} | ||
if workspace.LatestBuild.Job.CompletedAt == nil { | ||
err := cliui.WorkspaceBuild(ctx, inv.Stderr, client, workspace.LatestBuild.ID) | ||
@@ -915,3 +945,13 @@ func (c *rawSSHCopier) Close() error { | ||
} | ||
return err | ||
} | ||
func sshDisableAutostartOption(src *clibase.Bool) clibase.Option { | ||
return clibase.Option{ | ||
Flag: "disable-autostart", | ||
Description: "Disable starting the workspace automatically when connecting via SSH.", | ||
Env: "CODER_SSH_DISABLE_AUTOSTART", | ||
Value: src, | ||
Default: "false", | ||
} | ||
} |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.