2015-05-23 17:48:07 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-23 19:12:32 -04:00
|
|
|
"sort"
|
2015-05-23 17:48:07 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-06-29 14:49:45 -04:00
|
|
|
"github.com/hashicorp/go-version"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-05-23 17:48:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Core is the main executor of Packer. If Packer is being used as a
|
|
|
|
// library, this is the struct you'll want to instantiate to get anything done.
|
|
|
|
type Core struct {
|
2015-05-29 18:41:52 -04:00
|
|
|
Template *template.Template
|
|
|
|
|
2015-05-23 17:48:07 -04:00
|
|
|
components ComponentFinder
|
|
|
|
variables map[string]string
|
2015-05-23 19:12:32 -04:00
|
|
|
builds map[string]*template.Builder
|
2015-06-29 14:49:45 -04:00
|
|
|
version string
|
2015-05-23 17:48:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CoreConfig is the structure for initializing a new Core. Once a CoreConfig
|
|
|
|
// is used to initialize a Core, it shouldn't be re-used or modified again.
|
|
|
|
type CoreConfig struct {
|
|
|
|
Components ComponentFinder
|
|
|
|
Template *template.Template
|
|
|
|
Variables map[string]string
|
2015-06-29 14:49:45 -04:00
|
|
|
Version string
|
2015-05-23 17:48:07 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
// The function type used to lookup Builder implementations.
|
|
|
|
type BuilderFunc func(name string) (Builder, error)
|
|
|
|
|
|
|
|
// The function type used to lookup Hook implementations.
|
|
|
|
type HookFunc func(name string) (Hook, error)
|
|
|
|
|
|
|
|
// The function type used to lookup PostProcessor implementations.
|
|
|
|
type PostProcessorFunc func(name string) (PostProcessor, error)
|
|
|
|
|
|
|
|
// The function type used to lookup Provisioner implementations.
|
|
|
|
type ProvisionerFunc func(name string) (Provisioner, error)
|
|
|
|
|
|
|
|
// ComponentFinder is a struct that contains the various function
|
|
|
|
// pointers necessary to look up components of Packer such as builders,
|
|
|
|
// commands, etc.
|
|
|
|
type ComponentFinder struct {
|
|
|
|
Builder BuilderFunc
|
|
|
|
Hook HookFunc
|
|
|
|
PostProcessor PostProcessorFunc
|
|
|
|
Provisioner ProvisionerFunc
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:48:07 -04:00
|
|
|
// NewCore creates a new Core.
|
|
|
|
func NewCore(c *CoreConfig) (*Core, error) {
|
2015-05-28 17:40:45 -04:00
|
|
|
result := &Core{
|
2015-05-29 18:41:52 -04:00
|
|
|
Template: c.Template,
|
2015-05-23 17:48:07 -04:00
|
|
|
components: c.Components,
|
|
|
|
variables: c.Variables,
|
2015-06-29 14:49:45 -04:00
|
|
|
version: c.Version,
|
2015-05-28 17:40:45 -04:00
|
|
|
}
|
2015-05-28 17:42:53 -04:00
|
|
|
if err := result.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-28 17:40:45 -04:00
|
|
|
if err := result.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:29:32 -04:00
|
|
|
// Go through and interpolate all the build names. We shuld be able
|
|
|
|
// to do this at this point with the variables.
|
|
|
|
result.builds = make(map[string]*template.Builder)
|
|
|
|
for _, b := range c.Template.Builders {
|
2015-05-29 18:35:55 -04:00
|
|
|
v, err := interpolate.Render(b.Name, result.Context())
|
2015-05-29 17:29:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error interpolating builder '%s': %s",
|
|
|
|
b.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result.builds[v] = b
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:40:45 -04:00
|
|
|
return result, nil
|
2015-05-23 17:48:07 -04:00
|
|
|
}
|
|
|
|
|
2015-05-23 19:12:32 -04:00
|
|
|
// BuildNames returns the builds that are available in this configured core.
|
|
|
|
func (c *Core) BuildNames() []string {
|
|
|
|
r := make([]string, 0, len(c.builds))
|
2016-11-01 17:08:04 -04:00
|
|
|
for n := range c.builds {
|
2015-05-23 19:12:32 -04:00
|
|
|
r = append(r, n)
|
|
|
|
}
|
|
|
|
sort.Strings(r)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-05-23 18:08:50 -04:00
|
|
|
// Build returns the Build object for the given name.
|
|
|
|
func (c *Core) Build(n string) (Build, error) {
|
|
|
|
// Setup the builder
|
2015-05-25 21:15:07 -04:00
|
|
|
configBuilder, ok := c.builds[n]
|
2015-05-23 18:08:50 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no such build found: %s", n)
|
|
|
|
}
|
|
|
|
builder, err := c.components.Builder(configBuilder.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"error initializing builder '%s': %s",
|
|
|
|
configBuilder.Type, err)
|
|
|
|
}
|
|
|
|
if builder == nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"builder type not found: %s", configBuilder.Type)
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:14:29 -04:00
|
|
|
// rawName is the uninterpolated name that we use for various lookups
|
|
|
|
rawName := configBuilder.Name
|
|
|
|
|
|
|
|
// Setup the provisioners for this build
|
2015-05-29 18:41:52 -04:00
|
|
|
provisioners := make([]coreBuildProvisioner, 0, len(c.Template.Provisioners))
|
|
|
|
for _, rawP := range c.Template.Provisioners {
|
2015-05-26 12:14:29 -04:00
|
|
|
// If we're skipping this, then ignore it
|
|
|
|
if rawP.Skip(rawName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the provisioner
|
|
|
|
provisioner, err := c.components.Provisioner(rawP.Type)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the configuration
|
|
|
|
config := make([]interface{}, 1, 2)
|
|
|
|
config[0] = rawP.Config
|
2015-05-26 12:46:04 -04:00
|
|
|
if rawP.Override != nil {
|
|
|
|
if override, ok := rawP.Override[rawName]; ok {
|
|
|
|
config = append(config, override)
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 12:14:29 -04:00
|
|
|
|
|
|
|
// If we're pausing, we wrap the provisioner in a special pauser.
|
|
|
|
if rawP.PauseBefore > 0 {
|
|
|
|
provisioner = &PausedProvisioner{
|
|
|
|
PauseBefore: rawP.PauseBefore,
|
|
|
|
Provisioner: provisioner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioners = append(provisioners, coreBuildProvisioner{
|
2017-05-04 19:29:21 -04:00
|
|
|
pType: rawP.Type,
|
2015-05-26 12:14:29 -04:00
|
|
|
provisioner: provisioner,
|
|
|
|
config: config,
|
|
|
|
})
|
|
|
|
}
|
2015-05-23 18:08:50 -04:00
|
|
|
|
2015-05-26 12:28:59 -04:00
|
|
|
// Setup the post-processors
|
2015-05-29 18:41:52 -04:00
|
|
|
postProcessors := make([][]coreBuildPostProcessor, 0, len(c.Template.PostProcessors))
|
|
|
|
for _, rawPs := range c.Template.PostProcessors {
|
2015-05-26 12:28:59 -04:00
|
|
|
current := make([]coreBuildPostProcessor, 0, len(rawPs))
|
|
|
|
for _, rawP := range rawPs {
|
|
|
|
// If we skip, ignore
|
|
|
|
if rawP.Skip(rawName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the post-processor
|
|
|
|
postProcessor, err := c.components.PostProcessor(rawP.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"error initializing post-processor '%s': %s",
|
|
|
|
rawP.Type, err)
|
|
|
|
}
|
|
|
|
if postProcessor == nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"post-processor type not found: %s", rawP.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
current = append(current, coreBuildPostProcessor{
|
|
|
|
processor: postProcessor,
|
|
|
|
processorType: rawP.Type,
|
|
|
|
config: rawP.Config,
|
|
|
|
keepInputArtifact: rawP.KeepInputArtifact,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no post-processors in this chain, just continue.
|
|
|
|
if len(current) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
postProcessors = append(postProcessors, current)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO hooks one day
|
|
|
|
|
2015-05-23 18:08:50 -04:00
|
|
|
return &coreBuild{
|
2015-05-26 12:28:59 -04:00
|
|
|
name: n,
|
|
|
|
builder: builder,
|
|
|
|
builderConfig: configBuilder.Config,
|
|
|
|
builderType: configBuilder.Type,
|
|
|
|
postProcessors: postProcessors,
|
|
|
|
provisioners: provisioners,
|
2015-05-29 18:41:52 -04:00
|
|
|
templatePath: c.Template.Path,
|
2015-05-26 12:28:59 -04:00
|
|
|
variables: c.variables,
|
2015-05-23 18:08:50 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:35:55 -04:00
|
|
|
// Context returns an interpolation context.
|
|
|
|
func (c *Core) Context() *interpolate.Context {
|
|
|
|
return &interpolate.Context{
|
2015-05-29 18:41:52 -04:00
|
|
|
TemplatePath: c.Template.Path,
|
2015-05-29 18:35:55 -04:00
|
|
|
UserVariables: c.variables,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:42:53 -04:00
|
|
|
// validate does a full validation of the template.
|
2015-05-23 17:48:07 -04:00
|
|
|
//
|
2015-05-28 17:42:53 -04:00
|
|
|
// This will automatically call template.validate() in addition to doing
|
2015-05-23 17:48:07 -04:00
|
|
|
// richer semantic checks around variables and so on.
|
2015-05-28 17:42:53 -04:00
|
|
|
func (c *Core) validate() error {
|
2015-05-23 17:48:07 -04:00
|
|
|
// First validate the template in general, we can't do anything else
|
|
|
|
// unless the template itself is valid.
|
2015-05-29 18:41:52 -04:00
|
|
|
if err := c.Template.Validate(); err != nil {
|
2015-05-23 17:48:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:49:45 -04:00
|
|
|
// Validate the minimum version is satisfied
|
|
|
|
if c.Template.MinVersion != "" {
|
|
|
|
versionActual, err := version.NewVersion(c.version)
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen since we set it via the compiler
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
versionMin, err := version.NewVersion(c.Template.MinVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"min_version is invalid: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if versionActual.LessThan(versionMin) {
|
|
|
|
return fmt.Errorf(
|
2015-07-13 22:32:28 -04:00
|
|
|
"This template requires Packer version %s or higher; using %s",
|
2015-06-29 14:49:45 -04:00
|
|
|
versionMin,
|
|
|
|
versionActual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:48:07 -04:00
|
|
|
// Validate variables are set
|
|
|
|
var err error
|
2015-05-29 18:41:52 -04:00
|
|
|
for n, v := range c.Template.Variables {
|
2015-05-23 17:48:07 -04:00
|
|
|
if v.Required {
|
|
|
|
if _, ok := c.variables[n]; !ok {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"required variable not set: %s", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: validate all builders exist
|
|
|
|
// TODO: ^^ provisioner
|
|
|
|
// TODO: ^^ post-processor
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2015-05-28 17:40:45 -04:00
|
|
|
|
|
|
|
func (c *Core) init() error {
|
|
|
|
if c.variables == nil {
|
|
|
|
c.variables = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the variables and interpolate the environment variables
|
2015-05-29 18:35:55 -04:00
|
|
|
ctx := c.Context()
|
2015-05-29 17:29:32 -04:00
|
|
|
ctx.EnableEnv = true
|
|
|
|
ctx.UserVariables = nil
|
2015-05-29 18:41:52 -04:00
|
|
|
for k, v := range c.Template.Variables {
|
2015-05-28 17:40:45 -04:00
|
|
|
// Ignore variables that are required
|
|
|
|
if v.Required {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore variables that have a value
|
|
|
|
if _, ok := c.variables[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the default
|
|
|
|
def, err := interpolate.Render(v.Default, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"error interpolating default value for '%s': %s",
|
|
|
|
k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.variables[k] = def
|
|
|
|
}
|
|
|
|
|
2015-05-29 18:41:52 -04:00
|
|
|
// Interpolate the push configuration
|
|
|
|
if _, err := interpolate.RenderInterface(&c.Template.Push, c.Context()); err != nil {
|
|
|
|
return fmt.Errorf("Error interpolating 'push': %s", err)
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:40:45 -04:00
|
|
|
return nil
|
|
|
|
}
|