|
|
|
|
@ -23,17 +23,17 @@ type StepRunSourceInstance struct {
|
|
|
|
|
instance *ec2.Instance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
|
keyName := state["keyPair"].(string)
|
|
|
|
|
securityGroupId := state["securityGroupId"].(string)
|
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
|
keyName := state.Get("keyPair").(string)
|
|
|
|
|
securityGroupId := state.Get("securityGroupId").(string)
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
|
|
userData := s.UserData
|
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
state["error"] = fmt.Errorf("Problem reading user data file: %s", err)
|
|
|
|
|
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -56,27 +56,27 @@ func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
|
|
|
|
|
ui.Say("Launching a source AWS instance...")
|
|
|
|
|
imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
|
|
|
|
|
if err != nil {
|
|
|
|
|
state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
|
|
|
|
|
state.Put("error", fmt.Errorf("There was a problem with the source AMI: %s", err))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(imageResp.Images) != 1 {
|
|
|
|
|
state["error"] = fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI)
|
|
|
|
|
state.Put("error", fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
|
|
|
|
|
state["error"] = fmt.Errorf(
|
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
|
"The provided source AMI has an invalid root device type.\n"+
|
|
|
|
|
"Expected '%s', got '%s'.",
|
|
|
|
|
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
|
|
|
|
|
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType))
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error launching source instance: %s", err)
|
|
|
|
|
state["error"] = err
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
ui.Error(err.Error())
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
@ -95,7 +95,7 @@ func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
|
|
|
|
|
latestInstance, err := WaitForState(&stateChange)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
|
|
|
|
|
state["error"] = err
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
ui.Error(err.Error())
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
@ -112,18 +112,18 @@ func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state["instance"] = s.instance
|
|
|
|
|
state.Put("instance", s.instance)
|
|
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
|
|
|
|
|
func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
|
if s.instance == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
|
|
ui.Say("Terminating the source AWS instance...")
|
|
|
|
|
if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
|
|
|
|
|
|