2022-03-22 13:17:50 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2023-08-09 13:00:25 +02:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2025-06-23 09:12:37 +01:00
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
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) update() *serpent.Command {
|
2024-09-04 14:39:35 +01:00
|
|
|
var (
|
|
|
|
|
parameterFlags workspaceParameterFlags
|
|
|
|
|
bflags buildFlags
|
|
|
|
|
)
|
2024-03-17 09:45:26 -05:00
|
|
|
cmd := &serpent.Command{
|
2026-09-02 12:31:01 -07:00
|
|
|
Annotations: serpent.Annotations(workspaceCommand).Mark(annotationClientSessionID, ""),
|
2022-08-01 19:46:50 -05:00
|
|
|
Use: "update <workspace>",
|
2025-06-23 09:12:37 +01:00
|
|
|
Short: "Will update and start a given workspace if it is out of date. If the workspace is already running, it will be stopped first.",
|
2023-03-23 17:42:20 -05:00
|
|
|
Long: "Use --always-prompt to change the parameter values of the 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
|
|
|
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-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-10-16 23:54:12 +02:00
|
|
|
if !workspace.Outdated && !parameterFlags.promptRichParameters && !parameterFlags.promptEphemeralParameters && len(parameterFlags.ephemeralParameters) == 0 {
|
2024-10-10 12:56:53 +02:00
|
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Workspace is up-to-date.\n")
|
2022-03-22 13:17:50 -06:00
|
|
|
return nil
|
|
|
|
|
}
|
2023-08-09 13:00:25 +02:00
|
|
|
|
2025-06-23 09:12:37 +01:00
|
|
|
// #17840: If the workspace is already running, we will stop it before
|
|
|
|
|
// updating. Simply performing a new start transition may not work if the
|
|
|
|
|
// template specifies ignore_changes.
|
|
|
|
|
if workspace.LatestBuild.Transition == codersdk.WorkspaceTransitionStart {
|
|
|
|
|
build, err := stopWorkspace(inv, client, workspace, bflags)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("stop workspace: %w", err)
|
|
|
|
|
}
|
|
|
|
|
// Wait for the stop to complete.
|
|
|
|
|
if err := cliui.WorkspaceBuild(inv.Context(), inv.Stdout, client, build.ID); err != nil {
|
|
|
|
|
return xerrors.Errorf("wait for stop: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 14:39:35 +01:00
|
|
|
build, err := startWorkspace(inv, client, workspace, parameterFlags, bflags, WorkspaceUpdate)
|
2023-08-09 13:00:25 +02:00
|
|
|
if err != nil {
|
2023-11-02 14:41:34 -05:00
|
|
|
return xerrors.Errorf("start workspace: %w", err)
|
2023-08-09 13:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
logs, closer, err := client.WorkspaceBuildLogsAfter(inv.Context(), build.ID, 0)
|
2022-03-22 13:17:50 -06:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-09-22 13:26:05 -05:00
|
|
|
defer closer.Close()
|
2022-03-22 13:17:50 -06:00
|
|
|
for {
|
|
|
|
|
log, ok := <-logs
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-03-23 17:42:20 -05:00
|
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Output: %s\n", log.Output)
|
2022-03-22 13:17:50 -06:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-06-27 11:19:10 -05:00
|
|
|
|
2023-11-02 14:41:34 -05:00
|
|
|
cmd.Options = append(cmd.Options, parameterFlags.allOptions()...)
|
2024-09-04 14:39:35 +01:00
|
|
|
cmd.Options = append(cmd.Options, bflags.cliOptions()...)
|
2022-06-27 11:19:10 -05:00
|
|
|
return cmd
|
2022-03-22 13:17:50 -06:00
|
|
|
}
|