Merge pull request #18 from ddelnano/ensure-vm-is-cleaned-up-on-wait-for-ip-step
Ensure vm is cleaned up when build is interrupted on the wait for ip step
This commit is contained in:
commit
e4b9d11d85
@ -3,13 +3,14 @@ package common
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
||||||
"github.com/hashicorp/packer-plugin-sdk/packer"
|
"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 {
|
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
|
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()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type StepWaitForIP struct {
|
type StepWaitForIP struct {
|
||||||
|
VmCleanup
|
||||||
Chan <-chan string
|
Chan <-chan string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
@ -84,8 +85,6 @@ func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) mu
|
|||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StepWaitForIP) Cleanup(state multistep.StateBag) {}
|
|
||||||
|
|
||||||
func InstanceSSHIP(state multistep.StateBag) (string, error) {
|
func InstanceSSHIP(state multistep.StateBag) (string, error) {
|
||||||
ip := state.Get("instance_ssh_address").(string)
|
ip := state.Get("instance_ssh_address").(string)
|
||||||
return ip, nil
|
return ip, nil
|
||||||
|
31
builder/xenserver/common/vm_cleanup.go
Normal file
31
builder/xenserver/common/vm_cleanup.go
Normal file
@ -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()))
|
||||||
|
}
|
||||||
|
}
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/xenserver/packer-builder-xenserver
|
module github.com/xenserver/packer-builder-xenserver
|
||||||
|
|
||||||
go 1.14
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/amfranz/go-xmlrpc-client v0.0.0-20190612172737-76858463955d
|
github.com/amfranz/go-xmlrpc-client v0.0.0-20190612172737-76858463955d
|
||||||
|
Loading…
Reference in New Issue
Block a user