github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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  	Comm packer.Communicator
    21  }
    22  
    23  func (s *StepProvision) Run(state multistep.StateBag) multistep.StepAction {
    24  	comm := s.Comm
    25  	if comm == nil {
    26  		comm = state.Get("communicator").(packer.Communicator)
    27  	}
    28  
    29  	hook := state.Get("hook").(packer.Hook)
    30  	ui := state.Get("ui").(packer.Ui)
    31  
    32  	// Run the provisioner in a goroutine so we can continually check
    33  	// for cancellations...
    34  	log.Println("Running the provision hook")
    35  	errCh := make(chan error, 1)
    36  	go func() {
    37  		errCh <- hook.Run(packer.HookProvision, ui, comm, nil)
    38  	}()
    39  
    40  	for {
    41  		select {
    42  		case err := <-errCh:
    43  			if err != nil {
    44  				state.Put("error", err)
    45  				return multistep.ActionHalt
    46  			}
    47  
    48  			return multistep.ActionContinue
    49  		case <-time.After(1 * time.Second):
    50  			if _, ok := state.GetOk(multistep.StateCancelled); ok {
    51  				log.Println("Cancelling provisioning due to interrupt...")
    52  				hook.Cancel()
    53  				return multistep.ActionHalt
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func (*StepProvision) Cleanup(multistep.StateBag) {}