2013-12-22 18:19:19 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-25 13:17:08 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-22 18:19:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step imports an OVF VM into VirtualBox.
|
|
|
|
type StepImport struct {
|
2014-09-05 13:23:37 -04:00
|
|
|
Name string
|
|
|
|
ImportFlags []string
|
2013-12-22 18:19:19 -05:00
|
|
|
|
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepImport) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-22 18:19:19 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2016-10-10 22:16:25 -04:00
|
|
|
vmPath := state.Get("vm_path").(string)
|
2013-12-22 18:19:19 -05:00
|
|
|
|
2016-10-10 22:16:25 -04:00
|
|
|
ui.Say(fmt.Sprintf("Importing VM: %s", vmPath))
|
|
|
|
if err := driver.Import(s.Name, vmPath, s.ImportFlags); err != nil {
|
2013-12-22 18:19:19 -05:00
|
|
|
err := fmt.Errorf("Error importing VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = s.Name
|
|
|
|
state.Put("vmName", s.Name)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepImport) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-09-13 09:37:26 -04:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if (config.KeepRegistered) && (!cancelled && !halted) {
|
|
|
|
ui.Say("Keeping virtual machine registered with VirtualBox host (keep_registered = true)")
|
|
|
|
return
|
|
|
|
}
|
2013-12-22 18:19:19 -05:00
|
|
|
|
2017-10-25 13:17:08 -04:00
|
|
|
ui.Say("Deregistering and deleting imported VM...")
|
2013-12-22 18:19:19 -05:00
|
|
|
if err := driver.Delete(s.vmName); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|