2019-01-11 17:06:15 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-01-11 17:06:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09-10 13:46:16 -04:00
|
|
|
func (s *StepUp) generateArgs() []string {
|
|
|
|
box := "source"
|
2019-01-25 15:32:44 -05:00
|
|
|
if s.GlobalID != "" {
|
2019-09-10 13:46:16 -04:00
|
|
|
box = s.GlobalID
|
2019-01-25 15:32:44 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 13:46:16 -04:00
|
|
|
// start only the source box
|
|
|
|
args := []string{box}
|
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
if s.Provider != "" {
|
|
|
|
args = append(args, fmt.Sprintf("--provider=%s", s.Provider))
|
|
|
|
}
|
2019-09-10 13:46:16 -04:00
|
|
|
return args
|
|
|
|
}
|
2019-01-11 17:06:15 -05:00
|
|
|
|
2019-09-10 13:46:16 -04:00
|
|
|
func (s *StepUp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(VagrantDriver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-09-10 13:46:16 -04:00
|
|
|
|
2019-10-24 13:06:46 -04:00
|
|
|
ui.Say("Calling Vagrant Up (this can take some time)...")
|
2019-09-10 13:46:16 -04:00
|
|
|
|
2019-12-13 14:57:01 -05:00
|
|
|
args := s.generateArgs()
|
|
|
|
// 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", args[0])
|
|
|
|
_, _, err := driver.Up(args)
|
2019-01-11 17:06:15 -05:00
|
|
|
|
|
|
|
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)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-11 17:06:15 -05:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("%sing Vagrant box...", s.TeardownMethod))
|
|
|
|
|
2019-09-10 13:46:16 -04:00
|
|
|
box := "source"
|
|
|
|
if s.GlobalID != "" {
|
|
|
|
box = s.GlobalID
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
var err error
|
|
|
|
if s.TeardownMethod == "halt" {
|
2019-09-10 13:46:16 -04:00
|
|
|
err = driver.Halt(box)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else if s.TeardownMethod == "suspend" {
|
2019-09-10 13:46:16 -04:00
|
|
|
err = driver.Suspend(box)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else if s.TeardownMethod == "destroy" {
|
2019-09-10 13:46:16 -04:00
|
|
|
err = driver.Destroy(box)
|
2019-01-11 17:06:15 -05:00
|
|
|
} else {
|
|
|
|
// Should never get here because of template validation
|
2019-09-10 17:31:00 -04:00
|
|
|
state.Put("error", fmt.Errorf("Invalid teardown method selected; must be either halt, suspend, or destroy."))
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error halting Vagrant machine; please try to do this manually"))
|
|
|
|
}
|
|
|
|
}
|