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