Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/workspace-stop.go
2497 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"log/slog"
10
"time"
11
12
"github.com/bufbuild/connect-go"
13
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
14
"github.com/gitpod-io/local-app/pkg/prettyprint"
15
"github.com/spf13/cobra"
16
)
17
18
var stopDontWait = false
19
20
// workspaceStopCommand stops to a given workspace
21
var workspaceStopCommand = &cobra.Command{
22
Use: "stop <workspace-id>",
23
Short: "Stop a given workspace",
24
Args: cobra.ExactArgs(1),
25
RunE: func(cmd *cobra.Command, args []string) error {
26
cmd.SilenceUsage = true
27
workspaceID := args[0]
28
29
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)
30
defer cancel()
31
32
gitpod, err := getGitpodClient(ctx)
33
if err != nil {
34
return err
35
}
36
37
slog.Debug("stopping workspace...")
38
wsInfo, err := gitpod.Workspaces.StopWorkspace(ctx, connect.NewRequest(&v1.StopWorkspaceRequest{WorkspaceId: workspaceID}))
39
if err != nil {
40
return err
41
}
42
43
wsPhase := wsInfo.Msg.GetResult().Status.Instance.Status.Phase
44
switch wsPhase {
45
case v1.WorkspaceInstanceStatus_PHASE_STOPPED:
46
slog.Info("workspace is already stopped")
47
return nil
48
case v1.WorkspaceInstanceStatus_PHASE_STOPPING:
49
slog.Info("workspace is already stopping")
50
return nil
51
}
52
53
if stopDontWait {
54
slog.Info("workspace stopping")
55
return nil
56
}
57
58
stream, err := gitpod.Workspaces.StreamWorkspaceStatus(ctx, connect.NewRequest(&v1.StreamWorkspaceStatusRequest{WorkspaceId: workspaceID}))
59
if err != nil {
60
return err
61
}
62
63
// See EXP-909
64
// defer stream.Close()
65
66
slog.Info("waiting for workspace to stop...")
67
68
previousStatus := ""
69
for stream.Receive() {
70
msg := stream.Msg()
71
if msg == nil {
72
slog.Debug("no message received")
73
continue
74
}
75
76
wsPhase = msg.GetResult().Instance.Status.Phase
77
switch wsPhase {
78
case v1.WorkspaceInstanceStatus_PHASE_STOPPED:
79
{
80
slog.Info("workspace stopped")
81
return nil
82
}
83
case v1.WorkspaceInstanceStatus_PHASE_RUNNING:
84
// Skip reporting the "running" status as it is often the initial state and seems confusing to the user.
85
// There is some delay between requesting a workspace to stop and it actually stopping, so we don't want
86
// to report "running" in the meantime.
87
break
88
default:
89
{
90
currentStatus := prettyprint.FormatWorkspacePhase(wsPhase)
91
if currentStatus != previousStatus {
92
slog.Info("workspace status: " + currentStatus)
93
previousStatus = currentStatus
94
}
95
}
96
}
97
}
98
99
if err := stream.Err(); err != nil {
100
return err
101
}
102
103
return nil
104
},
105
}
106
107
func init() {
108
workspaceCmd.AddCommand(workspaceStopCommand)
109
workspaceStopCommand.Flags().BoolVarP(&stopDontWait, "dont-wait", "d", false, "do not wait for workspace to fully stop, only initialize")
110
}
111
112