Files
Packer-Cn/command/build/command.go
T

110 lines
2.5 KiB
Go
Raw Normal View History

2013-05-07 11:39:32 -07:00
package build
2013-04-21 19:04:35 -07:00
import (
"fmt"
"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-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
func (Command) Help() string {
return "help"
}
func (Command) Run(env packer.Environment, args []string) int {
if len(args) != 1 {
2013-05-22 14:25:58 -07:00
env.Ui().Error("A single template argument is required.")
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])
tplData, err := ioutil.ReadFile(args[0])
if err != nil {
env.Ui().Error(fmt.Sprintf("Failed to read template file: %s", err))
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)
if err != nil {
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
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,
Provisioner: env.Provisioner,
2013-05-11 09:56:42 -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-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 {
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
}
2013-05-08 16:59:36 -07:00
builds = append(builds, build)
}
// 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 {
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()])
if err != nil {
2013-05-22 14:25:58 -07:00
env.Ui().Error(err.Error())
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 {
2013-05-22 14:25:58 -07:00
log.Printf("Starting build run: %s", b.Name())
2013-05-10 13:01:54 -07:00
// 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 22:38:56 -07:00
artifacts[b.Name()] = b.Run(buildUis[b.Name()])
2013-05-10 13:01:54 -07:00
}()
}
wg.Wait()
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 {
env.Ui().Say(fmt.Sprintf("--> %s:", name))
2013-05-21 22:38:56 -07:00
env.Ui().Say(artifact.String())
}
2013-04-21 19:04:35 -07:00
return 0
}
func (Command) Synopsis() string {
return "build image(s) from template"
2013-04-21 19:04:35 -07:00
}