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 (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io/ioutil"
|
2013-05-08 19:59:36 -04:00
|
|
|
"log"
|
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-05-08 17:58:06 -04:00
|
|
|
func (Command) Run(env packer.Environment, args []string) int {
|
|
|
|
if len(args) != 1 {
|
2013-05-08 18:12:48 -04:00
|
|
|
env.Ui().Error("A single template argument is required.\n")
|
2013-05-08 17:58:06 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file into a byte array so that we can parse the template
|
2013-05-08 19:59:36 -04:00
|
|
|
log.Printf("Reading template: %s\n", args[0])
|
2013-05-08 17:58:06 -04:00
|
|
|
tplData, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
2013-05-08 18:14:08 -04:00
|
|
|
env.Ui().Error("Failed to read template file: %s\n", err.Error())
|
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-08 18:14:08 -04:00
|
|
|
env.Ui().Error("Failed to parse template: %s\n", err.Error())
|
2013-05-08 17:58:06 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
log.Printf("Creating build: %s\n", buildName)
|
|
|
|
build, err := tpl.Build(buildName, env.Builder)
|
|
|
|
if err != nil {
|
2013-05-08 20:01:57 -04:00
|
|
|
env.Ui().Error("Failed to create build '%s': \n\n%s\n", buildName, err.Error())
|
2013-05-08 19:59:36 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
builds = append(builds, build)
|
|
|
|
}
|
2013-05-08 17:58:06 -04:00
|
|
|
|
2013-05-08 19:59:36 -04:00
|
|
|
env.Ui().Say("YAY!\n")
|
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
|
|
|
}
|