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-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-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-05-08 14:58:06 -07:00
|
|
|
func (Command) Run(env packer.Environment, args []string) int {
|
|
|
|
if len(args) != 1 {
|
2013-05-08 15:12:48 -07:00
|
|
|
env.Ui().Error("A single template argument is required.\n")
|
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-08 16:59:36 -07:00
|
|
|
log.Printf("Reading template: %s\n", args[0])
|
2013-05-08 14:58:06 -07:00
|
|
|
tplData, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
2013-05-08 15:14:08 -07:00
|
|
|
env.Ui().Error("Failed to read template file: %s\n", err.Error())
|
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-08 15:14:08 -07:00
|
|
|
env.Ui().Error("Failed to parse template: %s\n", err.Error())
|
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{
|
|
|
|
Builder: env.Builder,
|
2013-05-20 16:50:35 -07:00
|
|
|
Hook: env.Hook,
|
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 {
|
|
|
|
log.Printf("Creating build: %s\n", 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-08 17:01:57 -07:00
|
|
|
env.Ui().Error("Failed to create build '%s': \n\n%s\n", buildName, err.Error())
|
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
|
|
|
|
buildUis := make(map[string]packer.Ui)
|
|
|
|
for _, b := range builds {
|
|
|
|
buildUis[b.Name()] = &packer.PrefixedUi{
|
|
|
|
fmt.Sprintf("==> %s", b.Name()),
|
|
|
|
env.Ui(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:32:03 -07:00
|
|
|
// Prepare all the builds
|
|
|
|
for _, b := range builds {
|
|
|
|
log.Printf("Preparing build: %s\n", b.Name())
|
2013-05-09 13:59:33 -07:00
|
|
|
err := b.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error("%s\n", err)
|
|
|
|
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
|
|
|
|
for _, b := range builds {
|
|
|
|
log.Printf("Starting build run: %s\n", b.Name())
|
|
|
|
|
|
|
|
// Increment the waitgroup so we wait for this item to finish properly
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// Run the build in a goroutine
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2013-05-21 15:43:50 -07:00
|
|
|
b.Run(buildUis[b.Name()])
|
2013-05-10 13:01:54 -07:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
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
|
|
|
}
|