github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/triton/step_create_image_from_machine.go (about)

     1  package triton
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/packer/packer"
     8  	"github.com/mitchellh/multistep"
     9  )
    10  
    11  // StepCreateImageFromMachine creates an image with the specified attributes
    12  // from the machine with the given ID, and waits for the image to be created.
    13  // The machine must be in the "stopped" state prior to this step being run.
    14  type StepCreateImageFromMachine struct{}
    15  
    16  func (s *StepCreateImageFromMachine) Run(state multistep.StateBag) multistep.StepAction {
    17  	config := state.Get("config").(Config)
    18  	driver := state.Get("driver").(Driver)
    19  	ui := state.Get("ui").(packer.Ui)
    20  
    21  	machineId := state.Get("machine").(string)
    22  
    23  	ui.Say("Creating image from source machine...")
    24  
    25  	imageId, err := driver.CreateImageFromMachine(machineId, config)
    26  	if err != nil {
    27  		state.Put("error", fmt.Errorf("Problem creating image from machine: %s", err))
    28  		return multistep.ActionHalt
    29  	}
    30  
    31  	ui.Say("Waiting for image to become available...")
    32  	err = driver.WaitForImageCreation(imageId, 10*time.Minute)
    33  	if err != nil {
    34  		state.Put("error", fmt.Errorf("Problem waiting for image to become available: %s", err))
    35  		return multistep.ActionHalt
    36  	}
    37  
    38  	state.Put("image", imageId)
    39  
    40  	return multistep.ActionContinue
    41  }
    42  
    43  func (s *StepCreateImageFromMachine) Cleanup(state multistep.StateBag) {
    44  	// No cleanup
    45  }