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"
|
|
|
|
|
|
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
|
2023-03-23 17:42:20 -05:00
|
|
|
client := new(codersdk.Client)
|
2024-03-17 09:45:26 -05:00
|
|
|
cmd := &serpent.Command{
|
2022-05-09 17:42:02 -05:00
|
|
|
Annotations: workspaceCommand,
|
|
|
|
|
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
|
|
|
r.InitClient(client),
|
|
|
|
|
),
|
2024-03-15 11:24:38 -05:00
|
|
|
Options: serpent.OptionSet{
|
2023-03-23 17:42:20 -05:00
|
|
|
cliui.SkipPromptOption(),
|
|
|
|
|
},
|
2024-03-15 11:24:38 -05:00
|
|
|
Handler: func(inv *serpent.Invocation) error {
|
2023-03-23 17:42:20 -05: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
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
2022-03-22 13:17:50 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-12-02 20:54:32 +00:00
|
|
|
|
2025-06-23 09:12:37 +01:00
|
|
|
build, err := stopWorkspace(inv, client, workspace, bflags)
|
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
|
|
|
|
|
|
|
|
func stopWorkspace(inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace, bflags buildFlags) (codersdk.WorkspaceBuild, error) {
|
|
|
|
|
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{
|
|
|
|
|
Transition: codersdk.WorkspaceTransitionStop,
|
|
|
|
|
}
|
|
|
|
|
if bflags.provisionerLogDebug {
|
|
|
|
|
wbr.LogLevel = codersdk.ProvisionerLogLevelDebug
|
|
|
|
|
}
|
|
|
|
|
return client.CreateWorkspaceBuild(inv.Context(), workspace.ID, wbr)
|
|
|
|
|
}
|