Commit 3a416bb1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon/chroot: step to gather instance info

parent fa92377a
......@@ -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{},
}
......
......@@ -3,19 +3,22 @@ package chroot
import (
"fmt"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
)
// StepCheckEC2 verifies that this builder is running on an EC2 instance.
type StepCheckEC2 struct{}
// StepInstanceInfo verifies that this builder is running on an EC2 instance.
type StepInstanceInfo struct{}
func (s *StepCheckEC2) Run(state map[string]interface{}) multistep.StepAction {
func (s *StepInstanceInfo) Run(state map[string]interface{}) multistep.StepAction {
ec2conn := state["ec2"].(*ec2.EC2)
ui := state["ui"].(packer.Ui)
ui.Say("Verifying we're on an EC2 instance...")
id, err := aws.GetMetaData("instance-id")
// 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(
......@@ -25,9 +28,30 @@ func (s *StepCheckEC2) Run(state map[string]interface{}) multistep.StepAction {
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("Instance ID: %s", string(id))
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 *StepCheckEC2) Cleanup(map[string]interface{}) {}
func (s *StepInstanceInfo) Cleanup(map[string]interface{}) {}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment