forked from fujiwara/lambroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback.go
119 lines (106 loc) · 3 KB
/
rollback.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
package lambroll
import (
"log"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/pkg/errors"
)
// RollbackOption represents option for Rollback()
type RollbackOption struct {
FunctionFilePath *string
DryRun *bool
DeleteVersion *bool
}
func (opt RollbackOption) label() string {
if *opt.DryRun {
return "**DRY RUN**"
}
return ""
}
// Rollback rollbacks function
func (app *App) Rollback(opt RollbackOption) error {
fn, err := app.loadFunction(*opt.FunctionFilePath)
if err != nil {
return errors.Wrap(err, "failed to load function")
}
log.Printf("[info] starting rollback function %s", *fn.FunctionName)
res, err := app.lambda.GetAlias(&lambda.GetAliasInput{
FunctionName: fn.FunctionName,
Name: aws.String(CurrentAliasName),
})
if err != nil {
return errors.Wrap(err, "failed to get alias")
}
currentVersion := *res.FunctionVersion
cv, err := strconv.ParseInt(currentVersion, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to pase %s as int", currentVersion)
}
var prevVersion string
VERSIONS:
for v := cv - 1; v > 0; v-- {
log.Printf("[debug] get function version %d", v)
vs := strconv.FormatInt(v, 10)
res, err := app.lambda.GetFunction(&lambda.GetFunctionInput{
FunctionName: fn.FunctionName,
Qualifier: aws.String(vs),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case lambda.ErrCodeResourceNotFoundException:
log.Printf("[debug] version %s not found", vs)
continue VERSIONS
}
}
return errors.Wrap(err, "failed to get function")
}
prevVersion = *res.Configuration.Version
break
}
if prevVersion == "" {
return errors.New("unable to detect previous version of function")
}
log.Printf("[info] rollbacking function version %s to %s %s", currentVersion, prevVersion, opt.label())
if *opt.DryRun {
return nil
}
err = app.updateAliases(*fn.FunctionName, versionAlias{Version: prevVersion, Name: CurrentAliasName})
if err != nil {
return err
}
if !*opt.DeleteVersion {
return nil
}
return app.deleteFunctionVersion(*fn.FunctionName, currentVersion)
}
func (app *App) deleteFunctionVersion(functionName, version string) error {
for {
log.Printf("[debug] checking aliased version")
res, err := app.lambda.GetAlias(&lambda.GetAliasInput{
FunctionName: aws.String(functionName),
Name: aws.String(CurrentAliasName),
})
if err != nil {
return errors.Wrap(err, "failed to get alias")
}
if *res.FunctionVersion == version {
log.Printf("[debug] version %s still has alias %s, retrying", version, CurrentAliasName)
time.Sleep(time.Second)
continue
}
break
}
log.Printf("[info] deleting function version %s", version)
_, err := app.lambda.DeleteFunction(&lambda.DeleteFunctionInput{
FunctionName: aws.String(functionName),
Qualifier: aws.String(version),
})
if err != nil {
return errors.Wrap(err, "failed to delete version")
}
return nil
}