Merge pull request #8155 from hashicorp/on_error_script
error cleanup provisioner
This commit is contained in:
commit
f4dca172e3
|
@ -29,15 +29,13 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|||
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
||||
steps := []multistep.Step{}
|
||||
|
||||
if b.config.CommConfig.Type != "none" {
|
||||
steps = append(steps,
|
||||
&communicator.StepConnect{
|
||||
Config: &b.config.CommConfig,
|
||||
Host: CommHost(b.config.CommConfig.Host()),
|
||||
SSHConfig: b.config.CommConfig.SSHConfigFunc(),
|
||||
},
|
||||
)
|
||||
}
|
||||
steps = append(steps,
|
||||
&communicator.StepConnect{
|
||||
Config: &b.config.CommConfig,
|
||||
Host: CommHost(b.config.CommConfig.Host()),
|
||||
SSHConfig: b.config.CommConfig.SSHConfigFunc(),
|
||||
},
|
||||
)
|
||||
|
||||
steps = append(steps,
|
||||
new(common.StepProvision),
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildWithCleanupScript(t *testing.T) {
|
||||
c := &BuildCommand{
|
||||
Meta: testMetaFile(t),
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-parallel=false",
|
||||
filepath.Join(testFixture("cleanup-script"), "template.json"),
|
||||
}
|
||||
|
||||
defer cleanup()
|
||||
|
||||
// build should exit with error code!
|
||||
if code := c.Run(args); code == 0 {
|
||||
fatalCommand(t, c.Meta)
|
||||
}
|
||||
|
||||
if !fileExists("ducky.txt") {
|
||||
t.Errorf("Expected to find ducky.txt")
|
||||
}
|
||||
|
||||
}
|
|
@ -10,8 +10,11 @@ import (
|
|||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/packer/builder/file"
|
||||
"github.com/hashicorp/packer/builder/null"
|
||||
"github.com/hashicorp/packer/packer"
|
||||
shell_local "github.com/hashicorp/packer/post-processor/shell-local"
|
||||
"github.com/hashicorp/packer/provisioner/shell"
|
||||
sl "github.com/hashicorp/packer/provisioner/shell-local"
|
||||
)
|
||||
|
||||
func TestBuildOnlyFileCommaFlags(t *testing.T) {
|
||||
|
@ -176,7 +179,18 @@ func fileExists(filename string) bool {
|
|||
func testCoreConfigBuilder(t *testing.T) *packer.CoreConfig {
|
||||
components := packer.ComponentFinder{
|
||||
Builder: func(n string) (packer.Builder, error) {
|
||||
return &file.Builder{}, nil
|
||||
if n == "file" {
|
||||
return &file.Builder{}, nil
|
||||
}
|
||||
return &null.Builder{}, nil
|
||||
},
|
||||
Provisioner: func(n string) (packer.Provisioner, error) {
|
||||
if n == "shell" {
|
||||
return &shell.Provisioner{}, nil
|
||||
} else if n == "shell-local" {
|
||||
return &sl.Provisioner{}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("requested provisioner not implemented in this test")
|
||||
},
|
||||
PostProcessor: func(n string) (packer.PostProcessor, error) {
|
||||
return &shell_local.PostProcessor{}, nil
|
||||
|
@ -212,6 +226,7 @@ func cleanup() {
|
|||
os.RemoveAll("fuchsias.txt")
|
||||
os.RemoveAll("lilas.txt")
|
||||
os.RemoveAll("campanules.txt")
|
||||
os.RemoveAll("ducky.txt")
|
||||
}
|
||||
|
||||
func TestBuildCommand_ParseArgs(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"builders": [
|
||||
{
|
||||
"type": "null",
|
||||
"communicator": "none"
|
||||
}
|
||||
],
|
||||
"provisioners": [
|
||||
{
|
||||
"type": "shell-local",
|
||||
"inline": ["exit 2"]
|
||||
}
|
||||
],
|
||||
"error-cleanup-provisioner": {
|
||||
"type": "shell-local",
|
||||
"inline": ["echo 'rubber ducky'> ducky.txt"]
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ package common
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
|
@ -22,7 +23,8 @@ type StepProvision struct {
|
|||
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
|
||||
if comm == nil {
|
||||
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
|
||||
// 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)
|
||||
go func() {
|
||||
errCh <- hook.Run(ctx, packer.HookProvision, ui, comm, nil)
|
||||
errCh <- hook.Run(ctx, hooktype, ui, comm, nil)
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case err := <-errCh:
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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 "error-cleanup-provisioner"
|
||||
// which we only call if there's an error during the provision run and
|
||||
// the "error-cleanup-provisioner" 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
|
||||
// (such as VirtualBox, EC2, etc.).
|
||||
type coreBuild struct {
|
||||
name string
|
||||
builder Builder
|
||||
builderConfig interface{}
|
||||
builderType string
|
||||
hooks map[string][]Hook
|
||||
postProcessors [][]coreBuildPostProcessor
|
||||
provisioners []coreBuildProvisioner
|
||||
templatePath string
|
||||
variables map[string]string
|
||||
name string
|
||||
builder Builder
|
||||
builderConfig interface{}
|
||||
builderType string
|
||||
hooks map[string][]Hook
|
||||
postProcessors [][]coreBuildPostProcessor
|
||||
provisioners []coreBuildProvisioner
|
||||
cleanupProvisioner coreBuildProvisioner
|
||||
templatePath string
|
||||
variables map[string]string
|
||||
|
||||
debug 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
|
||||
for _, ppSeq := range b.postProcessors {
|
||||
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}
|
||||
artifacts := make([]Artifact, 0, 1)
|
||||
|
||||
|
|
107
packer/core.go
107
packer/core.go
|
@ -112,6 +112,49 @@ func (c *Core) BuildNames() []string {
|
|||
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.
|
||||
func (c *Core) Build(n string) (Build, error) {
|
||||
// Setup the builder
|
||||
|
@ -140,46 +183,23 @@ func (c *Core) Build(n string) (Build, error) {
|
|||
if rawP.OnlyExcept.Skip(rawName) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get the provisioner
|
||||
provisioner, err := c.components.Provisioner(rawP.Type)
|
||||
cbp, err := c.generateCoreBuildProvisioner(rawP, rawName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"error initializing provisioner '%s': %s",
|
||||
rawP.Type, err)
|
||||
}
|
||||
if provisioner == nil {
|
||||
return nil, fmt.Errorf(
|
||||
"provisioner type not found: %s", rawP.Type)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
provisioners = append(provisioners, cbp)
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
var cleanupProvisioner coreBuildProvisioner
|
||||
if c.Template.CleanupProvisioner != nil {
|
||||
// This is a special instantiation of the shell-local provisioner that
|
||||
// is only run on error at end of provisioning step before other step
|
||||
// cleanup occurs.
|
||||
cleanupProvisioner, err = c.generateCoreBuildProvisioner(c.Template.CleanupProvisioner, rawName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
provisioners = append(provisioners, coreBuildProvisioner{
|
||||
pType: rawP.Type,
|
||||
provisioner: provisioner,
|
||||
config: config,
|
||||
})
|
||||
}
|
||||
|
||||
// Setup the post-processors
|
||||
|
@ -232,14 +252,15 @@ func (c *Core) Build(n string) (Build, error) {
|
|||
// TODO hooks one day
|
||||
|
||||
return &coreBuild{
|
||||
name: n,
|
||||
builder: builder,
|
||||
builderConfig: configBuilder.Config,
|
||||
builderType: configBuilder.Type,
|
||||
postProcessors: postProcessors,
|
||||
provisioners: provisioners,
|
||||
templatePath: c.Template.Path,
|
||||
variables: c.variables,
|
||||
name: n,
|
||||
builder: builder,
|
||||
builderConfig: configBuilder.Config,
|
||||
builderType: configBuilder.Type,
|
||||
postProcessors: postProcessors,
|
||||
provisioners: provisioners,
|
||||
cleanupProvisioner: cleanupProvisioner,
|
||||
templatePath: c.Template.Path,
|
||||
variables: c.variables,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
// This is the hook that should be fired for provisioners to run.
|
||||
const HookProvision = "packer_provision"
|
||||
const HookCleanupProvision = "packer_cleanup_provision"
|
||||
|
||||
// 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.
|
||||
|
@ -44,7 +45,6 @@ func (h *DispatchHook) Run(ctx context.Context, name string, ui Ui, comm Communi
|
|||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := hook.Run(ctx, name, ui, comm, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -50,7 +50,6 @@ func (h *ProvisionHook) Run(ctx context.Context, name string, ui Ui, comm Commun
|
|||
"`communicator` config was set to \"none\". If you have any provisioners\n" +
|
||||
"then a communicator is required. Please fix this to continue.")
|
||||
}
|
||||
|
||||
for _, p := range h.Provisioners {
|
||||
ts := CheckpointReporter.AddSpan(p.TypeName, "provisioner", p.Config)
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ type rawTemplate struct {
|
|||
Push map[string]interface{} `json:"push,omitempty"`
|
||||
PostProcessors []interface{} `mapstructure:"post-processors" json:"post-processors,omitempty"`
|
||||
Provisioners []interface{} `json:"provisioners,omitempty"`
|
||||
CleanupProvisioner interface{} `mapstructure:"error-cleanup-provisioner" json:"error-cleanup-provisioner,omitempty"`
|
||||
Variables map[string]interface{} `json:"variables,omitempty"`
|
||||
SensitiveVariables []string `mapstructure:"sensitive-variables" json:"sensitive-variables,omitempty"`
|
||||
|
||||
|
@ -56,6 +57,34 @@ func (r *rawTemplate) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func (r *rawTemplate) decodeProvisioner(raw interface{}) (Provisioner, error) {
|
||||
var p Provisioner
|
||||
if err := r.decoder(&p, nil).Decode(raw); err != nil {
|
||||
return p, fmt.Errorf("Error decoding provisioner: %s", err)
|
||||
|
||||
}
|
||||
|
||||
// Type is required before any richer validation
|
||||
if p.Type == "" {
|
||||
return p, fmt.Errorf("Provisioner missing 'type'")
|
||||
}
|
||||
|
||||
// Set the raw configuration and delete any special keys
|
||||
p.Config = raw.(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
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Template returns the actual Template object built from this raw
|
||||
// structure.
|
||||
func (r *rawTemplate) Template() (*Template, error) {
|
||||
|
@ -211,37 +240,27 @@ func (r *rawTemplate) Template() (*Template, error) {
|
|||
result.Provisioners = make([]*Provisioner, 0, len(r.Provisioners))
|
||||
}
|
||||
for i, v := range r.Provisioners {
|
||||
var p Provisioner
|
||||
if err := r.decoder(&p, nil).Decode(v); err != nil {
|
||||
p, err := r.decodeProvisioner(v)
|
||||
if err != nil {
|
||||
errs = multierror.Append(errs, fmt.Errorf(
|
||||
"provisioner %d: %s", i+1, err))
|
||||
continue
|
||||
}
|
||||
|
||||
// Type is required before any richer validation
|
||||
if p.Type == "" {
|
||||
errs = multierror.Append(errs, fmt.Errorf(
|
||||
"provisioner %d: missing 'type'", i+1))
|
||||
continue
|
||||
}
|
||||
|
||||
// Set the raw configuration and delete any special keys
|
||||
p.Config = v.(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.Provisioners = append(result.Provisioners, &p)
|
||||
}
|
||||
|
||||
// Gather the error-cleanup-provisioner
|
||||
if r.CleanupProvisioner != nil {
|
||||
p, err := r.decodeProvisioner(r.CleanupProvisioner)
|
||||
if err != nil {
|
||||
errs = multierror.Append(errs,
|
||||
fmt.Errorf("On Error Cleanup Provisioner error: %s", err))
|
||||
}
|
||||
|
||||
result.CleanupProvisioner = &p
|
||||
}
|
||||
|
||||
// If we have errors, return those with a nil result
|
||||
if errs != nil {
|
||||
return nil, errs
|
||||
|
|
|
@ -24,6 +24,7 @@ type Template struct {
|
|||
SensitiveVariables []*Variable
|
||||
Builders map[string]*Builder
|
||||
Provisioners []*Provisioner
|
||||
CleanupProvisioner *Provisioner
|
||||
PostProcessors [][]*PostProcessor
|
||||
|
||||
// RawContents is just the raw data for this template
|
||||
|
|
Loading…
Reference in New Issue