From 65ff092fd412e33f64acfbee0c437f43c9e3c58c Mon Sep 17 00:00:00 2001 From: James Griffith Date: Thu, 10 Sep 2020 16:18:57 -0400 Subject: [PATCH 01/12] hashicorp/packer#9924 --- builder/vsphere/iso/builder.go | 70 +++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index a7f6f29cb..e5f3a183f 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -77,40 +77,40 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &common.StepConfigParams{ Config: &b.config.ConfigParamsConfig, }, + &packerCommon.StepCreateFloppy{ + Files: b.config.FloppyFiles, + Directories: b.config.FloppyDirectories, + Label: b.config.FloppyLabel, + }, + &StepAddFloppy{ + Config: &b.config.FloppyConfig, + Datastore: b.config.Datastore, + Host: b.config.Host, + SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads, + }, + &common.StepHTTPIPDiscover{ + HTTPIP: b.config.BootConfig.HTTPIP, + Network: b.config.WaitIpConfig.GetIPNet(), + }, + &packerCommon.StepHTTPServer{ + HTTPDir: b.config.HTTPDir, + HTTPPortMin: b.config.HTTPPortMin, + HTTPPortMax: b.config.HTTPPortMax, + HTTPAddress: b.config.HTTPAddress, + }, + &common.StepRun{ + Config: &b.config.RunConfig, + SetOrder: true, + }, + &common.StepBootCommand{ + Config: &b.config.BootConfig, + Ctx: b.config.ctx, + VMName: b.config.VMName, + }, ) if b.config.Comm.Type != "none" { steps = append(steps, - &packerCommon.StepCreateFloppy{ - Files: b.config.FloppyFiles, - Directories: b.config.FloppyDirectories, - Label: b.config.FloppyLabel, - }, - &common.StepAddFloppy{ - Config: &b.config.FloppyConfig, - Datastore: b.config.Datastore, - Host: b.config.Host, - SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads, - }, - &common.StepHTTPIPDiscover{ - HTTPIP: b.config.BootConfig.HTTPIP, - Network: b.config.WaitIpConfig.GetIPNet(), - }, - &packerCommon.StepHTTPServer{ - HTTPDir: b.config.HTTPDir, - HTTPPortMin: b.config.HTTPPortMin, - HTTPPortMax: b.config.HTTPPortMax, - HTTPAddress: b.config.HTTPAddress, - }, - &common.StepRun{ - Config: &b.config.RunConfig, - SetOrder: true, - }, - &common.StepBootCommand{ - Config: &b.config.BootConfig, - Ctx: b.config.ctx, - VMName: b.config.VMName, - }, &common.StepWaitForIp{ Config: &b.config.WaitIpConfig, }, @@ -123,15 +123,15 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &common.StepShutdown{ Config: &b.config.ShutdownConfig, }, - &common.StepRemoveFloppy{ - Datastore: b.config.Datastore, - Host: b.config.Host, - }, ) } steps = append(steps, - &common.StepRemoveCDRom{ + &StepRemoveFloppy{ + Datastore: b.config.Datastore, + Host: b.config.Host, + }, + &StepRemoveCDRom{ Config: &b.config.RemoveCDRomConfig, }, &common.StepCreateSnapshot{ From 6b39b1eed696937258698e8eb96fe3770e1d377a Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 15 Sep 2020 09:56:06 -0400 Subject: [PATCH 02/12] StepShutdown should still occur if Comm.Type is none --- builder/vsphere/iso/builder.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index e5f3a183f..cd31bfe59 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -120,13 +120,13 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack SSHConfig: b.config.Comm.SSHConfigFunc(), }, &packerCommon.StepProvision{}, - &common.StepShutdown{ - Config: &b.config.ShutdownConfig, - }, ) } steps = append(steps, + &common.StepShutdown{ + Config: &b.config.ShutdownConfig, + }, &StepRemoveFloppy{ Datastore: b.config.Datastore, Host: b.config.Host, From 2ecf5cc9aa9b182dd7cf7c0d1fca11c519a5dffb Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 15 Sep 2020 09:56:36 -0400 Subject: [PATCH 03/12] Communicator is not needed unless shutdown_command is populated --- builder/vsphere/common/step_shutdown.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index f85749f40..78319fd4d 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -47,7 +47,6 @@ type StepShutdown struct { func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - comm := state.Get("communicator").(packer.Communicator) vm := state.Get("vm").(*driver.VirtualMachineDriver) if off, _ := vm.IsPoweredOff(); off { @@ -59,6 +58,9 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis if s.Config.DisableShutdown { ui.Say("Automatic shutdown disabled. Please shutdown virtual machine.") } else if s.Config.Command != "" { + // Communicator is not needed unless shutdown_command is populated + comm := state.Get("communicator").(packer.Communicator) + ui.Say("Executing shutdown command...") log.Printf("Shutdown command: %s", s.Config.Command) From 1048cdbcc4e57822e2ffe8c0f0c765c4cfeecba2 Mon Sep 17 00:00:00 2001 From: James Griffith Date: Fri, 18 Sep 2020 16:00:30 -0400 Subject: [PATCH 04/12] check for no communicator and provide messaging to user --- builder/vsphere/common/step_shutdown.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index 78319fd4d..fab80dcd2 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -55,7 +55,17 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis return multistep.ActionContinue } - if s.Config.DisableShutdown { + if state.Get("communicator") == nil { + + ui.Say("The `communicator` is 'none', automatic shutdown disabled.") + if s.Config.Command != "" { + ui.Message("The parameter `shutdown_command` is ignored as it requires a `communicator`.") + } + + msg := fmt.Sprintf("Please shutdown virtual machine within %s.", s.Config.Timeout) + ui.Message(msg) + + } else if s.Config.DisableShutdown { ui.Say("Automatic shutdown disabled. Please shutdown virtual machine.") } else if s.Config.Command != "" { // Communicator is not needed unless shutdown_command is populated From 572de129d72066ecc3e63bc4a6f336c32e3c31e9 Mon Sep 17 00:00:00 2001 From: James Griffith Date: Fri, 18 Sep 2020 16:01:16 -0400 Subject: [PATCH 05/12] update documentation --- builder/vsphere/common/step_shutdown.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index fab80dcd2..468caf675 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -16,16 +16,18 @@ import ( ) type ShutdownConfig struct { - // Specify a VM guest shutdown command. VMware guest tools are used by - // default. + // Specify a VM guest shutdown command. This command will be executed using + // the `communicator`. Otherwise the VMware guest tools are used to gracefully + // shutdown the VM guest. Command string `mapstructure:"shutdown_command"` // Amount of time to wait for graceful VM shutdown. // Defaults to 5m or five minutes. + // This will likely need to be modified if the `communicator` is 'none'. Timeout time.Duration `mapstructure:"shutdown_timeout"` // Packer normally halts the virtual machine after all provisioners have // run when no `shutdown_command` is defined. If this is set to `true`, Packer // *will not* halt the virtual machine but will assume that you will send the stop - // signal yourself through the preseed.cfg or your final provisioner. + // signal yourself through a script or the final provisioner. // Packer will wait for a default of five minutes until the virtual machine is shutdown. // The timeout can be changed using `shutdown_timeout` option. DisableShutdown bool `mapstructure:"disable_shutdown"` From 09edada93d5979deb761ce059e30946f3d86c81d Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 13 Oct 2020 15:12:28 -0400 Subject: [PATCH 06/12] moved the warning into Prepare() and updated the invocation --- builder/vsphere/common/step_shutdown.go | 15 +++++++-------- builder/vsphere/iso/config.go | 6 +++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index 468caf675..4eeb38e9c 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -11,6 +11,7 @@ import ( "time" "github.com/hashicorp/packer/builder/vsphere/driver" + "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -33,14 +34,17 @@ type ShutdownConfig struct { DisableShutdown bool `mapstructure:"disable_shutdown"` } -func (c *ShutdownConfig) Prepare() []error { - var errs []error +func (c *ShutdownConfig) Prepare(comm communicator.Config) (warnings []string, errs []error) { if c.Timeout == 0 { c.Timeout = 5 * time.Minute } - return errs + if comm.Type == "none" && c.Command != "" { + warnings = append(warnings, "The parameter `shutdown_command` is ignored as it requires a `communicator`.") + } + + return } type StepShutdown struct { @@ -59,11 +63,6 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis if state.Get("communicator") == nil { - ui.Say("The `communicator` is 'none', automatic shutdown disabled.") - if s.Config.Command != "" { - ui.Message("The parameter `shutdown_command` is ignored as it requires a `communicator`.") - } - msg := fmt.Sprintf("Please shutdown virtual machine within %s.", s.Config.Timeout) ui.Message(msg) diff --git a/builder/vsphere/iso/config.go b/builder/vsphere/iso/config.go index 3d9601524..791f80cc9 100644 --- a/builder/vsphere/iso/config.go +++ b/builder/vsphere/iso/config.go @@ -86,7 +86,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.WaitIpConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) - errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) + + shutdownWarnings, shutdownErrs := c.ShutdownConfig.Prepare(c.Comm) + warnings = append(warnings, shutdownWarnings...) + errs = packer.MultiErrorAppend(errs, shutdownErrs...) + if c.Export != nil { errs = packer.MultiErrorAppend(errs, c.Export.Prepare(&c.ctx, &c.LocationConfig, &c.PackerConfig)...) } From f86f7c44640cb1dec05ad39d9165fa6eaa807945 Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 13 Oct 2020 15:26:32 -0400 Subject: [PATCH 07/12] Another instance using `Prepare()` needed an update --- builder/vsphere/clone/config.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/clone/config.go b/builder/vsphere/clone/config.go index c841b5a56..3cfd16339 100644 --- a/builder/vsphere/clone/config.go +++ b/builder/vsphere/clone/config.go @@ -65,7 +65,9 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { return nil, err } + warnings := make([]string, 0) errs := new(packer.MultiError) + errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.CloneConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.LocationConfig.Prepare()...) @@ -77,7 +79,11 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.WaitIpConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) - errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) + + shutdownWarnings, shutdownErrs := c.ShutdownConfig.Prepare(c.Comm) + warnings = append(warnings, shutdownWarnings...) + errs = packer.MultiErrorAppend(errs, shutdownErrs...) + if c.Export != nil { errs = packer.MultiErrorAppend(errs, c.Export.Prepare(&c.ctx, &c.LocationConfig, &c.PackerConfig)...) } From c86a48fbc955d63b0016ecc201e89b300d78f361 Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 13 Oct 2020 15:53:27 -0400 Subject: [PATCH 08/12] syntax change --- builder/vsphere/common/step_shutdown.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index 4eeb38e9c..afb697cfa 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -61,7 +61,8 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis return multistep.ActionContinue } - if state.Get("communicator") == nil { + comm, _ := state.Get("communicator").(packer.Communicator) + if comm == nil { msg := fmt.Sprintf("Please shutdown virtual machine within %s.", s.Config.Timeout) ui.Message(msg) From c8ad66f419388884e23f0a5781e3e7fdd0f6467b Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 13 Oct 2020 16:29:33 -0400 Subject: [PATCH 09/12] update to docs as suggested --- builder/vsphere/common/step_shutdown.go | 2 +- .../vsphere/common/ShutdownConfig-not-required.mdx | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index afb697cfa..e14b67957 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -28,7 +28,7 @@ type ShutdownConfig struct { // Packer normally halts the virtual machine after all provisioners have // run when no `shutdown_command` is defined. If this is set to `true`, Packer // *will not* halt the virtual machine but will assume that you will send the stop - // signal yourself through a script or the final provisioner. + // signal yourself through a preseed.cfg, a script or the final provisioner. // Packer will wait for a default of five minutes until the virtual machine is shutdown. // The timeout can be changed using `shutdown_timeout` option. DisableShutdown bool `mapstructure:"disable_shutdown"` diff --git a/website/pages/partials/builder/vsphere/common/ShutdownConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/ShutdownConfig-not-required.mdx index 85915de71..1b950c176 100644 --- a/website/pages/partials/builder/vsphere/common/ShutdownConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/ShutdownConfig-not-required.mdx @@ -1,14 +1,16 @@ -- `shutdown_command` (string) - Specify a VM guest shutdown command. VMware guest tools are used by - default. +- `shutdown_command` (string) - Specify a VM guest shutdown command. This command will be executed using + the `communicator`. Otherwise the VMware guest tools are used to gracefully + shutdown the VM guest. - `shutdown_timeout` (duration string | ex: "1h5m2s") - Amount of time to wait for graceful VM shutdown. Defaults to 5m or five minutes. + This will likely need to be modified if the `communicator` is 'none'. - `disable_shutdown` (bool) - Packer normally halts the virtual machine after all provisioners have run when no `shutdown_command` is defined. If this is set to `true`, Packer *will not* halt the virtual machine but will assume that you will send the stop - signal yourself through the preseed.cfg or your final provisioner. + signal yourself through a preseed.cfg, a script or the final provisioner. Packer will wait for a default of five minutes until the virtual machine is shutdown. The timeout can be changed using `shutdown_timeout` option. From 185f3d9d48678b91dba8772e9348f7660539b1bf Mon Sep 17 00:00:00 2001 From: James Griffith Date: Tue, 13 Oct 2020 16:47:00 -0400 Subject: [PATCH 10/12] vsphere clone config not yet using warnings, leaving code commented out --- builder/vsphere/clone/config.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/builder/vsphere/clone/config.go b/builder/vsphere/clone/config.go index 3cfd16339..f7b2fd588 100644 --- a/builder/vsphere/clone/config.go +++ b/builder/vsphere/clone/config.go @@ -65,7 +65,7 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { return nil, err } - warnings := make([]string, 0) + // warnings := make([]string, 0) errs := new(packer.MultiError) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) @@ -80,8 +80,9 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, c.WaitIpConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) - shutdownWarnings, shutdownErrs := c.ShutdownConfig.Prepare(c.Comm) - warnings = append(warnings, shutdownWarnings...) + _, shutdownErrs := c.ShutdownConfig.Prepare(c.Comm) + // shutdownWarnings, shutdownErrs := c.ShutdownConfig.Prepare(c.Comm) + // warnings = append(warnings, shutdownWarnings...) errs = packer.MultiErrorAppend(errs, shutdownErrs...) if c.Export != nil { From 15467aa8680d098afa2f46d652f62c66d71c21c1 Mon Sep 17 00:00:00 2001 From: netapp-jgriffit <47190892+netapp-jgriffit@users.noreply.github.com> Date: Fri, 16 Oct 2020 10:34:45 -0400 Subject: [PATCH 11/12] Update builder/vsphere/common/step_shutdown.go Co-authored-by: Sylvia Moss --- builder/vsphere/common/step_shutdown.go | 1 - 1 file changed, 1 deletion(-) diff --git a/builder/vsphere/common/step_shutdown.go b/builder/vsphere/common/step_shutdown.go index e14b67957..8c71cc2d3 100644 --- a/builder/vsphere/common/step_shutdown.go +++ b/builder/vsphere/common/step_shutdown.go @@ -71,7 +71,6 @@ func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multis ui.Say("Automatic shutdown disabled. Please shutdown virtual machine.") } else if s.Config.Command != "" { // Communicator is not needed unless shutdown_command is populated - comm := state.Get("communicator").(packer.Communicator) ui.Say("Executing shutdown command...") log.Printf("Shutdown command: %s", s.Config.Command) From 56c7a9cda5c8bec326c17849dd94c6402f7b1ae8 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 23 Oct 2020 11:47:20 -0700 Subject: [PATCH 12/12] rebase --- builder/vsphere/iso/builder.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index cd31bfe59..45fe1ca0b 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -82,7 +82,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Directories: b.config.FloppyDirectories, Label: b.config.FloppyLabel, }, - &StepAddFloppy{ + &common.StepAddFloppy{ Config: &b.config.FloppyConfig, Datastore: b.config.Datastore, Host: b.config.Host, @@ -127,11 +127,11 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &common.StepShutdown{ Config: &b.config.ShutdownConfig, }, - &StepRemoveFloppy{ + &common.StepRemoveFloppy{ Datastore: b.config.Datastore, Host: b.config.Host, }, - &StepRemoveCDRom{ + &common.StepRemoveCDRom{ Config: &b.config.RemoveCDRomConfig, }, &common.StepCreateSnapshot{