Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/workspaces-stop.go
2498 views
1
// Copyright (c) 2020 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
"strings"
10
11
"github.com/spf13/cobra"
12
13
"github.com/gitpod-io/gitpod/common-go/log"
14
"github.com/gitpod-io/gitpod/ws-manager/api"
15
)
16
17
// workspacesStopCmd represents the describe command
18
var workspacesStopCmd = &cobra.Command{
19
Use: "stop <workspaceID>",
20
Short: "stops a workspace",
21
Args: cobra.ExactArgs(1),
22
Run: func(cmd *cobra.Command, args []string) {
23
ctx, cancel := context.WithCancel(context.Background())
24
defer cancel()
25
26
policy := api.StopWorkspacePolicy_NORMALLY
27
stopImmediately, _ := cmd.Flags().GetBool("immediately")
28
if stopImmediately {
29
policy = api.StopWorkspacePolicy_IMMEDIATELY
30
}
31
32
conn, client, err := getWorkspacesClient(ctx)
33
if err != nil {
34
log.WithError(err).Fatal("cannot connect")
35
}
36
defer conn.Close()
37
38
instanceID := args[0]
39
if strings.ContainsAny(instanceID, ".") || strings.HasPrefix(instanceID, "http://") || strings.HasPrefix(instanceID, "https://") {
40
s, err := getStatusByURL(ctx, client, instanceID)
41
if err != nil {
42
log.Fatal(err)
43
}
44
instanceID = s.Id
45
}
46
47
resp, err := client.StopWorkspace(ctx, &api.StopWorkspaceRequest{
48
Id: instanceID,
49
Policy: policy,
50
})
51
if err != nil {
52
log.WithError(err).Fatal("error during RPC call")
53
}
54
55
err = getOutputFormat("stopping\n", "").Print(resp)
56
if err != nil {
57
log.Fatal(err)
58
}
59
},
60
}
61
62
func init() {
63
workspacesCmd.AddCommand(workspacesStopCmd)
64
workspacesStopCmd.Flags().Bool("immediately", false, "stops a workspace immediately we no regard for backups or clean shutdown")
65
}
66
67