2013-05-22 16:25:03 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
import (
|
2013-12-21 00:39:23 -05:00
|
|
|
"fmt"
|
2017-03-12 18:46:35 -04:00
|
|
|
"log"
|
2013-08-31 02:39:29 -04:00
|
|
|
"sync"
|
2013-12-21 00:36:41 -05:00
|
|
|
"time"
|
2013-08-31 02:39:29 -04:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2017-11-04 16:06:36 -04:00
|
|
|
// A HookedProvisioner represents a provisioner and information describing it
|
|
|
|
type HookedProvisioner struct {
|
|
|
|
Provisioner Provisioner
|
|
|
|
Config interface{}
|
|
|
|
TypeName string
|
|
|
|
}
|
|
|
|
|
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.
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners []*HookedProvisioner
|
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 {
|
2015-06-15 13:26:46 -04:00
|
|
|
// Shortcut
|
|
|
|
if len(h.Provisioners) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if comm == nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"No communicator found for provisioners! This is usually because the\n" +
|
|
|
|
"`communicator` config was set to \"none\". If you have any provisioners\n" +
|
|
|
|
"then a communicator is required. Please fix this to continue.")
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
defer func() {
|
|
|
|
h.lock.Lock()
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
|
|
|
h.runningProvisioner = nil
|
|
|
|
}()
|
|
|
|
|
2017-11-03 02:31:32 -04:00
|
|
|
for _, p := range h.Provisioners {
|
2013-08-31 02:39:29 -04:00
|
|
|
h.lock.Lock()
|
2017-11-04 16:06:36 -04:00
|
|
|
h.runningProvisioner = p.Provisioner
|
2013-08-31 02:39:29 -04:00
|
|
|
h.lock.Unlock()
|
|
|
|
|
2017-11-04 16:06:36 -04:00
|
|
|
ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config)
|
2017-11-03 02:31:32 -04:00
|
|
|
|
2017-11-04 16:06:36 -04:00
|
|
|
err := p.Provisioner.Provision(ui, comm)
|
2017-11-03 02:31:32 -04:00
|
|
|
|
2017-05-04 19:29:21 -04:00
|
|
|
ts.End(err)
|
|
|
|
if err != nil {
|
2013-06-26 20:50:25 -04:00
|
|
|
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
|
|
|
|
2018-03-13 22:15:21 -04:00
|
|
|
// Cancels the provisioners that are still running.
|
2013-08-30 20:03:55 -04:00
|
|
|
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
|
|
|
}
|
2013-12-21 00:36:41 -05:00
|
|
|
|
|
|
|
// PausedProvisioner is a Provisioner implementation that pauses before
|
|
|
|
// the provisioner is actually run.
|
|
|
|
type PausedProvisioner struct {
|
|
|
|
PauseBefore time.Duration
|
|
|
|
Provisioner Provisioner
|
|
|
|
|
|
|
|
cancelCh chan struct{}
|
|
|
|
doneCh chan struct{}
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PausedProvisioner) Prepare(raws ...interface{}) error {
|
|
|
|
return p.Provisioner.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PausedProvisioner) Provision(ui Ui, comm Communicator) error {
|
|
|
|
p.lock.Lock()
|
|
|
|
cancelCh := make(chan struct{})
|
|
|
|
p.cancelCh = cancelCh
|
|
|
|
|
|
|
|
// Setup the done channel, which is trigger when we're done
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
p.doneCh = doneCh
|
|
|
|
p.lock.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
if p.cancelCh == cancelCh {
|
|
|
|
p.cancelCh = nil
|
|
|
|
}
|
|
|
|
if p.doneCh == doneCh {
|
|
|
|
p.doneCh = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Use a select to determine if we get cancelled during the wait
|
2013-12-21 00:39:23 -05:00
|
|
|
ui.Say(fmt.Sprintf("Pausing %s before the next provisioner...", p.PauseBefore))
|
2013-12-21 00:36:41 -05:00
|
|
|
select {
|
|
|
|
case <-time.After(p.PauseBefore):
|
|
|
|
case <-cancelCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
provDoneCh := make(chan error, 1)
|
|
|
|
go p.provision(provDoneCh, ui, comm)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-provDoneCh:
|
|
|
|
return err
|
|
|
|
case <-cancelCh:
|
|
|
|
p.Provisioner.Cancel()
|
|
|
|
return <-provDoneCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PausedProvisioner) Cancel() {
|
|
|
|
var doneCh chan struct{}
|
|
|
|
|
|
|
|
p.lock.Lock()
|
|
|
|
if p.cancelCh != nil {
|
|
|
|
close(p.cancelCh)
|
|
|
|
p.cancelCh = nil
|
|
|
|
}
|
|
|
|
if p.doneCh != nil {
|
|
|
|
doneCh = p.doneCh
|
|
|
|
}
|
|
|
|
p.lock.Unlock()
|
|
|
|
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PausedProvisioner) provision(result chan<- error, ui Ui, comm Communicator) {
|
|
|
|
result <- p.Provisioner.Provision(ui, comm)
|
|
|
|
}
|
2017-03-12 18:46:35 -04:00
|
|
|
|
2018-06-07 18:35:36 -04:00
|
|
|
// DebuggedProvisioner is a Provisioner implementation that waits until a key
|
|
|
|
// press before the provisioner is actually run.
|
2017-03-12 18:46:35 -04:00
|
|
|
type DebuggedProvisioner struct {
|
|
|
|
Provisioner Provisioner
|
|
|
|
|
|
|
|
cancelCh chan struct{}
|
|
|
|
doneCh chan struct{}
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DebuggedProvisioner) Prepare(raws ...interface{}) error {
|
|
|
|
return p.Provisioner.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DebuggedProvisioner) Provision(ui Ui, comm Communicator) error {
|
|
|
|
p.lock.Lock()
|
|
|
|
cancelCh := make(chan struct{})
|
|
|
|
p.cancelCh = cancelCh
|
|
|
|
|
|
|
|
// Setup the done channel, which is trigger when we're done
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
p.doneCh = doneCh
|
|
|
|
p.lock.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
if p.cancelCh == cancelCh {
|
|
|
|
p.cancelCh = nil
|
|
|
|
}
|
|
|
|
if p.doneCh == doneCh {
|
|
|
|
p.doneCh = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Use a select to determine if we get cancelled during the wait
|
|
|
|
message := "Pausing before the next provisioner . Press enter to continue."
|
|
|
|
|
|
|
|
result := make(chan string, 1)
|
|
|
|
go func() {
|
|
|
|
line, err := ui.Ask(message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error asking for input: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result <- line
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-result:
|
|
|
|
case <-cancelCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
provDoneCh := make(chan error, 1)
|
|
|
|
go p.provision(provDoneCh, ui, comm)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-provDoneCh:
|
|
|
|
return err
|
|
|
|
case <-cancelCh:
|
|
|
|
p.Provisioner.Cancel()
|
|
|
|
return <-provDoneCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DebuggedProvisioner) Cancel() {
|
|
|
|
var doneCh chan struct{}
|
|
|
|
|
|
|
|
p.lock.Lock()
|
|
|
|
if p.cancelCh != nil {
|
|
|
|
close(p.cancelCh)
|
|
|
|
p.cancelCh = nil
|
|
|
|
}
|
|
|
|
if p.doneCh != nil {
|
|
|
|
doneCh = p.doneCh
|
|
|
|
}
|
|
|
|
p.lock.Unlock()
|
|
|
|
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DebuggedProvisioner) provision(result chan<- error, ui Ui, comm Communicator) {
|
|
|
|
result <- p.Provisioner.Provision(ui, comm)
|
|
|
|
}
|