Skip to content

Commit

Permalink
enable force, debug, and on-error for hcl2 builds
Browse files Browse the repository at this point in the history
  • Loading branch information
SwampDragons committed May 14, 2020
1 parent e363556 commit ee56357
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
9 changes: 7 additions & 2 deletions command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
}

builds, diags := packerStarter.GetBuilds(packer.GetBuildsOptions{
Only: cla.Only,
Except: cla.Except,
Only: cla.Only,
Except: cla.Except,
Debug: cla.Debug,
Force: cla.Force,
OnError: cla.OnError,
})

// here, something could have gone wrong but we still want to run valid
Expand Down Expand Up @@ -186,6 +189,8 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
log.Printf("On error: %v", cla.OnError)

// Set the debug and force mode and prepare all the builds
// This is only affects json templates, because HCL2
// templates have already been prepared in GetBuilds() above.
for i := range builds {
b := builds[i]
log.Printf("Preparing build: %s", b.Name())
Expand Down
2 changes: 1 addition & 1 deletion hcl2template/types.packer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (cfg *PackerConfig) GetBuilds(opts packer.GetBuildsOptions) ([]packer.Build
}
}

builder, moreDiags, generatedVars := cfg.startBuilder(src, cfg.EvalContext(nil))
builder, moreDiags, generatedVars := cfg.startBuilder(src, cfg.EvalContext(nil), opts)
diags = append(diags, moreDiags...)
if moreDiags.HasErrors() {
continue
Expand Down
16 changes: 14 additions & 2 deletions hcl2template/types.source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hcl2template

import (
"fmt"
"strconv"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/packer/packer"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (p *Parser) decodeSource(block *hcl.Block) (*SourceBlock, hcl.Diagnostics)
return source, diags
}

func (cfg *PackerConfig) startBuilder(source *SourceBlock, ectx *hcl.EvalContext) (packer.Builder, hcl.Diagnostics, []string) {
func (cfg *PackerConfig) startBuilder(source *SourceBlock, ectx *hcl.EvalContext, opts packer.GetBuildsOptions) (packer.Builder, hcl.Diagnostics, []string) {
var diags hcl.Diagnostics

builder, err := cfg.builderSchemas.Start(source.Type)
Expand All @@ -57,12 +58,23 @@ func (cfg *PackerConfig) startBuilder(source *SourceBlock, ectx *hcl.EvalContext
return nil, diags, nil
}

generatedVars, warning, err := builder.Prepare(source.builderVariables(), decoded)
// Note: HCL prepares inside of the Start func, but Json does not. Json
// builds are instead prepared only in command/build.go
// TODO: either make json prepare when plugins are loaded, or make HCL
// prepare at a later step, to make builds from different template types
// easier to reason about.
builderVars := source.builderVariables()
builderVars["packer_debug"] = strconv.FormatBool(opts.Debug)
builderVars["packer_force"] = strconv.FormatBool(opts.Force)
builderVars["packer_on_error"] = opts.OnError

generatedVars, warning, err := builder.Prepare(builderVars, decoded)
moreDiags = warningErrorsToDiags(source.block, warning, err)
diags = append(diags, moreDiags...)
return builder, diags, generatedVars
}

// These variables will populate the PackerConfig inside of the builders.
func (source *SourceBlock) builderVariables() map[string]string {
return map[string]string{
"packer_build_name": source.Name,
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func wrappedMain() int {
runtime.Version(),
runtime.GOOS, runtime.GOARCH)

// The config being loaded here is the Packer config -- it defines
// the location of third party builder plugins, plugin ports to use, and
// whether to disable telemetry. It is a global config.
// Do not confuse this config with the .json Packer template which gets
// passed into commands like `packer build`
config, err := loadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err)
Expand Down Expand Up @@ -328,6 +333,7 @@ func loadConfig() (*config, error) {
}
defer f.Close()

// This loads a json config, defined in packer/config.go
if err := decodeConfig(f, &config); err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions packer/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func (c *Core) generateCoreBuildProvisioner(rawP *template.Provisioner, rawName
return cbp, nil
}

// This is used for json templates to launch the build plugins.
// They will be prepared via b.Prepare() later.
func (c *Core) GetBuilds(opts GetBuildsOptions) ([]Build, hcl.Diagnostics) {
buildNames := c.BuildNames(opts.Only, opts.Except)
builds := []Build{}
Expand All @@ -225,6 +227,12 @@ func (c *Core) Build(n string) (Build, error) {
if !ok {
return nil, fmt.Errorf("no such build found: %s", n)
}
// BuilderStore = config.Builders, gathered in loadConfig() in main.go
// For reference, the builtin BuilderStore is generated in
// packer/config.go in the Discover() func.

// the Start command launches the builder plugin of the given type without
// calling Prepare() or passing any build-specific details.
builder, err := c.components.BuilderStore.Start(configBuilder.Type)
if err != nil {
return nil, fmt.Errorf(
Expand Down Expand Up @@ -315,6 +323,8 @@ func (c *Core) Build(n string) (Build, error) {

// TODO hooks one day

// Return a structure that contains the plugins, their types, variables, and
// the raw builder config loaded from the json template
return &CoreBuild{
Type: n,
Builder: builder,
Expand Down
2 changes: 2 additions & 0 deletions packer/run_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type GetBuildsOptions struct {
// Get builds except the ones that match with except and with only the ones
// that match with Only. When those are empty everything matches.
Except, Only []string
Debug, Force bool
OnError string
}

type BuildGetter interface {
Expand Down

0 comments on commit ee56357

Please sign in to comment.