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"
|
2013-06-13 13:03:44 -04:00
|
|
|
"strings"
|
2015-05-25 20:29:10 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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 {
|
2020-06-02 14:58:33 -04:00
|
|
|
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
|
|
|
|
if ret != 0 {
|
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 {
|
2020-07-24 04:58:03 -04:00
|
|
|
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
|
2015-05-25 20:29:10 -04:00
|
|
|
return 0
|
2013-08-09 18:46:59 -04:00
|
|
|
}
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
diags := packerStarter.Initialize(packer.InitializeOptions{
|
|
|
|
SkipDatasourcesExecution: true,
|
|
|
|
})
|
2020-07-24 04:58:03 -04:00
|
|
|
ret = writeDiags(c.Ui, nil, diags)
|
|
|
|
if ret != 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
_, diags = packerStarter.GetBuilds(packer.GetBuildsOptions{
|
2020-06-02 14:58:33 -04:00
|
|
|
Only: cla.Only,
|
|
|
|
Except: cla.Except,
|
|
|
|
})
|
2013-06-13 13:03:44 -04:00
|
|
|
|
2020-06-02 14:58:33 -04:00
|
|
|
fixerDiags := packerStarter.FixConfig(packer.FixConfigOptions{
|
|
|
|
Mode: packer.Diff,
|
|
|
|
})
|
|
|
|
diags = append(diags, fixerDiags...)
|
2013-11-03 00:09:30 -04:00
|
|
|
|
2020-06-02 14:58:33 -04:00
|
|
|
return writeDiags(c.Ui, nil, diags)
|
2013-06-13 13:03:44 -04:00
|
|
|
}
|
|
|
|
|
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.
|
2021-02-19 05:17:47 -05:00
|
|
|
-machine-readable Produce machine-readable output.
|
2018-10-10 21:34:35 -04:00
|
|
|
-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.
|
2021-01-11 05:50:23 -05:00
|
|
|
-var-file=path JSON or HCL2 file containing user variables.
|
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{
|
2021-02-19 05:17:47 -05:00
|
|
|
"-syntax-only": complete.PredictNothing,
|
|
|
|
"-except": complete.PredictNothing,
|
|
|
|
"-only": complete.PredictNothing,
|
|
|
|
"-var": complete.PredictNothing,
|
|
|
|
"-machine-readable": complete.PredictNothing,
|
|
|
|
"-var-file": complete.PredictNothing,
|
2017-10-13 14:42:22 -04:00
|
|
|
}
|
|
|
|
}
|