2013-08-21 16:53:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-08-21 16:53:07 -04:00
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"sync"
|
|
|
|
|
2015-07-28 20:10:21 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-08-21 16:53:07 -04:00
|
|
|
)
|
|
|
|
|
2013-08-22 18:03:30 -04:00
|
|
|
type StepAMIRegionCopy struct {
|
2017-05-26 15:21:07 -04:00
|
|
|
AccessConfig *AccessConfig
|
|
|
|
Regions []string
|
2019-04-15 19:44:56 -04:00
|
|
|
AMIKmsKeyId string
|
2017-05-26 15:21:07 -04:00
|
|
|
RegionKeyIds map[string]string
|
2018-11-26 05:33:44 -05:00
|
|
|
EncryptBootVolume *bool // nil means preserve
|
2017-05-26 15:21:07 -04:00
|
|
|
Name string
|
2013-08-21 16:53:07 -04:00
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
amis := state.Get("amis").(map[string]string)
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshots := state.Get("snapshots").(map[string][]string)
|
2015-07-28 20:10:21 -04:00
|
|
|
ami := amis[*ec2conn.Config.Region]
|
2013-08-21 16:53:07 -04:00
|
|
|
|
2018-11-26 05:33:44 -05:00
|
|
|
if s.EncryptBootVolume != nil {
|
|
|
|
// encrypt_boot was set, we now have to copy the temporary
|
|
|
|
// AMI with required encryption setting.
|
|
|
|
// temp image was created by stepCreateAMI.
|
|
|
|
s.Regions = append(s.Regions, *ec2conn.Config.Region)
|
2019-04-17 15:55:30 -04:00
|
|
|
if s.RegionKeyIds == nil {
|
|
|
|
s.RegionKeyIds = make(map[string]string)
|
|
|
|
}
|
2019-04-15 19:44:56 -04:00
|
|
|
s.RegionKeyIds[*ec2conn.Config.Region] = s.AMIKmsKeyId
|
2018-11-26 05:33:44 -05:00
|
|
|
}
|
|
|
|
|
2013-08-21 16:53:07 -04:00
|
|
|
if len(s.Regions) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:32:24 -04:00
|
|
|
ui.Say(fmt.Sprintf("Copying AMI (%s) to other regions...", ami))
|
2013-12-12 15:24:32 -05:00
|
|
|
|
|
|
|
var lock sync.Mutex
|
|
|
|
var wg sync.WaitGroup
|
2017-05-31 15:27:45 -04:00
|
|
|
var regKeyID string
|
2013-12-12 15:24:32 -05:00
|
|
|
errs := new(packer.MultiError)
|
2018-11-26 05:32:54 -05:00
|
|
|
|
|
|
|
wg.Add(len(s.Regions))
|
2013-08-21 16:53:07 -04:00
|
|
|
for _, region := range s.Regions {
|
2018-11-26 05:33:44 -05:00
|
|
|
if region == *ec2conn.Config.Region && s.EncryptBootVolume == nil {
|
2015-06-08 12:25:56 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
2015-06-08 12:34:53 -04:00
|
|
|
"Avoiding copying AMI to duplicate region %s", region))
|
2015-06-04 06:16:44 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:32:24 -04:00
|
|
|
ui.Message(fmt.Sprintf("Copying to: %s", region))
|
2013-08-21 16:53:07 -04:00
|
|
|
|
2018-11-26 05:33:44 -05:00
|
|
|
if s.EncryptBootVolume != nil && *s.EncryptBootVolume {
|
2017-05-31 16:41:32 -04:00
|
|
|
regKeyID = s.RegionKeyIds[region]
|
2017-05-26 15:21:07 -04:00
|
|
|
}
|
|
|
|
|
2013-12-12 15:24:32 -05:00
|
|
|
go func(region string) {
|
|
|
|
defer wg.Done()
|
2018-11-26 05:33:44 -05:00
|
|
|
id, snapshotIds, err := amiRegionCopy(ctx, state, s.AccessConfig, s.Name, ami, region, *ec2conn.Config.Region, regKeyID, s.EncryptBootVolume)
|
2013-12-12 15:24:32 -05:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
amis[region] = id
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshots[region] = snapshotIds
|
2013-12-12 15:24:32 -05:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(errs, err)
|
|
|
|
}
|
|
|
|
}(region)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mitchellh): Wait but also allow for cancels to go through...
|
|
|
|
ui.Message("Waiting for all copies to complete...")
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// If there were errors, show them
|
|
|
|
if len(errs.Errors) > 0 {
|
|
|
|
state.Put("error", errs)
|
|
|
|
ui.Error(errs.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-08-21 16:53:07 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("amis", amis)
|
2013-08-21 16:53:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepAMIRegionCopy) Cleanup(state multistep.StateBag) {
|
2013-08-21 16:53:07 -04:00
|
|
|
// No cleanup...
|
|
|
|
}
|
2013-12-12 15:24:32 -05:00
|
|
|
|
|
|
|
// amiRegionCopy does a copy for the given AMI to the target region and
|
2016-09-11 08:37:24 -04:00
|
|
|
// returns the resulting ID and snapshot IDs, or error.
|
2018-11-26 05:33:44 -05:00
|
|
|
func amiRegionCopy(ctx context.Context, state multistep.StateBag, config *AccessConfig, name, imageId,
|
|
|
|
target, source, keyId string, encrypt *bool) (string, []string, error) {
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshotIds := []string{}
|
2013-12-12 15:24:32 -05:00
|
|
|
|
|
|
|
// Connect to the region where the AMI will be copied to
|
2017-03-01 19:43:09 -05:00
|
|
|
session, err := config.Session()
|
2015-05-28 11:31:22 -04:00
|
|
|
if err != nil {
|
2016-09-11 08:37:24 -04:00
|
|
|
return "", snapshotIds, err
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2018-11-26 05:33:44 -05:00
|
|
|
|
2017-03-01 19:43:09 -05:00
|
|
|
regionconn := ec2.New(session.Copy(&aws.Config{
|
2019-03-04 19:22:52 -05:00
|
|
|
Region: aws.String(target),
|
2019-01-23 19:58:48 -05:00
|
|
|
}))
|
2017-03-01 19:43:09 -05:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := regionconn.CopyImage(&ec2.CopyImageInput{
|
|
|
|
SourceRegion: &source,
|
2015-08-17 20:44:01 -04:00
|
|
|
SourceImageId: &imageId,
|
2015-06-05 07:15:48 -04:00
|
|
|
Name: &name,
|
2018-11-26 05:33:44 -05:00
|
|
|
Encrypted: encrypt,
|
|
|
|
KmsKeyId: aws.String(keyId),
|
2013-12-12 15:24:32 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-09-11 08:37:24 -04:00
|
|
|
return "", snapshotIds, fmt.Errorf("Error Copying AMI (%s) to region (%s): %s",
|
2015-04-05 17:58:48 -04:00
|
|
|
imageId, target, err)
|
2013-12-12 15:24:32 -05:00
|
|
|
}
|
|
|
|
|
2018-05-31 14:29:04 -04:00
|
|
|
// Wait for the image to become ready
|
2018-06-01 19:17:30 -04:00
|
|
|
if err := WaitUntilAMIAvailable(ctx, regionconn, *resp.ImageId); err != nil {
|
2016-09-11 08:37:24 -04:00
|
|
|
return "", snapshotIds, fmt.Errorf("Error waiting for AMI (%s) in region (%s): %s",
|
2015-08-17 20:44:01 -04:00
|
|
|
*resp.ImageId, target, err)
|
2013-12-12 15:24:32 -05:00
|
|
|
}
|
|
|
|
|
2016-09-11 08:37:24 -04:00
|
|
|
// Getting snapshot IDs out of the copied AMI
|
|
|
|
describeImageResp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{resp.ImageId}})
|
|
|
|
if err != nil {
|
|
|
|
return "", snapshotIds, fmt.Errorf("Error describing copied AMI (%s) in region (%s): %s",
|
|
|
|
imageId, target, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blockDeviceMapping := range describeImageResp.Images[0].BlockDeviceMappings {
|
2017-03-08 12:12:37 -05:00
|
|
|
if blockDeviceMapping.Ebs != nil && blockDeviceMapping.Ebs.SnapshotId != nil {
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshotIds = append(snapshotIds, *blockDeviceMapping.Ebs.SnapshotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *resp.ImageId, snapshotIds, nil
|
2013-12-12 15:24:32 -05:00
|
|
|
}
|