2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-13 03:21:07 -05:00
|
|
|
"log"
|
|
|
|
|
2014-12-17 12:45:25 -06:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepDetachVdi struct {
|
2014-12-17 12:45:25 -06:00
|
|
|
VdiUuidKey string
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepDetachVdi) Run(state multistep.StateBag) multistep.StepAction {
|
2014-12-17 12:45:25 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-13 03:21:07 -05:00
|
|
|
c := state.Get("client").(*Connection)
|
2014-12-17 12:45:25 -06:00
|
|
|
|
|
|
|
var vdiUuid string
|
|
|
|
if vdiUuidRaw, ok := state.GetOk(self.VdiUuidKey); ok {
|
|
|
|
vdiUuid = vdiUuidRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Printf("Skipping detach of '%s'", self.VdiUuidKey)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
vdi, err := c.client.VDI.GetByUUID(c.session, vdiUuid)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get VDI from UUID '%s': %s", vdiUuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid := state.Get("instance_uuid").(string)
|
2020-09-13 03:21:07 -05:00
|
|
|
instance, err := c.client.VM.GetByUUID(c.session, uuid)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2020-09-13 03:21:07 -05:00
|
|
|
err = DisconnectVdi(c, instance, vdi)
|
2014-12-17 12:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Unable to detach VDI '%s': %s", vdiUuid, err.Error()))
|
2014-12-31 09:21:54 -06:00
|
|
|
//return multistep.ActionHalt
|
|
|
|
return multistep.ActionContinue
|
2014-12-17 12:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Detached VDI '%s'", vdiUuid)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepDetachVdi) Cleanup(state multistep.StateBag) {}
|