From 9acaa97a32cbf628a450b81aa52343b913ce7f42 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 2 Nov 2013 23:17:21 -0500 Subject: [PATCH] builder/virtualbox,vmware: warning if shutdown_command is not specified --- builder/virtualbox/builder.go | 16 ++++++++++++---- builder/virtualbox/builder_test.go | 16 ++++++++++++++++ builder/vmware/builder.go | 14 +++++++++++--- builder/vmware/builder_test.go | 16 ++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 2744375d2..5df6c30eb 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -84,8 +84,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } b.config.tpl.UserVars = b.config.PackerUserVars - // Accumulate any errors + // Accumulate any errors and warnings errs := common.CheckUnusedConfig(md) + warnings := make([]string, 0) if b.config.DiskSize == 0 { b.config.DiskSize = 40000 @@ -363,11 +364,18 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - if errs != nil && len(errs.Errors) > 0 { - return nil, errs + // Warnings + if b.config.ShutdownCommand == "" { + warnings = append(warnings, + "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ + "will forcibly halt the virtual machine, which may result in data loss.") } - return nil, nil + if errs != nil && len(errs.Errors) > 0 { + return warnings, errs + } + + return warnings, nil } func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { diff --git a/builder/virtualbox/builder_test.go b/builder/virtualbox/builder_test.go index f5f263524..942bc059f 100644 --- a/builder/virtualbox/builder_test.go +++ b/builder/virtualbox/builder_test.go @@ -43,6 +43,7 @@ func testConfig() map[string]interface{} { "iso_checksum": "foo", "iso_checksum_type": "md5", "iso_url": "http://www.google.com/", + "shutdown_command": "yes", "ssh_username": "foo", packer.BuildNameConfigKey: "foo", @@ -645,6 +646,21 @@ func TestBuilderPrepare_OutputDir(t *testing.T) { } } +func TestBuilderPrepare_ShutdownCommand(t *testing.T) { + var b Builder + config := testConfig() + delete(config, "shutdown_command") + + warns, err := b.Prepare(config) + if err != nil { + t.Fatalf("bad: %s", err) + } + + if len(warns) != 1 { + t.Fatalf("bad: %#v", warns) + } +} + func TestBuilderPrepare_ShutdownTimeout(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index c5a657b68..f75014459 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -80,6 +80,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Accumulate any errors errs := common.CheckUnusedConfig(md) + warnings := make([]string, 0) if b.config.DiskName == "" { b.config.DiskName = "disk" @@ -326,11 +327,18 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max")) } - if errs != nil && len(errs.Errors) > 0 { - return nil, errs + // Warnings + if b.config.ShutdownCommand == "" { + warnings = append(warnings, + "A shutdown_command was not specified. Without a shutdown command, Packer\n"+ + "will forcibly halt the virtual machine, which may result in data loss.") } - return nil, nil + if errs != nil && len(errs.Errors) > 0 { + return warnings, errs + } + + return warnings, nil } func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { diff --git a/builder/vmware/builder_test.go b/builder/vmware/builder_test.go index 7d627d09d..893970768 100644 --- a/builder/vmware/builder_test.go +++ b/builder/vmware/builder_test.go @@ -44,6 +44,7 @@ func testConfig() map[string]interface{} { "iso_checksum": "foo", "iso_checksum_type": "md5", "iso_url": "http://www.packer.io", + "shutdown_command": "foo", "ssh_username": "foo", packer.BuildNameConfigKey: "foo", @@ -418,6 +419,21 @@ func TestBuilderPrepare_OutputDir(t *testing.T) { } } +func TestBuilderPrepare_ShutdownCommand(t *testing.T) { + var b Builder + config := testConfig() + delete(config, "shutdown_command") + + warns, err := b.Prepare(config) + if err != nil { + t.Fatalf("bad: %s", err) + } + + if len(warns) != 1 { + t.Fatalf("bad: %#v", warns) + } +} + func TestBuilderPrepare_ShutdownTimeout(t *testing.T) { var b Builder config := testConfig()