2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-02-13 05:35:14 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-07 06:20:33 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateInstance struct{}
|
|
|
|
|
2018-06-26 05:05:56 -04:00
|
|
|
func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-02-13 05:35:14 -05:00
|
|
|
var (
|
2018-08-29 08:01:55 -04:00
|
|
|
driver = state.Get("driver").(Driver)
|
|
|
|
ui = state.Get("ui").(packer.Ui)
|
|
|
|
config = state.Get("config").(*Config)
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
ui.Say("Creating instance...")
|
|
|
|
|
2018-08-29 08:01:55 -04:00
|
|
|
instanceID, err := driver.CreateInstance(ctx, string(config.Comm.SSHPublicKey))
|
2017-02-13 05:35:14 -05:00
|
|
|
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...")
|
|
|
|
|
2018-06-26 05:05:56 -04:00
|
|
|
if err = driver.WaitForInstanceState(ctx, instanceID, []string{"STARTING", "PROVISIONING"}, "RUNNING"); err != nil {
|
2017-08-02 06:53:14 -04:00
|
|
|
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))
|
|
|
|
|
2018-06-26 05:05:56 -04:00
|
|
|
if err := driver.TerminateInstance(context.TODO(), 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
|
|
|
|
}
|
|
|
|
|
2018-06-26 05:05:56 -04:00
|
|
|
err := driver.WaitForInstanceState(context.TODO(), id, []string{"TERMINATING"}, "TERMINATED")
|
2017-02-13 05:35:14 -05:00
|
|
|
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.")
|
|
|
|
}
|