diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index ecb9f8605..4ab739bbb 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -72,7 +72,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ - &StepCheckEC2{}, + &StepInstanceInfo{}, &StepSourceAMIInfo{}, &StepCreateVolume{}, } diff --git a/builder/amazon/chroot/step_check_ec2.go b/builder/amazon/chroot/step_check_ec2.go deleted file mode 100644 index 4e8d5e6e8..000000000 --- a/builder/amazon/chroot/step_check_ec2.go +++ /dev/null @@ -1,33 +0,0 @@ -package chroot - -import ( - "fmt" - "github.com/mitchellh/goamz/aws" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" - "log" -) - -// StepCheckEC2 verifies that this builder is running on an EC2 instance. -type StepCheckEC2 struct{} - -func (s *StepCheckEC2) Run(state map[string]interface{}) multistep.StepAction { - ui := state["ui"].(packer.Ui) - - ui.Say("Verifying we're on an EC2 instance...") - id, err := aws.GetMetaData("instance-id") - if err != nil { - log.Printf("Error: %s", err) - err := fmt.Errorf( - "Error retrieving the ID of the instance Packer is running on.\n" + - "Please verify Packer is running on a proper AWS EC2 instance.") - state["error"] = err - ui.Error(err.Error()) - return multistep.ActionHalt - } - log.Printf("Instance ID: %s", string(id)) - - return multistep.ActionContinue -} - -func (s *StepCheckEC2) Cleanup(map[string]interface{}) {} diff --git a/builder/amazon/chroot/step_instance_info.go b/builder/amazon/chroot/step_instance_info.go new file mode 100644 index 000000000..b318f161e --- /dev/null +++ b/builder/amazon/chroot/step_instance_info.go @@ -0,0 +1,57 @@ +package chroot + +import ( + "fmt" + "github.com/mitchellh/goamz/aws" + "github.com/mitchellh/goamz/ec2" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "log" +) + +// StepInstanceInfo verifies that this builder is running on an EC2 instance. +type StepInstanceInfo struct{} + +func (s *StepInstanceInfo) Run(state map[string]interface{}) multistep.StepAction { + ec2conn := state["ec2"].(*ec2.EC2) + ui := state["ui"].(packer.Ui) + + // Get our own instance ID + ui.Say("Gathering information about this EC2 instance...") + instanceIdBytes, err := aws.GetMetaData("instance-id") + if err != nil { + log.Printf("Error: %s", err) + err := fmt.Errorf( + "Error retrieving the ID of the instance Packer is running on.\n" + + "Please verify Packer is running on a proper AWS EC2 instance.") + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + instanceId := string(instanceIdBytes) + log.Printf("Instance ID: %s", instanceId) + + // Query the entire instance metadata + instancesResp, err := ec2conn.Instances([]string{instanceId}, ec2.NewFilter()) + if err != nil { + err := fmt.Errorf("Error getting instance data: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + if len(instancesResp.Reservations) == 0 { + err := fmt.Errorf("Error getting instance data: no instance found.") + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } + + instance := instancesResp.Reservations[0].Instances[0] + state["instance"] = instance + + return multistep.ActionContinue +} + +func (s *StepInstanceInfo) Cleanup(map[string]interface{}) {}