2013-05-22 16:25:03 -04:00
|
|
|
package packer
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
import (
|
2019-03-19 12:29:00 -04:00
|
|
|
"context"
|
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"
|
2019-12-14 06:32:38 -05:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
|
2013-08-31 02:39:29 -04:00
|
|
|
)
|
|
|
|
|
2017-11-04 16:06:36 -04:00
|
|
|
// A HookedProvisioner represents a provisioner and information describing it
|
|
|
|
type HookedProvisioner struct {
|
2020-12-01 17:48:55 -05:00
|
|
|
Provisioner packersdk.Provisioner
|
2017-11-04 16:06:36 -04:00
|
|
|
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-05-24 00:13:18 -04:00
|
|
|
}
|
|
|
|
|
2020-07-07 05:28:07 -04:00
|
|
|
// BuilderDataCommonKeys is the list of common keys that all builder will
|
|
|
|
// return
|
|
|
|
var BuilderDataCommonKeys = []string{
|
|
|
|
"ID",
|
2020-07-07 05:52:08 -04:00
|
|
|
// The following correspond to communicator-agnostic functions that are }
|
|
|
|
// part of the SSH and WinRM communicator implementations. These functions
|
|
|
|
// are not part of the communicator interface, but are stored on the
|
|
|
|
// Communicator Config and return the appropriate values rather than
|
|
|
|
// depending on the actual communicator config values. E.g "Password"
|
|
|
|
// reprosents either WinRMPassword or SSHPassword, which makes this more
|
|
|
|
// useful if a template contains multiple builds.
|
2020-07-07 05:28:07 -04:00
|
|
|
"Host",
|
|
|
|
"Port",
|
|
|
|
"User",
|
|
|
|
"Password",
|
|
|
|
"ConnType",
|
|
|
|
"PackerRunUUID",
|
|
|
|
"PackerHTTPPort",
|
|
|
|
"PackerHTTPIP",
|
|
|
|
"PackerHTTPAddr",
|
|
|
|
"SSHPublicKey",
|
|
|
|
"SSHPrivateKey",
|
|
|
|
"WinRMPassword",
|
|
|
|
}
|
|
|
|
|
2019-12-14 06:32:38 -05:00
|
|
|
// Provisioners interpolate most of their fields in the prepare stage; this
|
|
|
|
// placeholder map helps keep fields that are only generated at build time from
|
|
|
|
// accidentally being interpolated into empty strings at prepare time.
|
|
|
|
// This helper function generates the most basic placeholder data which should
|
|
|
|
// be accessible to the provisioners. It is used to initialize provisioners, to
|
|
|
|
// force validation using the `generated` template function. In the future,
|
|
|
|
// custom generated data could be passed into provisioners from builders to
|
|
|
|
// enable specialized builder-specific (but still validated!!) access to builder
|
|
|
|
// data.
|
2020-07-06 05:18:02 -04:00
|
|
|
func BasicPlaceholderData() map[string]string {
|
|
|
|
placeholderData := map[string]string{}
|
2020-07-07 05:28:07 -04:00
|
|
|
for _, key := range BuilderDataCommonKeys {
|
2020-11-04 18:44:05 -05:00
|
|
|
placeholderData[key] = fmt.Sprintf("Build_%s. "+packerbuilderdata.PlaceholderMsg, key)
|
2020-07-07 05:28:07 -04:00
|
|
|
}
|
2019-12-14 06:32:38 -05:00
|
|
|
|
|
|
|
// Backwards-compatability: WinRM Password can get through without forcing
|
|
|
|
// the generated func validation.
|
|
|
|
placeholderData["WinRMPassword"] = "{{.WinRMPassword}}"
|
|
|
|
|
|
|
|
return placeholderData
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func CastDataToMap(data interface{}) map[string]interface{} {
|
2020-02-13 11:35:23 -05:00
|
|
|
|
|
|
|
if interMap, ok := data.(map[string]interface{}); ok {
|
|
|
|
// null and file builder sometimes don't use a communicator and
|
|
|
|
// therefore don't go through RPC
|
|
|
|
return interMap
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
// Provisioners expect a map[string]interface{} in their data field, but
|
|
|
|
// it gets converted into a map[interface]interface on the way over the
|
|
|
|
// RPC. Check that data can be cast into such a form, and cast it.
|
|
|
|
cast := make(map[string]interface{})
|
|
|
|
interMap, ok := data.(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Unable to read map[string]interface out of data."+
|
|
|
|
"Using empty interface: %#v", data)
|
|
|
|
} else {
|
|
|
|
for key, val := range interMap {
|
|
|
|
keyString, ok := key.(string)
|
|
|
|
if ok {
|
|
|
|
cast[keyString] = val
|
|
|
|
} else {
|
|
|
|
log.Printf("Error casting generated data key to a string.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cast
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:13:18 -04:00
|
|
|
// Runs the provisioners in order.
|
2020-11-19 18:10:00 -05:00
|
|
|
func (h *ProvisionHook) Run(ctx context.Context, name string, ui packersdk.Ui, comm packersdk.Communicator, data interface{}) error {
|
2015-06-15 13:26:46 -04:00
|
|
|
// Shortcut
|
2020-07-02 09:07:29 -04:00
|
|
|
if len(h.Provisioners) == 0 {
|
2015-06-15 13:26:46 -04:00
|
|
|
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.")
|
|
|
|
}
|
2017-11-03 02:31:32 -04:00
|
|
|
for _, p := range h.Provisioners {
|
2017-11-04 16:06:36 -04:00
|
|
|
ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config)
|
2017-11-03 02:31:32 -04:00
|
|
|
|
2020-07-02 09:07:29 -04:00
|
|
|
cast := CastDataToMap(data)
|
2019-12-12 13:59:44 -05:00
|
|
|
err := p.Provisioner.Provision(ctx, ui, comm, cast)
|
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
|
|
|
|
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
|
2020-12-01 17:48:55 -05:00
|
|
|
Provisioner packersdk.Provisioner
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PausedProvisioner) ConfigSpec() hcldec.ObjectSpec { return p.ConfigSpec() }
|
|
|
|
func (p *PausedProvisioner) FlatConfig() interface{} { return p.FlatConfig() }
|
2013-12-21 00:36:41 -05:00
|
|
|
func (p *PausedProvisioner) Prepare(raws ...interface{}) error {
|
|
|
|
return p.Provisioner.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *PausedProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2013-12-21 00:36:41 -05:00
|
|
|
|
|
|
|
// 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):
|
2019-03-19 13:11:19 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
return p.Provisioner.Provision(ctx, ui, comm, generatedData)
|
2013-12-21 00:36:41 -05:00
|
|
|
}
|
2017-03-12 18:46:35 -04:00
|
|
|
|
2020-04-16 05:58:54 -04:00
|
|
|
// RetriedProvisioner is a Provisioner implementation that retries
|
|
|
|
// the provisioner whenever there's an error.
|
|
|
|
type RetriedProvisioner struct {
|
|
|
|
MaxRetries int
|
2020-12-01 17:48:55 -05:00
|
|
|
Provisioner packersdk.Provisioner
|
2020-04-16 05:58:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RetriedProvisioner) ConfigSpec() hcldec.ObjectSpec { return r.ConfigSpec() }
|
|
|
|
func (r *RetriedProvisioner) FlatConfig() interface{} { return r.FlatConfig() }
|
|
|
|
func (r *RetriedProvisioner) Prepare(raws ...interface{}) error {
|
|
|
|
return r.Provisioner.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (r *RetriedProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2020-04-16 05:58:54 -04:00
|
|
|
if ctx.Err() != nil { // context was cancelled
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.Provisioner.Provision(ctx, ui, comm, generatedData)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
leftTries := r.MaxRetries
|
|
|
|
for ; leftTries > 0; leftTries-- {
|
|
|
|
if ctx.Err() != nil { // context was cancelled
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Provisioner failed with %q, retrying with %d trie(s) left", err, leftTries))
|
|
|
|
|
|
|
|
err := r.Provisioner.Provision(ctx, ui, comm, generatedData)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ui.Say("retry limit reached.")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-12-01 17:48:55 -05:00
|
|
|
Provisioner packersdk.Provisioner
|
2017-03-12 18:46:35 -04:00
|
|
|
|
|
|
|
cancelCh chan struct{}
|
|
|
|
doneCh chan struct{}
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *DebuggedProvisioner) ConfigSpec() hcldec.ObjectSpec { return p.ConfigSpec() }
|
|
|
|
func (p *DebuggedProvisioner) FlatConfig() interface{} { return p.FlatConfig() }
|
2017-03-12 18:46:35 -04:00
|
|
|
func (p *DebuggedProvisioner) Prepare(raws ...interface{}) error {
|
|
|
|
return p.Provisioner.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *DebuggedProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2017-03-12 18:46:35 -04:00
|
|
|
// 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:
|
2019-03-19 13:11:19 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2017-03-12 18:46:35 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
return p.Provisioner.Provision(ctx, ui, comm, generatedData)
|
2017-03-12 18:46:35 -04:00
|
|
|
}
|