-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollback.go
More file actions
69 lines (57 loc) · 1.72 KB
/
Copy pathrollback.go
File metadata and controls
69 lines (57 loc) · 1.72 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
package lamvms
import (
"context"
"fmt"
"log/slog"
"slices"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambdamicrovms"
"github.com/aws/aws-sdk-go-v2/service/lambdamicrovms/types"
)
// Rollback deactivates the latest active version.
func (app *App) Rollback(ctx context.Context, opt *RollbackOption) error {
img := app.microvmImage
name := aws.ToString(img.Name)
existing, err := app.findMicrovmImageByName(ctx, name)
if err != nil {
return err
}
if existing == nil {
return fmt.Errorf("microvm image %q not found", name)
}
imageARN := aws.ToString(existing.ImageArn)
activeVersions, err := app.listActiveVersions(ctx, imageARN)
if err != nil {
return err
}
if len(activeVersions) < 2 {
return fmt.Errorf("rollback requires at least 2 active versions, found %d", len(activeVersions))
}
slices.SortFunc(activeVersions, versionNewerFirst)
latest := activeVersions[0]
previous := activeVersions[1]
latestVersion := aws.ToString(latest.ImageVersion)
previousVersion := aws.ToString(previous.ImageVersion)
slog.Info("rolling back",
"name", name,
"deactivating", latestVersion,
"falling_back_to", previousVersion,
)
if opt.DryRun {
slog.Info("dry run: would deactivate version", "version", latestVersion)
return nil
}
_, err = app.client.UpdateMicrovmImageVersion(ctx, &lambdamicrovms.UpdateMicrovmImageVersionInput{
ImageIdentifier: aws.String(imageARN),
ImageVersion: aws.String(latestVersion),
Status: types.MicrovmImageVersionStatusInactive,
})
if err != nil {
return fmt.Errorf("failed to deactivate version %s: %w", latestVersion, err)
}
slog.Info("rollback completed",
"deactivated", latestVersion,
"active", previousVersion,
)
return nil
}