generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
recreate.go
226 lines (196 loc) · 6.83 KB
/
recreate.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Package recreate is for the recreate command
package recreate
import (
_ "embed"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/cmd/util"
"github.com/brevdev/brev-cli/pkg/config"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/featureflag"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
stripmd "github.com/writeas/go-strip-markdown"
)
//go:embed doc.md
var long string
type recreateStore interface {
completions.CompletionStore
util.GetWorkspaceByNameOrIDErrStore
ResetWorkspace(workspaceID string) (*entity.Workspace, error)
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetCurrentUser() (*entity.User, error)
CreateWorkspace(organizationID string, options *store.CreateWorkspacesOptions) (*entity.Workspace, error)
GetWorkspace(id string) (*entity.Workspace, error)
DeleteWorkspace(workspaceID string) (*entity.Workspace, error)
}
func NewCmdRecreate(t *terminal.Terminal, store recreateStore) *cobra.Command {
cmd := &cobra.Command{
Use: "recreate",
DisableFlagsInUseLine: true,
Short: "TODO",
Long: stripmd.Strip(long),
Example: "TODO",
RunE: func(cmd *cobra.Command, args []string) error {
err := RunRecreate(t, args, store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
return cmd
}
func RunRecreate(t *terminal.Terminal, args []string, recreateStore recreateStore) error {
for _, arg := range args {
err := hardResetProcess(arg, t, recreateStore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
return nil
}
// hardResetProcess deletes an existing workspace and creates a new one
func hardResetProcess(workspaceName string, t *terminal.Terminal, recreateStore recreateStore) error {
t.Vprint(t.Green("recreating 🤙 " + t.Yellow("This can take a couple of minutes.\n")))
workspace, err := util.GetUserWorkspaceByNameOrIDErr(recreateStore, workspaceName)
if err != nil {
return breverrors.WrapAndTrace(err)
}
deletedWorkspace, err := recreateStore.DeleteWorkspace(workspace.ID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
t.Vprint(t.Yellow("Deleting instance - %s.", deletedWorkspace.Name))
time.Sleep(10 * time.Second)
if len(deletedWorkspace.GitRepo) != 0 {
err := hardResetCreateWorkspaceFromRepo(t, recreateStore, deletedWorkspace)
if err != nil {
return breverrors.WrapAndTrace(err)
}
} else {
err := hardResetCreateEmptyWorkspace(t, recreateStore, deletedWorkspace)
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
return nil
}
// hardResetCreateWorkspaceFromRepo clone a GIT repository, triggeres from the --hardreset flag
func hardResetCreateWorkspaceFromRepo(t *terminal.Terminal, recreateStore recreateStore, workspace *entity.Workspace) error {
t.Vprint(t.Green("Instance is starting. ") + t.Yellow("This can take up to 2 minutes the first time."))
var orgID string
activeorg, err := recreateStore.GetActiveOrganizationOrDefault()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if activeorg == nil {
return breverrors.NewValidationError("no org exist")
}
orgID = activeorg.ID
clusterID := config.GlobalConfig.GetDefaultClusterID()
options := store.NewCreateWorkspacesOptions(clusterID, workspace.Name).WithGitRepo(workspace.GitRepo)
user, err := recreateStore.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(err)
}
options = resolveWorkspaceUserOptions(options, user)
options.StartupScriptPath = workspace.StartupScriptPath
options.Execs = workspace.ExecsV0
options.Repos = workspace.ReposV0
options.IDEConfig = &workspace.IDEConfig
w, err := recreateStore.CreateWorkspace(orgID, options)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = pollUntil(t, w.ID, entity.Running, recreateStore, true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
t.Vprint(t.Green("\nYour instance is ready!"))
t.Vprintf(t.Green("\nSSH into your machine:\n\tssh %s\n", w.GetLocalIdentifier()))
return nil
}
// hardResetCreateEmptyWorkspace creates a new empty worksapce, triggered from the --hardreset flag
func hardResetCreateEmptyWorkspace(t *terminal.Terminal, recreateStore recreateStore, workspace *entity.Workspace) error {
t.Vprint(t.Green("Instance is starting. ") + t.Yellow("This can take up to 2 minutes the first time.\n"))
// ensure name
if len(workspace.Name) == 0 {
return breverrors.NewValidationError("name field is required for empty workspaces")
}
// ensure org
var orgID string
activeorg, err := recreateStore.GetActiveOrganizationOrDefault()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if activeorg == nil {
return breverrors.NewValidationError("no org exist")
}
orgID = activeorg.ID
clusterID := config.GlobalConfig.GetDefaultClusterID()
options := store.NewCreateWorkspacesOptions(clusterID, workspace.Name)
user, err := recreateStore.GetCurrentUser()
if err != nil {
return breverrors.WrapAndTrace(err)
}
options = resolveWorkspaceUserOptions(options, user)
options.StartupScriptPath = workspace.StartupScriptPath
options.Execs = workspace.ExecsV0
options.Repos = workspace.ReposV0
options.IDEConfig = &workspace.IDEConfig
w, err := recreateStore.CreateWorkspace(orgID, options)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = pollUntil(t, w.ID, entity.Running, recreateStore, true)
if err != nil {
return breverrors.WrapAndTrace(err)
}
t.Vprint(t.Green("\nYour instance is ready!"))
t.Vprintf(t.Green("\nSSH into your machine:\n\tssh %s\n", w.GetLocalIdentifier()))
return nil
}
func pollUntil(t *terminal.Terminal, wsid string, state string, recreateStore recreateStore, 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 := recreateStore.GetWorkspace(wsid)
if err != nil {
return breverrors.WrapAndTrace(err)
}
s.Suffix = " workspace is " + strings.ToLower(ws.Status)
if ws.Status == state {
s.Suffix = "Workspace is ready!"
s.Stop()
isReady = true
}
}
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
}