2013-05-07 11:39:32 -07:00
|
|
|
package build
|
2013-04-21 19:04:35 -07:00
|
|
|
|
2013-05-08 14:58:06 -07:00
|
|
|
import (
|
2013-06-04 14:13:02 -07:00
|
|
|
"flag"
|
2013-05-21 15:10:51 -07:00
|
|
|
"fmt"
|
2013-05-08 14:58:06 -07:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io/ioutil"
|
2013-05-08 16:59:36 -07:00
|
|
|
"log"
|
2013-06-03 16:14:10 -07:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-06-02 15:17:04 -07:00
|
|
|
"strings"
|
2013-05-10 13:01:54 -07:00
|
|
|
"sync"
|
2013-05-08 14:58:06 -07:00
|
|
|
)
|
2013-04-21 19:04:35 -07:00
|
|
|
|
2013-05-07 11:39:32 -07:00
|
|
|
type Command byte
|
2013-04-21 19:04:35 -07:00
|
|
|
|
2013-06-02 11:41:12 -07:00
|
|
|
func (Command) Help() string {
|
2013-06-02 15:17:04 -07:00
|
|
|
return strings.TrimSpace(helpText)
|
2013-06-02 11:41:12 -07:00
|
|
|
}
|
|
|
|
|
2013-06-02 15:17:04 -07:00
|
|
|
func (c Command) Run(env packer.Environment, args []string) int {
|
2013-06-04 14:13:02 -07:00
|
|
|
var cfgOnly []string
|
|
|
|
|
|
|
|
cmdFlags := flag.NewFlagSet("build", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
|
|
|
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 14:58:06 -07:00
|
|
|
if len(args) != 1 {
|
2013-06-04 14:13:02 -07:00
|
|
|
cmdFlags.Usage()
|
2013-05-08 14:58:06 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file into a byte array so that we can parse the template
|
2013-05-22 14:25:58 -07:00
|
|
|
log.Printf("Reading template: %s", args[0])
|
2013-05-08 14:58:06 -07:00
|
|
|
tplData, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
2013-05-27 15:15:42 -07:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to read template file: %s", err))
|
2013-05-08 14:58:06 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the template into a machine-usable format
|
2013-05-08 16:59:36 -07:00
|
|
|
log.Println("Parsing template...")
|
|
|
|
tpl, err := packer.ParseTemplate(tplData)
|
2013-05-08 14:58:06 -07:00
|
|
|
if err != nil {
|
2013-05-27 15:15:42 -07:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
|
2013-05-08 14:58:06 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-05-11 09:56:42 -07:00
|
|
|
// The component finder for our builds
|
|
|
|
components := &packer.ComponentFinder{
|
2013-05-23 21:59:03 -07:00
|
|
|
Builder: env.Builder,
|
|
|
|
Hook: env.Hook,
|
2013-05-22 16:56:04 -07:00
|
|
|
Provisioner: env.Provisioner,
|
2013-05-11 09:56:42 -07:00
|
|
|
}
|
|
|
|
|
2013-05-08 14:58:06 -07:00
|
|
|
// Go through each builder and compile the builds that we care about
|
2013-05-08 16:59:36 -07:00
|
|
|
buildNames := tpl.BuildNames()
|
|
|
|
builds := make([]packer.Build, 0, len(buildNames))
|
|
|
|
for _, buildName := range buildNames {
|
2013-06-04 14:13:02 -07: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 14:25:58 -07:00
|
|
|
log.Printf("Creating build: %s", buildName)
|
2013-05-11 09:56:42 -07:00
|
|
|
build, err := tpl.Build(buildName, components)
|
2013-05-08 16:59:36 -07:00
|
|
|
if err != nil {
|
2013-05-27 15:15:42 -07:00
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to create build '%s': \n\n%s", buildName, err))
|
2013-05-08 16:59:36 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
builds = append(builds, build)
|
|
|
|
}
|
2013-05-08 14:58:06 -07:00
|
|
|
|
2013-05-21 15:10:51 -07:00
|
|
|
// Compile all the UIs for the builds
|
2013-06-03 13:43:38 -07:00
|
|
|
colors := [5]packer.UiColor{
|
|
|
|
packer.UiColorGreen,
|
|
|
|
packer.UiColorYellow,
|
|
|
|
packer.UiColorBlue,
|
|
|
|
packer.UiColorMagenta,
|
|
|
|
packer.UiColorCyan,
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:10:51 -07:00
|
|
|
buildUis := make(map[string]packer.Ui)
|
2013-06-03 13:43:38 -07:00
|
|
|
for i, b := range builds {
|
|
|
|
var ui packer.Ui
|
|
|
|
|
2013-06-03 13:47:49 -07:00
|
|
|
ui = &packer.ColoredUi{
|
|
|
|
colors[i%len(colors)],
|
2013-06-03 13:54:28 -07:00
|
|
|
env.Ui(),
|
2013-06-03 13:47:49 -07:00
|
|
|
}
|
|
|
|
|
2013-06-03 13:43:38 -07:00
|
|
|
ui = &packer.PrefixedUi{
|
2013-05-21 15:10:51 -07:00
|
|
|
fmt.Sprintf("==> %s", b.Name()),
|
2013-06-03 11:40:08 -07:00
|
|
|
fmt.Sprintf(" %s", b.Name()),
|
2013-06-03 13:54:28 -07:00
|
|
|
ui,
|
2013-05-21 15:10:51 -07:00
|
|
|
}
|
2013-06-03 13:43:38 -07:00
|
|
|
|
|
|
|
buildUis[b.Name()] = ui
|
|
|
|
ui.Say(fmt.Sprintf("%s output will be in this color.", b.Name()))
|
2013-05-21 15:10:51 -07:00
|
|
|
}
|
|
|
|
|
2013-05-09 11:32:03 -07:00
|
|
|
// Prepare all the builds
|
|
|
|
for _, b := range builds {
|
2013-05-22 14:25:58 -07:00
|
|
|
log.Printf("Preparing build: %s", b.Name())
|
2013-05-22 16:20:40 -07:00
|
|
|
err := b.Prepare(buildUis[b.Name()])
|
2013-05-09 13:59:33 -07:00
|
|
|
if err != nil {
|
2013-05-22 14:25:58 -07:00
|
|
|
env.Ui().Error(err.Error())
|
2013-05-09 13:59:33 -07:00
|
|
|
return 1
|
|
|
|
}
|
2013-05-09 11:32:03 -07:00
|
|
|
}
|
|
|
|
|
2013-05-10 13:01:54 -07:00
|
|
|
// Run all the builds in parallel and wait for them to complete
|
|
|
|
var wg sync.WaitGroup
|
2013-05-21 22:38:56 -07:00
|
|
|
artifacts := make(map[string]packer.Artifact)
|
2013-05-10 13:01:54 -07:00
|
|
|
for _, b := range builds {
|
|
|
|
// Increment the waitgroup so we wait for this item to finish properly
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// Run the build in a goroutine
|
2013-06-05 17:46:23 -07:00
|
|
|
go func(b packer.Build) {
|
2013-05-10 13:01:54 -07:00
|
|
|
defer wg.Done()
|
2013-06-05 17:46:23 -07:00
|
|
|
|
|
|
|
log.Printf("Starting build run: %s", b.Name())
|
2013-05-21 22:38:56 -07:00
|
|
|
artifacts[b.Name()] = b.Run(buildUis[b.Name()])
|
2013-06-05 18:36:59 -07:00
|
|
|
log.Printf("Build finished: %s", b.Name())
|
2013-06-05 17:46:23 -07:00
|
|
|
}(b)
|
2013-05-10 13:01:54 -07:00
|
|
|
}
|
|
|
|
|
2013-06-03 16:14:10 -07:00
|
|
|
// Handle signals
|
2013-06-04 08:40:17 -07:00
|
|
|
var interruptWg sync.WaitGroup
|
|
|
|
interrupted := false
|
2013-06-03 16:14:10 -07:00
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sigCh
|
2013-06-04 08:40:17 -07:00
|
|
|
interruptWg.Add(1)
|
|
|
|
defer interruptWg.Done()
|
|
|
|
interrupted = true
|
|
|
|
|
2013-06-03 16:14:10 -07:00
|
|
|
log.Println("Interrupted! Cancelling builds...")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, b := range builds {
|
|
|
|
wg.Add(1)
|
|
|
|
|
2013-06-05 17:51:13 -07:00
|
|
|
go func(b packer.Build) {
|
2013-06-03 16:14:10 -07:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
log.Printf("Stopping build: %s", b.Name())
|
|
|
|
b.Cancel()
|
2013-06-05 18:36:59 -07:00
|
|
|
log.Printf("Build cancelled: %s", b.Name())
|
2013-06-05 17:51:13 -07:00
|
|
|
}(b)
|
2013-06-03 16:14:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
2013-06-04 08:40:17 -07:00
|
|
|
// Wait for both the builds to complete and the interrupt handler,
|
|
|
|
// if it is interrupted.
|
2013-06-05 18:36:59 -07:00
|
|
|
log.Printf("Waiting on builds to complete...")
|
2013-05-10 13:01:54 -07:00
|
|
|
wg.Wait()
|
2013-06-05 18:36:59 -07:00
|
|
|
|
|
|
|
log.Printf("Builds completed. Waiting on interrupt barrier...")
|
2013-06-04 08:40:17 -07:00
|
|
|
interruptWg.Wait()
|
2013-06-05 18:36:59 -07:00
|
|
|
log.Printf("Interrupt barrier passed.")
|
2013-06-04 08:40:17 -07:00
|
|
|
|
|
|
|
if interrupted {
|
|
|
|
env.Ui().Say("Cleanly cancelled builds after being interrupted.")
|
|
|
|
return 1
|
|
|
|
}
|
2013-05-21 22:38:56 -07:00
|
|
|
|
|
|
|
// Output all the artifacts
|
2013-05-22 13:25:12 -07:00
|
|
|
env.Ui().Say("\n==> The build completed! The artifacts created were:")
|
2013-05-21 22:38:56 -07:00
|
|
|
for name, artifact := range artifacts {
|
2013-05-27 15:15:42 -07:00
|
|
|
env.Ui().Say(fmt.Sprintf("--> %s:", name))
|
2013-06-05 15:36:41 -07:00
|
|
|
|
|
|
|
if artifact != nil {
|
|
|
|
env.Ui().Say(artifact.String())
|
|
|
|
} else {
|
|
|
|
env.Ui().Say("<nothing>")
|
|
|
|
}
|
2013-05-21 22:38:56 -07:00
|
|
|
}
|
|
|
|
|
2013-04-21 19:04:35 -07:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Command) Synopsis() string {
|
2013-05-08 17:28:05 -07:00
|
|
|
return "build image(s) from template"
|
2013-04-21 19:04:35 -07:00
|
|
|
}
|