github.com/HashDataInc/packer@v1.3.2/common/step_provision.go (about)

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