@@ -14,16 +14,18 @@ import (
14
14
15
15
// ManagerOpts are options to configure the backup manager
16
16
type ManagerOpts struct {
17
- Client * ec2.EC2
17
+ client * ec2.EC2
18
18
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
24
25
25
26
DefaultImageNameTemplate * template.Template
26
27
DefaultMaxKeepImages int
28
+ DefaultRebootOnImage bool
27
29
28
30
Verbose bool
29
31
}
@@ -33,13 +35,16 @@ type ManagerOpts struct {
33
35
func NewManagerOptsFromConfig (client * ec2.EC2 ) (* ManagerOpts , error ) {
34
36
var err error
35
37
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 (),
43
48
}
44
49
45
50
opts .DefaultImageNameTemplate , err = template .New ("default-image-name" ).Parse (config .DefaultImageNameFormat ())
@@ -53,22 +58,10 @@ func NewManagerOptsFromConfig(client *ec2.EC2) (*ManagerOpts, error) {
53
58
54
59
// Manager manages backups/images of ec2 resources(volumes, instances, etc.)
55
60
type Manager struct {
56
- client * ec2. EC2
61
+ * ManagerOpts
57
62
58
63
volumes []* ec2.Volume
59
64
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
72
65
}
73
66
74
67
// NewManager creates a new backup manager from the provided options
@@ -77,19 +70,12 @@ func NewManager(opts *ManagerOpts) (*Manager, error) {
77
70
volumes : make ([]* ec2.Volume , 0 ),
78
71
instances : make ([]* ec2.Instance , 0 ),
79
72
}
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
+
87
74
if opts .DefaultImageNameTemplate == nil {
88
75
return nil , fmt .Errorf ("DefaultImageNameTemplate is a required field for ManagerOpts" )
89
76
}
90
77
91
- m .DefaultImageNameTemplate = opts .DefaultImageNameTemplate
92
- m .DefaultMaxKeepImages = opts .DefaultMaxKeepImages
78
+ m .ManagerOpts = opts
93
79
return m , nil
94
80
}
95
81
@@ -217,6 +203,7 @@ func (m *Manager) backupInstances() error {
217
203
image , err := m .client .CreateImage (& ec2.CreateImageInput {
218
204
InstanceId : i .InstanceId ,
219
205
Name : aws .String (imageName ),
206
+ NoReboot : aws .Bool (! m .instanceRebootParam (i )),
220
207
})
221
208
if err != nil {
222
209
m .logf (
@@ -300,6 +287,19 @@ func (m *Manager) formatImageName(i *ec2.Instance) (string, error) {
300
287
return buf .String (), err
301
288
}
302
289
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
+
303
303
func (m * Manager ) log (v ... interface {}) {
304
304
if m .Verbose {
305
305
fmt .Println (v ... )
0 commit comments