2013-03-25 19:29:26 -04:00
|
|
|
package packer
|
|
|
|
|
2013-06-18 12:37:14 -04:00
|
|
|
import (
|
2013-06-18 13:54:22 -04:00
|
|
|
"fmt"
|
2013-06-18 12:37:14 -04:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
)
|
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"
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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.
|
2013-06-18 13:24:23 -04:00
|
|
|
Run(Ui, Cache) ([]Artifact, error)
|
2013-06-14 15:15:51 -04:00
|
|
|
|
|
|
|
// Cancel will cancel a running build. This will block until the build
|
|
|
|
// is actually completely cancelled.
|
2013-06-03 19:03:08 -04:00
|
|
|
Cancel()
|
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)
|
2013-05-03 23:45:38 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 19:29:26 -04:00
|
|
|
// A build struct represents a single build job, the result of which should
|
|
|
|
// be a single machine image artifact. This artifact may be comprised of
|
|
|
|
// multiple files, of course, but it should be for only a single provider
|
|
|
|
// (such as VirtualBox, EC2, etc.).
|
2013-05-03 23:45:38 -04:00
|
|
|
type coreBuild struct {
|
2013-06-18 12:58:39 -04:00
|
|
|
name string
|
|
|
|
builder Builder
|
|
|
|
builderConfig interface{}
|
2013-06-19 02:05:02 -04:00
|
|
|
builderType string
|
2013-06-18 12:58:39 -04:00
|
|
|
hooks map[string][]Hook
|
|
|
|
postProcessors [][]coreBuildPostProcessor
|
|
|
|
provisioners []coreBuildProvisioner
|
2013-12-27 11:17:51 -05:00
|
|
|
variables map[string]string
|
2013-04-20 22:03:53 -04:00
|
|
|
|
2013-06-14 15:22:19 -04:00
|
|
|
debug bool
|
2013-07-12 00:43:23 -04:00
|
|
|
force bool
|
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
|
|
|
}
|
|
|
|
|
2013-06-18 12:58:39 -04:00
|
|
|
// Keeps track of the post-processor and the configuration of the
|
|
|
|
// post-processor used within a build.
|
|
|
|
type coreBuildPostProcessor struct {
|
2013-06-19 01:45:53 -04:00
|
|
|
processor PostProcessor
|
2013-06-19 02:05:02 -04:00
|
|
|
processorType string
|
2013-08-19 19:00:13 -04:00
|
|
|
config map[string]interface{}
|
2013-06-19 01:45:53 -04:00
|
|
|
keepInputArtifact bool
|
2013-06-18 12:58:39 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 19:15:57 -04:00
|
|
|
// Keeps track of the provisioner and the configuration of the provisioner
|
|
|
|
// within the build.
|
|
|
|
type coreBuildProvisioner struct {
|
|
|
|
provisioner 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.
|
|
|
|
func (b *coreBuild) Name() string {
|
|
|
|
return b.name
|
|
|
|
}
|
|
|
|
|
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).
|
2013-12-27 11:17:51 -05:00
|
|
|
func (b *coreBuild) Prepare() (warn []string, err error) {
|
2013-06-18 12:37:14 -04:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare already called")
|
|
|
|
}
|
|
|
|
|
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{}{
|
2013-08-09 17:07:49 -04:00
|
|
|
BuildNameConfigKey: b.name,
|
|
|
|
BuilderTypeConfigKey: b.builderType,
|
|
|
|
DebugConfigKey: b.debug,
|
|
|
|
ForceConfigKey: b.force,
|
2013-12-27 11:17:51 -05:00
|
|
|
UserVariablesConfigKey: b.variables,
|
2013-06-14 15:38:54 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 19:29:07 -04:00
|
|
|
// Prepare the builder
|
2013-11-02 23:31:12 -04:00
|
|
|
warn, err = b.builder.Prepare(b.builderConfig, packerConfig)
|
2013-05-09 16:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Build '%s' prepare failure: %s\n", b.name, err)
|
2013-06-10 02:05:11 -04:00
|
|
|
return
|
2013-05-09 16:26:40 -04:00
|
|
|
}
|
|
|
|
|
2013-05-22 19:29:07 -04:00
|
|
|
// Prepare the provisioners
|
|
|
|
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)
|
2013-06-14 15:38:54 -04: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
|
|
|
}
|
|
|
|
|
2013-06-18 13:31:52 -04:00
|
|
|
// Prepare the post-processors
|
|
|
|
for _, ppSeq := range b.postProcessors {
|
|
|
|
for _, corePP := range ppSeq {
|
2013-07-01 18:00:21 -04:00
|
|
|
err = corePP.processor.Configure(corePP.config, packerConfig)
|
|
|
|
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.
|
2013-06-27 21:55:59 -04:00
|
|
|
func (b *coreBuild) Run(originalUi Ui, cache Cache) ([]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
|
|
|
|
hooks := make(map[string][]Hook)
|
|
|
|
for hookName, hookList := range b.hooks {
|
|
|
|
hooks[hookName] = make([]Hook, len(hookList))
|
|
|
|
copy(hooks[hookName], hookList)
|
|
|
|
}
|
|
|
|
|
2013-05-24 00:26:24 -04:00
|
|
|
// Add a hook for the provisioners if we have provisioners
|
|
|
|
if len(b.provisioners) > 0 {
|
|
|
|
provisioners := make([]Provisioner, len(b.provisioners))
|
|
|
|
for i, p := range b.provisioners {
|
|
|
|
provisioners[i] = p.provisioner
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := hooks[HookProvision]; !ok {
|
|
|
|
hooks[HookProvision] = make([]Hook, 0, 1)
|
|
|
|
}
|
|
|
|
|
2013-08-31 02:39:29 -04:00
|
|
|
hooks[HookProvision] = append(hooks[HookProvision], &ProvisionHook{
|
|
|
|
Provisioners: provisioners,
|
|
|
|
})
|
2013-05-24 00:26:24 -04:00
|
|
|
}
|
|
|
|
|
2013-08-30 20:26:51 -04:00
|
|
|
hook := &DispatchHook{Mapping: hooks}
|
2013-06-18 13:24:23 -04:00
|
|
|
artifacts := make([]Artifact, 0, 1)
|
|
|
|
|
2013-08-11 21:31:28 -04:00
|
|
|
// The builder just has a normal Ui, but targetted
|
|
|
|
builderUi := &TargettedUi{
|
|
|
|
Target: b.Name(),
|
|
|
|
Ui: originalUi,
|
2013-06-27 21:55:59 -04:00
|
|
|
}
|
|
|
|
|
2013-06-19 02:05:02 -04:00
|
|
|
log.Printf("Running builder: %s", b.builderType)
|
2013-06-27 21:55:59 -04:00
|
|
|
builderArtifact, err := b.builder.Run(builderUi, hook, cache)
|
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)
|
2013-06-19 01:45:53 -04:00
|
|
|
keepOriginalArtifact := len(b.postProcessors) == 0
|
2013-06-18 13:54:22 -04:00
|
|
|
|
|
|
|
// Run the post-processors
|
2013-06-28 09:44:03 -04:00
|
|
|
PostProcessorRunSeqLoop:
|
2013-06-18 13:54:22 -04: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 {
|
2013-08-11 21:31:28 -04:00
|
|
|
ppUi := &TargettedUi{
|
|
|
|
Target: fmt.Sprintf("%s (%s)", b.Name(), corePP.processorType),
|
|
|
|
Ui: originalUi,
|
2013-06-27 21:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType))
|
2013-07-01 14:30:39 -04:00
|
|
|
artifact, keep, err := corePP.processor.PostProcess(ppUi, priorArtifact)
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:30:39 -04:00
|
|
|
keep = keep || corePP.keepInputArtifact
|
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'",
|
|
|
|
corePP.processorType)
|
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 {
|
2013-06-28 09:44:03 -04:00
|
|
|
log.Printf("Deleting prior artifact from post-processor '%s'", corePP.processorType)
|
|
|
|
if err := priorArtifact.Destroy(); err != nil {
|
|
|
|
errors = append(errors, fmt.Errorf("Failed cleaning up prior artifact: %s", err))
|
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 {
|
|
|
|
log.Printf("Deleting original artifact for build '%s'", b.name)
|
|
|
|
if err := builderArtifact.Destroy(); err != nil {
|
|
|
|
errors = append(errors, fmt.Errorf("Error destroying builder artifact: %s", err))
|
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 {
|
|
|
|
err = &MultiError{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
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
func (b *coreBuild) SetDebug(val bool) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:43:23 -04:00
|
|
|
func (b *coreBuild) SetForce(val bool) {
|
|
|
|
if b.prepareCalled {
|
|
|
|
panic("prepare has already been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.force = val
|
|
|
|
}
|
|
|
|
|
2013-06-28 09:44:03 -04:00
|
|
|
// Cancels the build if it is running.
|
|
|
|
func (b *coreBuild) Cancel() {
|
|
|
|
b.builder.Cancel()
|
|
|
|
}
|