Files
Packer-Cn/builder/parallels/common/step_run.go
T

61 lines
1.4 KiB
Go
Raw Normal View History

package common
import (
2018-01-22 15:32:33 -08:00
"context"
"fmt"
2016-12-11 21:13:37 +02:00
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
)
2016-12-12 00:37:41 +02:00
// StepRun is a step that starts the virtual machine.
//
// Uses:
// driver Driver
// ui packer.Ui
// vmName string
//
// Produces:
type StepRun struct {
vmName string
}
2016-12-16 21:51:55 +02:00
// Run starts the VM.
func (s *StepRun) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
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 23:12:55 +02:00
err = fmt.Errorf("Error starting VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.vmName = vmName
// 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)
return multistep.ActionContinue
}
2016-12-16 21:51:55 +02:00
// Cleanup stops the VM.
func (s *StepRun) Cleanup(state multistep.StateBag) {
if s.vmName == "" {
return
}
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
if running, _ := driver.IsRunning(s.vmName); running {
2018-04-19 16:11:29 -07:00
if err := driver.Stop(s.vmName); err != nil {
ui.Error(fmt.Sprintf("Error stopping VM: %s", err))
}
}
}