2013-03-25 19:29:26 -04:00
|
|
|
package packer
|
|
|
|
|
2013-06-18 12:37:14 -04:00
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-06-18 13:54:22 -04:00
|
|
|
"fmt"
|
2013-06-18 12:37:14 -04:00
|
|
|
"log"
|
|
|
|
"sync"
|
2019-12-17 00:23:05 -05:00
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata"
|
2020-11-09 15:29:53 -05:00
|
|
|
"github.com/hashicorp/packer/version"
|
2013-06-18 12:37:14 -04:00
|
|
|
)
|
2013-05-09 16:26:40 -04:00
|
|
|
|
2013-07-14 20:55:41 -04:00
|
|
|
const (
|
|
|
|
// This is the key in configurations that is set to the name of the
|
|
|
|
// build.
|
|
|
|
BuildNameConfigKey = "packer_build_name"
|
2013-07-01 14:06:28 -04:00
|
|
|
|
2013-07-14 20:58:32 -04:00
|
|
|
// This is the key in the configuration that is set to the type
|
|
|
|
// of the builder that is run. This is useful for provisioners and
|
|
|
|
// such who want to make use of this.
|
|
|
|
BuilderTypeConfigKey = "packer_builder_type"
|
|
|
|
|
2020-11-09 15:29:53 -05:00
|
|
|
// this is the key in the configuration that is set to the version of the
|
|
|
|
// Packer Core. This can be used by plugins to set user agents, etc, without
|
|
|
|
// having to import the Core to find out the Packer version.
|
|
|
|
CoreVersionConfigKey = "packer_core_version"
|
|
|
|
|
2013-07-14 20:55:41 -04:00
|
|
|
// This is the key in configurations that is set to "true" when Packer
|
|
|
|
// debugging is enabled.
|
|
|
|
DebugConfigKey = "packer_debug"
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2013-07-14 20:55:41 -04:00
|
|
|
// This is the key in configurations that is set to "true" when Packer
|
|
|
|
// force build is enabled.
|
|
|
|
ForceConfigKey = "packer_force"
|
2013-08-09 17:07:49 -04:00
|
|
|
|
2016-09-13 20:04:18 -04:00
|
|
|
// This key determines what to do when a normal multistep step fails
|
|
|
|
// - "cleanup" - run cleanup steps
|
|
|
|
// - "abort" - exit without cleanup
|
|
|
|
// - "ask" - ask the user
|
|
|
|
OnErrorConfigKey = "packer_on_error"
|
|
|
|
|
2015-05-29 17:29:32 -04:00
|
|
|
// TemplatePathKey is the path to the template that configured this build
|
|
|
|
TemplatePathKey = "packer_template_path"
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
// This key contains a map[string]string of the user variables for
|
|
|
|
// template processing.
|
|
|
|
UserVariablesConfigKey = "packer_user_variables"
|
2013-07-14 20:55:41 -04:00
|
|
|
)
|
2013-07-12 00:43:23 -04:00
|
|
|
|
2013-05-03 23:45:38 -04:00
|
|
|
// A Build represents a single job within Packer that is responsible for
|
|
|
|
// building some machine image artifact. Builds are meant to be parallelized.
|
|
|
|
type Build interface {
|
2013-06-14 15:15:51 -04:00
|
|
|
// Name is the name of the build. This is unique across a single template,
|
|
|
|
// but not absolutely unique. This is meant more to describe to the user
|
|
|
|
// what is being built rather than being a unique identifier.
|
2013-05-09 14:32:03 -04:00
|
|
|
Name() string
|
2013-06-14 15:15:51 -04:00
|
|
|
|
|
|
|
// Prepare configures the various components of this build and reports
|
2013-11-02 23:31:12 -04:00
|
|
|
// any errors in doing so (such as syntax errors, validation errors, etc.).
|
|
|
|
// It also reports any warnings.
|
2013-12-27 11:17:51 -05:00
|
|
|
Prepare() ([]string, error)
|
2013-06-14 15:15:51 -04:00
|
|
|
|
|
|
|
// Run runs the actual builder, returning an artifact implementation
|
|
|
|
// of what is built. If anything goes wrong, an error is returned.
|
2019-03-22 09:53:28 -04:00
|
|
|
// Run can be context cancelled.
|
2020-11-19 15:17:11 -05:00
|
|
|
Run(context.Context, packersdk.Ui) ([]packersdk.Artifact, error)
|
2013-06-14 15:22:19 -04:00
|
|
|
|
|
|
|
// SetDebug will enable/disable debug mode. Debug mode is always
|
|
|
|
// enabled by adding the additional key "packer_debug" to boolean
|
|
|
|
// true in the configuration of the various components. This must
|
|
|
|
// be called prior to Prepare.
|
2013-06-14 15:32:14 -04:00
|
|
|
//
|
|
|
|
// When SetDebug is set to true, parallelism between builds is
|
|
|
|
// strictly prohibited.
|
2013-06-14 15:22:19 -04:00
|
|
|
SetDebug(bool)
|
2013-07-12 00:43:23 -04:00
|
|
|
|
2013-07-12 21:01:27 -04:00
|
|
|
// SetForce will enable/disable forcing a build when artifacts exist.
|
2013-07-12 00:43:23 -04:00
|
|
|
//
|
|
|
|
// When SetForce is set to true, existing artifacts from the build are
|
|
|
|
// deleted prior to the build.
|
|
|
|
SetForce(bool)
|
2016-09-13 20:04:18 -04:00
|
|
|
|
2016-09-14 05:29:20 -04:00
|
|
|
// SetOnError will determine what to do when a normal multistep step fails
|
2016-09-13 20:04:18 -04:00
|
|
|
// - "cleanup" - run cleanup steps
|
|
|
|
// - "abort" - exit without cleanup
|
|
|
|
// - "ask" - ask the user
|
|
|
|
SetOnError(string)
|
2013-05-03 23:45:38 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
// A CoreBuild struct represents a single build job, the result of which should
|
2013-03-25 19:29:26 -04:00
|
|
|
// be a single machine image artifact. This artifact may be comprised of
|
2019-12-17 05:25:56 -05:00
|
|
|
// multiple files, of course, but it should be for only a single provider (such
|
|
|
|
// as VirtualBox, EC2, etc.).
|
|
|
|
type CoreBuild struct {
|
2020-05-18 01:13:35 -04:00
|
|
|
BuildName string
|
2019-12-17 05:25:56 -05:00
|
|
|
Type string
|
2020-12-01 16:42:11 -05:00
|
|
|
Builder packersdk.Builder
|
2019-12-17 05:25:56 -05:00
|
|
|
BuilderConfig interface{}
|
|
|
|
BuilderType string
|
2020-11-19 18:10:00 -05:00
|
|
|
hooks map[string][]packersdk.Hook
|
2019-12-17 05:25:56 -05:00
|
|
|
Provisioners []CoreBuildProvisioner
|
|
|
|
PostProcessors [][]CoreBuildPostProcessor
|
|
|
|
CleanupProvisioner CoreBuildProvisioner
|
|
|
|
TemplatePath string
|
|
|
|
Variables map[string]string
|
2013-04-20 22:03:53 -04:00
|
|
|
|
2020-02-14 11:39:32 -05:00
|
|
|
// Indicates whether the build is already initialized before calling Prepare(..)
|
|
|
|
Prepared bool
|
|
|
|
|
2013-06-14 15:22:19 -04:00
|
|
|
debug bool
|
2013-07-12 00:43:23 -04:00
|
|
|
force bool
|
2016-09-13 20:04:18 -04:00
|
|
|
onError string
|
2013-06-18 12:37:14 -04:00
|
|
|
l sync.Mutex
|
2013-04-20 22:03:53 -04:00
|
|
|
prepareCalled bool
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
// CoreBuildPostProcessor Keeps track of the post-processor and the
|
|
|
|
// configuration of the post-processor used within a build.
|
|
|
|
type CoreBuildPostProcessor struct {
|
2020-12-01 17:48:55 -05:00
|
|
|
PostProcessor packersdk.PostProcessor
|
2019-12-17 05:25:56 -05:00
|
|
|
PType string
|
2020-01-15 17:07:53 -05:00
|
|
|
PName string
|
2013-08-19 19:00:13 -04:00
|
|
|
config map[string]interface{}
|
2020-06-25 03:36:48 -04:00
|
|
|
KeepInputArtifact *bool
|
2013-06-18 12:58:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
// CoreBuildProvisioner keeps track of the provisioner and the configuration of
|
|
|
|
// the provisioner within the build.
|
|
|
|
type CoreBuildProvisioner struct {
|
|
|
|
PType string
|
2020-01-16 06:08:39 -05:00
|
|
|
PName string
|
2020-12-01 17:48:55 -05:00
|
|
|
Provisioner packersdk.Provisioner
|
2013-06-07 13:35:26 -04:00
|
|
|
config []interface{}
|
2013-05-22 19:15:57 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:32:03 -04:00
|
|
|
// Returns the name of the build.
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *CoreBuild) Name() string {
|
2020-05-18 01:13:35 -04:00
|
|
|
if b.BuildName != "" {
|
2020-06-02 05:43:27 -04:00
|
|
|
return b.BuildName + "." + b.Type
|
2020-05-18 01:13:35 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
return b.Type
|
2013-05-09 14:32:03 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
// Prepare prepares the build by doing some initialization for the builder
|
2013-08-09 16:40:00 -04:00
|
|
|
// and any hooks. This _must_ be called prior to Run. The parameter is the
|
|
|
|
// overrides for the variables within the template (if any).
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *CoreBuild) Prepare() (warn []string, err error) {
|
2020-05-28 04:43:58 -04:00
|
|
|
// For HCL2 templates, the builder and hooks are initialized when the
|
|
|
|
// template is parsed. Calling Prepare(...) is not necessary
|
2020-02-14 11:39:32 -05:00
|
|
|
if b.Prepared {
|
|
|
|
b.prepareCalled = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:37:14 -04:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare already called")
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:43:58 -04:00
|
|
|
// Templates loaded from HCL2 will never get here. TODO: move this code into
|
|
|
|
// a custom json area instead of just aborting early for HCL.
|
2013-04-20 22:03:53 -04:00
|
|
|
b.prepareCalled = true
|
2013-05-22 19:29:07 -04:00
|
|
|
|
2013-07-01 14:06:28 -04:00
|
|
|
packerConfig := map[string]interface{}{
|
2019-12-17 05:25:56 -05:00
|
|
|
BuildNameConfigKey: b.Type,
|
|
|
|
BuilderTypeConfigKey: b.BuilderType,
|
2020-11-09 16:20:36 -05:00
|
|
|
CoreVersionConfigKey: version.FormattedVersion(),
|
2013-08-09 17:07:49 -04:00
|
|
|
DebugConfigKey: b.debug,
|
|
|
|
ForceConfigKey: b.force,
|
2016-09-13 20:04:18 -04:00
|
|
|
OnErrorConfigKey: b.onError,
|
2019-12-17 05:25:56 -05:00
|
|
|
TemplatePathKey: b.TemplatePath,
|
|
|
|
UserVariablesConfigKey: b.Variables,
|
2013-06-14 15:38:54 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 19:29:07 -04:00
|
|
|
// Prepare the builder
|
2019-12-17 05:57:09 -05:00
|
|
|
generatedVars, warn, err := b.Builder.Prepare(b.BuilderConfig, packerConfig)
|
2013-05-09 16:26:40 -04:00
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
log.Printf("Build '%s' prepare failure: %s\n", b.Type, err)
|
2013-06-10 02:05:11 -04:00
|
|
|
return
|
2013-05-09 16:26:40 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
// If the builder has provided a list of to-be-generated variables that
|
|
|
|
// should be made accessible to provisioners, pass that list into
|
|
|
|
// the provisioner prepare() so that the provisioner can appropriately
|
|
|
|
// validate user input against what will become available.
|
|
|
|
generatedPlaceholderMap := BasicPlaceholderData()
|
|
|
|
if generatedVars != nil {
|
|
|
|
for _, k := range generatedVars {
|
2019-12-17 16:41:48 -05:00
|
|
|
generatedPlaceholderMap[k] = fmt.Sprintf("Build_%s. "+
|
2020-11-04 18:44:05 -05:00
|
|
|
packerbuilderdata.PlaceholderMsg, k)
|
2019-12-17 00:23:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:29:07 -04:00
|
|
|
// Prepare the provisioners
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, coreProv := range b.Provisioners {
|
2013-06-14 15:38:54 -04:00
|
|
|
configs := make([]interface{}, len(coreProv.config), len(coreProv.config)+1)
|
|
|
|
copy(configs, coreProv.config)
|
2013-07-01 14:06:28 -04:00
|
|
|
configs = append(configs, packerConfig)
|
2019-12-17 00:23:05 -05:00
|
|
|
configs = append(configs, generatedPlaceholderMap)
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
if err = coreProv.Provisioner.Prepare(configs...); err != nil {
|
2013-06-06 20:09:51 -04:00
|
|
|
return
|
|
|
|
}
|
2013-05-22 19:29:07 -04:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:44:19 -04:00
|
|
|
// Prepare the on-error-cleanup provisioner
|
2019-12-17 05:25:56 -05:00
|
|
|
if b.CleanupProvisioner.PType != "" {
|
|
|
|
configs := make([]interface{}, len(b.CleanupProvisioner.config), len(b.CleanupProvisioner.config)+1)
|
|
|
|
copy(configs, b.CleanupProvisioner.config)
|
2019-09-24 12:44:19 -04:00
|
|
|
configs = append(configs, packerConfig)
|
2019-12-17 00:23:05 -05:00
|
|
|
configs = append(configs, generatedPlaceholderMap)
|
2019-12-17 05:25:56 -05:00
|
|
|
err = b.CleanupProvisioner.Provisioner.Prepare(configs...)
|
2019-09-24 12:44:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:31:52 -04:00
|
|
|
// Prepare the post-processors
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, ppSeq := range b.PostProcessors {
|
2013-06-18 13:31:52 -04:00
|
|
|
for _, corePP := range ppSeq {
|
2020-01-16 06:04:03 -05:00
|
|
|
err = corePP.PostProcessor.Configure(corePP.config, packerConfig, generatedPlaceholderMap)
|
2013-07-01 18:00:21 -04:00
|
|
|
if err != nil {
|
2013-06-18 13:31:52 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:26:40 -04:00
|
|
|
return
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs the actual build. Prepare must be called prior to running this.
|
2020-11-19 15:17:11 -05:00
|
|
|
func (b *CoreBuild) Run(ctx context.Context, originalUi packersdk.Ui) ([]packersdk.Artifact, error) {
|
2013-04-20 22:03:53 -04:00
|
|
|
if !b.prepareCalled {
|
|
|
|
panic("Prepare must be called first")
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// Copy the hooks
|
2020-11-19 18:10:00 -05:00
|
|
|
hooks := make(map[string][]packersdk.Hook)
|
2013-05-22 19:46:23 -04:00
|
|
|
for hookName, hookList := range b.hooks {
|
2020-11-19 18:10:00 -05:00
|
|
|
hooks[hookName] = make([]packersdk.Hook, len(hookList))
|
2013-05-22 19:46:23 -04:00
|
|
|
copy(hooks[hookName], hookList)
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:26:24 -04:00
|
|
|
// Add a hook for the provisioners if we have provisioners
|
2019-12-17 05:25:56 -05:00
|
|
|
if len(b.Provisioners) > 0 {
|
|
|
|
hookedProvisioners := make([]*HookedProvisioner, len(b.Provisioners))
|
|
|
|
for i, p := range b.Provisioners {
|
2017-11-04 16:06:36 -04:00
|
|
|
var pConfig interface{}
|
|
|
|
if len(p.config) > 0 {
|
|
|
|
pConfig = p.config[0]
|
|
|
|
}
|
2018-06-07 18:35:36 -04:00
|
|
|
if b.debug {
|
|
|
|
hookedProvisioners[i] = &HookedProvisioner{
|
2019-12-17 05:25:56 -05:00
|
|
|
&DebuggedProvisioner{Provisioner: p.Provisioner},
|
2018-06-07 18:35:36 -04:00
|
|
|
pConfig,
|
2019-12-17 05:25:56 -05:00
|
|
|
p.PType,
|
2018-06-07 18:35:36 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hookedProvisioners[i] = &HookedProvisioner{
|
2019-12-17 05:25:56 -05:00
|
|
|
p.Provisioner,
|
2018-06-07 18:35:36 -04:00
|
|
|
pConfig,
|
2019-12-17 05:25:56 -05:00
|
|
|
p.PType,
|
2018-06-07 18:35:36 -04:00
|
|
|
}
|
2017-11-04 16:06:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
if _, ok := hooks[packersdk.HookProvision]; !ok {
|
|
|
|
hooks[packersdk.HookProvision] = make([]packersdk.Hook, 0, 1)
|
2013-05-24 00:26:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
hooks[packersdk.HookProvision] = append(hooks[packersdk.HookProvision], &ProvisionHook{
|
2017-11-04 16:06:36 -04:00
|
|
|
Provisioners: hookedProvisioners,
|
2013-08-31 02:39:29 -04:00
|
|
|
})
|
2013-05-24 00:26:24 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
if b.CleanupProvisioner.PType != "" {
|
2019-09-24 12:44:19 -04:00
|
|
|
hookedCleanupProvisioner := &HookedProvisioner{
|
2019-12-17 05:25:56 -05:00
|
|
|
b.CleanupProvisioner.Provisioner,
|
|
|
|
b.CleanupProvisioner.config,
|
|
|
|
b.CleanupProvisioner.PType,
|
2019-09-24 12:44:19 -04:00
|
|
|
}
|
2020-11-19 18:10:00 -05:00
|
|
|
hooks[packersdk.HookCleanupProvision] = []packersdk.Hook{&ProvisionHook{
|
2019-09-24 12:44:19 -04:00
|
|
|
Provisioners: []*HookedProvisioner{hookedCleanupProvisioner},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
hook := &packersdk.DispatchHook{Mapping: hooks}
|
2020-11-19 15:17:11 -05:00
|
|
|
artifacts := make([]packersdk.Artifact, 0, 1)
|
2013-06-18 13:24:23 -04:00
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
// The builder just has a normal Ui, but targeted
|
2017-03-29 15:44:42 -04:00
|
|
|
builderUi := &TargetedUI{
|
2013-08-11 21:31:28 -04:00
|
|
|
Target: b.Name(),
|
|
|
|
Ui: originalUi,
|
2013-06-27 21:55:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
log.Printf("Running builder: %s", b.BuilderType)
|
|
|
|
ts := CheckpointReporter.AddSpan(b.BuilderType, "builder", b.BuilderConfig)
|
|
|
|
builderArtifact, err := b.Builder.Run(ctx, builderUi, hook)
|
2017-05-04 19:29:21 -04:00
|
|
|
ts.End(err)
|
2013-06-18 13:54:22 -04:00
|
|
|
if err != nil {
|
2013-06-19 01:53:30 -04:00
|
|
|
return nil, err
|
2013-06-18 13:54:22 -04:00
|
|
|
}
|
|
|
|
|
2013-06-27 18:50:02 -04:00
|
|
|
// If there was no result, don't worry about running post-processors
|
|
|
|
// because there is nothing they can do, just return.
|
|
|
|
if builderArtifact == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:54:22 -04:00
|
|
|
errors := make([]error, 0)
|
2019-12-17 05:25:56 -05:00
|
|
|
keepOriginalArtifact := len(b.PostProcessors) == 0
|
2013-06-18 13:54:22 -04:00
|
|
|
|
2020-08-06 08:40:02 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Println("Build was cancelled. Skipping post-processors.")
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:54:22 -04:00
|
|
|
// Run the post-processors
|
2013-06-28 09:44:03 -04:00
|
|
|
PostProcessorRunSeqLoop:
|
2019-12-17 05:25:56 -05:00
|
|
|
for _, ppSeq := range b.PostProcessors {
|
2013-06-19 01:58:23 -04:00
|
|
|
priorArtifact := builderArtifact
|
2013-06-19 01:45:53 -04:00
|
|
|
for i, corePP := range ppSeq {
|
2017-03-29 15:44:42 -04:00
|
|
|
ppUi := &TargetedUI{
|
2019-12-17 05:25:56 -05:00
|
|
|
Target: fmt.Sprintf("%s (%s)", b.Name(), corePP.PType),
|
2013-08-11 21:31:28 -04:00
|
|
|
Ui: originalUi,
|
2013-06-27 21:55:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:07:53 -05:00
|
|
|
if corePP.PName == corePP.PType {
|
|
|
|
builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.PType))
|
|
|
|
} else {
|
|
|
|
builderUi.Say(fmt.Sprintf("Running post-processor: %s (type %s)", corePP.PName, corePP.PType))
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
ts := CheckpointReporter.AddSpan(corePP.PType, "post-processor", corePP.config)
|
|
|
|
artifact, defaultKeep, forceOverride, err := corePP.PostProcessor.PostProcess(ctx, ppUi, priorArtifact)
|
2017-05-04 19:29:21 -04:00
|
|
|
ts.End(err)
|
2013-06-19 01:58:23 -04:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, fmt.Errorf("Post-processor failed: %s", err))
|
|
|
|
continue PostProcessorRunSeqLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact == nil {
|
|
|
|
log.Println("Nil artifact, halting post-processor chain.")
|
|
|
|
continue PostProcessorRunSeqLoop
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
keep := defaultKeep
|
2020-01-06 08:02:29 -05:00
|
|
|
// When user has not set keep_input_artifact
|
2019-04-03 17:04:03 -04:00
|
|
|
// corePP.keepInputArtifact is nil.
|
|
|
|
// In this case, use the keepDefault provided by the postprocessor.
|
2020-01-06 08:02:29 -05:00
|
|
|
// When user _has_ set keep_input_artifact, go with that instead.
|
2019-04-02 19:51:58 -04:00
|
|
|
// Exception: for postprocessors that will fail/become
|
2019-04-03 17:04:03 -04:00
|
|
|
// useless if keep isn't true, heed forceOverride and keep the
|
|
|
|
// input artifact regardless of user preference.
|
2020-06-25 03:36:48 -04:00
|
|
|
if corePP.KeepInputArtifact != nil {
|
|
|
|
if defaultKeep && *corePP.KeepInputArtifact == false && forceOverride {
|
2019-04-02 19:51:58 -04:00
|
|
|
log.Printf("The %s post-processor forces "+
|
|
|
|
"keep_input_artifact=true to preserve integrity of the"+
|
|
|
|
"build chain. User-set keep_input_artifact=false will be"+
|
2019-12-17 05:25:56 -05:00
|
|
|
"ignored.", corePP.PType)
|
2019-04-02 19:51:58 -04:00
|
|
|
} else {
|
2019-04-03 17:04:03 -04:00
|
|
|
// User overrides default.
|
2020-06-25 03:36:48 -04:00
|
|
|
keep = *corePP.KeepInputArtifact
|
2019-04-02 19:51:58 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:45:53 -04:00
|
|
|
if i == 0 {
|
|
|
|
// This is the first post-processor. We handle deleting
|
|
|
|
// previous artifacts a bit different because multiple
|
|
|
|
// post-processors may be using the original and need it.
|
2013-07-01 14:30:39 -04:00
|
|
|
if !keepOriginalArtifact && keep {
|
2013-06-19 02:05:02 -04:00
|
|
|
log.Printf(
|
|
|
|
"Flagging to keep original artifact from post-processor '%s'",
|
2019-12-17 05:25:56 -05:00
|
|
|
corePP.PType)
|
2013-06-28 09:44:03 -04:00
|
|
|
keepOriginalArtifact = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have a prior artifact. If we want to keep it, we append
|
|
|
|
// it to the results list. Otherwise, we destroy it.
|
2013-07-01 14:30:39 -04:00
|
|
|
if keep {
|
2013-06-28 09:44:03 -04:00
|
|
|
artifacts = append(artifacts, priorArtifact)
|
2013-06-19 01:45:53 -04:00
|
|
|
} else {
|
2019-12-17 05:25:56 -05:00
|
|
|
log.Printf("Deleting prior artifact from post-processor '%s'", corePP.PType)
|
2013-06-28 09:44:03 -04:00
|
|
|
if err := priorArtifact.Destroy(); err != nil {
|
2019-04-03 12:43:39 -04:00
|
|
|
log.Printf("Error is %#v", err)
|
2019-12-17 05:25:56 -05:00
|
|
|
errors = append(errors, fmt.Errorf("Failed cleaning up prior artifact: %s; pp is %s", err, corePP.PType))
|
2013-06-19 01:45:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
priorArtifact = artifact
|
2013-06-19 01:45:53 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
// Add on the last artifact to the results
|
|
|
|
if priorArtifact != nil {
|
|
|
|
artifacts = append(artifacts, priorArtifact)
|
2013-06-19 01:45:53 -04:00
|
|
|
}
|
2013-06-28 09:44:03 -04:00
|
|
|
}
|
2013-06-19 01:45:53 -04:00
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
if keepOriginalArtifact {
|
|
|
|
artifacts = append(artifacts, nil)
|
|
|
|
copy(artifacts[1:], artifacts)
|
|
|
|
artifacts[0] = builderArtifact
|
|
|
|
} else {
|
2019-12-17 05:25:56 -05:00
|
|
|
log.Printf("Deleting original artifact for build '%s'", b.Type)
|
2013-06-28 09:44:03 -04:00
|
|
|
if err := builderArtifact.Destroy(); err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
errors = append(errors, fmt.Errorf("Error destroying builder artifact: %s; bad artifact: %#v", err, builderArtifact.Files()))
|
2013-06-18 13:54:22 -04:00
|
|
|
}
|
2013-06-28 09:44:03 -04:00
|
|
|
}
|
2013-06-18 13:54:22 -04:00
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
if len(errors) > 0 {
|
2020-11-20 13:21:29 -05:00
|
|
|
err = &packersdk.MultiError{Errors: errors}
|
2013-06-18 13:24:23 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
return artifacts, err
|
|
|
|
}
|
2013-06-03 19:03:08 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *CoreBuild) SetDebug(val bool) {
|
2013-06-28 09:44:03 -04:00
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare has already been called")
|
2013-06-14 15:22:19 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
b.debug = val
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *CoreBuild) SetForce(val bool) {
|
2013-07-12 00:43:23 -04:00
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare has already been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.force = val
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *CoreBuild) SetOnError(val string) {
|
2016-09-13 20:04:18 -04:00
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare has already been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.onError = val
|
|
|
|
}
|