2019-01-11 17:06:15 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
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 StepAddBox struct {
|
|
|
|
BoxVersion string
|
|
|
|
CACert string
|
|
|
|
CAPath string
|
|
|
|
DownloadCert string
|
|
|
|
Clean bool
|
|
|
|
Force bool
|
|
|
|
Insecure bool
|
|
|
|
Provider string
|
|
|
|
SourceBox string
|
|
|
|
BoxName string
|
2019-01-25 15:32:44 -05:00
|
|
|
GlobalID string
|
2019-02-14 17:46:14 -05:00
|
|
|
SkipAdd bool
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
func (s *StepAddBox) generateAddArgs() []string {
|
2019-01-11 17:06:15 -05:00
|
|
|
addArgs := []string{}
|
|
|
|
|
|
|
|
if strings.HasSuffix(s.SourceBox, ".box") {
|
|
|
|
addArgs = append(addArgs, s.BoxName)
|
|
|
|
}
|
|
|
|
|
|
|
|
addArgs = append(addArgs, s.SourceBox)
|
|
|
|
|
|
|
|
if s.BoxVersion != "" {
|
|
|
|
addArgs = append(addArgs, "--box-version", s.BoxVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.CACert != "" {
|
|
|
|
addArgs = append(addArgs, "--cacert", s.CACert)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.CAPath != "" {
|
|
|
|
addArgs = append(addArgs, "--capath", s.CAPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.DownloadCert != "" {
|
|
|
|
addArgs = append(addArgs, "--cert", s.DownloadCert)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Clean {
|
|
|
|
addArgs = append(addArgs, "--clean")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Force {
|
|
|
|
addArgs = append(addArgs, "--force")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Insecure {
|
|
|
|
addArgs = append(addArgs, "--insecure")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Provider != "" {
|
|
|
|
addArgs = append(addArgs, "--provider", s.Provider)
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
return addArgs
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepAddBox) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-01-25 15:32:44 -05:00
|
|
|
driver := state.Get("driver").(VagrantDriver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-25 15:32:44 -05:00
|
|
|
|
2019-02-14 17:46:14 -05:00
|
|
|
if s.SkipAdd {
|
|
|
|
ui.Say("skip_add was set so we assume the box is already in Vagrant...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
if s.GlobalID != "" {
|
|
|
|
ui.Say("Using a global-id; skipping Vagrant add command...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:56:53 -05:00
|
|
|
ui.Say("Adding box using vagrant box add ...")
|
|
|
|
ui.Message("(this can take some time if we need to download the box)")
|
2019-01-25 15:32:44 -05:00
|
|
|
addArgs := s.generateAddArgs()
|
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
log.Printf("[vagrant] Calling box add with following args %s", strings.Join(addArgs, " "))
|
|
|
|
// Call vagrant using prepared arguments
|
|
|
|
err := driver.Add(addArgs)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepAddBox) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|