2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-07 06:20:33 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 05:35:14 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateInstance struct{}
|
|
|
|
|
|
|
|
func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
var (
|
|
|
|
driver = state.Get("driver").(Driver)
|
|
|
|
ui = state.Get("ui").(packer.Ui)
|
|
|
|
publicKey = state.Get("publicKey").(string)
|
|
|
|
)
|
|
|
|
|
|
|
|
ui.Say("Creating instance...")
|
|
|
|
|
|
|
|
instanceID, err := driver.CreateInstance(publicKey)
|
|
|
|
if err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Problem creating instance: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("instance_id", instanceID)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Created instance (%s).", instanceID))
|
|
|
|
|
|
|
|
ui.Say("Waiting for instance to enter 'RUNNING' state...")
|
|
|
|
|
2017-08-02 06:53:14 -04:00
|
|
|
if err = driver.WaitForInstanceState(instanceID, []string{"STARTING", "PROVISIONING"}, "RUNNING"); err != nil {
|
|
|
|
err = fmt.Errorf("Error waiting for instance to start: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Instance 'RUNNING'.")
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
idRaw, ok := state.GetOk("instance_id")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id := idRaw.(string)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Terminating instance (%s)...", id))
|
|
|
|
|
|
|
|
if err := driver.TerminateInstance(id); err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error terminating instance. Please terminate manually: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := driver.WaitForInstanceState(id, []string{"TERMINATING"}, "TERMINATED")
|
|
|
|
if err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
err = fmt.Errorf("Error terminating instance. Please terminate manually: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Terminated instance.")
|
|
|
|
}
|