2013-07-29 21:13:22 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2013-07-29 21:13:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2015-04-05 17:58:48 -04:00
|
|
|
"github.com/mitchellh/packer/builder/amazon/common"
|
2013-07-29 21:13:22 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepInstanceInfo verifies that this builder is running on an EC2 instance.
|
|
|
|
type StepInstanceInfo struct{}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepInstanceInfo) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 21:13:22 -04:00
|
|
|
|
|
|
|
// Get our own instance ID
|
|
|
|
ui.Say("Gathering information about this EC2 instance...")
|
2015-04-05 17:58:48 -04:00
|
|
|
instanceIdBytes, err := common.GetInstanceMetaData("instance-id")
|
2013-07-29 21:13:22 -04:00
|
|
|
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.")
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:13:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceId := string(instanceIdBytes)
|
|
|
|
log.Printf("Instance ID: %s", instanceId)
|
|
|
|
|
|
|
|
// Query the entire instance metadata
|
2015-04-05 17:58:48 -04:00
|
|
|
instancesResp, err := ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{InstanceIDs: []*string{&instanceId}})
|
2013-07-29 21:13:22 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error getting instance data: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:13:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(instancesResp.Reservations) == 0 {
|
|
|
|
err := fmt.Errorf("Error getting instance data: no instance found.")
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:13:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-30 01:48:01 -04:00
|
|
|
instance := &instancesResp.Reservations[0].Instances[0]
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("instance", instance)
|
2013-07-29 21:13:22 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepInstanceInfo) Cleanup(multistep.StateBag) {}
|