From 5a484e08d0db1f6ce4a31aa3ff82f6ad9d110e79 Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Tue, 23 Mar 2021 23:36:34 -0700 Subject: [PATCH 1/2] Add a shared vm cleanup struct and function and ensure that the wait for ip step uses it --- .../xenserver/common/step_start_vm_paused.go | 26 ++-------------- builder/xenserver/common/step_wait_for_ip.go | 3 +- builder/xenserver/common/vm_cleanup.go | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 builder/xenserver/common/vm_cleanup.go diff --git a/builder/xenserver/common/step_start_vm_paused.go b/builder/xenserver/common/step_start_vm_paused.go index e046651..d383878 100644 --- a/builder/xenserver/common/step_start_vm_paused.go +++ b/builder/xenserver/common/step_start_vm_paused.go @@ -3,13 +3,14 @@ package common import ( "context" "fmt" - "log" "github.com/hashicorp/packer-plugin-sdk/multistep" "github.com/hashicorp/packer-plugin-sdk/packer" ) -type StepStartVmPaused struct{} +type StepStartVmPaused struct { + VmCleanup +} func (self *StepStartVmPaused) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -54,24 +55,3 @@ func (self *StepStartVmPaused) Run(ctx context.Context, state multistep.StateBag return multistep.ActionContinue } - -func (self *StepStartVmPaused) Cleanup(state multistep.StateBag) { - config := state.Get("commonconfig").(CommonConfig) - c := state.Get("client").(*Connection) - - if config.ShouldKeepVM(state) { - return - } - - uuid := state.Get("instance_uuid").(string) - instance, err := c.client.VM.GetByUUID(c.session, uuid) - if err != nil { - log.Printf(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error())) - return - } - - err = c.client.VM.HardShutdown(c.session, instance) - if err != nil { - log.Printf(fmt.Sprintf("Unable to force shutdown VM '%s': %s", uuid, err.Error())) - } -} diff --git a/builder/xenserver/common/step_wait_for_ip.go b/builder/xenserver/common/step_wait_for_ip.go index eedea3b..5272094 100644 --- a/builder/xenserver/common/step_wait_for_ip.go +++ b/builder/xenserver/common/step_wait_for_ip.go @@ -10,6 +10,7 @@ import ( ) type StepWaitForIP struct { + VmCleanup Chan <-chan string Timeout time.Duration } @@ -84,8 +85,6 @@ func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) mu return multistep.ActionContinue } -func (self *StepWaitForIP) Cleanup(state multistep.StateBag) {} - func InstanceSSHIP(state multistep.StateBag) (string, error) { ip := state.Get("instance_ssh_address").(string) return ip, nil diff --git a/builder/xenserver/common/vm_cleanup.go b/builder/xenserver/common/vm_cleanup.go new file mode 100644 index 0000000..0c2c59c --- /dev/null +++ b/builder/xenserver/common/vm_cleanup.go @@ -0,0 +1,31 @@ +package common + +import ( + "fmt" + "log" + + "github.com/hashicorp/packer-plugin-sdk/multistep" +) + +type VmCleanup struct{} + +func (self *VmCleanup) Cleanup(state multistep.StateBag) { + config := state.Get("commonconfig").(CommonConfig) + c := state.Get("client").(*Connection) + + if config.ShouldKeepVM(state) { + return + } + + uuid := state.Get("instance_uuid").(string) + instance, err := c.client.VM.GetByUUID(c.session, uuid) + if err != nil { + log.Printf(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error())) + return + } + + err = c.client.VM.HardShutdown(c.session, instance) + if err != nil { + log.Printf(fmt.Sprintf("Unable to force shutdown VM '%s': %s", uuid, err.Error())) + } +} From fd4034839ff71a63f4e10768e9bed0eedb772eed Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Tue, 23 Mar 2021 23:36:50 -0700 Subject: [PATCH 2/2] Update go.mod to reflect currently supported go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 2561521..e5166a2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/xenserver/packer-builder-xenserver -go 1.14 +go 1.16 require ( github.com/amfranz/go-xmlrpc-client v0.0.0-20190612172737-76858463955d