2014-10-27 23:31:02 -04:00
|
|
|
package command
|
2013-06-13 13:03:44 -04:00
|
|
|
|
|
|
|
import (
|
2020-05-12 05:24:22 -04:00
|
|
|
"context"
|
2018-06-25 18:03:09 -04:00
|
|
|
"encoding/json"
|
2013-06-13 13:03:44 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2015-05-25 20:29:10 -04:00
|
|
|
|
2018-06-25 18:03:09 -04:00
|
|
|
"github.com/hashicorp/packer/fix"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template"
|
2017-10-13 14:42:22 -04:00
|
|
|
|
2018-06-25 18:03:09 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2017-10-13 14:42:22 -04:00
|
|
|
"github.com/posener/complete"
|
2013-06-13 13:03:44 -04:00
|
|
|
)
|
|
|
|
|
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 {
|
2020-05-12 05:24:22 -04:00
|
|
|
ctx, cleanup := handleTermInterrupt(c.Ui)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
cfg, ret := c.ParseArgs(args)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RunContext(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ValidateCommand) ParseArgs(args []string) (*ValidateArgs, int) {
|
|
|
|
var cfg ValidateArgs
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
flags := c.Meta.FlagSet("validate", FlagSetBuildFilter|FlagSetVars)
|
|
|
|
flags.Usage = func() { c.Ui.Say(c.Help()) }
|
2020-05-12 05:24:22 -04:00
|
|
|
cfg.AddFlagSets(flags)
|
2015-05-25 20:29:10 -04:00
|
|
|
if err := flags.Parse(args); err != nil {
|
2020-05-12 05:24:22 -04:00
|
|
|
return &cfg, 1
|
2014-10-27 23:31:02 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
args = flags.Args()
|
2013-06-13 13:03:44 -04:00
|
|
|
if len(args) != 1 {
|
2015-05-25 20:29:10 -04:00
|
|
|
flags.Usage()
|
2020-05-12 05:24:22 -04:00
|
|
|
return &cfg, 1
|
2013-06-13 13:03:44 -04:00
|
|
|
}
|
2020-05-12 05:24:22 -04:00
|
|
|
cfg.Path = args[0]
|
|
|
|
return &cfg, 0
|
|
|
|
}
|
2013-06-13 13:03:44 -04:00
|
|
|
|
2020-05-12 05:24:22 -04:00
|
|
|
func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int {
|
2015-05-25 20:29:10 -04:00
|
|
|
// Parse the template
|
2020-05-12 05:24:22 -04:00
|
|
|
tpl, err := template.ParseFile(cla.Path)
|
2015-05-25 20:29:10 -04:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse template: %s", err))
|
2013-08-09 18:12:33 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
// If we're only checking syntax, then we're done already
|
2020-05-12 05:24:22 -04:00
|
|
|
if cla.SyntaxOnly {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
|
|
|
|
return 0
|
2013-08-09 18:46:59 -04:00
|
|
|
}
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
// Get the core
|
2020-05-12 05:24:22 -04:00
|
|
|
core, err := c.Meta.Core(tpl, &cla.MetaArgs)
|
2013-06-13 13:03:44 -04:00
|
|
|
if err != nil {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Error(err.Error())
|
2013-06-13 13:03:44 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
// Get the builds we care about
|
2020-04-30 10:36:01 -04:00
|
|
|
buildNames := core.BuildNames(c.CoreConfig.Only, c.CoreConfig.Except)
|
2015-05-25 20:29:10 -04:00
|
|
|
builds := make([]packer.Build, 0, len(buildNames))
|
|
|
|
for _, n := range buildNames {
|
|
|
|
b, err := core.Build(n)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Failed to initialize build '%s': %s",
|
|
|
|
n, err))
|
2015-05-29 19:09:37 -04:00
|
|
|
return 1
|
2015-05-25 20:29:10 -04:00
|
|
|
}
|
2013-06-13 13:03:44 -04:00
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
builds = append(builds, b)
|
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
|
|
|
|
2018-06-25 18:03:09 -04:00
|
|
|
// Check if any of the configuration is fixable
|
2018-07-02 16:57:11 -04:00
|
|
|
var rawTemplateData map[string]interface{}
|
2018-06-25 18:03:09 -04:00
|
|
|
input := make(map[string]interface{})
|
2018-07-02 16:57:11 -04:00
|
|
|
templateData := make(map[string]interface{})
|
|
|
|
json.Unmarshal(tpl.RawContents, &rawTemplateData)
|
|
|
|
for k, v := range rawTemplateData {
|
|
|
|
if vals, ok := v.([]interface{}); ok {
|
|
|
|
if len(vals) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
templateData[strings.ToLower(k)] = v
|
|
|
|
input[strings.ToLower(k)] = v
|
2018-06-25 18:03:09 -04:00
|
|
|
}
|
2018-07-02 16:57:11 -04:00
|
|
|
|
|
|
|
// fix rawTemplateData into input
|
2018-06-25 18:03:09 -04:00
|
|
|
for _, name := range fix.FixerOrder {
|
|
|
|
var err error
|
|
|
|
fixer, ok := fix.Fixers[name]
|
|
|
|
if !ok {
|
|
|
|
panic("fixer not found: " + name)
|
|
|
|
}
|
|
|
|
input, err = fixer.Fix(input)
|
|
|
|
if err != nil {
|
2018-07-02 16:57:11 -04:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error checking against fixers: %s", err))
|
2018-06-25 18:03:09 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// delete empty top-level keys since the fixers seem to add them
|
|
|
|
// willy-nilly
|
|
|
|
for k := range input {
|
|
|
|
ml, ok := input[k].([]map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(ml) == 0 {
|
|
|
|
delete(input, k)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 16:57:11 -04:00
|
|
|
// marshal/unmarshal to make comparable to templateData
|
2018-06-25 18:03:09 -04:00
|
|
|
var fixedData map[string]interface{}
|
2018-07-02 16:57:11 -04:00
|
|
|
// Guaranteed to be valid json, so we can ignore errors
|
2018-06-25 18:03:09 -04:00
|
|
|
j, _ := json.Marshal(input)
|
|
|
|
json.Unmarshal(j, &fixedData)
|
2018-07-02 16:57:11 -04:00
|
|
|
|
2018-06-25 18:03:09 -04:00
|
|
|
if diff := cmp.Diff(templateData, fixedData); diff != "" {
|
|
|
|
c.Ui.Say("[warning] Fixable configuration found.")
|
|
|
|
c.Ui.Say("You may need to run `packer fix` to get your build to run")
|
|
|
|
c.Ui.Say("correctly. See debug log for more information.\n")
|
|
|
|
log.Printf("Fixable config differences:\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:03:44 -04:00
|
|
|
if len(errs) > 0 {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Error("Template validation failed. Errors are shown below.\n")
|
2013-06-13 13:21:56 -04:00
|
|
|
for i, err := range errs {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Error(err.Error())
|
2013-06-13 13:21:56 -04:00
|
|
|
|
2013-06-13 13:24:10 -04:00
|
|
|
if (i + 1) < len(errs) {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Error("")
|
2013-06-13 13:21:56 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-13 13:03:44 -04:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-11-03 00:09:30 -04:00
|
|
|
if len(warnings) > 0 {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Say("Template validation succeeded, but there were some warnings.")
|
|
|
|
c.Ui.Say("These are ONLY WARNINGS, and Packer will attempt to build the")
|
|
|
|
c.Ui.Say("template despite them, but they should be paid attention to.\n")
|
2013-11-03 00:09:30 -04:00
|
|
|
|
|
|
|
for build, warns := range warnings {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Say(fmt.Sprintf("Warnings for build '%s':\n", build))
|
2013-11-03 00:09:30 -04:00
|
|
|
for _, warning := range warns {
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Say(fmt.Sprintf("* %s", warning))
|
2013-11-03 00:09:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
c.Ui.Say("Template validated successfully.")
|
2013-06-13 13:03:44 -04:00
|
|
|
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.
|
2018-10-10 21:34:35 -04:00
|
|
|
-except=foo,bar,baz Validate all builds other than these.
|
|
|
|
-only=foo,bar,baz Validate only these builds.
|
2014-10-27 23:31:02 -04:00
|
|
|
-var 'key=value' Variable for templates, can be used multiple times.
|
2020-03-03 11:19:51 -05:00
|
|
|
-var-file=path JSON file containing user variables. [ Note that even in HCL mode this expects file to contain JSON, a fix is comming soon ]
|
2014-10-27 23:31:02 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ValidateCommand) Synopsis() string {
|
2013-06-13 13:03:44 -04:00
|
|
|
return "check that a template is valid"
|
|
|
|
}
|
2017-10-13 14:42:22 -04:00
|
|
|
|
|
|
|
func (*ValidateCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ValidateCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
|
|
|
"-syntax-only": complete.PredictNothing,
|
|
|
|
"-except": complete.PredictNothing,
|
|
|
|
"-only": complete.PredictNothing,
|
|
|
|
"-var": complete.PredictNothing,
|
|
|
|
"-var-file": complete.PredictNothing,
|
|
|
|
}
|
|
|
|
}
|