packer-cn/command/validate.go

118 lines
2.8 KiB
Go
Raw Normal View History

package command
2013-06-13 13:03:44 -04:00
import (
"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"
"github.com/posener/complete"
2013-06-13 13:03:44 -04:00
)
type ValidateCommand struct {
Meta
2013-06-13 13:03:44 -04:00
}
func (c *ValidateCommand) Run(args []string) int {
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()) }
cfg.AddFlagSets(flags)
2015-05-25 20:29:10 -04:00
if err := flags.Parse(args); err != nil {
return &cfg, 1
}
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()
return &cfg, 1
2013-06-13 13:03:44 -04:00
}
cfg.Path = args[0]
return &cfg, 0
}
2013-06-13 13:03:44 -04:00
func (c *ValidateCommand) RunContext(ctx context.Context, cla *ValidateArgs) int {
command/validate: Add support for HCL2 configuration files * Update validate command to use FixConfig for checking against known fixers * Update validation command flag docs * Add ConfigFixer method to PackerHandler Interface * Implement ConfigFixer interface in PackerConfig * Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate command. The command will only display hcl.Diagnotic messaging when there is an error or warning. HCL2 Configs ``` ⇶ packer validate docker_centos_shell_provisioner.pkr.hcl ``` JSON Configs ``` ⇶ packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json Error: Failed to prepare build: "vmware-iso" 1 error occurred: * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix` against your template to update your template to be compatable with the current version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more detail. Warning: Fixable configuration found. You may need to run `packer fix` to get your build to run correctly. See debug log for more information. map[string]interface{}{ "builders": []interface{}{ map[string]interface{}{ ... // 3 identical entries "guest_os_type": string("ubuntu-64"), "http_directory": string("http"), - "iso_checksum": string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), + "iso_checksum": string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), - "iso_checksum_type": string("sha256"), "iso_url": string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"), "shutdown_command": string("echo 'vagrant' | sudo -S shutdown -P now"), ... // 4 identical entries }, }, } ```
2020-06-02 14:58:33 -04:00
packerStarter, ret := c.GetConfig(&cla.MetaArgs)
if ret != 0 {
return 1
}
2015-05-25 20:29:10 -04:00
// If we're only checking syntax, then we're done already
if cla.SyntaxOnly {
c.Ui.Say("Syntax-only check passed. Everything looks okay.")
2015-05-25 20:29:10 -04:00
return 0
}
diags := packerStarter.Initialize()
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
}
_, diags = packerStarter.GetBuilds(packer.GetBuildsOptions{
command/validate: Add support for HCL2 configuration files * Update validate command to use FixConfig for checking against known fixers * Update validation command flag docs * Add ConfigFixer method to PackerHandler Interface * Implement ConfigFixer interface in PackerConfig * Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate command. The command will only display hcl.Diagnotic messaging when there is an error or warning. HCL2 Configs ``` ⇶ packer validate docker_centos_shell_provisioner.pkr.hcl ``` JSON Configs ``` ⇶ packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json Error: Failed to prepare build: "vmware-iso" 1 error occurred: * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix` against your template to update your template to be compatable with the current version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more detail. Warning: Fixable configuration found. You may need to run `packer fix` to get your build to run correctly. See debug log for more information. map[string]interface{}{ "builders": []interface{}{ map[string]interface{}{ ... // 3 identical entries "guest_os_type": string("ubuntu-64"), "http_directory": string("http"), - "iso_checksum": string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), + "iso_checksum": string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), - "iso_checksum_type": string("sha256"), "iso_url": string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"), "shutdown_command": string("echo 'vagrant' | sudo -S shutdown -P now"), ... // 4 identical entries }, }, } ```
2020-06-02 14:58:33 -04:00
Only: cla.Only,
Except: cla.Except,
})
2013-06-13 13:03:44 -04:00
command/validate: Add support for HCL2 configuration files * Update validate command to use FixConfig for checking against known fixers * Update validation command flag docs * Add ConfigFixer method to PackerHandler Interface * Implement ConfigFixer interface in PackerConfig * Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate command. The command will only display hcl.Diagnotic messaging when there is an error or warning. HCL2 Configs ``` ⇶ packer validate docker_centos_shell_provisioner.pkr.hcl ``` JSON Configs ``` ⇶ packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json Error: Failed to prepare build: "vmware-iso" 1 error occurred: * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix` against your template to update your template to be compatable with the current version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more detail. Warning: Fixable configuration found. You may need to run `packer fix` to get your build to run correctly. See debug log for more information. map[string]interface{}{ "builders": []interface{}{ map[string]interface{}{ ... // 3 identical entries "guest_os_type": string("ubuntu-64"), "http_directory": string("http"), - "iso_checksum": string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), + "iso_checksum": string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), - "iso_checksum_type": string("sha256"), "iso_url": string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"), "shutdown_command": string("echo 'vagrant' | sudo -S shutdown -P now"), ... // 4 identical entries }, }, } ```
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
command/validate: Add support for HCL2 configuration files * Update validate command to use FixConfig for checking against known fixers * Update validation command flag docs * Add ConfigFixer method to PackerHandler Interface * Implement ConfigFixer interface in PackerConfig * Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate command. The command will only display hcl.Diagnotic messaging when there is an error or warning. HCL2 Configs ``` ⇶ packer validate docker_centos_shell_provisioner.pkr.hcl ``` JSON Configs ``` ⇶ packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json Error: Failed to prepare build: "vmware-iso" 1 error occurred: * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix` against your template to update your template to be compatable with the current version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more detail. Warning: Fixable configuration found. You may need to run `packer fix` to get your build to run correctly. See debug log for more information. map[string]interface{}{ "builders": []interface{}{ map[string]interface{}{ ... // 3 identical entries "guest_os_type": string("ubuntu-64"), "http_directory": string("http"), - "iso_checksum": string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), + "iso_checksum": string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"), - "iso_checksum_type": string("sha256"), "iso_url": string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"), "shutdown_command": string("echo 'vagrant' | sudo -S shutdown -P now"), ... // 4 identical entries }, }, } ```
2020-06-02 14:58:33 -04:00
return writeDiags(c.Ui, nil, diags)
2013-06-13 13:03:44 -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.
2020-10-14 15:58:04 -04:00
-var-file=path JSON or HCL2 file containing user variables. [ Note that even in HCL mode this expects file to contain JSON, a fix is comming soon ]
`
return strings.TrimSpace(helpText)
}
func (*ValidateCommand) Synopsis() string {
2013-06-13 13:03:44 -04:00
return "check that a template is valid"
}
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,
}
}