github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/step_stop_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  // StepStopMachine stops the machine with the given Machine ID, and waits
    13  // for it to reach the stopped state.
    14  type StepStopMachine struct{}
    15  
    16  func (s *StepStopMachine) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    17  	driver := state.Get("driver").(Driver)
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	machineId := state.Get("machine").(string)
    21  
    22  	ui.Say(fmt.Sprintf("Stopping source machine (%s)...", machineId))
    23  	err := driver.StopMachine(machineId)
    24  	if err != nil {
    25  		state.Put("error", fmt.Errorf("Problem stopping source machine: %s", err))
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	ui.Say(fmt.Sprintf("Waiting for source machine to stop (%s)...", machineId))
    30  	err = driver.WaitForMachineState(machineId, "stopped", 10*time.Minute)
    31  	if err != nil {
    32  		state.Put("error", fmt.Errorf("Problem waiting for source machine to stop: %s", err))
    33  		return multistep.ActionHalt
    34  	}
    35  
    36  	return multistep.ActionContinue
    37  }
    38  
    39  func (s *StepStopMachine) Cleanup(state multistep.StateBag) {
    40  	// Explicitly don't clean up here as StepCreateSourceMachine will do it if necessary
    41  	// and there is no real meaning to cleaning this up.
    42  }