2013-07-16 02:43:01 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-16 02:43:01 -04:00
|
|
|
"log"
|
2013-08-31 02:28:31 -04:00
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-16 02:43:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepProvision runs the provisioners.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// communicator packer.Communicator
|
|
|
|
// hook packer.Hook
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-11-12 11:24:16 -05:00
|
|
|
type StepProvision struct {
|
|
|
|
Comm packer.Communicator
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:50:33 -04:00
|
|
|
func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-11-12 11:24:16 -05:00
|
|
|
comm := s.Comm
|
|
|
|
if comm == nil {
|
2015-06-15 13:26:46 -04:00
|
|
|
raw, ok := state.Get("communicator").(packer.Communicator)
|
|
|
|
if ok {
|
|
|
|
comm = raw.(packer.Communicator)
|
|
|
|
}
|
2013-11-12 11:24:16 -05:00
|
|
|
}
|
2013-08-31 15:17:59 -04:00
|
|
|
hook := state.Get("hook").(packer.Hook)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-16 02:43:01 -04:00
|
|
|
|
2013-08-31 02:28:31 -04:00
|
|
|
// Run the provisioner in a goroutine so we can continually check
|
|
|
|
// for cancellations...
|
2013-07-16 02:43:01 -04:00
|
|
|
log.Println("Running the provision hook")
|
2013-08-31 02:28:31 -04:00
|
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
2019-03-22 09:50:33 -04:00
|
|
|
errCh <- hook.Run(ctx, packer.HookProvision, ui, comm, nil)
|
2013-08-31 02:28:31 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
2013-08-31 15:17:59 -04:00
|
|
|
state.Put("error", err)
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-16 02:43:01 -04:00
|
|
|
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionContinue
|
2019-03-27 07:29:09 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("Cancelling provisioning due to context cancellation: %s", ctx.Err())
|
|
|
|
return multistep.ActionHalt
|
2013-08-31 02:28:31 -04:00
|
|
|
case <-time.After(1 * time.Second):
|
2013-08-31 15:17:59 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-08-31 02:39:29 -04:00
|
|
|
log.Println("Cancelling provisioning due to interrupt...")
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 02:43:01 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
func (*StepProvision) Cleanup(multistep.StateBag) {}
|