2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-04-06 13:21:22 -04:00
|
|
|
"fmt"
|
2016-12-11 14:13:37 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2016-12-11 17:37:41 -05:00
|
|
|
// StepRun is a step that starts the virtual machine.
|
2014-04-06 13:21:22 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2014-04-06 13:21:22 -04:00
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type StepRun struct {
|
|
|
|
vmName string
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Run starts the VM.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-04-06 13:21:22 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-04-06 13:21:22 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
ui.Say("Starting the virtual machine...")
|
|
|
|
command := []string{"start", vmName}
|
|
|
|
if err := driver.Prlctl(command...); err != nil {
|
2016-12-11 16:12:55 -05:00
|
|
|
err = fmt.Errorf("Error starting VM: %s", err)
|
2014-04-06 13:21:22 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmName = vmName
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", vmName)
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Cleanup stops the VM.
|
2014-04-06 13:21:22 -04:00
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.vmName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-04-06 13:21:22 -04:00
|
|
|
|
|
|
|
if running, _ := driver.IsRunning(s.vmName); running {
|
2018-04-19 19:11:29 -04:00
|
|
|
if err := driver.Stop(s.vmName); err != nil {
|
2014-04-06 13:21:22 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|