2015-06-08 18:08:39 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-08 18:08:39 -04:00
|
|
|
"fmt"
|
2019-02-05 17:07:04 -05:00
|
|
|
"log"
|
2015-06-08 18:08:39 -04:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2019-02-05 17:07:04 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-08 18:08:39 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2019-02-05 17:07:04 -05:00
|
|
|
retry "github.com/hashicorp/packer/common"
|
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"
|
2015-06-08 18:08:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepPreValidate provides an opportunity to pre-validate any configuration for
|
|
|
|
// the build before actually doing any time consuming work
|
|
|
|
//
|
|
|
|
type StepPreValidate struct {
|
2015-06-12 14:05:15 -04:00
|
|
|
DestAmiName string
|
|
|
|
ForceDeregister bool
|
2015-06-08 18:08:39 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepPreValidate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-08 18:08:39 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-02-05 17:07:04 -05:00
|
|
|
|
|
|
|
if accessConfig, ok := state.GetOk("access_config"); ok {
|
|
|
|
accessconf := accessConfig.(*AccessConfig)
|
|
|
|
if !accessconf.VaultAWSEngine.Empty() {
|
|
|
|
// loop over the authentication a few times to give vault-created creds
|
|
|
|
// time to become eventually-consistent
|
|
|
|
ui.Say("You're using Vault-generated AWS credentials. It may take a " +
|
|
|
|
"few moments for them to become available on AWS. Waiting...")
|
|
|
|
err := retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) {
|
|
|
|
ec2conn, err := accessconf.NewEC2Connection()
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
_, err = listEC2Regions(ec2conn)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "AuthFailure" {
|
|
|
|
log.Printf("Waiting for Vault-generated AWS credentials" +
|
|
|
|
" to pass authentication... trying again.")
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-02-11 14:41:58 -05:00
|
|
|
return true, err
|
2019-02-05 17:07:04 -05:00
|
|
|
} else {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Was unable to Authenticate to AWS using Vault-"+
|
|
|
|
"Generated Credentials within the retry timeout."))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if amiConfig, ok := state.GetOk("ami_config"); ok {
|
|
|
|
amiconf := amiConfig.(*AMIConfig)
|
|
|
|
if !amiconf.AMISkipRegionValidation {
|
|
|
|
regionsToValidate := append(amiconf.AMIRegions, accessconf.RawRegion)
|
|
|
|
err := accessconf.ValidateRegion(regionsToValidate...)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("error validating regions: %v", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 14:05:15 -04:00
|
|
|
if s.ForceDeregister {
|
|
|
|
ui.Say("Force Deregister flag found, skipping prevalidating AMI Name")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2015-06-08 18:08:39 -04:00
|
|
|
|
2017-09-26 19:04:40 -04:00
|
|
|
ui.Say(fmt.Sprintf("Prevalidating AMI Name: %s", s.DestAmiName))
|
2015-06-08 18:08:39 -04:00
|
|
|
resp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{
|
2016-11-01 17:08:04 -04:00
|
|
|
Filters: []*ec2.Filter{{
|
2015-06-08 18:08:39 -04:00
|
|
|
Name: aws.String("name"),
|
|
|
|
Values: []*string{aws.String(s.DestAmiName)},
|
|
|
|
}}})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Images) > 0 {
|
2015-08-17 20:44:01 -04:00
|
|
|
err := fmt.Errorf("Error: name conflicts with an existing AMI: %s", *resp.Images[0].ImageId)
|
2015-06-08 18:08:39 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepPreValidate) Cleanup(multistep.StateBag) {}
|