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 ...@@ -72,7 +72,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
&StepCheckEC2{}, &StepInstanceInfo{},
&StepSourceAMIInfo{}, &StepSourceAMIInfo{},
&StepCreateVolume{}, &StepCreateVolume{},
} }
......
...@@ -3,19 +3,22 @@ package chroot ...@@ -3,19 +3,22 @@ package chroot
import ( import (
"fmt" "fmt"
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log" "log"
) )
// StepCheckEC2 verifies that this builder is running on an EC2 instance. // StepInstanceInfo verifies that this builder is running on an EC2 instance.
type StepCheckEC2 struct{} 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 := state["ui"].(packer.Ui)
ui.Say("Verifying we're on an EC2 instance...") // Get our own instance ID
id, err := aws.GetMetaData("instance-id") ui.Say("Gathering information about this EC2 instance...")
instanceIdBytes, err := aws.GetMetaData("instance-id")
if err != nil { if err != nil {
log.Printf("Error: %s", err) log.Printf("Error: %s", err)
err := fmt.Errorf( err := fmt.Errorf(
...@@ -25,9 +28,30 @@ func (s *StepCheckEC2) Run(state map[string]interface{}) multistep.StepAction { ...@@ -25,9 +28,30 @@ func (s *StepCheckEC2) Run(state map[string]interface{}) multistep.StepAction {
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt 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 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