forked from skevetter/devpod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename.go
More file actions
285 lines (247 loc) · 8.42 KB
/
rename.go
File metadata and controls
285 lines (247 loc) · 8.42 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package provider
import (
"context"
"errors"
"fmt"
"strings"
"github.com/skevetter/devpod/cmd/flags"
"github.com/skevetter/devpod/pkg/config"
"github.com/skevetter/devpod/pkg/provider"
workspace "github.com/skevetter/devpod/pkg/workspace"
"github.com/skevetter/log"
"github.com/spf13/cobra"
)
// RenameCmd implements the provider rename command.
type RenameCmd struct {
*flags.GlobalFlags
}
// NewRenameCmd creates a new command for renaming a provider.
func NewRenameCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &RenameCmd{
GlobalFlags: globalFlags,
}
return &cobra.Command{
Use: "rename <current-name> <new-name>",
Short: "Rename a provider",
Args: cobra.ExactArgs(2),
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.Run(cobraCmd.Context(), args)
},
}
}
// Run validates inputs, loads config, and executes the provider rename.
func (cmd *RenameCmd) Run(ctx context.Context, args []string) error {
oldName, newName := args[0], args[1]
if oldName == newName {
return fmt.Errorf("new name is the same as the current name")
}
if err := validateProviderName(newName); err != nil {
return err
}
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}
if err := validateProviderRename(devPodConfig, oldName); err != nil {
return err
}
if devPodConfig.Current().Providers[newName] != nil {
return fmt.Errorf("provider %s already exists", newName)
}
return renameProvider(ctx, devPodConfig, oldName, newName)
}
// validateProviderName checks that the given name is non-empty, matches the
// allowed character set (lowercase letters, numbers, dashes), and does not
// exceed the maximum length of 32 characters.
func validateProviderName(newName string) error {
if strings.TrimSpace(newName) == "" {
return fmt.Errorf("provider name cannot be empty")
}
if provider.ProviderNameRegEx.MatchString(newName) {
return fmt.Errorf("provider name can only include lowercase letters, numbers or dashes")
}
if len(newName) > 32 {
return fmt.Errorf("provider name cannot be longer than 32 characters")
}
return nil
}
// getWorkspacesForProvider returns all local workspaces whose provider matches
// the given name.
func getWorkspacesForProvider(
devPodConfig *config.Config,
providerName string,
) ([]*provider.Workspace, error) {
workspaces, err := workspace.ListLocalWorkspaces(
devPodConfig.DefaultContext,
false,
log.Default,
)
if err != nil {
return nil, fmt.Errorf("listing workspaces: %w", err)
}
var matched []*provider.Workspace
for _, ws := range workspaces {
if ws.Provider.Name == providerName {
matched = append(matched, ws)
}
}
return matched, nil
}
// getMachinesForProvider returns all machines whose provider matches the given
// name.
func getMachinesForProvider(
devPodConfig *config.Config,
providerName string,
) ([]*provider.Machine, error) {
machines, err := workspace.ListMachines(devPodConfig, log.Default)
if err != nil {
return nil, fmt.Errorf("listing machines: %w", err)
}
var matched []*provider.Machine
for _, m := range machines {
if m.Provider.Name == providerName {
matched = append(matched, m)
}
}
return matched, nil
}
// switchWorkspaces updates each workspace to reference the new provider name.
// It stops on the first failure and returns the successfully switched
// workspaces so the caller can roll them back.
func switchWorkspaces(
ctx context.Context,
devPodConfig *config.Config,
workspaces []*provider.Workspace,
newName string,
) ([]*provider.Workspace, error) {
var switched []*provider.Workspace
for _, ws := range workspaces {
if err := workspace.SwitchProvider(ctx, devPodConfig, ws, newName); err != nil {
return switched, fmt.Errorf("failed to switch workspace %s: %w", ws.ID, err)
}
switched = append(switched, ws)
}
return switched, nil
}
// switchMachines updates each machine to reference the new provider name.
// It stops on the first failure and returns the successfully switched
// machines so the caller can roll them back.
func switchMachines(machines []*provider.Machine, newName string) ([]*provider.Machine, error) {
var switched []*provider.Machine
for _, m := range machines {
oldName := m.Provider.Name
m.Provider.Name = newName
if err := provider.SaveMachineConfig(m); err != nil {
m.Provider.Name = oldName
return switched, fmt.Errorf("failed to switch machine %s: %w", m.ID, err)
}
switched = append(switched, m)
}
return switched, nil
}
// setDefaultProvider updates the default provider setting if it currently
// points to oldName. Returns true if the default was changed.
func setDefaultProvider(devPodConfig *config.Config, oldName, newName string) (bool, error) {
if devPodConfig.Current().DefaultProvider != oldName {
return false, nil
}
devPodConfig.Current().DefaultProvider = newName
if err := config.SaveConfig(devPodConfig); err != nil {
devPodConfig.Current().DefaultProvider = oldName
return false, err
}
return true, nil
}
// renameState tracks the mutations performed during a rename so they can be
// undone if a later step fails.
type renameState struct {
devPodConfig *config.Config
switchedWorkspaces []*provider.Workspace
switchedMachines []*provider.Machine
defaultChanged bool
oldName, newName string
}
// restoreProviderState reverts all recorded mutations in reverse order: default provider,
// workspaces, machines, and finally the provider directory move.
func (r *renameState) restoreProviderState(ctx context.Context) error {
log.Default.Info("rolling back changes")
var errs error
if r.defaultChanged {
r.devPodConfig.Current().DefaultProvider = r.oldName
if err := config.SaveConfig(r.devPodConfig); err != nil {
errs = errors.Join(errs, fmt.Errorf("rollback default provider: %w", err))
}
}
_, err := switchWorkspaces(ctx, r.devPodConfig, r.switchedWorkspaces, r.oldName)
errs = errors.Join(errs, err)
_, err = switchMachines(r.switchedMachines, r.oldName)
errs = errors.Join(errs, err)
if moveErr := workspace.MoveProvider(r.devPodConfig, r.newName, r.oldName); moveErr != nil {
errs = errors.Join(errs, fmt.Errorf("rollback move provider: %w", moveErr))
}
return errs
}
// validateProviderRename verifies that the provider exists, is not a pro
// provider, is not backing a pro instance, and has configuration state.
func validateProviderRename(devPodConfig *config.Config, oldName string) error {
providerWithOptions, err := workspace.FindProvider(devPodConfig, oldName, log.Default)
if err != nil {
return fmt.Errorf("provider %s not found", oldName)
}
if providerWithOptions.Config.IsProxyProvider() ||
providerWithOptions.Config.IsDaemonProvider() {
return fmt.Errorf("cannot rename a pro provider; pro providers are managed by the platform")
}
proInstances, err := workspace.ListProInstances(devPodConfig, log.Default)
if err != nil {
return fmt.Errorf("listing pro instances: %w", err)
}
for _, inst := range proInstances {
if inst.Provider == oldName {
return fmt.Errorf(
"cannot rename provider %s: it is used by pro instance %s",
oldName,
inst.Host,
)
}
}
if devPodConfig.Current().Providers[oldName] == nil {
return fmt.Errorf("provider %s has no configuration state", oldName)
}
return nil
}
// renameProvider performs the rename: moves the provider directory, switches all
// associated workspaces and machines, and adjusts the default provider. If any
// step fails the entire operation is rolled back.
func renameProvider(
ctx context.Context,
devPodConfig *config.Config,
oldName, newName string,
) error {
workspaces, err := getWorkspacesForProvider(devPodConfig, oldName)
if err != nil {
return err
}
machines, err := getMachinesForProvider(devPodConfig, oldName)
if err != nil {
return err
}
if err := workspace.MoveProvider(devPodConfig, oldName, newName); err != nil {
return fmt.Errorf("moving provider: %w", err)
}
rb := &renameState{devPodConfig: devPodConfig, oldName: oldName, newName: newName}
rb.switchedWorkspaces, err = switchWorkspaces(ctx, devPodConfig, workspaces, newName)
if err != nil {
return errors.Join(err, rb.restoreProviderState(ctx))
}
rb.switchedMachines, err = switchMachines(machines, newName)
if err != nil {
return errors.Join(err, rb.restoreProviderState(ctx))
}
rb.defaultChanged, err = setDefaultProvider(devPodConfig, oldName, newName)
if err != nil {
return errors.Join(err, rb.restoreProviderState(ctx))
}
log.Default.Donef("renamed provider %s to %s", oldName, newName)
return nil
}