Creates a final "cleanup" provisioner to run if an error occurs during a provisioning step, allowing users to perform any custom cleanup tasks that must happen on the VM before the VM is shut down and destroyed.

pull/8155/head
Megan Marsh 7 years ago
parent 2a662b451c
commit 5bd8fee708

@ -2,6 +2,7 @@ package common
import ( import (
"context" "context"
"fmt"
"log" "log"
"time" "time"
@ -22,7 +23,8 @@ type StepProvision struct {
Comm packer.Communicator Comm packer.Communicator
} }
func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepProvision) runWithHook(ctx context.Context, state multistep.StateBag, hooktype string) multistep.StepAction {
// hooktype will be either packer.HookProvision or packer.HookCleanupProvision
comm := s.Comm comm := s.Comm
if comm == nil { if comm == nil {
raw, ok := state.Get("communicator").(packer.Communicator) raw, ok := state.Get("communicator").(packer.Communicator)
@ -35,17 +37,29 @@ func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multi
// Run the provisioner in a goroutine so we can continually check // Run the provisioner in a goroutine so we can continually check
// for cancellations... // for cancellations...
log.Println("Running the provision hook") if hooktype == packer.HookProvision {
log.Println("Running the provision hook")
} else if hooktype == packer.HookCleanupProvision {
ui.Say("Provisioning step had errors: Running the cleanup provisioner, if present...")
}
errCh := make(chan error, 1) errCh := make(chan error, 1)
go func() { go func() {
errCh <- hook.Run(ctx, packer.HookProvision, ui, comm, nil) errCh <- hook.Run(ctx, hooktype, ui, comm, nil)
}() }()
for { for {
select { select {
case err := <-errCh: case err := <-errCh:
if err != nil { if err != nil {
state.Put("error", err) if hooktype == packer.HookProvision {
// We don't overwrite the error if it's a cleanup
// provisioner being run.
state.Put("error", err)
} else if hooktype == packer.HookCleanupProvision {
origErr := state.Get("error").(error)
state.Put("error", fmt.Errorf("Cleanup failed: %s. "+
"Original Provisioning error: %s", err, origErr))
}
return multistep.ActionHalt return multistep.ActionHalt
} }
@ -62,4 +76,15 @@ func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multi
} }
} }
func (*StepProvision) Cleanup(multistep.StateBag) {} func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
return s.runWithHook(ctx, state, packer.HookProvision)
}
func (s *StepProvision) Cleanup(state multistep.StateBag) {
// We have a "final" provisioner that gets defined by "on-error-script"
// which we only call if there's an error during the provision run and
// the "on-error-script" is defined.
if _, ok := state.GetOk("error"); ok {
s.runWithHook(context.Background(), state, packer.HookCleanupProvision)
}
}

@ -84,15 +84,16 @@ type Build interface {
// multiple files, of course, but it should be for only a single provider // multiple files, of course, but it should be for only a single provider
// (such as VirtualBox, EC2, etc.). // (such as VirtualBox, EC2, etc.).
type coreBuild struct { type coreBuild struct {
name string name string
builder Builder builder Builder
builderConfig interface{} builderConfig interface{}
builderType string builderType string
hooks map[string][]Hook hooks map[string][]Hook
postProcessors [][]coreBuildPostProcessor postProcessors [][]coreBuildPostProcessor
provisioners []coreBuildProvisioner provisioners []coreBuildProvisioner
templatePath string cleanupProvisioner coreBuildProvisioner
variables map[string]string templatePath string
variables map[string]string
debug bool debug bool
force bool force bool
@ -164,6 +165,17 @@ func (b *coreBuild) Prepare() (warn []string, err error) {
} }
} }
// Prepare the on-error-cleanup provisioner
if b.cleanupProvisioner.pType != "" {
configs := make([]interface{}, len(b.cleanupProvisioner.config), len(b.cleanupProvisioner.config)+1)
copy(configs, b.cleanupProvisioner.config)
configs = append(configs, packerConfig)
err = b.cleanupProvisioner.provisioner.Prepare(configs...)
if err != nil {
return
}
}
// Prepare the post-processors // Prepare the post-processors
for _, ppSeq := range b.postProcessors { for _, ppSeq := range b.postProcessors {
for _, corePP := range ppSeq { for _, corePP := range ppSeq {
@ -222,6 +234,17 @@ func (b *coreBuild) Run(ctx context.Context, originalUi Ui) ([]Artifact, error)
}) })
} }
if b.cleanupProvisioner.pType != "" {
hookedCleanupProvisioner := &HookedProvisioner{
b.cleanupProvisioner.provisioner,
b.cleanupProvisioner.config,
b.cleanupProvisioner.pType,
}
hooks[HookCleanupProvision] = []Hook{&ProvisionHook{
Provisioners: []*HookedProvisioner{hookedCleanupProvisioner},
}}
}
hook := &DispatchHook{Mapping: hooks} hook := &DispatchHook{Mapping: hooks}
artifacts := make([]Artifact, 0, 1) artifacts := make([]Artifact, 0, 1)

