Skip to content

Commit b135515

Browse files
committed
Add tag for specifying reboot behavior on an instance by instance basis
1 parent 2e07e1c commit b135515

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

pkg/backup/manager.go

+35-35
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ import (
1414

1515
// ManagerOpts are options to configure the backup manager
1616
type ManagerOpts struct {
17-
Client *ec2.EC2
17+
client *ec2.EC2
1818

19-
BackupTagKey string
20-
BackupTagValue string
21-
ImageTagKey string
22-
ImageTagValue string
23-
ImageNameTag string
19+
BackupTagKey string
20+
BackupTagValue string
21+
ImageTagKey string
22+
ImageTagValue string
23+
ImageNameTag string
24+
RebootOnImageTag string
2425

2526
DefaultImageNameTemplate *template.Template
2627
DefaultMaxKeepImages int
28+
DefaultRebootOnImage bool
2729

2830
Verbose bool
2931
}
@@ -33,13 +35,16 @@ type ManagerOpts struct {
3335
func NewManagerOptsFromConfig(client *ec2.EC2) (*ManagerOpts, error) {
3436
var err error
3537
opts := &ManagerOpts{
36-
Client: client,
37-
BackupTagKey: config.BackupTagKey(),
38-
BackupTagValue: config.BackupTagValue(),
39-
ImageTagKey: config.ImageTagKey(),
40-
ImageTagValue: config.ImageTagValue(),
41-
ImageNameTag: config.ImageNameTag(),
42-
Verbose: true,
38+
client: client,
39+
BackupTagKey: config.BackupTagKey(),
40+
BackupTagValue: config.BackupTagValue(),
41+
ImageTagKey: config.ImageTagKey(),
42+
ImageTagValue: config.ImageTagValue(),
43+
ImageNameTag: config.ImageNameTag(),
44+
RebootOnImageTag: config.RebootOnImageTag(),
45+
Verbose: true,
46+
47+
DefaultRebootOnImage: config.DefaultRebootOnImage(),
4348
}
4449

4550
opts.DefaultImageNameTemplate, err = template.New("default-image-name").Parse(config.DefaultImageNameFormat())
@@ -53,22 +58,10 @@ func NewManagerOptsFromConfig(client *ec2.EC2) (*ManagerOpts, error) {
5358

5459
// Manager manages backups/images of ec2 resources(volumes, instances, etc.)
5560
type Manager struct {
56-
client *ec2.EC2
61+
*ManagerOpts
5762

5863
volumes []*ec2.Volume
5964
instances []*ec2.Instance
60-
61-
BackupTagKey string
62-
BackupTagValue string
63-
ImageTagKey string
64-
ImageTagValue string
65-
ImageNameTag string
66-
MaxKeepImagesTag string
67-
68-
DefaultImageNameTemplate *template.Template
69-
DefaultMaxKeepImages int
70-
71-
Verbose bool
7265
}
7366

7467
// NewManager creates a new backup manager from the provided options
@@ -77,19 +70,12 @@ func NewManager(opts *ManagerOpts) (*Manager, error) {
7770
volumes: make([]*ec2.Volume, 0),
7871
instances: make([]*ec2.Instance, 0),
7972
}
80-
m.client = opts.Client
81-
m.BackupTagKey = opts.BackupTagKey
82-
m.BackupTagValue = opts.BackupTagValue
83-
m.ImageTagKey = opts.ImageTagKey
84-
m.ImageTagValue = opts.ImageTagValue
85-
m.ImageNameTag = opts.ImageNameTag
86-
m.Verbose = opts.Verbose
73+
8774
if opts.DefaultImageNameTemplate == nil {
8875
return nil, fmt.Errorf("DefaultImageNameTemplate is a required field for ManagerOpts")
8976
}
9077

91-
m.DefaultImageNameTemplate = opts.DefaultImageNameTemplate
92-
m.DefaultMaxKeepImages = opts.DefaultMaxKeepImages
78+
m.ManagerOpts = opts
9379
return m, nil
9480
}
9581

@@ -217,6 +203,7 @@ func (m *Manager) backupInstances() error {
217203
image, err := m.client.CreateImage(&ec2.CreateImageInput{
218204
InstanceId: i.InstanceId,
219205
Name: aws.String(imageName),
206+
NoReboot: aws.Bool(!m.instanceRebootParam(i)),
220207
})
221208
if err != nil {
222209
m.logf(
@@ -300,6 +287,19 @@ func (m *Manager) formatImageName(i *ec2.Instance) (string, error) {
300287
return buf.String(), err
301288
}
302289

290+
func (m *Manager) instanceRebootParam(i *ec2.Instance) bool {
291+
tags := utils.TagSliceToMap(i.Tags)
292+
if rebootVal, ok := tags.Get(m.RebootOnImageTag); ok {
293+
for _, v := range []string{"true", "True", "TRUE"} {
294+
if rebootVal == v {
295+
return true
296+
}
297+
}
298+
return false
299+
}
300+
return m.DefaultRebootOnImage
301+
}
302+
303303
func (m *Manager) log(v ...interface{}) {
304304
if m.Verbose {
305305
fmt.Println(v...)

pkg/config/config.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ func envDefaultInt(key, defaultValue string) (int, error) {
1717
return strconv.Atoi(envDefault(key, defaultValue))
1818
}
1919

20+
func envDefaultBool(key string, defaultValue bool) bool {
21+
value := os.Getenv(key)
22+
if value == "" {
23+
return defaultValue
24+
}
25+
26+
for _, v := range []string{"true", "True", "TRUE"} {
27+
if value == v {
28+
return true
29+
}
30+
}
31+
return false
32+
}
33+
2034
// BackupTagKey is the volume tag key to look for when determing if we should
2135
// perform a backup or not.
2236
func BackupTagKey() string {
@@ -61,8 +75,22 @@ func MaxKeepImagesTag() string {
6175
return envDefault("MAX_KEEP_IMAGES_TAG", "lambda-ebs-backup/max-keep-images")
6276
}
6377

64-
// MaxKeepImagesDefault is the default number of images to keep if not specified
78+
// DefaultMaxKeepImages is the default number of images to keep if not specified
6579
// on the instance via the MaxKeepImagesTag
6680
func DefaultMaxKeepImages() (int, error) {
6781
return envDefaultInt("DEFAULT_MAX_KEEP_IMAGES", "2")
6882
}
83+
84+
// RebootOnImageTag returns the name of the EC2 tag to look at to see if the
85+
// instance should be rebooted or not when an image is made. If not supplied,
86+
// the value will default to that of "DEFAULT_REBOOT_ON_IMAGE"
87+
func RebootOnImageTag() string {
88+
return envDefault("REBOOT_ON_IMAGE_TAG", "lambda-ebs-backup/reboot-on-image")
89+
}
90+
91+
// DefaultRebootOnImage determines the default behavior for rebooting when we
92+
// take an image of an ec2 instance. If not specified, it defaults to true as
93+
// this is the aws CreateImage default.
94+
func DefaultRebootOnImage() bool {
95+
return envDefaultBool("DEFAULT_REBOOT_ON_IMAGE", true)
96+
}

0 commit comments

Comments
 (0)