2014-10-27 23:31:02 -04:00
|
|
|
package command
|
2013-06-13 13:03:44 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-08-09 18:12:33 -04:00
|
|
|
cmdcommon "github.com/mitchellh/packer/common/command"
|
2013-06-13 13:03:44 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-10-27 23:31:02 -04:00
|
|
|
type ValidateCommand struct {
|
|
|
|
Meta
|
2013-06-13 13:03:44 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 23:31:02 -04:00
|
|
|
func (c *ValidateCommand) Run(args []string) int {
|
2013-06-13 13:03:44 -04:00
|
|
|
var cfgSyntaxOnly bool
|
2013-08-09 18:21:24 -04:00
|
|
|
buildOptions := new(cmdcommon.BuildOptions)
|
2013-06-13 13:03:44 -04:00
|
|
|
|
2014-10-27 23:31:02 -04:00
|
|
|
env, err := c.Meta.Environment()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
|
|
|
cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only")
|
2013-08-09 18:21:24 -04:00
|
|
|
cmdcommon.BuildOptionFlags(cmdFlags, buildOptions)
|
2013-06-13 13:03:44 -04:00
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:21:24 -04:00
|
|
|
if err := buildOptions.Validate(); err != nil {
|
2013-08-09 18:12:33 -04:00
|
|
|
env.Ui().Error(err.Error())
|
|
|
|
env.Ui().Error("")
|
|
|
|
env.Ui().Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:46:59 -04:00
|
|
|
userVars, err := buildOptions.AllUserVars()
|
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
|
|
|
|
env.Ui().Error("")
|
|
|
|
env.Ui().Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
// Parse the template into a machine-usable format
|
2013-08-09 18:13:44 -04:00
|
|
|
log.Printf("Reading template: %s", args[0])
|
2013-12-27 11:21:17 -05:00
|
|
|
tpl, err := packer.ParseTemplateFile(args[0], userVars)
|
2013-06-13 13:03:44 -04:00
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfgSyntaxOnly {
|
|
|
|
env.Ui().Say("Syntax-only check passed. Everything looks okay.")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
errs := make([]error, 0)
|
2013-11-03 00:09:30 -04:00
|
|
|
warnings := make(map[string][]string)
|
2013-06-13 13:03:44 -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-06-13 13:03:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, get all the builds
|
2013-08-09 18:21:24 -04:00
|
|
|
builds, err := buildOptions.Builds(tpl, components)
|
2013-08-09 18:12:33 -04:00
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error(err.Error())
|
|
|
|
return 1
|
2013-06-13 13:03:44 -04:00
|
|
|
}
|
|
|
|
|
2013-06-13 13:21:56 -04:00
|
|
|
// Check the configuration of all builds
|
|
|
|
for _, b := range builds {
|
2013-06-19 00:10:34 -04:00
|
|
|
log.Printf("Preparing build: %s", b.Name())
|
2013-12-27 11:21:17 -05:00
|
|
|
warns, err := b.Prepare()
|
2013-11-03 00:09:30 -04:00
|
|
|
if len(warns) > 0 {
|
|
|
|
warnings[b.Name()] = warns
|
|
|
|
}
|
2013-06-13 13:21:56 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Errors validating build '%s'. %s", b.Name(), err))
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 13:06:06 -04:00
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
if len(errs) > 0 {
|
2013-06-13 13:21:56 -04:00
|
|
|
env.Ui().Error("Template validation failed. Errors are shown below.\n")
|
|
|
|
for i, err := range errs {
|
|
|
|
env.Ui().Error(err.Error())
|
|
|
|
|
2013-06-13 13:24:10 -04:00
|
|
|
if (i + 1) < len(errs) {
|
2013-06-13 13:21:56 -04:00
|
|
|
env.Ui().Error("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-11-03 00:09:30 -04:00
|
|
|
if len(warnings) > 0 {
|
|
|
|
env.Ui().Say("Template validation succeeded, but there were some warnings.")
|
|
|
|
env.Ui().Say("These are ONLY WARNINGS, and Packer will attempt to build the")
|
|
|
|
env.Ui().Say("template despite them, but they should be paid attention to.\n")
|
|
|
|
|
|
|
|
for build, warns := range warnings {
|
|
|
|
env.Ui().Say(fmt.Sprintf("Warnings for build '%s':\n", build))
|
|
|
|
for _, warning := range warns {
|
|
|
|
env.Ui().Say(fmt.Sprintf("* %s", warning))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
env.Ui().Say("Template validated successfully.")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:31:02 -04:00
|
|
|
func (*ValidateCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: packer validate [options] TEMPLATE
|
|
|
|
|
|
|
|
Checks the template is valid by parsing the template and also
|
|
|
|
checking the configuration with the various builders, provisioners, etc.
|
|
|
|
|
|
|
|
If it is not valid, the errors will be shown and the command will exit
|
|
|
|
with a non-zero exit status. If it is valid, it will exit with a zero
|
|
|
|
exit status.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-syntax-only Only check syntax. Do not verify config of the template.
|
|
|
|
-except=foo,bar,baz Validate all builds other than these
|
|
|
|
-only=foo,bar,baz Validate only these builds
|
|
|
|
-var 'key=value' Variable for templates, can be used multiple times.
|
|
|
|
-var-file=path JSON file containing user variables.
|
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ValidateCommand) Synopsis() string {
|
2013-06-13 13:03:44 -04:00
|
|
|
return "check that a template is valid"
|
|
|
|
}
|