2013-03-25 19:29:26 -04:00
|
|
|
package packer
|
|
|
|
|
2013-05-09 16:26:40 -04:00
|
|
|
import "log"
|
|
|
|
|
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-05-09 14:32:03 -04:00
|
|
|
Name() string
|
2013-05-22 19:20:40 -04:00
|
|
|
Prepare(Ui) error
|
2013-06-12 18:58:02 -04:00
|
|
|
Run(Ui, Cache) (Artifact, error)
|
2013-06-03 19:03:08 -04:00
|
|
|
Cancel()
|
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-05-22 19:15:57 -04:00
|
|
|
name string
|
|
|
|
builder Builder
|
|
|
|
builderConfig interface{}
|
|
|
|
hooks map[string][]Hook
|
|
|
|
provisioners []coreBuildProvisioner
|
2013-04-20 22:03:53 -04:00
|
|
|
|
|
|
|
prepareCalled bool
|
2013-03-25 19:29:26 -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
|
|
|
|
// and any hooks. This _must_ be called prior to Run.
|
2013-05-22 19:20:40 -04:00
|
|
|
func (b *coreBuild) Prepare(ui Ui) (err error) {
|
2013-05-22 19:29:07 -04:00
|
|
|
// TODO: lock
|
2013-04-20 22:03:53 -04:00
|
|
|
b.prepareCalled = true
|
2013-05-22 19:29:07 -04:00
|
|
|
|
|
|
|
// Prepare the builder
|
2013-05-22 19:15:57 -04:00
|
|
|
err = b.builder.Prepare(b.builderConfig)
|
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-07 13:35:26 -04:00
|
|
|
if err = coreProv.provisioner.Prepare(coreProv.config...); err != nil {
|
2013-06-06 20:09:51 -04:00
|
|
|
return
|
|
|
|
}
|
2013-05-22 19:29:07 -04:00
|
|
|
}
|
|
|
|
|
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-12 18:58:02 -04:00
|
|
|
func (b *coreBuild) Run(ui 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
hooks[HookProvision] = append(hooks[HookProvision], &ProvisionHook{provisioners})
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
hook := &DispatchHook{hooks}
|
2013-06-10 01:00:47 -04:00
|
|
|
return b.builder.Run(ui, hook, cache)
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
2013-06-03 19:03:08 -04:00
|
|
|
|
|
|
|
// Cancels the build if it is running.
|
|
|
|
func (b *coreBuild) Cancel() {
|
2013-06-03 19:06:04 -04:00
|
|
|
b.builder.Cancel()
|
2013-06-03 19:03:08 -04:00
|
|
|
}
|