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