2013-03-25 19:29:26 -04:00
|
|
|
package packer
|
|
|
|
|
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 13:54:42 -04:00
|
|
|
Prepare() error
|
2013-05-03 23:45:38 -04:00
|
|
|
Run(ui Ui)
|
|
|
|
}
|
|
|
|
|
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-03-25 19:29:26 -04:00
|
|
|
name string
|
|
|
|
builder Builder
|
2013-04-21 15:36:55 -04:00
|
|
|
rawConfig interface{}
|
2013-04-20 22:03:53 -04:00
|
|
|
|
|
|
|
prepareCalled bool
|
2013-03-25 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 20:05:27 -04:00
|
|
|
// Implementers of Builder are responsible for actually building images
|
|
|
|
// on some platform given some configuration.
|
|
|
|
//
|
|
|
|
// Prepare is responsible for reading in some configuration, in the raw form
|
|
|
|
// of map[string]interface{}, and storing that state for use later. Any setup
|
|
|
|
// should be done in this method. Note that NO side effects should really take
|
|
|
|
// place in prepare. It is meant as a state setup step only.
|
|
|
|
//
|
|
|
|
// Run is where the actual build should take place. It takes a Build and a Ui.
|
2013-03-25 19:29:26 -04:00
|
|
|
type Builder interface {
|
2013-05-09 13:54:42 -04:00
|
|
|
Prepare(config interface{}) error
|
2013-05-03 23:45:38 -04:00
|
|
|
Run(build Build, ui Ui)
|
2013-04-20 20:05:27 -04:00
|
|
|
}
|
|
|
|
|
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-09 13:54:42 -04:00
|
|
|
func (b *coreBuild) Prepare() error {
|
2013-04-20 22:03:53 -04:00
|
|
|
b.prepareCalled = true
|
2013-05-09 13:54:42 -04:00
|
|
|
return b.builder.Prepare(b.rawConfig)
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs the actual build. Prepare must be called prior to running this.
|
2013-05-03 23:45:38 -04:00
|
|
|
func (b *coreBuild) Run(ui Ui) {
|
2013-04-20 22:03:53 -04:00
|
|
|
if !b.prepareCalled {
|
|
|
|
panic("Prepare must be called first")
|
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
b.builder.Run(b, ui)
|
|
|
|
}
|