2013-05-10 19:58:50 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-30 20:26:51 -04:00
|
|
|
import (
|
2019-03-22 09:50:33 -04:00
|
|
|
"context"
|
2013-08-30 20:26:51 -04:00
|
|
|
)
|
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// This is the hook that should be fired for provisioners to run.
|
|
|
|
const HookProvision = "packer_provision"
|
2019-09-24 12:44:19 -04:00
|
|
|
const HookCleanupProvision = "packer_cleanup_provision"
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2013-05-10 19:58:50 -04:00
|
|
|
// A Hook is used to hook into an arbitrarily named location in a build,
|
|
|
|
// allowing custom behavior to run at certain points along a build.
|
|
|
|
//
|
|
|
|
// Run is called when the hook is called, with the name of the hook and
|
|
|
|
// arbitrary data associated with it. To know what format the data is in,
|
|
|
|
// you must reference the documentation for the specific hook you're interested
|
2013-05-11 12:51:49 -04:00
|
|
|
// in. In addition to that, the Hook is given access to a UI so that it can
|
|
|
|
// output things to the user.
|
2013-08-30 20:03:55 -04:00
|
|
|
//
|
2019-07-25 00:49:51 -04:00
|
|
|
// The first context argument controls cancellation, the context will usually
|
2013-08-30 20:03:55 -04:00
|
|
|
// be called when Run is still in progress so the mechanism that handles this
|
2019-04-03 11:14:55 -04:00
|
|
|
// must be race-free. Cancel should attempt to cancel the hook in the quickest,
|
|
|
|
// safest way possible.
|
2013-05-10 19:58:50 -04:00
|
|
|
type Hook interface {
|
2019-03-22 09:50:33 -04:00
|
|
|
Run(context.Context, string, Ui, Communicator, interface{}) error
|
2013-05-10 19:58:50 -04:00
|
|
|
}
|
2013-05-11 13:27:07 -04:00
|
|
|
|
|
|
|
// A Hook implementation that dispatches based on an internal mapping.
|
|
|
|
type DispatchHook struct {
|
|
|
|
Mapping map[string][]Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs the hook with the given name by dispatching it to the proper
|
|
|
|
// hooks if a mapping exists. If a mapping doesn't exist, then nothing
|
|
|
|
// happens.
|
2019-03-22 09:50:33 -04:00
|
|
|
func (h *DispatchHook) Run(ctx context.Context, name string, ui Ui, comm Communicator, data interface{}) error {
|
2013-08-31 01:59:13 -04:00
|
|
|
|
2013-05-11 13:27:07 -04:00
|
|
|
hooks, ok := h.Mapping[name]
|
|
|
|
if !ok {
|
|
|
|
// No hooks for that name. No problem.
|
2013-06-26 20:50:25 -04:00
|
|
|
return nil
|
2013-05-11 13:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range hooks {
|
2019-03-22 09:50:33 -04:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
2013-08-30 20:26:51 -04:00
|
|
|
}
|
2019-03-22 09:50:33 -04:00
|
|
|
if err := hook.Run(ctx, name, ui, comm, data); err != nil {
|
2013-06-26 20:50:25 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-11 13:27:07 -04:00
|
|
|
}
|
2013-06-26 20:50:25 -04:00
|
|
|
|
|
|
|
return nil
|
2013-05-11 13:27:07 -04:00
|
|
|
}
|