@ -112,6 +112,49 @@ func (c *Core) BuildNames() []string {
return r return r
} }
func (c *Core) generateCoreBuildProvisioner(rawP *template.Provisioner, rawName string) (coreBuildProvisioner, error) {
// Get the provisioner
cbp := coreBuildProvisioner{}
provisioner, err := c.components.Provisioner(rawP.Type)
if err != nil {
return cbp, fmt.Errorf(
"error initializing provisioner '%s': %s",
rawP.Type, err)
}
if provisioner == nil {
return cbp, fmt.Errorf(
"provisioner type not found: %s", rawP.Type)
}
// Get the configuration
config := make([]interface{}, 1, 2)
config[0] = rawP.Config
if rawP.Override != nil {
if override, ok := rawP.Override[rawName]; ok {
config = append(config, override)
}
}
// If we're pausing, we wrap the provisioner in a special pauser.
if rawP.PauseBefore > 0 {
provisioner = &PausedProvisioner{
PauseBefore: rawP.PauseBefore,
Provisioner: provisioner,
}
} else if rawP.Timeout > 0 {
provisioner = &TimeoutProvisioner{
Timeout: rawP.Timeout,
Provisioner: provisioner,
}
}
cbp = coreBuildProvisioner{
pType: rawP.Type,
provisioner: provisioner,
config: config,
}
return cbp, nil
}
// Build returns the Build object for the given name. // Build returns the Build object for the given name.
func (c *Core) Build(n string) (Build, error) { func (c *Core) Build(n string) (Build, error) {
// Setup the builder // Setup the builder
@ -140,46 +183,23 @@ func (c *Core) Build(n string) (Build, error) {
if rawP.OnlyExcept.Skip(rawName) { if rawP.OnlyExcept.Skip(rawName) {
continue continue
} }
cbp, err := c.generateCoreBuildProvisioner(rawP, rawName)
// Get the provisioner
provisioner, err := c.components.Provisioner(rawP.Type)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, err
"error initializing provisioner '%s': %s",
rawP.Type, err)
}
if provisioner == nil {
return nil, fmt.Errorf(
"provisioner type not found: %s", rawP.Type)
} }
// Get the configuration provisioners = append(provisioners, cbp)
config := make([]interface{}, 1, 2) }
config[0] = rawP.Config
if rawP.Override != nil {
if override, ok := rawP.Override[rawName]; ok {
config = append(config, override)
}
}
// If we're pausing, we wrap the provisioner in a special pauser. var cleanupProvisioner coreBuildProvisioner
if rawP.PauseBefore > 0 { if c.Template.CleanupProvisioner != nil {
provisioner = &PausedProvisioner{ // This is a special instantiation of the shell-local provisioner that
PauseBefore: rawP.PauseBefore, // is only run on error at end of provisioning step before other step
Provisioner: provisioner, // cleanup occurs.
} cleanupProvisioner, err = c.generateCoreBuildProvisioner(c.Template.CleanupProvisioner, rawName)
} else if rawP.Timeout > 0 { if err != nil {
provisioner = &TimeoutProvisioner{ return nil, err
Timeout: rawP.Timeout,
Provisioner: provisioner,
}
} }
provisioners = append(provisioners, coreBuildProvisioner{
pType: rawP.Type,
provisioner: provisioner,
config: config,
})
} }
// Setup the post-processors // Setup the post-processors
@ -232,14 +252,15 @@ func (c *Core) Build(n string) (Build, error) {
// TODO hooks one day // TODO hooks one day
return &coreBuild{ return &coreBuild{
name: n, name: n,
builder: builder, builder: builder,
builderConfig: configBuilder.Config, builderConfig: configBuilder.Config,
builderType: configBuilder.Type, builderType: configBuilder.Type,
postProcessors: postProcessors, postProcessors: postProcessors,
provisioners: provisioners, provisioners: provisioners,
templatePath: c.Template.Path, cleanupProvisioner: cleanupProvisioner,
variables: c.variables, templatePath: c.Template.Path,
variables: c.variables,
}, nil }, nil
} }

