generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
206 lines (182 loc) · 6.65 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package create
import (
"fmt"
"strings"
"time"
"github.com/brevdev/brev-cli/pkg/cmd/util"
"github.com/brevdev/brev-cli/pkg/config"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/featureflag"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/spf13/cobra"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
)
var (
createLong = "Create a new Brev machine"
createExample = `
brev create <name>
`
// instanceTypes = []string{"p4d.24xlarge", "p3.2xlarge", "p3.8xlarge", "p3.16xlarge", "p3dn.24xlarge", "p2.xlarge", "p2.8xlarge", "p2.16xlarge", "g5.xlarge", "g5.2xlarge", "g5.4xlarge", "g5.8xlarge", "g5.16xlarge", "g5.12xlarge", "g5.24xlarge", "g5.48xlarge", "g5g.xlarge", "g5g.2xlarge", "g5g.4xlarge", "g5g.8xlarge", "g5g.16xlarge", "g5g.metal", "g4dn.xlarge", "g4dn.2xlarge", "g4dn.4xlarge", "g4dn.8xlarge", "g4dn.16xlarge", "g4dn.12xlarge", "g4dn.metal", "g4ad.xlarge", "g4ad.2xlarge", "g4ad.4xlarge", "g4ad.8xlarge", "g4ad.16xlarge", "g3s.xlarge", "g3.4xlarge", "g3.8xlarge", "g3.16xlarge"}
)
type CreateStore interface {
util.GetWorkspaceByNameOrIDErrStore
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetCurrentUser() (*entity.User, error)
GetWorkspace(workspaceID string) (*entity.Workspace, error)
CreateWorkspace(organizationID string, options *store.CreateWorkspacesOptions) (*entity.Workspace, error)
}
func NewCmdCreate(t *terminal.Terminal, createStore CreateStore) *cobra.Command {
var detached bool
var gpu string
var cpu string
cmd := &cobra.Command{
Annotations: map[string]string{"workspace": ""},
Use: "create",
DisableFlagsInUseLine: true,
Short: "Create a new instance ",
Long: createLong,
Example: createExample,
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) > 0 {
name = args[0]
}
err := runCreateWorkspace(t, CreateOptions{
Name: name,
WorkspaceClass: cpu,
Detached: detached,
InstanceType: gpu,
}, createStore)
if err != nil {
if strings.Contains(err.Error(), "duplicate environment with name") {
t.Vprint(t.Yellow("try running:"))
t.Vprint(t.Yellow("\tbrev start --name [different name] [repo] # or"))
t.Vprint(t.Yellow("\tbrev delete [name]"))
}
return breverrors.WrapAndTrace(err)
}
return nil
},
}
cmd.Flags().BoolVarP(&detached, "detached", "d", false, "run the command in the background instead of blocking the shell")
cmd.Flags().StringVarP(&cpu, "cpu", "c", "", "CPU instance type. Defaults to 2x8 [2x8, 4x16, 8x32, 16x32]. See docs.brev.dev/cpu for details")
cmd.Flags().StringVarP(&gpu, "gpu", "g", "", "GPU instance type. See docs.brev.dev/gpu for details")
return cmd
}
type CreateOptions struct {
Name string
WorkspaceClass string
Detached bool
InstanceType string
}
func runCreateWorkspace(t *terminal.Terminal, options CreateOptions, createStore CreateStore) error {
user, err := createStore.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = createEmptyWorkspace(user, t, options, createStore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func createEmptyWorkspace(user *entity.User, t *terminal.Terminal, options CreateOptions, createStore CreateStore) error {
// ensure name
if len(options.Name) == 0 {
return breverrors.NewValidationError("A name field is required to create a workspace!")
}
// ensure org
var orgID string
activeorg, err := createStore.GetActiveOrganizationOrDefault()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if activeorg == nil {
return breverrors.NewValidationError("no org exist")
}
orgID = activeorg.ID
clusterID := config.GlobalConfig.GetDefaultClusterID()
cwOptions := store.NewCreateWorkspacesOptions(clusterID, options.Name)
if options.WorkspaceClass != "" {
cwOptions.WithClassID(options.WorkspaceClass)
}
cwOptions = resolveWorkspaceUserOptions(cwOptions, user)
if options.InstanceType != "" {
cwOptions.WithInstanceType(options.InstanceType)
}
t.Vprintf("Creating environment %s in org %s\n", t.Green(cwOptions.Name), t.Green(orgID))
t.Vprintf("\tname %s\n", t.Green(cwOptions.Name))
if options.InstanceType != "" {
t.Vprintf("\tGPU instance %s\n", t.Green(options.InstanceType))
} else {
t.Vprintf("\tCPU instance %s\n", t.Green(cwOptions.WorkspaceClassID))
}
t.Vprintf("\tCloud %s\n\n", t.Green(cwOptions.WorkspaceGroupID))
s := t.NewSpinner()
s.Suffix = " Creating your instance. Hang tight 🤙"
s.Start()
w, err := createStore.CreateWorkspace(orgID, cwOptions)
if err != nil {
return breverrors.WrapAndTrace(err)
}
s.Stop()
if options.Detached {
return nil
} else {
err = pollUntil(t, w.ID, entity.Running, createStore, true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
fmt.Print("\n")
t.Vprint(t.Green("Your dev environment is ready!\n"))
displayConnectBreadCrumb(t, w)
return nil
}
}
func resolveWorkspaceUserOptions(options *store.CreateWorkspacesOptions, user *entity.User) *store.CreateWorkspacesOptions {
if options.WorkspaceTemplateID == "" {
if featureflag.IsAdmin(user.GlobalUserType) {
options.WorkspaceTemplateID = store.DevWorkspaceTemplateID
} else {
options.WorkspaceTemplateID = store.UserWorkspaceTemplateID
}
}
if options.WorkspaceClassID == "" {
if featureflag.IsAdmin(user.GlobalUserType) {
options.WorkspaceClassID = store.DevWorkspaceClassID
} else {
options.WorkspaceClassID = store.UserWorkspaceClassID
}
}
return options
}
func displayConnectBreadCrumb(t *terminal.Terminal, workspace *entity.Workspace) {
t.Vprintf(t.Green("Connect to the dev environment:\n"))
t.Vprintf(t.Yellow(fmt.Sprintf("\tbrev open %s\t# brev open <NAME> -> open dev environment in VS Code\n", workspace.Name)))
t.Vprintf(t.Yellow(fmt.Sprintf("\tbrev shell %s\t# brev shell <NAME> -> ssh into dev environment (shortcut)\n", workspace.Name)))
// t.Vprintf(t.Yellow(fmt.Sprintf("\tssh %s\t# ssh <SSH-NAME> -> ssh directly to dev environment\n", workspace.GetLocalIdentifier())))
}
func pollUntil(t *terminal.Terminal, wsid string, state string, createStore CreateStore, canSafelyExit bool) error {
s := t.NewSpinner()
isReady := false
if canSafelyExit {
t.Vprintf("You can safely ctrl+c to exit\n")
}
s.Suffix = " hang tight 🤙"
s.Start()
for !isReady {
time.Sleep(5 * time.Second)
ws, err := createStore.GetWorkspace(wsid)
if err != nil {
return breverrors.WrapAndTrace(err)
}
s.Suffix = " environment is " + strings.ToLower(ws.Status)
if ws.Status == state {
s.Suffix = "Environment is ready!"
s.Stop()
isReady = true
}
}
return nil
}