-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
405 lines (345 loc) · 11.5 KB
/
upgrade.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package cli
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/futurice/jalapeno/internal/cli/option"
"github.com/futurice/jalapeno/internal/cliutil"
"github.com/futurice/jalapeno/pkg/engine"
"github.com/futurice/jalapeno/pkg/recipe"
"github.com/futurice/jalapeno/pkg/recipeutil"
"github.com/futurice/jalapeno/pkg/ui/conflict"
"github.com/futurice/jalapeno/pkg/ui/survey"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)
type upgradeOptions struct {
RecipeURL string
ReuseOldValues bool
SauceID string
option.Common
option.OCIRepository
option.Values
option.WorkingDirectory
}
func NewUpgradeCmd() *cobra.Command {
var opts upgradeOptions
var cmd = &cobra.Command{
Use: "upgrade RECIPE_URL",
Short: "Upgrade a recipe in a project",
Long: "Upgrade a recipe in a project with a newer version. Recipe URL can be a local path or a remote URL (ex. 'oci://docker.io/my-recipe').",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.RecipeURL = args[0]
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
err := runUpgrade(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Upgrade recipe with local recipe
jalapeno upgrade path/to/recipe
# Upgrade recipe with remote recipe from OCI repository
jalapeno upgrade oci://ghcr.io/user/my-recipe:v2.0.0
# Upgrade recipe with remote recipe from OCI repository with inline authentication
jalapeno upgrade oci://ghcr.io/user/my-recipe:v2.0.0 --username user --password pass
# Upgrade recipe with remote recipe from OCI repository with Docker authentication
docker login ghcr.io
jalapeno upgrade oci://ghcr.io/user/my-recipe:v2.0.0
# Upgrade recipe to different directory
jalapeno upgrade path/to/recipe --dir other/dir
# Set values for new variables introduced in the upgrade
jalapeno upgrade path/to/recipe --set NEW_VAR=foo`,
}
cmd.Flags().StringVar(&opts.SauceID, "sauce-id", "", "If the project contains multiple sauces with the same recipe, specify the ID of the sauce to be upgraded")
cmd.Flags().BoolVar(&opts.ReuseOldValues, "reuse-old-values", true, "Automatically set values for variables which already have a value in the existing sauce")
if err := option.ApplyFlags(&opts, cmd.Flags()); err != nil {
return nil
}
return cmd
}
func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
var (
re *recipe.Recipe
oldSauce *recipe.Sauce
err error
)
if strings.HasPrefix(opts.RecipeURL, "oci://") {
ctx := context.Background()
re, err = recipe.PullRecipe(ctx, opts.Repository(opts.RecipeURL))
} else {
re, err = recipe.LoadRecipe(opts.RecipeURL)
}
if err != nil {
return err
}
if opts.SauceID != "" {
id, uuidErr := uuid.FromString(opts.SauceID)
if uuidErr != nil {
return fmt.Errorf("invalid sauce ID: %w", err)
}
oldSauce, err = recipe.LoadSauceByID(opts.Dir, id)
} else {
oldSauce, err = recipe.LoadSauceByRecipe(opts.Dir, re.Name)
if err != nil && errors.Is(err, recipe.ErrAmbiguousSauce) {
return fmt.Errorf(`project '%s' contains multiple sauces with recipe '%s'. Use --sauce-id to specify the ID of the sauce to be upgraded. You can check the IDs from %s/%s.%s`, opts.Dir, re.Name, recipe.SauceDirName, recipe.SaucesFileName, recipe.YAMLExtension)
}
}
if err != nil {
if errors.Is(err, recipe.ErrSauceNotFound) {
return fmt.Errorf("project '%s' does not contain sauce with recipe '%s'. Recipe name used in the project should match the recipe which is used for upgrading", opts.Dir, re.Name)
}
return err
}
versionComparison := semver.Compare(re.Metadata.Version, oldSauce.Recipe.Metadata.Version)
if versionComparison < 0 {
return errors.New("new recipe version is less than the existing one")
}
if versionComparison > 0 {
if opts.SauceID == "" {
cmd.Printf("Upgrading sauce with recipe '%s' from version %s to %s\n",
oldSauce.Recipe.Name,
oldSauce.Recipe.Metadata.Version,
re.Metadata.Version,
)
} else {
cmd.Printf("Upgrading sauce (ID '%s') with recipe '%s' from version %s to %s\n",
oldSauce.ID,
oldSauce.Recipe.Metadata.Name,
oldSauce.Recipe.Metadata.Version,
re.Metadata.Version,
)
}
} else {
opts.ReuseOldValues = false
cmd.Printf(
"Modifying values for sauce with recipe '%s' version %s\n",
oldSauce.Recipe.Name,
re.Metadata.Version,
)
}
// Check if the new version of the recipe has removed some variables
// which existed on previous version
for valueName := range oldSauce.Values {
found := false
for _, variable := range re.Variables {
if variable.Name == valueName {
found = true
break
}
}
if !found {
delete(oldSauce.Values, valueName)
}
}
var values recipe.VariableValues
// Check if predefined values should be parsed (from flags and env. variables)
// This is disabled when upgrading happens through `check` command
reusedValues := make(recipe.VariableValues)
if opts.ReuseOtherSauceValues {
sauces, err := recipe.LoadSauces(opts.Dir)
if err != nil {
return err
}
for _, sauce := range sauces {
// Skip if the sauce is the one which is being upgraded
if sauce.Recipe.Name == re.Name {
continue
}
overlappingSauceValues := make(recipe.VariableValues)
for _, v := range re.Variables {
if val, found := sauce.Values[v.Name]; found {
overlappingSauceValues[v.Name] = val
}
}
if len(overlappingSauceValues) > 0 {
reusedValues = recipeutil.MergeValues(reusedValues, overlappingSauceValues)
}
}
}
providedValues, err := recipeutil.ParseProvidedValues(
re.Variables,
opts.Values.Flags,
opts.Values.CSVDelimiter,
opts.Values.ParseEnvironmentVariables,
)
if err != nil {
return fmt.Errorf("failed to parse provided values: %w", err)
}
values = recipeutil.MergeValues(reusedValues, providedValues)
if opts.ReuseOldValues {
values = recipeutil.MergeValues(oldSauce.Values, values)
}
// Don't prompt variables which already has a value in existing sauce or is predefined
varsWithoutValues := make([]recipe.Variable, 0, len(re.Variables))
for _, v := range re.Variables {
if _, predefinedValueExists := values[v.Name]; !predefinedValueExists {
varsWithoutValues = append(varsWithoutValues, v)
}
}
if len(varsWithoutValues) > 0 {
// If --no-input flag is set, try to use default values
if opts.NoInput {
varsWithoutDefaultValues := make([]recipe.Variable, 0, len(varsWithoutValues))
for _, v := range varsWithoutValues {
if v.Default != "" {
defaultValue, err := v.ParseDefaultValue()
if err != nil {
return fmt.Errorf("failed to parse default value for variable '%s': %w", v.Name, err)
}
values[v.Name] = defaultValue
} else {
varsWithoutDefaultValues = append(varsWithoutDefaultValues, v)
}
}
// If there are still variables without values, return error
if len(varsWithoutDefaultValues) > 0 {
return recipeutil.NewNoInputError(varsWithoutDefaultValues)
}
}
cmd.Println()
promptedValues, err := survey.PromptUserForValues(
cmd.InOrStdin(),
cmd.OutOrStdout(),
varsWithoutValues,
values,
)
if err != nil {
return fmt.Errorf("error when prompting for values: %w", err)
}
values = recipeutil.MergeValues(values, promptedValues)
}
newSauce, err := re.Execute(engine.New(), values, oldSauce.ID)
if err != nil {
return err
}
if oldSauce.CheckFrom != "" {
newSauce.CheckFrom = oldSauce.CheckFrom
} else if strings.HasPrefix(opts.RecipeURL, "oci://") {
newSauce.CheckFrom = strings.TrimSuffix(opts.RecipeURL, fmt.Sprintf(":%s", re.Metadata.Version))
}
newSauce.SubPath = oldSauce.SubPath
// read common ignore file if it exists
ignorePatterns := make([]string, 0)
if data, err := os.ReadFile(filepath.Join(opts.Dir, recipe.IgnoreFileName)); err == nil {
ignorePatterns = append(ignorePatterns, strings.Split(string(data), "\n")...)
} else if !errors.Is(err, fs.ErrNotExist) {
// something else happened than trying to read an ignore file that does not exist
return fmt.Errorf("failed to read ignore file: %w\n\n%s", err, cliutil.MakeRetryMessage(os.Args, values))
}
ignorePatterns = append(ignorePatterns, re.IgnorePatterns...)
overrideNoticed := false
fileStatuses := make(map[string]recipeutil.FileStatus, len(newSauce.Files))
for path := range newSauce.Files {
skip := false
for _, pattern := range ignorePatterns {
if matched, err := filepath.Match(pattern, path); err != nil {
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, cliutil.MakeRetryMessage(os.Args, values))
} else if matched {
// file was marked as ignored for upgrades
skip = true
break
}
}
if skip {
delete(newSauce.Files, path)
continue
}
// Check if the file from previous recipe version has been modified manually
prevFile, exists := oldSauce.Files[path]
if exists {
if !prevFile.HasBeenModifiedByUser() {
if prevFile.Checksum == newSauce.Files[path].Checksum {
fileStatuses[path] = recipeutil.FileUnchanged
} else {
fileStatuses[path] = recipeutil.FileModified
}
continue
}
// Check if the file has been already created manually by the user
} else {
existingFileContent, err := os.ReadFile(filepath.Join(opts.Dir, path))
if errors.Is(err, os.ErrNotExist) {
fileStatuses[path] = recipeutil.FileAdded
continue
} else if err != nil {
return err
}
prevFile = recipe.NewFile(existingFileContent)
}
if opts.NoInput {
return recipeutil.NewNoInputError(nil)
}
if !overrideNoticed {
cmd.Println("\nSome of the files has been manually modified. Do you want to override the following files:")
overrideNoticed = true
}
conflictResult, err := conflict.Solve(
cmd.InOrStdin(),
cmd.OutOrStdout(),
path,
prevFile.Content,
newSauce.Files[path].Content,
)
if err != nil {
return fmt.Errorf("error when solving file conflicts: %w", err)
}
if bytes.Equal(conflictResult, prevFile.Content) {
newSauce.Files[path] = prevFile
fileStatuses[path] = recipeutil.FileUnchanged
continue
}
fileStatuses[path] = recipeutil.FileModified
// NOTE: We need to save the checksum of the original file from the new sauce
// so we would detect again if the file has been modified manually
// when upgrading again
newSauce.Files[path] = recipe.File{
Checksum: newSauce.Files[path].Checksum,
Content: conflictResult,
}
}
for filename := range oldSauce.Files {
if _, found := newSauce.Files[filename]; !found {
keep := false
for _, pattern := range ignorePatterns {
if matched, err := filepath.Match(pattern, filename); err != nil {
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, cliutil.MakeRetryMessage(os.Args, values))
} else if matched {
keep = true
break
}
}
if !keep {
err := os.Remove(filepath.Join(opts.Dir, filename))
if err != nil {
return fmt.Errorf("failed to remove deprecated file '%s': %w", filename, err)
}
fileStatuses[filename] = recipeutil.FileDeleted
}
}
}
err = newSauce.Save(opts.Dir)
if err != nil {
return err
}
changesFound := false
for _, status := range fileStatuses {
if status != recipeutil.FileUnchanged {
changesFound = true
cmd.Printf("Recipe upgraded %s\n", opts.Colors.Green.Render("successfully!"))
tree := recipeutil.CreateFileTree(opts.Dir, fileStatuses)
cmd.Printf("The following files have been processed by the recipe:\n\n%s", tree)
break
}
}
if !changesFound {
cmd.Println("Upgrade completed, but no changes were made to any files.")
}
return nil
}