github.phpd.cn/hashicorp/packer@v1.3.2/builder/hyperv/common/step_reboot_vm.go (about)

     1  package common
     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  type StepRebootVm struct {
    13  }
    14  
    15  func (s *StepRebootVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    16  	driver := state.Get("driver").(Driver)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	errorMsg := "Error rebooting vm: %s"
    20  	vmName := state.Get("vmName").(string)
    21  
    22  	ui.Say("Rebooting vm...")
    23  
    24  	err := driver.RestartVirtualMachine(vmName)
    25  	if err != nil {
    26  		err := fmt.Errorf(errorMsg, err)
    27  		state.Put("error", err)
    28  		ui.Error(err.Error())
    29  		return multistep.ActionHalt
    30  	}
    31  
    32  	ui.Say("Waiting the VM to complete rebooting (2 minutes)...")
    33  
    34  	sleepTime := time.Minute * 2
    35  	time.Sleep(sleepTime)
    36  
    37  	return multistep.ActionContinue
    38  }
    39  
    40  func (s *StepRebootVm) Cleanup(state multistep.StateBag) {
    41  	// do nothing
    42  }