Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/workspaces-start.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
"encoding/json"
10
"fmt"
11
"os"
12
"strings"
13
14
"github.com/alecthomas/repr"
15
"github.com/spf13/cobra"
16
17
"github.com/gitpod-io/gitpod/common-go/log"
18
"github.com/gitpod-io/gitpod/common-go/namegen"
19
"github.com/gitpod-io/gitpod/ws-manager/api"
20
)
21
22
var startWorkspaceReq = api.StartWorkspaceRequest{
23
Metadata: &api.WorkspaceMetadata{},
24
}
25
26
// workspacesStartCmd starts a new workspace
27
var workspacesStartCmd = &cobra.Command{
28
Use: "start <id> <configfile.json>",
29
Short: "starts a new workspace",
30
Args: cobra.ExactArgs(2),
31
Run: func(cmd *cobra.Command, args []string) {
32
ctx, cancel := context.WithCancel(context.Background())
33
defer cancel()
34
35
fc, err := os.ReadFile(args[1])
36
if err != nil {
37
log.WithError(err).Fatal("cannot read workspace spec")
38
}
39
var workspace api.StartWorkspaceSpec
40
if err := json.Unmarshal(fc, &workspace); err != nil {
41
log.WithError(err).Fatal("cannot parse workspace spec")
42
}
43
44
count, _ := cmd.Flags().GetInt("count")
45
id := args[0]
46
workspaceID := startWorkspaceReq.Metadata.MetaId
47
setServicePrefix := startWorkspaceReq.ServicePrefix == ""
48
startWorkspaceReq.Spec = &workspace
49
50
tpe, _ := cmd.Flags().GetString("type")
51
tpeidx, ok := api.WorkspaceType_value[strings.ToUpper(tpe)]
52
if !ok {
53
log.Fatalf("unknown workspace type: %s", tpe)
54
}
55
startWorkspaceReq.Type = api.WorkspaceType(tpeidx)
56
57
conn, client, err := getWorkspacesClient(ctx)
58
if err != nil {
59
log.WithError(err).Fatal("cannot connect")
60
}
61
defer conn.Close()
62
63
for i := 0; i < count; i++ {
64
startWorkspaceReq.Id = id
65
if count > 1 {
66
startWorkspaceReq.Id = fmt.Sprintf("%s%04d", id, i)
67
startWorkspaceReq.Metadata.MetaId = fmt.Sprintf("%s%04d", workspaceID, i)
68
}
69
if setServicePrefix {
70
startWorkspaceReq.ServicePrefix = startWorkspaceReq.Id
71
}
72
73
resp, err := client.StartWorkspace(ctx, &startWorkspaceReq)
74
if err != nil {
75
log.WithError(err).Fatal("error during RPC call")
76
}
77
if count <= 1 {
78
repr.Println(resp)
79
continue
80
}
81
82
log.WithField("id", startWorkspaceReq.Id).WithField("url", resp.Url).Info("workspace started")
83
}
84
85
},
86
}
87
88
func init() {
89
wsid, err := namegen.GenerateWorkspaceID()
90
if err != nil {
91
log.WithError(err).Fatal("cannot generate workspace id")
92
return
93
}
94
95
workspacesCmd.AddCommand(workspacesStartCmd)
96
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.ServicePrefix, "service-prefix", "", "use a service prefix different from the workspace ID")
97
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.Metadata.Owner, "owner", "gpctl", "set the workspace owner")
98
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.Metadata.MetaId, "workspace-id", wsid, "set the workspace ID")
99
workspacesStartCmd.Flags().IntP("count", "n", 1, "start multiple workspaces with the same spec - useful for load tests")
100
101
var types []string
102
for k := range api.WorkspaceType_value {
103
types = append(types, strings.ToLower(k))
104
}
105
workspacesStartCmd.Flags().String("type", "regular", fmt.Sprintf("type of the workspace - valid values are: %s", strings.Join(types, ", ")))
106
}
107
108