github.com/rothwerx/packer@v0.9.0/builder/qemu/step_wait_for_shutdown.go (about)

     1  package qemu
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/packer"
     6  	"time"
     7  )
     8  
     9  // stepWaitForShutdown waits for the shutdown of the currently running
    10  // qemu VM.
    11  type stepWaitForShutdown struct {
    12  	Message string
    13  }
    14  
    15  func (s *stepWaitForShutdown) Run(state multistep.StateBag) multistep.StepAction {
    16  	driver := state.Get("driver").(Driver)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	stopCh := make(chan struct{})
    20  	defer close(stopCh)
    21  
    22  	cancelCh := make(chan struct{})
    23  	go func() {
    24  		for {
    25  			if _, ok := state.GetOk(multistep.StateCancelled); ok {
    26  				close(cancelCh)
    27  				return
    28  			}
    29  
    30  			select {
    31  			case <-stopCh:
    32  				return
    33  			case <-time.After(100 * time.Millisecond):
    34  			}
    35  		}
    36  	}()
    37  
    38  	ui.Say(s.Message)
    39  	driver.WaitForShutdown(cancelCh)
    40  	return multistep.ActionContinue
    41  }
    42  
    43  func (s *stepWaitForShutdown) Cleanup(state multistep.StateBag) {}