2022-03-22 13:17:50 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-24 12:30:22 -04:00
|
|
|
"fmt"
|
2022-03-22 13:17:50 -06:00
|
|
|
"time"
|
|
|
|
|
|
2026-10-07 16:17:12 -04:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
2024-12-02 20:54:32 +00:00
|
|
|
"github.com/coder/coder/v2/cli/cliutil"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2024-03-15 11:24:38 -05:00
|
|
|
"github.com/coder/serpent"
|
2022-03-22 13:17:50 -06:00
|
|
|
)
|
|
|
|
|
|
2024-03-17 09:45:26 -05:00
|
|
|
func (r *RootCmd) stop() *serpent.Command {
|
2024-09-04 14:39:35 +01:00
|
|
|
var bflags buildFlags
|
2026-10-07 16:17:12 -04:00
|
|
|
var templateVersion string
|
2024-03-17 09:45:26 -05:00
|
|
|
cmd := &serpent.Command{
|
2026-09-29 15:27:30 -07:00
|
|
|
Annotations: serpent.Annotations(workspaceCommand).Mark(annotationClientSessionID, "").Mark(annotationFlightRecorder, ""),
|
2022-05-09 17:42:02 -05:00
|
|
|
Use: "stop <workspace>",
|
2022-09-19 11:36:18 -05:00
|
|
|
Short: "Stop a workspace",
|
2024-03-15 11:24:38 -05:00
|
|
|
Middleware: serpent.Chain(
|
|
|
|
|
serpent.RequireNArgs(1),
|
2023-03-23 17:42:20 -05:00
|
|
|
),
|
2024-03-15 11:24:38 -05:00
|
|
|
Options: serpent.OptionSet{
|
2026-10-07 16:17:12 -04:00
|
|
|
{
|
|
|
|
|
Flag: "template-version",
|
2026-10-14 11:52:36 -04:00
|
|
|
Description: "Stop with a version of the workspace template, by name or ID. Defaults to the workspace's current version.",
|
2026-10-07 16:17:12 -04:00
|
|
|
Value: serpent.StringOf(&templateVersion),
|
|
|
|
|
},
|
2023-03-23 17:42:20 -05:00
|
|
|
cliui.SkipPromptOption(),
|
|
|
|
|
},
|
2024-03-15 11:24:38 -05:00
|
|
|
Handler: func(inv *serpent.Invocation) error {
|
2025-09-22 17:14:07 +04:00
|
|
|
client, err := r.InitClient(inv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-09-29 15:27:30 -07:00
|
|
|
// The invocation logger records debug detail and emits it to stderr
|
|
|
|
|
// only if the command fails (see flightRecorderMiddleware).
|
|
|
|
|
client.SetLogger(inv.Logger)
|
2025-09-22 17:14:07 +04:00
|
|
|
|
|
|
|
|
_, err = cliui.Prompt(inv, cliui.PromptOptions{
|
2022-05-20 10:59:04 -05:00
|
|
|
Text: "Confirm stop workspace?",
|
|
|
|
|
IsConfirm: true,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 12:58:26 +01:00
|
|
|
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
2022-03-22 13:17:50 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-12-02 20:54:32 +00:00
|
|
|
|
2026-10-14 11:52:36 -04:00
|
|
|
versionID, err := resolveTemplateVersionID(inv.Context(), client, workspace.TemplateID, workspace.TemplateName, templateVersion)
|
2026-10-07 16:17:12 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
build, err := stopWorkspace(inv, client, workspace, bflags, versionID)
|
2022-03-22 13:17:50 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-06-24 12:30:22 -04:00
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
err = cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID)
|
2022-06-24 12:30:22 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 17:28:22 -04:00
|
|
|
_, _ = fmt.Fprintf(
|
|
|
|
|
inv.Stdout,
|
2025-06-23 09:12:37 +01:00
|
|
|
"\nThe %s workspace has been stopped at %s!\n",
|
|
|
|
|
cliui.Keyword(workspace.Name),
|
2023-09-07 17:28:22 -04:00
|
|
|
cliui.Timestamp(time.Now()),
|
|
|
|
|
)
|
2022-06-24 12:30:22 -04:00
|
|
|
return nil
|
2022-03-22 13:17:50 -06:00
|
|
|
},
|
|
|
|
|
}
|
2024-09-04 14:39:35 +01:00
|
|
|
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
|
|
|
|
|
|
2022-05-20 10:59:04 -05:00
|
|
|
return cmd
|
2022-03-22 13:17:50 -06:00
|
|
|
}
|
2025-06-23 09:12:37 +01:00
|
|
|
|
2026-10-07 16:17:12 -04:00
|
|
|
func stopWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, bflags buildFlags, templateVersionID uuid.UUID) (codersdk.WorkspaceBuild, error) {
|
2025-06-23 09:12:37 +01:00
|
|
|
if workspace.LatestBuild.Job.Status == codersdk.ProvisionerJobPending {
|
|
|
|
|
// cliutil.WarnMatchedProvisioners also checks if the job is pending
|
|
|
|
|
// but we still want to avoid users spamming multiple builds that will
|
|
|
|
|
// not be picked up.
|
|
|
|
|
cliui.Warn(inv.Stderr, "The workspace is already stopping!")
|
|
|
|
|
cliutil.WarnMatchedProvisioners(inv.Stderr, workspace.LatestBuild.MatchedProvisioners, workspace.LatestBuild.Job)
|
|
|
|
|
if _, err := cliui.Prompt(inv, cliui.PromptOptions{
|
|
|
|
|
Text: "Enqueue another stop?",
|
|
|
|
|
IsConfirm: true,
|
|
|
|
|
Default: cliui.ConfirmNo,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return codersdk.WorkspaceBuild{}, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wbr := codersdk.CreateWorkspaceBuildRequest{
|
2026-10-07 16:17:12 -04:00
|
|
|
Transition: codersdk.WorkspaceTransitionStop,
|
|
|
|
|
TemplateVersionID: templateVersionID,
|
2025-06-23 09:12:37 +01:00
|
|
|
}
|
|
|
|
|
if bflags.provisionerLogDebug {
|
|
|
|
|
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
|
|
|
|
|
}
|
|
|
|
|
return client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
|
|
|
|
|
}
|