github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/step_create_image_from_machine.go (about)

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