2013-08-21 16:53:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-12-12 15:24:32 -05:00
|
|
|
"sync"
|
2013-08-21 16:53:07 -04:00
|
|
|
)
|
|
|
|
|
2013-08-22 18:03:30 -04:00
|
|
|
type StepAMIRegionCopy struct {
|
2013-08-21 16:53:07 -04:00
|
|
|
Regions []string
|
|
|
|
}
|
|
|
|
|
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)
|
2013-08-21 16:53:07 -04:00
|
|
|
ami := amis[ec2conn.Region.Name]
|
|
|
|
|
|
|
|
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 {
|
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()
|
|
|
|
id, err := amiRegionCopy(state, ec2conn.Auth, ami,
|
|
|
|
aws.Regions[region], ec2conn.Region)
|
|
|
|
|
|
|
|
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.
|
|
|
|
func amiRegionCopy(state multistep.StateBag, auth aws.Auth, imageId string,
|
|
|
|
target aws.Region, source aws.Region) (string, error) {
|
|
|
|
|
|
|
|
// Connect to the region where the AMI will be copied to
|
|
|
|
regionconn := ec2.New(auth, target)
|
|
|
|
resp, err := regionconn.CopyImage(&ec2.CopyImage{
|
|
|
|
SourceRegion: source.Name,
|
|
|
|
SourceImageId: imageId,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error Copying AMI (%s) to region (%s): %s",
|
|
|
|
imageId, target, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "available",
|
|
|
|
Refresh: AMIStateRefreshFunc(regionconn, resp.ImageId),
|
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := WaitForState(&stateChange); err != nil {
|
|
|
|
return "", fmt.Errorf("Error waiting for AMI (%s) in region (%s): %s",
|
|
|
|
resp.ImageId, target, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.ImageId, nil
|
|
|
|
}
|