From dc848ea5d75ed896ed08599560d691a5a3626c59 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 7 Feb 2019 12:39:56 -0800 Subject: [PATCH] just make vagrantfile instead of calling init --- builder/vagrant/builder.go | 15 +-- ..._vagrant.go => step_create_vagrantfile.go} | 55 ++------- .../vagrant/step_create_vagrantfile_test.go | 60 ++++++++++ .../vagrant/step_initialize_vagrant_test.go | 110 ------------------ website/source/docs/builders/vagrant.html.md | 14 +-- 5 files changed, 80 insertions(+), 174 deletions(-) rename builder/vagrant/{step_initialize_vagrant.go => step_create_vagrantfile.go} (60%) create mode 100644 builder/vagrant/step_create_vagrantfile_test.go delete mode 100644 builder/vagrant/step_initialize_vagrant_test.go diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index b44f4a56d..d14f6df8e 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -56,7 +56,6 @@ type Config struct { // Options for the "vagrant init" command BoxVersion string `mapstructure:"box_version"` - Minimal bool `mapstructure:"init_minimal"` Template string `mapstructure:"template"` SyncedFolder string `mapstructure:"synced_folder"` @@ -200,14 +199,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Force: b.config.PackerForce, Path: b.config.OutputDir, }, - &StepInitializeVagrant{ - BoxVersion: b.config.BoxVersion, - Minimal: b.config.Minimal, - Template: b.config.Template, - SourceBox: b.config.SourceBox, - OutputDir: b.config.OutputDir, - BoxName: b.config.BoxName, - GlobalID: b.config.GlobalID, + &StepCreateVagrantfile{ + Template: b.config.Template, + SyncedFolder: b.config.SyncedFolder, + SourceBox: b.config.SourceBox, + OutputDir: b.config.OutputDir, + GlobalID: b.config.GlobalID, }, &StepAddBox{ BoxVersion: b.config.BoxVersion, diff --git a/builder/vagrant/step_initialize_vagrant.go b/builder/vagrant/step_create_vagrantfile.go similarity index 60% rename from builder/vagrant/step_initialize_vagrant.go rename to builder/vagrant/step_create_vagrantfile.go index 51b4710f6..375b9b423 100644 --- a/builder/vagrant/step_initialize_vagrant.go +++ b/builder/vagrant/step_create_vagrantfile.go @@ -3,6 +3,7 @@ package vagrant import ( "context" "fmt" + "log" "os" "path/filepath" "text/template" @@ -11,10 +12,7 @@ import ( "github.com/hashicorp/packer/packer" ) -type StepInitializeVagrant struct { - BoxName string - BoxVersion string - Minimal bool +type StepCreateVagrantfile struct { Template string SourceBox string OutputDir string @@ -36,8 +34,8 @@ type VagrantfileOptions struct { BoxName string } -func (s *StepInitializeVagrant) getVagrantfileTemplate() (string, error) { - tplPath := filepath.Join(s.OutputDir, "packer-vagrantfile-template.erb") +func (s *StepCreateVagrantfile) createVagrantfile() (string, error) { + tplPath := filepath.Join(s.OutputDir, "Vagrantfile") templateFile, err := os.Create(tplPath) if err != nil { retErr := fmt.Errorf("Error creating vagrantfile %s", err.Error()) @@ -74,36 +72,7 @@ func (s *StepInitializeVagrant) getVagrantfileTemplate() (string, error) { return abspath, nil } -func (s *StepInitializeVagrant) prepInitArgs() ([]string, error) { - // Prepare arguments - initArgs := []string{} - - if s.BoxName != "" { - initArgs = append(initArgs, s.BoxName) - } - - initArgs = append(initArgs, s.SourceBox) - - if s.BoxVersion != "" { - initArgs = append(initArgs, "--box-version", s.BoxVersion) - } - - if s.Minimal { - initArgs = append(initArgs, "-m") - } - - tplPath, err := s.getVagrantfileTemplate() - if err != nil { - return initArgs, err - } - - initArgs = append(initArgs, "--template", tplPath) - - return initArgs, nil -} - -func (s *StepInitializeVagrant) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(VagrantDriver) +func (s *StepCreateVagrantfile) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) // Skip the initialize step if we're trying to launch from a global ID. @@ -112,24 +81,18 @@ func (s *StepInitializeVagrant) Run(_ context.Context, state multistep.StateBag) return multistep.ActionContinue } - ui.Say("Initializing Vagrant in build directory...") - - initArgs, err := s.prepInitArgs() + ui.Say("Creating a Vagrantfile in the build directory...") + vagrantfilePath, err := s.createVagrantfile() if err != nil { state.Put("error", err) return multistep.ActionHalt } + log.Printf("Created vagrantfile at %s", vagrantfilePath) os.Chdir(s.OutputDir) - // Call vagrant using prepared arguments - err = driver.Init(initArgs) - if err != nil { - state.Put("error", err) - return multistep.ActionHalt - } return multistep.ActionContinue } -func (s *StepInitializeVagrant) Cleanup(state multistep.StateBag) { +func (s *StepCreateVagrantfile) Cleanup(state multistep.StateBag) { } diff --git a/builder/vagrant/step_create_vagrantfile_test.go b/builder/vagrant/step_create_vagrantfile_test.go new file mode 100644 index 000000000..d3bcf21b9 --- /dev/null +++ b/builder/vagrant/step_create_vagrantfile_test.go @@ -0,0 +1,60 @@ +package vagrant + +import ( + "io/ioutil" + "os" + "strings" + "testing" + + "github.com/hashicorp/packer/helper/multistep" +) + +func TestStepCreateVagrantfile_Impl(t *testing.T) { + var raw interface{} + raw = new(StepCreateVagrantfile) + if _, ok := raw.(multistep.Step); !ok { + t.Fatalf("initialize should be a step") + } +} + +func TestCreateFile(t *testing.T) { + testy := StepCreateVagrantfile{ + OutputDir: "./", + SourceBox: "bananas", + } + templatePath, err := testy.createVagrantfile() + if err != nil { + t.Fatalf(err.Error()) + } + defer os.Remove(templatePath) + contents, err := ioutil.ReadFile(templatePath) + actual := string(contents) + expected := `Vagrant.configure("2") do |config| + config.vm.box = "bananas" + config.vm.synced_folder ".", "/vagrant", disabled: true +end` + if ok := strings.Compare(actual, expected); ok != 0 { + t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual) + } +} + +func TestCreateFile_customSync(t *testing.T) { + testy := StepCreateVagrantfile{ + OutputDir: "./", + SyncedFolder: "myfolder/foldertimes", + } + templatePath, err := testy.createVagrantfile() + if err != nil { + t.Fatalf(err.Error()) + } + defer os.Remove(templatePath) + contents, err := ioutil.ReadFile(templatePath) + actual := string(contents) + expected := `Vagrant.configure("2") do |config| + config.vm.box = "" + config.vm.synced_folder "myfolder/foldertimes", "/vagrant" +end` + if ok := strings.Compare(actual, expected); ok != 0 { + t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual) + } +} diff --git a/builder/vagrant/step_initialize_vagrant_test.go b/builder/vagrant/step_initialize_vagrant_test.go deleted file mode 100644 index 4df7284f4..000000000 --- a/builder/vagrant/step_initialize_vagrant_test.go +++ /dev/null @@ -1,110 +0,0 @@ -package vagrant - -import ( - "io/ioutil" - "os" - "strings" - "testing" - - "github.com/hashicorp/packer/helper/multistep" -) - -func TestStepInitialize_Impl(t *testing.T) { - var raw interface{} - raw = new(StepInitializeVagrant) - if _, ok := raw.(multistep.Step); !ok { - t.Fatalf("initialize should be a step") - } -} - -func TestCreateFile(t *testing.T) { - testy := StepInitializeVagrant{ - OutputDir: "./", - SourceBox: "bananas", - } - templatePath, err := testy.getVagrantfileTemplate() - if err != nil { - t.Fatalf(err.Error()) - } - contents, err := ioutil.ReadFile(templatePath) - actual := string(contents) - expected := `Vagrant.configure("2") do |config| - config.vm.box = "bananas" - config.vm.synced_folder ".", "/vagrant", disabled: true -end` - if ok := strings.Compare(actual, expected); ok != 0 { - t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual) - } - os.Remove(templatePath) -} - -func TestCreateFile_customSync(t *testing.T) { - testy := StepInitializeVagrant{ - OutputDir: "./", - SyncedFolder: "myfolder/foldertimes", - } - templatePath, err := testy.getVagrantfileTemplate() - defer os.Remove(templatePath) - if err != nil { - t.Fatalf(err.Error()) - } - contents, err := ioutil.ReadFile(templatePath) - actual := string(contents) - expected := `Vagrant.configure("2") do |config| - config.vm.box = "" - config.vm.synced_folder "myfolder/foldertimes", "/vagrant" -end` - if ok := strings.Compare(actual, expected); ok != 0 { - t.Fatalf("EXPECTED: \n%s\n\n RECEIVED: \n%s\n\n", expected, actual) - } -} - -func TestPrepInitArgs(t *testing.T) { - type testArgs struct { - Step StepInitializeVagrant - Expected []string - } - initTests := []testArgs{ - { - Step: StepInitializeVagrant{ - SourceBox: "my_source_box.box", - }, - Expected: []string{"my_source_box.box", "--template"}, - }, - { - Step: StepInitializeVagrant{ - SourceBox: "my_source_box", - BoxName: "My Box", - }, - Expected: []string{"My Box", "my_source_box", "--template"}, - }, - { - Step: StepInitializeVagrant{ - SourceBox: "my_source_box", - BoxName: "My Box", - BoxVersion: "42", - }, - Expected: []string{"My Box", "my_source_box", "--box-version", "42", "--template"}, - }, - { - Step: StepInitializeVagrant{ - SourceBox: "my_source_box", - BoxName: "My Box", - Minimal: true, - }, - Expected: []string{"My Box", "my_source_box", "-m", "--template"}, - }, - } - for _, initTest := range initTests { - initArgs, err := initTest.Step.prepInitArgs() - defer os.Remove(initArgs[len(initArgs)-1]) - if err != nil { - t.Fatalf(err.Error()) - } - for i, val := range initTest.Expected { - if strings.Compare(initArgs[i], val) != 0 { - t.Fatalf("expected %#v but received %#v", initTest.Expected, initArgs[:len(initArgs)-1]) - } - } - } -} diff --git a/website/source/docs/builders/vagrant.html.md b/website/source/docs/builders/vagrant.html.md index ae661629f..fc93d00c9 100644 --- a/website/source/docs/builders/vagrant.html.md +++ b/website/source/docs/builders/vagrant.html.md @@ -58,11 +58,11 @@ Optional: not recommended since OVA files can be very large and corruption does happen from time to time. -- `vagrantfile_template` (string) - a path to an ERB template to use for the - vagrantfile when calling `vagrant init`. See the blog post - [here](https://www.hashicorp.com/blog/hashicorp-vagrant-2-0-2#customized-vagrantfile-templates) - for some more details on how this works. Available variables are `box_name`, - `box_url`, and `box_version`. +- `vagrantfile_template` (string) - a path to a golang template for a + vagrantfile. Our default template can be found + [here](https://github.com/hashicorp/packer/tree/master/builder/vagrant/step_initialize_vagrant.go#L23-L30). So far the only template variables available to you are {{ .BoxName }} and + {{ .SyncedFolder }}, which correspond to the Packer options `box_name` and + `synced_folder` - `skip_add` (string) - Don't call "vagrant add" to add the box to your local environment; this is necesasry if you want to launch a box that is already @@ -73,10 +73,6 @@ Optional: - `box_version` (string) - What box version to use when initializing Vagrant. -- `init_minimal` (bool) - If true, will add the --minimal flag to the Vagrant - init command, creating a minimal vagrantfile instead of one filled with helpful - comments. - - `add_cacert` (string) - Equivalent to setting the [`--cacert`](https://www.vagrantup.com/docs/cli/box.html#cacert-certfile) option in `vagrant add`; defaults to unset.