@ -6,6 +6,7 @@ import (
// This is the hook that should be fired for provisioners to run. // This is the hook that should be fired for provisioners to run.
const HookProvision = "packer_provision" const HookProvision = "packer_provision"
const HookCleanupProvision = "packer_cleanup_provision"
// A Hook is used to hook into an arbitrarily named location in a build, // 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. // allowing custom behavior to run at certain points along a build.

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -28,6 +29,7 @@ type rawTemplate struct {
Push map[string]interface{} `json:"push,omitempty"` Push map[string]interface{} `json:"push,omitempty"`
PostProcessors []interface{} `mapstructure:"post-processors" json:"post-processors,omitempty"` PostProcessors []interface{} `mapstructure:"post-processors" json:"post-processors,omitempty"`
Provisioners []interface{} `json:"provisioners,omitempty"` Provisioners []interface{} `json:"provisioners,omitempty"`
CleanupProvisioner interface{} `mapstructure:"on-error-script" json:"on-error-script,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"` Variables map[string]interface{} `json:"variables,omitempty"`
SensitiveVariables []string `mapstructure:"sensitive-variables" json:"sensitive-variables,omitempty"` SensitiveVariables []string `mapstructure:"sensitive-variables" json:"sensitive-variables,omitempty"`
@ -242,6 +244,38 @@ func (r *rawTemplate) Template() (*Template, error) {
result.Provisioners = append(result.Provisioners, &p) result.Provisioners = append(result.Provisioners, &p)
} }
// Gather the on-error-script
log.Printf("r.CleanupProvisioner is %#v", r.CleanupProvisioner)
if r.CleanupProvisioner != nil {
var p Provisioner
if err := r.decoder(&p, nil).Decode(r.CleanupProvisioner); err != nil {
errs = multierror.Append(errs, fmt.Errorf(
"On Error Cleanup provisioner: %s", err))
}
// Type is required before any richer validation
log.Printf("p is %#v", p)
if p.Type == "" {
errs = multierror.Append(errs, fmt.Errorf(
"on error cleanup provisioner missing 'type'"))
}
// Set the raw configuration and delete any special keys
p.Config = r.CleanupProvisioner.(map[string]interface{})
delete(p.Config, "except")
delete(p.Config, "only")
delete(p.Config, "override")
delete(p.Config, "pause_before")
delete(p.Config, "type")
delete(p.Config, "timeout")
if len(p.Config) == 0 {
p.Config = nil
}
result.CleanupProvisioner = &p
}
// If we have errors, return those with a nil result // If we have errors, return those with a nil result
if errs != nil { if errs != nil {
return nil, errs return nil, errs

@ -24,6 +24,7 @@ type Template struct {
SensitiveVariables []*Variable SensitiveVariables []*Variable
Builders map[string]*Builder Builders map[string]*Builder
Provisioners []*Provisioner Provisioners []*Provisioner
CleanupProvisioner *Provisioner
PostProcessors [][]*PostProcessor PostProcessors [][]*PostProcessor
// RawContents is just the raw data for this template // RawContents is just the raw data for this template

Loading…
Cancel
Save