Merge pull request #5211 from hashicorp/4653_dont_delete_snapshots
use block device mappings to know whether a snapshot existed before p…
This commit is contained in:
commit
968eb94b80
|
@ -256,6 +256,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
KeyID: b.config.AMIKmsKeyId,
|
KeyID: b.config.AMIKmsKeyId,
|
||||||
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
||||||
Name: b.config.AMIName,
|
Name: b.config.AMIName,
|
||||||
|
AMIMappings: b.config.AMIBlockDevices.AMIMappings,
|
||||||
},
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
&awscommon.StepAMIRegionCopy{
|
||||||
AccessConfig: &b.config.AccessConfig,
|
AccessConfig: &b.config.AccessConfig,
|
||||||
|
|
|
@ -15,6 +15,7 @@ type StepCreateEncryptedAMICopy struct {
|
||||||
KeyID string
|
KeyID string
|
||||||
EncryptBootVolume bool
|
EncryptBootVolume bool
|
||||||
Name string
|
Name string
|
||||||
|
AMIMappings []BlockDevice
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateEncryptedAMICopy) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateEncryptedAMICopy) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -116,9 +117,18 @@ func (s *StepCreateEncryptedAMICopy) Run(state multistep.StateBag) multistep.Ste
|
||||||
ui.Say("Deleting unencrypted snapshots")
|
ui.Say("Deleting unencrypted snapshots")
|
||||||
snapshots := state.Get("snapshots").(map[string][]string)
|
snapshots := state.Get("snapshots").(map[string][]string)
|
||||||
|
|
||||||
|
OuterLoop:
|
||||||
for _, blockDevice := range unencImage.BlockDeviceMappings {
|
for _, blockDevice := range unencImage.BlockDeviceMappings {
|
||||||
if blockDevice.Ebs != nil && blockDevice.Ebs.SnapshotId != nil {
|
if blockDevice.Ebs != nil && blockDevice.Ebs.SnapshotId != nil {
|
||||||
ui.Message(fmt.Sprintf("Snapshot ID: %s", *blockDevice.Ebs.SnapshotId))
|
// If this packer run didn't create it, then don't delete it
|
||||||
|
for _, origDevice := range s.AMIMappings {
|
||||||
|
if origDevice.SnapshotId == *blockDevice.Ebs.SnapshotId {
|
||||||
|
ui.Message(fmt.Sprintf("Keeping Snapshot ID: %s", *blockDevice.Ebs.SnapshotId))
|
||||||
|
break OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Message(fmt.Sprintf("Deleting Snapshot ID: %s", *blockDevice.Ebs.SnapshotId))
|
||||||
deleteSnapOpts := &ec2.DeleteSnapshotInput{
|
deleteSnapOpts := &ec2.DeleteSnapshotInput{
|
||||||
SnapshotId: aws.String(*blockDevice.Ebs.SnapshotId),
|
SnapshotId: aws.String(*blockDevice.Ebs.SnapshotId),
|
||||||
}
|
}
|
||||||
|
|
|
@ -191,6 +191,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
KeyID: b.config.AMIKmsKeyId,
|
KeyID: b.config.AMIKmsKeyId,
|
||||||
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
||||||
Name: b.config.AMIName,
|
Name: b.config.AMIName,
|
||||||
|
AMIMappings: b.config.AMIBlockDevices.AMIMappings,
|
||||||
},
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
&awscommon.StepAMIRegionCopy{
|
||||||
AccessConfig: &b.config.AccessConfig,
|
AccessConfig: &b.config.AccessConfig,
|
||||||
|
|
Loading…
Reference in New Issue