github.com/jerryclinesmith/packer@v0.3.7/common/step_provision.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/packer"
     6  	"log"
     7  	"time"
     8  )
     9  
    10  // StepProvision runs the provisioners.
    11  //
    12  // Uses:
    13  //   communicator packer.Communicator
    14  //   hook         packer.Hook
    15  //   ui           packer.Ui
    16  //
    17  // Produces:
    18  //   <nothing>
    19  type StepProvision struct{}
    20  
    21  func (*StepProvision) Run(state multistep.StateBag) multistep.StepAction {
    22  	comm := state.Get("communicator").(packer.Communicator)
    23  	hook := state.Get("hook").(packer.Hook)
    24  	ui := state.Get("ui").(packer.Ui)
    25  
    26  	// Run the provisioner in a goroutine so we can continually check
    27  	// for cancellations...
    28  	log.Println("Running the provision hook")
    29  	errCh := make(chan error, 1)
    30  	go func() {
    31  		errCh <- hook.Run(packer.HookProvision, ui, comm, nil)
    32  	}()
    33  
    34  	for {
    35  		select {
    36  		case err := <-errCh:
    37  			if err != nil {
    38  				state.Put("error", err)
    39  				return multistep.ActionHalt
    40  			}
    41  
    42  			return multistep.ActionContinue
    43  		case <-time.After(1 * time.Second):
    44  			if _, ok := state.GetOk(multistep.StateCancelled); ok {
    45  				log.Println("Cancelling provisioning due to interrupt...")
    46  				hook.Cancel()
    47  				return multistep.ActionHalt
    48  			}
    49  		}
    50  	}
    51  }
    52  
    53  func (*StepProvision) Cleanup(multistep.StateBag) {}