2023-01-19 23:56:41 +02:00
package cli
import (
"fmt"
2023-10-19 17:16:15 -05:00
"net/http"
2023-01-19 23:56:41 +02:00
"time"
2023-08-09 13:00:25 +02:00
"golang.org/x/xerrors"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
2023-10-19 17:16:15 -05:00
"github.com/coder/pretty"
2024-03-15 11:24:38 -05:00
"github.com/coder/serpent"
2023-01-19 23:56:41 +02:00
)
2024-03-17 09:45:26 -05:00
func ( r * RootCmd ) restart () * serpent . Command {
2024-09-04 14:39:35 +01:00
var (
parameterFlags workspaceParameterFlags
bflags buildFlags
)
2023-07-13 12:56:02 +02:00
2024-03-17 09:45:26 -05:00
cmd := & serpent . Command {
2023-01-19 23:56:41 +02:00
Annotations : workspaceCommand ,
Use : "restart <workspace>" ,
Short : "Restart 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 { cliui . SkipPromptOption ()},
Handler : func ( inv * serpent . Invocation ) error {
2025-09-22 17:14:07 +04:00
client , err := r . InitClient ( inv )
if err != nil {
return err
}
2023-03-23 17:42:20 -05:00
ctx := inv . Context ()
out := inv . Stdout
2023-01-19 23:56:41 +02:00
2026-04-27 12:58:26 +01:00
workspace , err := client . ResolveWorkspace ( inv . Context (), inv . Args [ 0 ])
2023-07-13 12:56:02 +02:00
if err != nil {
return err
}
2024-09-04 14:39:35 +01:00
startReq , err := buildWorkspaceStartRequest ( inv , client , workspace , parameterFlags , bflags , WorkspaceRestart )
2023-01-19 23:56:41 +02:00
if err != nil {
return err
}
2023-07-13 12:56:02 +02:00
_ , err = cliui . Prompt ( inv , cliui . PromptOptions {
2024-01-29 13:56:43 -07:00
Text : "Restart workspace?" ,
2023-07-13 12:56:02 +02:00
IsConfirm : true ,
})
2023-01-19 23:56:41 +02:00
if err != nil {
return err
}
2025-07-29 03:41:49 +02:00
stopParamValues , err := asWorkspaceBuildParameters ( parameterFlags . ephemeralParameters )
if err != nil {
return xerrors . Errorf ( "parse ephemeral parameters: %w" , err )
}
2024-09-04 14:39:35 +01:00
wbr := codersdk . CreateWorkspaceBuildRequest {
2023-01-19 23:56:41 +02:00
Transition : codersdk . WorkspaceTransitionStop ,
2025-07-29 03:41:49 +02:00
// Ephemeral parameters should be passed to both stop and start builds.
// TODO: maybe these values should be sourced from the previous build?
// It has to be manually sourced, as ephemeral parameters do not carry across
// builds.
RichParameterValues : stopParamValues ,
2024-09-04 14:39:35 +01:00
}
if bflags . provisionerLogDebug {
wbr . LogLevel = codersdk . ProvisionerLogLevelDebug
}
build , err := client . CreateWorkspaceBuild ( ctx , workspace . ID , wbr )
2023-01-19 23:56:41 +02:00
if err != nil {
return err
}
2023-11-02 14:41:34 -05:00
2023-01-19 23:56:41 +02:00
err = cliui . WorkspaceBuild ( ctx , out , client , build . ID )
if err != nil {
return err
}
2023-11-02 14:41:34 -05:00
build , err = client . CreateWorkspaceBuild ( ctx , workspace . ID , startReq )
2023-10-19 17:16:15 -05:00
// It's possible for a workspace build to fail due to the template requiring starting
// workspaces with the active version.
2023-12-08 23:03:46 -06:00
if cerr , ok := codersdk . AsError ( err ); ok && cerr . StatusCode () == http . StatusForbidden {
2024-01-23 18:01:44 +01:00
_ , _ = fmt . Fprintln ( inv . Stdout , "Unable to restart the workspace with the template version from the last build. Policy may require you to restart with the current active template version." )
2024-09-04 14:39:35 +01:00
build , err = startWorkspace ( inv , client , workspace , parameterFlags , bflags , WorkspaceUpdate )
2023-10-19 17:16:15 -05:00
if err != nil {
return xerrors . Errorf ( "start workspace with active template version: %w" , err )
}
} else if err != nil {
2023-01-19 23:56:41 +02:00
return err
}
2023-10-19 17:16:15 -05:00
2023-01-19 23:56:41 +02:00
err = cliui . WorkspaceBuild ( ctx , out , client , build . ID )
if err != nil {
return err
}
2023-09-07 17:28:22 -04:00
_ , _ = fmt . Fprintf ( out ,
"\nThe %s workspace has been restarted at %s!\n" ,
pretty . Sprint ( cliui . DefaultStyles . Keyword , workspace . Name ), cliui . Timestamp ( time . Now ()),
)
2023-01-19 23:56:41 +02:00
return nil
},
}
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 () ... )
2023-11-02 14:41:34 -05:00
2023-01-19 23:56:41 +02:00
return cmd
}