github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/chroot/step_instance_info.go (about)

     1  package chroot
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/goamz/aws"
     6  	"github.com/mitchellh/goamz/ec2"
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  	"log"
    10  )
    11  
    12  // StepInstanceInfo verifies that this builder is running on an EC2 instance.
    13  type StepInstanceInfo struct{}
    14  
    15  func (s *StepInstanceInfo) Run(state map[string]interface{}) multistep.StepAction {
    16  	ec2conn := state["ec2"].(*ec2.EC2)
    17  	ui := state["ui"].(packer.Ui)
    18  
    19  	// Get our own instance ID
    20  	ui.Say("Gathering information about this EC2 instance...")
    21  	instanceIdBytes, err := aws.GetMetaData("instance-id")
    22  	if err != nil {
    23  		log.Printf("Error: %s", err)
    24  		err := fmt.Errorf(
    25  			"Error retrieving the ID of the instance Packer is running on.\n" +
    26  				"Please verify Packer is running on a proper AWS EC2 instance.")
    27  		state["error"] = err
    28  		ui.Error(err.Error())
    29  		return multistep.ActionHalt
    30  	}
    31  
    32  	instanceId := string(instanceIdBytes)
    33  	log.Printf("Instance ID: %s", instanceId)
    34  
    35  	// Query the entire instance metadata
    36  	instancesResp, err := ec2conn.Instances([]string{instanceId}, ec2.NewFilter())
    37  	if err != nil {
    38  		err := fmt.Errorf("Error getting instance data: %s", err)
    39  		state["error"] = err
    40  		ui.Error(err.Error())
    41  		return multistep.ActionHalt
    42  	}
    43  
    44  	if len(instancesResp.Reservations) == 0 {
    45  		err := fmt.Errorf("Error getting instance data: no instance found.")
    46  		state["error"] = err
    47  		ui.Error(err.Error())
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	instance := &instancesResp.Reservations[0].Instances[0]
    52  	state["instance"] = instance
    53  
    54  	return multistep.ActionContinue
    55  }
    56  
    57  func (s *StepInstanceInfo) Cleanup(map[string]interface{}) {}