2014-10-27 23:21:13 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-09-08 14:31:19 -04:00
|
|
|
"syscall"
|
2015-05-23 19:30:45 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/enumflag"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template"
|
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 {
|
2018-09-29 09:09:24 -04:00
|
|
|
var cfgColor, cfgDebug, cfgForce, cfgTimestamp, cfgParallel bool
|
2016-09-13 20:04:18 -04:00
|
|
|
var cfgOnError string
|
2015-05-23 19:30:45 -04:00
|
|
|
flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars)
|
|
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
|
|
|
flags.BoolVar(&cfgColor, "color", true, "")
|
|
|
|
flags.BoolVar(&cfgDebug, "debug", false, "")
|
|
|
|
flags.BoolVar(&cfgForce, "force", false, "")
|
2018-10-03 08:40:13 -04:00
|
|
|
flags.BoolVar(&cfgTimestamp, "timestamp-ui", false, "")
|
2016-09-17 10:34:37 -04:00
|
|
|
flagOnError := enumflag.New(&cfgOnError, "cleanup", "abort", "ask")
|
|
|
|
flags.Var(flagOnError, "on-error", "")
|
2015-05-23 19:30:45 -04:00
|
|
|
flags.BoolVar(&cfgParallel, "parallel", true, "")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
args = flags.Args()
|
2014-10-27 23:21:13 -04:00
|
|
|
if len(args) != 1 {
|
2015-05-23 19:30:45 -04:00
|
|
|
flags.Usage()
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Parse the template
|
2015-06-30 13:56:14 -04:00
|
|
|
var tpl *template.Template
|
|
|
|
var err error
|
2015-10-25 15:28:06 -04:00
|
|
|
tpl, err = template.ParseFile(args[0])
|
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))
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
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())
|
2014-10-27 23:21:13 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Get the builds we care about
|
|
|
|
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))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if cfgDebug {
|
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,
|
|
|
|
}
|
|
|
|
buildUis := make(map[string]packer.Ui)
|
2015-05-23 19:30:45 -04:00
|
|
|
for i, b := range buildNames {
|
2014-10-27 23:21:13 -04:00
|
|
|
var ui packer.Ui
|
2015-05-23 19:30:45 -04:00
|
|
|
ui = c.Ui
|
2014-10-27 23:21:13 -04:00
|
|
|
if cfgColor {
|
|
|
|
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 {
|
|
|
|
ui.Say(fmt.Sprintf("%s output will be in this color.", b))
|
2017-02-09 20:45:42 -05:00
|
|
|
if i+1 == len(buildNames) {
|
|
|
|
// Add a newline between the color output and the actual output
|
|
|
|
c.Ui.Say("")
|
|
|
|
}
|
2018-09-29 09:09:24 -04:00
|
|
|
// Now add timestamps if requested
|
|
|
|
if cfgTimestamp {
|
|
|
|
ui = &packer.TimestampedUi{
|
|
|
|
Ui: ui,
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 20:41:42 -05:00
|
|
|
}
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
buildUis[b] = ui
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Build debug mode: %v", cfgDebug)
|
|
|
|
log.Printf("Force build: %v", cfgForce)
|
2016-09-13 20:04:18 -04:00
|
|
|
log.Printf("On error: %v", cfgOnError)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
// Set the debug and force mode and prepare all the builds
|
|
|
|
for _, b := range builds {
|
|
|
|
log.Printf("Preparing build: %s", b.Name())
|
|
|
|
b.SetDebug(cfgDebug)
|
|
|
|
b.SetForce(cfgForce)
|
2016-09-13 20:04:18 -04:00
|
|
|
b.SetOnError(cfgOnError)
|
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 {
|
|
|
|
ui := buildUis[b.Name()]
|
|
|
|
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
|
|
|
|
var interruptWg, wg sync.WaitGroup
|
|
|
|
interrupted := false
|
2015-10-14 11:25:13 -04:00
|
|
|
var artifacts = struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string][]packer.Artifact
|
|
|
|
}{m: make(map[string][]packer.Artifact)}
|
2014-10-27 23:21:13 -04:00
|
|
|
errors := make(map[string]error)
|
2018-01-18 01:49:03 -05:00
|
|
|
// ctx := context.Background()
|
2014-10-27 23:21:13 -04:00
|
|
|
for _, b := range builds {
|
|
|
|
// Increment the waitgroup so we wait for this item to finish properly
|
|
|
|
wg.Add(1)
|
2018-01-18 01:49:03 -05:00
|
|
|
// buildCtx, cancelCtx := ctx.WithCancel()
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
// Handle interrupts for this build
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
2017-09-08 14:31:19 -04:00
|
|
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
2014-10-27 23:21:13 -04:00
|
|
|
defer signal.Stop(sigCh)
|
|
|
|
go func(b packer.Build) {
|
|
|
|
<-sigCh
|
|
|
|
interruptWg.Add(1)
|
|
|
|
defer interruptWg.Done()
|
|
|
|
interrupted = true
|
|
|
|
|
|
|
|
log.Printf("Stopping build: %s", b.Name())
|
|
|
|
b.Cancel()
|
2018-01-18 01:49:03 -05:00
|
|
|
//cancelCtx()
|
2014-10-27 23:21:13 -04:00
|
|
|
log.Printf("Build cancelled: %s", b.Name())
|
|
|
|
}(b)
|
|
|
|
|
|
|
|
// Run the build in a goroutine
|
|
|
|
go func(b packer.Build) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
name := b.Name()
|
|
|
|
log.Printf("Starting build run: %s", name)
|
|
|
|
ui := buildUis[name]
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
runArtifacts, err := b.Run(ui)
|
2014-10-27 23:21:13 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err))
|
|
|
|
errors[name] = err
|
|
|
|
} else {
|
|
|
|
ui.Say(fmt.Sprintf("Build '%s' finished.", name))
|
2015-10-14 11:25:13 -04:00
|
|
|
artifacts.Lock()
|
|
|
|
artifacts.m[name] = runArtifacts
|
|
|
|
artifacts.Unlock()
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
|
|
|
}(b)
|
|
|
|
|
|
|
|
if cfgDebug {
|
|
|
|
log.Printf("Debug enabled, so waiting for build to finish: %s", b.Name())
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cfgParallel {
|
|
|
|
log.Printf("Parallelization disabled, waiting for build to finish: %s", b.Name())
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if interrupted {
|
|
|
|
log.Println("Interrupted, not going to start any more builds.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for both the builds to complete and the interrupt handler,
|
|
|
|
// if it is interrupted.
|
|
|
|
log.Printf("Waiting on builds to complete...")
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
log.Printf("Builds completed. Waiting on interrupt barrier...")
|
|
|
|
interruptWg.Wait()
|
|
|
|
|
|
|
|
if interrupted {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
2015-05-23 19:30:45 -04:00
|
|
|
c.Ui.Machine("error-count", strconv.FormatInt(int64(len(errors)), 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:")
|
2014-10-27 23:21:13 -04:00
|
|
|
for name, err := range errors {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
// If any errors occurred, exit with a non-zero exit status
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
-parallel=false Disable parallelization. (Default: parallel)
|
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.
|
|
|
|
-var-file=path JSON file containing user variables.
|
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,
|
|
|
|
}
|
|
|
|
}
|