-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpost-processor.go
122 lines (108 loc) · 3.61 KB
/
post-processor.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
package main
import (
"context"
"errors"
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
//go:generate mockgen -destination ec2iface_mock.go -package main github.com/aws/aws-sdk-go/service/ec2/ec2iface EC2API
// PostProcessor is the core of this library
// Packer performs `PostProcess()` method of this processor
type PostProcessor struct {
testMode bool
cleaner Cleanable
config Config
}
// ConfigSpec returns HCL object spec
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
return p.config.FlatMapstructure().HCL2Spec()
}
// Configure generates post-processor's configuration
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
PluginType: "packer.post-processor.amazon-ami-management",
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
if len(p.config.Tags) == 0 && p.config.Identifier == "" {
return errors.New("`identifier` or `tags` must be defined. Please make sure that it is set correctly")
}
if len(p.config.Tags) > 0 && p.config.Identifier != "" {
log.Println("[WARNING] `tags` is ignored because `identifier` is defined")
}
if p.config.KeepReleases != 0 && p.config.KeepDays != 0 {
return errors.New("`keep_releases` and `keep_days` cannot be set as the same time")
}
if p.config.KeepReleases == 0 && p.config.KeepDays == 0 {
return errors.New("`keep_releases` or `keep_days` must be greater than 1. Please make sure that it is set correctly")
}
if p.config.KeepReleases < 1 && p.config.KeepDays == 0 {
return errors.New("`keep_releases` must be greater than 1. Please make sure that it is set correctly")
}
if p.config.KeepDays < 1 && p.config.KeepReleases == 0 {
return errors.New("`keep_days` must be greater than 1. Please make sure that it is set correctly")
}
if len(p.config.Regions) == 0 {
return errors.New("empty `regions` is not allowed. Please make sure that it is set correctly")
}
return nil
}
// PostProcess deletes old AMI and snapshot so as to maintain the number of AMIs expected
func (p *PostProcessor) PostProcess(
ctx context.Context,
ui packer.Ui,
artifact packer.Artifact,
) (packer.Artifact, bool, bool, error) {
log.Println("Running the post-processor")
for _, region := range p.config.Regions {
ui.Message(p.uiMessage(fmt.Sprintf("Processing in %s", region)))
if !p.testMode {
sess, err := p.config.AccessConfig.Session()
if err != nil {
return nil, true, false, err
}
p.cleaner, err = NewCleaner(
sess.Copy(&aws.Config{Region: aws.String(region)}),
p.config,
)
if err != nil {
return nil, true, false, err
}
}
images, err := p.cleaner.RetrieveCandidateImages()
if err != nil {
return nil, true, false, err
}
log.Println("Deleting old images...")
for _, image := range images {
ui.Message(p.uiMessage(fmt.Sprintf("Deleting image: %s", *image.ImageId)))
used := p.cleaner.IsUsed(image)
if used != nil {
ui.Message(fmt.Sprintf("[WARN] %s is used in %s: %s. Skipped...", *image.ImageId, used.Type, used.ID))
} else {
err := p.cleaner.DeleteImage(image)
if err != nil {
return nil, true, false, err
}
}
}
}
return artifact, true, false, nil
}
func (p *PostProcessor) uiMessage(message string) string {
if p.config.DryRun {
return "[DryRun] " + message
}
return message
}