2013-05-22 16:25:03 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2013-05-22 16:25:03 -04:00
|
|
|
// A provisioner is responsible for installing and configuring software
|
|
|
|
// on a machine prior to building the actual image.
|
|
|
|
type Provisioner interface {
|
2013-06-06 20:02:02 -04:00
|
|
|
// Prepare is called with a set of configurations to setup the
|
|
|
|
// internal state of the provisioner. The multiple configurations
|
|
|
|
// should be merged in some sane way.
|
2013-06-06 20:07:42 -04:00
|
|
|
Prepare(...interface{}) error
|
2013-05-22 16:25:03 -04:00
|
|
|
|
|
|
|
// Provision is called to actually provision the machine. A UI is
|
|
|
|
// given to communicate with the user, and a communicator is given that
|
|
|
|
// is guaranteed to be connected to some machine so that provisioning
|
|
|
|
// can be done.
|
2013-06-26 20:50:25 -04:00
|
|
|
Provision(Ui, Communicator) error
|
2013-08-31 02:21:15 -04:00
|
|
|
|
|
|
|
// Cancel is called to cancel the provisioning. This is usually called
|
|
|
|
// while Provision is still being called. The Provisioner should act
|
|
|
|
// to stop its execution as quickly as possible in a race-free way.
|
|
|
|
Cancel()
|
2013-05-22 16:25:03 -04:00
|
|
|
}
|
2013-05-24 00:13:18 -04:00
|
|
|
|
|
|
|
// A Hook implementation that runs the given provisioners.
|
|
|
|
type ProvisionHook struct {
|
|
|
|
// The provisioners to run as part of the hook. These should already
|
|
|
|
// be prepared (by calling Prepare) at some earlier stage.
|
|
|
|
Provisioners []Provisioner
|
2013-08-31 02:39:29 -04:00
|
|
|
|
|
|
|
lock sync.Mutex
|
|
|
|
runningProvisioner Provisioner
|
2013-05-24 00:13:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs the provisioners in order.
|
2013-06-26 20:50:25 -04:00
|
|
|
func (h *ProvisionHook) Run(name string, ui Ui, comm Communicator, data interface{}) error {
|
2013-08-31 02:39:29 -04:00
|
|
|
defer func() {
|
|
|
|
h.lock.Lock()
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
|
|
|
h.runningProvisioner = nil
|
|
|
|
}()
|
|
|
|
|
2013-05-24 00:13:18 -04:00
|
|
|
for _, p := range h.Provisioners {
|
2013-08-31 02:39:29 -04:00
|
|
|
h.lock.Lock()
|
|
|
|
h.runningProvisioner = p
|
|
|
|
h.lock.Unlock()
|
|
|
|
|
2013-06-26 20:50:25 -04:00
|
|
|
if err := p.Provision(ui, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-24 00:13:18 -04:00
|
|
|
}
|
2013-06-26 20:50:25 -04:00
|
|
|
|
|
|
|
return nil
|
2013-05-24 00:13:18 -04:00
|
|
|
}
|
2013-08-30 20:03:55 -04:00
|
|
|
|
|
|
|
// Cancels the privisioners that are still running.
|
|
|
|
func (h *ProvisionHook) Cancel() {
|
2013-08-31 02:39:29 -04:00
|
|
|
h.lock.Lock()
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
|
|
|
if h.runningProvisioner != nil {
|
|
|
|
h.runningProvisioner.Cancel()
|
|
|
|
}
|
2013-08-30 20:03:55 -04:00
|
|
|
}
|