2019-01-11 17:06:15 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepUp struct {
|
|
|
|
TeardownMethod string
|
|
|
|
Provider string
|
2019-01-25 15:32:44 -05:00
|
|
|
GlobalID string
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepUp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-01-11 17:06:15 -05:00
|
|
|
driver := state.Get("driver").(VagrantDriver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Calling Vagrant Up...")
|
|
|
|
|
|
|
|
var args []string
|
2019-01-25 15:32:44 -05:00
|
|
|
if s.GlobalID != "" {
|
|
|
|
args = append(args, s.GlobalID)
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
if s.Provider != "" {
|
|
|
|
args = append(args, fmt.Sprintf("--provider=%s", s.Provider))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := driver.Up(args)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepUp) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(VagrantDriver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("%sing Vagrant box...", s.TeardownMethod))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if s.TeardownMethod == "halt" {
|
2019-01-25 15:32:44 -05:00
|
|
|
err = driver.Halt(s.GlobalID)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else if s.TeardownMethod == "suspend" {
|
2019-01-25 15:32:44 -05:00
|
|
|
err = driver.Suspend(s.GlobalID)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else if s.TeardownMethod == "destroy" {
|
2019-01-25 15:32:44 -05:00
|
|
|
err = driver.Destroy(s.GlobalID)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else {
|
|
|
|
// Should never get here because of template validation
|
|
|
|
state.Put("error", fmt.Errorf("Invalid teardown method selected; must be either halt, suspend, or destory."))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error halting Vagrant machine; please try to do this manually"))
|
|
|
|
}
|
|
|
|
}
|