2013-08-21 16:53:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
"sync"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2013-08-21 16:53:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2013-08-22 18:03:30 -04:00
|
|
|
type StepAMIRegionCopy struct {
|
2015-05-28 11:31:22 -04:00
|
|
|
AccessConfig *AccessConfig
|
|
|
|
Regions []string
|
2015-06-05 07:15:48 -04:00
|
|
|
Name string
|
2013-08-21 16:53:07 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
amis := state.Get("amis").(map[string]string)
|
2015-04-05 17:58:48 -04:00
|
|
|
ami := amis[ec2conn.Config.Region]
|
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
|
|
|
|
errs := new(packer.MultiError)
|
2013-08-21 16:53:07 -04:00
|
|
|
for _, region := range s.Regions {
|
2015-06-04 06:16:44 -04:00
|
|
|
if region == ec2conn.Config.Region {
|
2015-06-08 12:25:56 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Avoid copying AMI to duplicate region %s", region))
|
2015-06-04 06:16:44 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-12 15:24:32 -05:00
|
|
|
wg.Add(1)
|
2013-08-22 18:32:24 -04:00
|
|
|
ui.Message(fmt.Sprintf("Copying to: %s", region))
|
2013-08-21 16:53:07 -04:00
|
|
|
|
2013-12-12 15:24:32 -05:00
|
|
|
go func(region string) {
|
|
|
|
defer wg.Done()
|
2015-06-05 07:15:48 -04:00
|
|
|
id, err := amiRegionCopy(state, s.AccessConfig, s.Name, ami, region, ec2conn.Config.Region)
|
2013-12-12 15:24:32 -05:00
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
amis[region] = id
|
|
|
|
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
|
|
|
|
// returns the resulting ID or error.
|
2015-06-05 07:15:48 -04:00
|
|
|
func amiRegionCopy(state multistep.StateBag, config *AccessConfig, name string, imageId string,
|
2015-04-05 17:58:48 -04:00
|
|
|
target string, source string) (string, error) {
|
2013-12-12 15:24:32 -05:00
|
|
|
|
|
|
|
// Connect to the region where the AMI will be copied to
|
2015-05-28 11:31:22 -04:00
|
|
|
awsConfig, err := config.Config()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2015-05-28 11:31:22 -04:00
|
|
|
awsConfig.Region = target
|
|
|
|
|
|
|
|
regionconn := ec2.New(awsConfig)
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := regionconn.CopyImage(&ec2.CopyImageInput{
|
|
|
|
SourceRegion: &source,
|
|
|
|
SourceImageID: &imageId,
|
2015-06-05 07:15:48 -04:00
|
|
|
Name: &name,
|
2013-12-12 15:24:32 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", 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
|
|
|
}
|
|
|
|
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "available",
|
2015-04-05 17:58:48 -04:00
|
|
|
Refresh: AMIStateRefreshFunc(regionconn, *resp.ImageID),
|
2013-12-12 15:24:32 -05:00
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := WaitForState(&stateChange); err != nil {
|
|
|
|
return "", fmt.Errorf("Error waiting for AMI (%s) in region (%s): %s",
|
2015-04-05 17:58:48 -04:00
|
|
|
*resp.ImageID, target, err)
|
2013-12-12 15:24:32 -05:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
return *resp.ImageID, nil
|
2013-12-12 15:24:32 -05:00
|
|
|
}
|