2013-05-07 14:39:32 -04:00
|
|
|
package build
|
2013-04-21 22:04:35 -04:00
|
|
|
|
2013-05-08 17:58:06 -04:00
|
|
|
import (
|
2013-06-08 01:15:12 -04:00
|
|
|
"bytes"
|
2013-06-04 17:13:02 -04:00
|
|
|
"flag"
|
2013-05-21 18:10:51 -04:00
|
|
|
"fmt"
|
2013-05-08 17:58:06 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io/ioutil"
|
2013-05-08 19:59:36 -04:00
|
|
|
"log"
|
2013-06-03 19:14:10 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-06-02 18:17:04 -04:00
|
|
|
"strings"
|
2013-05-10 16:01:54 -04:00
|
|
|
"sync"
|
2013-05-08 17:58:06 -04:00
|
|
|
)
|
2013-04-21 22:04:35 -04:00
|
|
|
|
2013-05-07 14:39:32 -04:00
|
|
|
type Command byte
|
2013-04-21 22:04:35 -04:00
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
func (Command) Help() string {
|
2013-06-02 18:17:04 -04:00
|
|
|
return strings.TrimSpace(helpText)
|
2013-06-02 14:41:12 -04:00
|
|
|
}
|
|
|
|
|
2013-06-02 18:17:04 -04:00
|
|
|
func (c Command) Run(env packer.Environment, args []string) int {
|
2013-06-14 16:14:17 -04:00
|
|
|
var cfgDebug bool
|
2013-06-13 12:47:13 -04:00
|
|
|
var cfgExcept []string
|
2013-06-04 17:13:02 -04:00
|
|
|
var cfgOnly []string
|
|
|
|
|
|
|
|
cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
2013-06-14 16:14:17 -04:00
|
|
|
cmdFlags.BoolVar(&cfgDebug, "debug", false, "debug mode for builds")
|
2013-06-13 12:47:13 -04:00
|
|
|
cmdFlags.Var((*stringSliceValue)(&cfgExcept), "except", "build all builds except these")
|
2013-06-04 17:13:02 -04:00
|
|
|
cmdFlags.Var((*stringSliceValue)(&cfgOnly), "only", "only build the given builds by name")
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
2013-05-08 17:58:06 -04:00
|
|
|
if len(args) != 1 {
|
2013-06-04 17:13:02 -04:00
|
|
|
cmdFlags.Usage()
|
2013-05-08 17:58:06 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-06-13 12:47:13 -04:00
|
|
|
if len(cfgOnly) > 0 && len(cfgExcept) > 0 {
|
|
|
|
env.Ui().Error("Only one of '-except' or '-only' may be specified.\n")
|
|
|
|
env.Ui().Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-05-08 17:58:06 -04:00
|
|
|
// Read the file into a byte array so that we can parse the template
|
2013-05-22 17:25:58 -04:00
|
|
|
log.Printf("Reading template: %s", args[0])
|
2013-05-08 17:58:06 -04:00
|
|
|
tplData, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to read template file: %s", err))
|
2013-05-08 17:58:06 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the template into a machine-usable format
|
2013-05-08 19:59:36 -04:00
|
|
|
log.Println("Parsing template...")
|
|
|
|
tpl, err := packer.ParseTemplate(tplData)
|
2013-05-08 17:58:06 -04:00
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
|
2013-05-08 17:58:06 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-05-11 12:56:42 -04:00
|
|
|
// The component finder for our builds
|
|
|
|
components := &packer.ComponentFinder{
|
2013-06-18 19:29:29 -04:00
|
|
|
Builder: env.Builder,
|
|
|
|
Hook: env.Hook,
|
|
|
|
PostProcessor: env.PostProcessor,
|
|
|
|
Provisioner: env.Provisioner,
|
2013-05-11 12:56:42 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 17:58:06 -04:00
|
|
|
// Go through each builder and compile the builds that we care about
|
2013-05-08 19:59:36 -04:00
|
|
|
buildNames := tpl.BuildNames()
|
|
|
|
builds := make([]packer.Build, 0, len(buildNames))
|
|
|
|
for _, buildName := range buildNames {
|
2013-06-13 12:47:13 -04:00
|
|
|
if len(cfgExcept) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, only := range cfgExcept {
|
|
|
|
if buildName == only {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
log.Printf("Skipping build '%s' because specified by -except.", buildName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:13:02 -04:00
|
|
|
if len(cfgOnly) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, only := range cfgOnly {
|
|
|
|
if buildName == only {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
log.Printf("Skipping build '%s' because not specified by -only.", buildName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 17:25:58 -04:00
|
|
|
log.Printf("Creating build: %s", buildName)
|
2013-05-11 12:56:42 -04:00
|
|
|
build, err := tpl.Build(buildName, components)
|
2013-05-08 19:59:36 -04:00
|
|
|
if err != nil {
|
2013-05-27 18:15:42 -04:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to create build '%s': \n\n%s", buildName, err))
|
2013-05-08 19:59:36 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
builds = append(builds, build)
|
|
|
|
}
|
2013-05-08 17:58:06 -04:00
|
|
|
|
2013-06-14 17:47:28 -04:00
|
|
|
if cfgDebug {
|
|
|
|
env.Ui().Say("Debug mode enabled. Builds will not be parallelized.")
|
|
|
|
}
|
|
|
|
|
2013-05-21 18:10:51 -04:00
|
|
|
// Compile all the UIs for the builds
|
2013-06-03 16:43:38 -04:00
|
|
|
colors := [5]packer.UiColor{
|
|
|
|
packer.UiColorGreen,
|
2013-06-12 13:47:01 -04:00
|
|
|
packer.UiColorCyan,
|
|
|
|
packer.UiColorMagenta,
|
2013-06-03 16:43:38 -04:00
|
|
|
packer.UiColorYellow,
|
|
|
|
packer.UiColorBlue,
|
|
|
|
}
|
|
|
|
|
2013-05-21 18:10:51 -04:00
|
|
|
buildUis := make(map[string]packer.Ui)
|
2013-06-03 16:43:38 -04:00
|
|
|
for i, b := range builds {
|
2013-06-27 21:55:59 -04:00
|
|
|
ui := &packer.ColoredUi{
|
2013-06-12 13:41:58 -04:00
|
|
|
Color: colors[i%len(colors)],
|
2013-06-12 20:41:44 -04:00
|
|
|
Ui: env.Ui(),
|
2013-06-03 16:47:49 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 16:43:38 -04:00
|
|
|
buildUis[b.Name()] = ui
|
|
|
|
ui.Say(fmt.Sprintf("%s output will be in this color.", b.Name()))
|
2013-05-21 18:10:51 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 13:47:01 -04:00
|
|
|
// Add a newline between the color output and the actual output
|
|
|
|
env.Ui().Say("")
|
|
|
|
|
2013-06-14 16:14:17 -04:00
|
|
|
log.Printf("Build debug mode: %v", cfgDebug)
|
|
|
|
|
|
|
|
// Set the debug mode and prepare all the builds
|
2013-05-09 14:32:03 -04:00
|
|
|
for _, b := range builds {
|
2013-05-22 17:25:58 -04:00
|
|
|
log.Printf("Preparing build: %s", b.Name())
|
2013-06-14 16:14:17 -04:00
|
|
|
b.SetDebug(cfgDebug)
|
2013-06-13 13:08:31 -04:00
|
|
|
err := b.Prepare()
|
2013-05-09 16:59:33 -04:00
|
|
|
if err != nil {
|
2013-05-22 17:25:58 -04:00
|
|
|
env.Ui().Error(err.Error())
|
2013-05-09 16:59:33 -04:00
|
|
|
return 1
|
|
|
|
}
|
2013-05-09 14:32:03 -04:00
|
|
|
}
|
|
|
|
|
2013-05-10 16:01:54 -04:00
|
|
|
// Run all the builds in parallel and wait for them to complete
|
2013-06-14 20:20:11 -04:00
|
|
|
var interruptWg, wg sync.WaitGroup
|
|
|
|
interrupted := false
|
2013-06-18 13:24:23 -04:00
|
|
|
artifacts := make(map[string][]packer.Artifact)
|
2013-06-17 14:48:21 -04:00
|
|
|
errors := make(map[string]error)
|
2013-05-10 16:01:54 -04:00
|
|
|
for _, b := range builds {
|
|
|
|
// Increment the waitgroup so we wait for this item to finish properly
|
|
|
|
wg.Add(1)
|
|
|
|
|
2013-06-14 20:20:11 -04:00
|
|
|
// Handle interrupts for this build
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, os.Interrupt)
|
|
|
|
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()
|
|
|
|
log.Printf("Build cancelled: %s", b.Name())
|
|
|
|
}(b)
|
|
|
|
|
2013-05-10 16:01:54 -04:00
|
|
|
// Run the build in a goroutine
|
2013-06-05 20:46:23 -04:00
|
|
|
go func(b packer.Build) {
|
2013-05-10 16:01:54 -04:00
|
|
|
defer wg.Done()
|
2013-06-05 20:46:23 -04:00
|
|
|
|
2013-06-17 14:48:21 -04:00
|
|
|
name := b.Name()
|
|
|
|
log.Printf("Starting build run: %s", name)
|
|
|
|
ui := buildUis[name]
|
2013-06-18 13:24:23 -04:00
|
|
|
runArtifacts, err := b.Run(ui, env.Cache())
|
2013-06-12 19:06:56 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2013-06-27 22:26:48 -04:00
|
|
|
ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err))
|
2013-06-17 14:48:21 -04:00
|
|
|
errors[name] = err
|
2013-06-12 19:06:56 -04:00
|
|
|
} else {
|
2013-06-27 22:26:48 -04:00
|
|
|
ui.Say(fmt.Sprintf("Build '%s' finished.", name))
|
2013-06-18 13:24:23 -04:00
|
|
|
artifacts[name] = runArtifacts
|
2013-06-12 19:06:56 -04:00
|
|
|
}
|
2013-06-05 20:46:23 -04:00
|
|
|
}(b)
|
2013-06-14 16:14:17 -04:00
|
|
|
|
|
|
|
if cfgDebug {
|
|
|
|
log.Printf("Debug enabled, so waiting for build to finish: %s", b.Name())
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2013-06-03 19:14:10 -04:00
|
|
|
|
2013-06-14 20:20:11 -04:00
|
|
|
if interrupted {
|
|
|
|
log.Println("Interrupted, not going to start any more builds.")
|
|
|
|
break
|
2013-06-03 19:14:10 -04:00
|
|
|
}
|
2013-06-14 20:20:11 -04:00
|
|
|
}
|
2013-06-03 19:14:10 -04:00
|
|
|
|
2013-06-04 11:40:17 -04:00
|
|
|
// Wait for both the builds to complete and the interrupt handler,
|
|
|
|
// if it is interrupted.
|
2013-06-05 21:36:59 -04:00
|
|
|
log.Printf("Waiting on builds to complete...")
|
2013-05-10 16:01:54 -04:00
|
|
|
wg.Wait()
|
2013-06-05 21:36:59 -04:00
|
|
|
|
|
|
|
log.Printf("Builds completed. Waiting on interrupt barrier...")
|
2013-06-04 11:40:17 -04:00
|
|
|
interruptWg.Wait()
|
|
|
|
|
|
|
|
if interrupted {
|
|
|
|
env.Ui().Say("Cleanly cancelled builds after being interrupted.")
|
|
|
|
return 1
|
|
|
|
}
|
2013-05-22 01:38:56 -04:00
|
|
|
|
2013-06-17 14:48:21 -04:00
|
|
|
if len(errors) > 0 {
|
|
|
|
env.Ui().Error("\n==> Some builds didn't complete successfully and had errors:")
|
|
|
|
for name, err := range errors {
|
|
|
|
env.Ui().Error(fmt.Sprintf("--> %s: %s", name, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 14:49:12 -04:00
|
|
|
if len(artifacts) > 0 {
|
|
|
|
env.Ui().Say("\n==> Builds finished. The artifacts of successful builds are:")
|
2013-06-18 13:24:23 -04:00
|
|
|
for name, buildArtifacts := range artifacts {
|
|
|
|
for _, artifact := range buildArtifacts {
|
|
|
|
var message bytes.Buffer
|
|
|
|
fmt.Fprintf(&message, "--> %s: ", name)
|
|
|
|
|
|
|
|
if artifact != nil {
|
|
|
|
fmt.Fprintf(&message, artifact.String())
|
|
|
|
} else {
|
2013-06-19 16:07:52 -04:00
|
|
|
fmt.Fprint(&message, "<nothing>")
|
2013-06-18 13:24:23 -04:00
|
|
|
}
|
2013-06-17 14:49:12 -04:00
|
|
|
|
2013-06-18 13:24:23 -04:00
|
|
|
env.Ui().Say(message.String())
|
2013-06-17 14:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-19 01:53:30 -04:00
|
|
|
} else {
|
|
|
|
env.Ui().Say("\n==> Builds finished but no artifacts were created.")
|
2013-05-22 01:38:56 -04:00
|
|
|
}
|
|
|
|
|
2013-04-21 22:04:35 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Command) Synopsis() string {
|
2013-05-08 20:28:05 -04:00
|
|
|
return "build image(s) from template"
|
2013-04-21 22:04:35 -04:00
|
|
|
}
|