packer-cn/common/step_provision.go

53 lines
1.1 KiB
Go
Raw Normal View History

2013-07-16 02:43:01 -04:00
package common
import (
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
2013-08-31 02:28:31 -04:00
"time"
2013-07-16 02:43:01 -04:00
)
// StepProvision runs the provisioners.
//
// Uses:
// communicator packer.Communicator
// hook packer.Hook
// ui packer.Ui
//
// Produces:
// <nothing>
type StepProvision struct{}
func (*StepProvision) Run(state map[string]interface{}) multistep.StepAction {
comm := state["communicator"].(packer.Communicator)
hook := state["hook"].(packer.Hook)
ui := state["ui"].(packer.Ui)
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() {
errCh <- hook.Run(packer.HookProvision, ui, comm, nil)
}()
for {
select {
case err := <-errCh:
if err != nil {
state["error"] = err
return multistep.ActionHalt
}
2013-07-16 02:43:01 -04:00
2013-08-31 02:28:31 -04:00
return multistep.ActionContinue
case <-time.After(1 * time.Second):
if _, ok := state[multistep.StateCancelled]; ok {
hook.Cancel()
return multistep.ActionHalt
}
}
}
2013-07-16 02:43:01 -04:00
}
func (*StepProvision) Cleanup(map[string]interface{}) {}