2016-04-14 20:29:27 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-04-14 20:29:27 -04:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-04-14 20:29:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateImageFromMachine creates an image with the specified attributes
|
|
|
|
// from the machine with the given ID, and waits for the image to be created.
|
|
|
|
// The machine must be in the "stopped" state prior to this step being run.
|
|
|
|
type StepCreateImageFromMachine struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateImageFromMachine) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 10:17:42 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2016-04-14 20:29:27 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
machineId := state.Get("machine").(string)
|
|
|
|
|
|
|
|
ui.Say("Creating image from source machine...")
|
|
|
|
|
2018-09-18 10:17:42 -04:00
|
|
|
imageId, err := driver.CreateImageFromMachine(machineId, *config)
|
2016-04-14 20:29:27 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Problem creating image from machine: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for image to become available...")
|
|
|
|
err = driver.WaitForImageCreation(imageId, 10*time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Problem waiting for image to become available: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("image", imageId)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateImageFromMachine) Cleanup(state multistep.StateBag) {
|
|
|
|
// No cleanup
|
|
|
|
}
|