2013-12-22 18:19:19 -05:00
|
|
|
package ovf
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-12-22 18:19:19 -05:00
|
|
|
"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 {
|
2019-12-20 05:50:18 -05:00
|
|
|
Name string
|
|
|
|
ImportFlags []string
|
|
|
|
KeepRegistered bool
|
2013-12-22 18:19:19 -05:00
|
|
|
|
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepImport) Run(ctx 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
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-12-20 05:50:18 -05:00
|
|
|
if (s.KeepRegistered) && (!cancelled && !halted) {
|
2017-09-13 09:37:26 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|