2014-10-27 23:21:13 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2014-10-27 23:21:13 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-04-13 08:24:35 -04:00
|
|
|
"math"
|
2014-10-27 23:21:13 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-09-08 14:31:19 -04:00
|
|
|
"syscall"
|
2015-05-23 19:30:45 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclparse"
|
|
|
|
"github.com/hashicorp/packer/hcl2template"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/enumflag"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template"
|
2019-04-13 08:24:35 -04:00
|
|
|
"golang.org/x/sync/semaphore"
|
2017-10-13 14:42:22 -04:00
|
|
|
|
|
|
|
"github.com/posener/complete"
|
2014-10-27 23:21:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type BuildCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2017-10-13 14:41:42 -04:00
|
|
|
func (c *BuildCommand) Run(args []string) int {
|
2019-05-06 06:26:22 -04:00
|
|
|
buildCtx, cancelBuildCtx := context.WithCancel(context.Background())
|
2019-05-06 06:19:59 -04:00
|
|
|
// Handle interrupts for this build
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
defer func() {
|
2019-05-06 06:26:22 -04:00
|
|
|
cancelBuildCtx()
|
2019-05-06 06:19:59 -04:00
|
|
|
signal.Stop(sigCh)
|
|
|
|
close(sigCh)
|
|
|
|
}()
|
|
|
|
go func() {
|
2019-05-06 06:26:22 -04:00
|
|
|
select {
|
|
|
|
case sig := <-sigCh:
|
2019-05-06 09:29:59 -04:00
|
|
|
if sig == nil {
|
|
|
|
// context got cancelled and this closed chan probably
|
|
|
|
// triggered first
|
|
|
|
return
|
|
|
|
}
|
2019-05-06 06:26:22 -04:00
|
|
|
c.Ui.Error(fmt.Sprintf("Cancelling build after receiving %s", sig))
|
|
|
|
cancelBuildCtx()
|
|
|
|
case <-buildCtx.Done():
|
|
|
|
}
|
2019-05-06 06:19:59 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
return c.RunContext(buildCtx, args)
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
// Config is the command-configuration parsed from the command line.
|
2019-05-07 05:43:18 -04:00
|
|
|
type Config struct {
|
|
|
|
Color, Debug, Force, Timestamp bool
|
|
|
|
ParallelBuilds int64
|
|
|
|
OnError string
|
2019-05-07 05:51:21 -04:00
|
|
|
Path string
|
2019-05-07 05:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BuildCommand) ParseArgs(args []string) (Config, int) {
|
|
|
|
var cfg Config
|
|
|
|
var parallel bool
|
2015-05-23 19:30:45 -04:00
|
|
|
flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars)
|
|
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
2019-05-07 05:15:35 -04:00
|
|
|
flags.BoolVar(&cfg.Color, "color", true, "")
|
|
|
|
flags.BoolVar(&cfg.Debug, "debug", false, "")
|
|
|
|
flags.BoolVar(&cfg.Force, "force", false, "")
|
|
|
|
flags.BoolVar(&cfg.Timestamp, "timestamp-ui", false, "")
|
|
|
|
flagOnError := enumflag.New(&cfg.OnError, "cleanup", "abort", "ask")
|
2016-09-17 10:34:37 -04:00
|
|
|
flags.Var(flagOnError, "on-error", "")
|
2019-05-07 05:43:18 -04:00
|
|
|
flags.BoolVar(¶llel, "parallel", true, "")
|
2019-05-07 05:15:35 -04:00
|
|
|
flags.Int64Var(&cfg.ParallelBuilds, "parallel-builds", 0, "")
|
2015-05-23 19:30:45 -04:00
|
|
|
if err := flags.Parse(args); err != nil {
|
2019-05-07 05:43:18 -04:00
|
|
|
return cfg, 1
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-05-07 05:43:18 -04:00
|
|
|
if parallel == false && cfg.ParallelBuilds == 0 {
|
|
|
|
cfg.ParallelBuilds = 1
|
|
|
|
}
|
|
|
|
if cfg.ParallelBuilds < 1 {
|
|
|
|
cfg.ParallelBuilds = math.MaxInt64
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:51:21 -04:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
2015-05-23 19:30:45 -04:00
|
|
|
flags.Usage()
|
2019-05-07 05:43:18 -04:00
|
|
|
return cfg, 1
|
|
|
|
}
|
2019-05-07 05:51:21 -04:00
|
|
|
cfg.Path = args[0]
|
2019-05-07 05:43:18 -04:00
|
|
|
return cfg, 0
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *BuildCommand) GetBuildsFromHCL(path string) ([]packer.Build, int) {
|
|
|
|
parser := &hcl2template.Parser{
|
|
|
|
Parser: hclparse.NewParser(),
|
|
|
|
BuilderSchemas: c.CoreConfig.Components.BuilderStore,
|
|
|
|
ProvisionersSchemas: c.CoreConfig.Components.ProvisionerStore,
|
|
|
|
PostProcessorsSchemas: c.CoreConfig.Components.PostProcessorStore,
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 05:49:21 -05:00
|
|
|
builds, diags := parser.Parse(path, c.flagVars)
|
2019-12-17 05:25:56 -05:00
|
|
|
{
|
|
|
|
// write HCL errors/diagnostics if any.
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
err := hcl.NewDiagnosticTextWriter(b, parser.Files(), 80, false).WriteDiagnostics(diags)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error("could not write diagnostic: " + err.Error())
|
|
|
|
return nil, 1
|
|
|
|
}
|
|
|
|
if b.Len() != 0 {
|
|
|
|
c.Ui.Message(b.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := 0
|
|
|
|
if diags.HasErrors() {
|
|
|
|
ret = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return builds, ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BuildCommand) GetBuilds(path string) ([]packer.Build, int) {
|
|
|
|
|
|
|
|
isHCLLoaded, err := isHCLLoaded(path)
|
|
|
|
if path != "-" && err != nil {
|
2019-12-17 15:27:51 -05:00
|
|
|
c.Ui.Error(fmt.Sprintf("could not tell whether %s is hcl enabled: %s", path, err))
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, 1
|
|
|
|
}
|
|
|
|
if isHCLLoaded {
|
|
|
|
return c.GetBuildsFromHCL(path)
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:13:21 -05:00
|
|
|
// TODO: uncomment in v1.5.1 once we've polished HCL a bit more.
|
|
|
|
// c.Ui.Say(`Legacy JSON Configuration Will Be Used.
|
|
|
|
// The template will be parsed in the legacy configuration style. This style
|
|
|
|
// will continue to work but users are encouraged to move to the new style.
|
|
|
|
// See: https://packer.io/guides/hcl
|
|
|
|
// `)
|
2019-12-17 05:25:56 -05:00
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Parse the template
|
2015-06-30 13:56:14 -04:00
|
|
|
var tpl *template.Template
|
2019-12-17 05:25:56 -05:00
|
|
|
tpl, err = template.ParseFile(path)
|
2014-10-27 23:21:13 -04:00
|
|
|
if err != nil {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err))
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, 1
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Get the core
|
|
|
|
core, err := c.Meta.Core(tpl)
|
2014-10-27 23:21:13 -04:00
|
|
|
if err != nil {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Error(err.Error())
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, 1
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
ret := 0
|
2015-05-23 19:30:45 -04:00
|
|
|
buildNames := c.Meta.BuildNames(core)
|
|
|
|
builds := make([]packer.Build, 0, len(buildNames))
|
|
|
|
for _, n := range buildNames {
|
|
|
|
b, err := core.Build(n)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Failed to initialize build '%s': %s",
|
|
|
|
n, err))
|
2019-12-17 05:25:56 -05:00
|
|
|
ret = 1
|
2015-06-04 10:25:14 -04:00
|
|
|
continue
|
2015-05-23 19:30:45 -04:00
|
|
|
}
|
2014-10-27 23:21:13 -04:00
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
builds = append(builds, b)
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
return builds, ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BuildCommand) RunContext(buildCtx context.Context, args []string) int {
|
|
|
|
cfg, ret := c.ParseArgs(args)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
builds, ret := c.GetBuilds(cfg.Path)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
2019-05-07 05:15:35 -04:00
|
|
|
if cfg.Debug {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Say("Debug mode enabled. Builds will not be parallelized.")
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile all the UIs for the builds
|
|
|
|
colors := [5]packer.UiColor{
|
|
|
|
packer.UiColorGreen,
|
|
|
|
packer.UiColorCyan,
|
|
|
|
packer.UiColorMagenta,
|
|
|
|
packer.UiColorYellow,
|
|
|
|
packer.UiColorBlue,
|
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
buildUis := make(map[packer.Build]packer.Ui)
|
|
|
|
for i := range builds {
|
|
|
|
ui := c.Ui
|
2019-05-07 05:15:35 -04:00
|
|
|
if cfg.Color {
|
2014-10-27 23:21:13 -04:00
|
|
|
ui = &packer.ColoredUi{
|
|
|
|
Color: colors[i%len(colors)],
|
2015-05-23 19:30:45 -04:00
|
|
|
Ui: ui,
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
2017-02-09 20:41:42 -05:00
|
|
|
if _, ok := c.Ui.(*packer.MachineReadableUi); !ok {
|
2019-12-17 05:25:56 -05:00
|
|
|
ui.Say(fmt.Sprintf("%s: output will be in this color.", builds[i].Name()))
|
|
|
|
if i+1 == len(builds) {
|
2017-02-09 20:45:42 -05:00
|
|
|
// Add a newline between the color output and the actual output
|
|
|
|
c.Ui.Say("")
|
|
|
|
}
|
2019-06-19 09:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now add timestamps if requested
|
|
|
|
if cfg.Timestamp {
|
|
|
|
ui = &packer.TimestampedUi{
|
|
|
|
Ui: ui,
|
2017-02-09 20:41:42 -05:00
|
|
|
}
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
buildUis[builds[i]] = ui
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-05-07 05:15:35 -04:00
|
|
|
log.Printf("Build debug mode: %v", cfg.Debug)
|
|
|
|
log.Printf("Force build: %v", cfg.Force)
|
|
|
|
log.Printf("On error: %v", cfg.OnError)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
// Set the debug and force mode and prepare all the builds
|
2019-12-17 05:25:56 -05:00
|
|
|
for i := range builds {
|
|
|
|
b := builds[i]
|
2014-10-27 23:21:13 -04:00
|
|
|
log.Printf("Preparing build: %s", b.Name())
|
2019-05-07 05:15:35 -04:00
|
|
|
b.SetDebug(cfg.Debug)
|
|
|
|
b.SetForce(cfg.Force)
|
|
|
|
b.SetOnError(cfg.OnError)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
warnings, err := b.Prepare()
|
|
|
|
if err != nil {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Error(err.Error())
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
ui := buildUis[b]
|
2014-10-27 23:21:13 -04:00
|
|
|
ui.Say(fmt.Sprintf("Warnings for build '%s':\n", b.Name()))
|
|
|
|
for _, warning := range warnings {
|
|
|
|
ui.Say(fmt.Sprintf("* %s", warning))
|
|
|
|
}
|
|
|
|
ui.Say("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all the builds in parallel and wait for them to complete
|
2019-05-06 06:19:59 -04:00
|
|
|
var wg sync.WaitGroup
|
2015-10-14 11:25:13 -04:00
|
|
|
var artifacts = struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string][]packer.Artifact
|
|
|
|
}{m: make(map[string][]packer.Artifact)}
|
2019-12-17 05:25:56 -05:00
|
|
|
// Get the builds we care about
|
|
|
|
var errors = struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string]error
|
|
|
|
}{m: make(map[string]error)}
|
2019-05-07 05:15:35 -04:00
|
|
|
limitParallel := semaphore.NewWeighted(cfg.ParallelBuilds)
|
2019-05-02 10:34:48 -04:00
|
|
|
for i := range builds {
|
2019-05-06 06:19:59 -04:00
|
|
|
if err := buildCtx.Err(); err != nil {
|
|
|
|
log.Println("Interrupted, not going to start any more builds.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-05-02 10:34:48 -04:00
|
|
|
b := builds[i]
|
|
|
|
name := b.Name()
|
2019-12-17 05:25:56 -05:00
|
|
|
ui := buildUis[b]
|
2019-05-02 10:34:48 -04:00
|
|
|
if err := limitParallel.Acquire(buildCtx, 1); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Build '%s' failed to acquire semaphore: %s", name, err))
|
2019-05-06 09:42:23 -04:00
|
|
|
errors.Lock()
|
|
|
|
errors.m[name] = err
|
|
|
|
errors.Unlock()
|
2019-05-02 10:34:48 -04:00
|
|
|
break
|
|
|
|
}
|
2019-05-03 01:46:26 -04:00
|
|
|
// Increment the waitgroup so we wait for this item to finish properly
|
|
|
|
wg.Add(1)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
// Run the build in a goroutine
|
2019-05-02 10:34:48 -04:00
|
|
|
go func() {
|
2014-10-27 23:21:13 -04:00
|
|
|
defer wg.Done()
|
|
|
|
|
2019-04-19 03:10:36 -04:00
|
|
|
defer limitParallel.Release(1)
|
|
|
|
|
|
|
|
log.Printf("Starting build run: %s", name)
|
2019-03-22 09:53:28 -04:00
|
|
|
runArtifacts, err := b.Run(buildCtx, ui)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err))
|
2019-05-06 09:42:23 -04:00
|
|
|
errors.Lock()
|
|
|
|
errors.m[name] = err
|
|
|
|
errors.Unlock()
|
2014-10-27 23:21:13 -04:00
|
|
|
} else {
|
|
|
|
ui.Say(fmt.Sprintf("Build '%s' finished.", name))
|
2019-03-10 10:38:38 -04:00
|
|
|
if nil != runArtifacts {
|
|
|
|
artifacts.Lock()
|
|
|
|
artifacts.m[name] = runArtifacts
|
|
|
|
artifacts.Unlock()
|
|
|
|
}
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
2019-05-02 10:34:48 -04:00
|
|
|
}()
|
2014-10-27 23:21:13 -04:00
|
|
|
|
2019-05-07 05:15:35 -04:00
|
|
|
if cfg.Debug {
|
2014-10-27 23:21:13 -04:00
|
|
|
log.Printf("Debug enabled, so waiting for build to finish: %s", b.Name())
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:15:35 -04:00
|
|
|
if cfg.ParallelBuilds == 1 {
|
2014-10-27 23:21:13 -04:00
|
|
|
log.Printf("Parallelization disabled, waiting for build to finish: %s", b.Name())
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for both the builds to complete and the interrupt handler,
|
|
|
|
// if it is interrupted.
|
|
|
|
log.Printf("Waiting on builds to complete...")
|
|
|
|
wg.Wait()
|
|
|
|
|
2019-05-06 06:19:59 -04:00
|
|
|
if err := buildCtx.Err(); err != nil {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Say("Cleanly cancelled builds after being interrupted.")
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-05-06 09:42:23 -04:00
|
|
|
if len(errors.m) > 0 {
|
|
|
|
c.Ui.Machine("error-count", strconv.FormatInt(int64(len(errors.m)), 10))
|
2014-10-27 23:21:13 -04:00
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Error("\n==> Some builds didn't complete successfully and had errors:")
|
2019-05-06 09:42:23 -04:00
|
|
|
for name, err := range errors.m {
|
2017-03-28 20:45:01 -04:00
|
|
|
// Create a UI for the machine readable stuff to be targeted
|
2017-03-29 15:44:42 -04:00
|
|
|
ui := &packer.TargetedUI{
|
2014-10-27 23:21:13 -04:00
|
|
|
Target: name,
|
2015-05-23 19:30:45 -04:00
|
|
|
Ui: c.Ui,
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Machine("error", err.Error())
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Error(fmt.Sprintf("--> %s: %s", name, err))
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 11:25:13 -04:00
|
|
|
if len(artifacts.m) > 0 {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Say("\n==> Builds finished. The artifacts of successful builds are:")
|
2015-10-14 11:25:13 -04:00
|
|
|
for name, buildArtifacts := range artifacts.m {
|
2017-03-28 20:45:01 -04:00
|
|
|
// Create a UI for the machine readable stuff to be targeted
|
2017-03-29 15:44:42 -04:00
|
|
|
ui := &packer.TargetedUI{
|
2014-10-27 23:21:13 -04:00
|
|
|
Target: name,
|
2015-05-23 19:30:45 -04:00
|
|
|
Ui: c.Ui,
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Machine-readable helpful
|
|
|
|
ui.Machine("artifact-count", strconv.FormatInt(int64(len(buildArtifacts)), 10))
|
|
|
|
|
|
|
|
for i, artifact := range buildArtifacts {
|
|
|
|
var message bytes.Buffer
|
|
|
|
fmt.Fprintf(&message, "--> %s: ", name)
|
|
|
|
|
|
|
|
if artifact != nil {
|
2016-04-06 01:41:26 -04:00
|
|
|
fmt.Fprint(&message, artifact.String())
|
2014-10-27 23:21:13 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprint(&message, "<nothing>")
|
|
|
|
}
|
|
|
|
|
|
|
|
iStr := strconv.FormatInt(int64(i), 10)
|
|
|
|
if artifact != nil {
|
|
|
|
ui.Machine("artifact", iStr, "builder-id", artifact.BuilderId())
|
|
|
|
ui.Machine("artifact", iStr, "id", artifact.Id())
|
|
|
|
ui.Machine("artifact", iStr, "string", artifact.String())
|
|
|
|
|
|
|
|
files := artifact.Files()
|
|
|
|
ui.Machine("artifact",
|
|
|
|
iStr,
|
|
|
|
"files-count", strconv.FormatInt(int64(len(files)), 10))
|
|
|
|
for fi, file := range files {
|
|
|
|
fiStr := strconv.FormatInt(int64(fi), 10)
|
|
|
|
ui.Machine("artifact", iStr, "file", fiStr, file)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ui.Machine("artifact", iStr, "nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Machine("artifact", iStr, "end")
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Say(message.String())
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Say("\n==> Builds finished but no artifacts were created.")
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-05-06 09:42:23 -04:00
|
|
|
if len(errors.m) > 0 {
|
2014-10-27 23:21:13 -04:00
|
|
|
// If any errors occurred, exit with a non-zero exit status
|
2019-12-17 05:25:56 -05:00
|
|
|
ret = 1
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return ret
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2017-10-13 14:41:42 -04:00
|
|
|
func (*BuildCommand) Help() string {
|
2014-10-27 23:21:13 -04:00
|
|
|
helpText := `
|
|
|
|
Usage: packer build [options] TEMPLATE
|
|
|
|
|
|
|
|
Will execute multiple builds in parallel as defined in the template.
|
|
|
|
The various artifacts created by the template will be outputted.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2018-10-10 21:34:35 -04:00
|
|
|
-color=false Disable color output. (Default: color)
|
|
|
|
-debug Debug mode enabled for builds.
|
2019-03-05 10:54:35 -05:00
|
|
|
-except=foo,bar,baz Run all builds and post-procesors other than these.
|
2018-10-10 21:34:35 -04:00
|
|
|
-only=foo,bar,baz Build only the specified builds.
|
|
|
|
-force Force a build to continue if artifacts exist, deletes existing artifacts.
|
|
|
|
-machine-readable Produce machine-readable output.
|
|
|
|
-on-error=[cleanup|abort|ask] If the build fails do: clean up (default), abort, or ask.
|
2019-05-02 10:01:35 -04:00
|
|
|
-parallel=false Disable parallelization. (Default: true)
|
|
|
|
-parallel-builds=1 Number of builds to run in parallel. 0 means no limit (Default: 0)
|
2018-10-03 08:40:13 -04:00
|
|
|
-timestamp-ui Enable prefixing of each ui output with an RFC3339 timestamp.
|
2018-09-01 03:32:23 -04:00
|
|
|
-var 'key=value' Variable for templates, can be used multiple times.
|
2020-03-03 11:19:51 -05:00
|
|
|
-var-file=path JSON file containing user variables. [ Note that even in HCL mode this expects file to contain JSON, a fix is comming soon ]
|
2014-10-27 23:21:13 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-10-13 14:41:42 -04:00
|
|
|
func (*BuildCommand) Synopsis() string {
|
2014-10-27 23:21:13 -04:00
|
|
|
return "build image(s) from template"
|
|
|
|
}
|
2017-10-13 14:42:22 -04:00
|
|
|
|
|
|
|
func (*BuildCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*BuildCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
|
|
|
"-color": complete.PredictNothing,
|
|
|
|
"-debug": complete.PredictNothing,
|
|
|
|
"-except": complete.PredictNothing,
|
|
|
|
"-only": complete.PredictNothing,
|
|
|
|
"-force": complete.PredictNothing,
|
|
|
|
"-machine-readable": complete.PredictNothing,
|
|
|
|
"-on-error": complete.PredictNothing,
|
|
|
|
"-parallel": complete.PredictNothing,
|
2018-10-03 08:40:13 -04:00
|
|
|
"-timestamp-ui": complete.PredictNothing,
|
2017-10-13 14:42:22 -04:00
|
|
|
"-var": complete.PredictNothing,
|
|
|
|
"-var-file": complete.PredictNothing,
|
|
|
|
}
|
|
|
|
}
|