vagrant builder: define source box and output box in Vagrantfile

This commit is contained in:
Miles Crabill 2019-08-07 08:51:14 -07:00
parent c0e37e6045
commit ff157c8a6d
No known key found for this signature in database
GPG Key ID: 931A9CF57AEAC02D
4 changed files with 28 additions and 4 deletions

View File

@ -199,6 +199,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepCreateVagrantfile{ &StepCreateVagrantfile{
Template: b.config.Template, Template: b.config.Template,
SyncedFolder: b.config.SyncedFolder, SyncedFolder: b.config.SyncedFolder,
SourceBox: b.config.SourceBox,
BoxName: b.config.BoxName, BoxName: b.config.BoxName,
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,
GlobalID: b.config.GlobalID, GlobalID: b.config.GlobalID,

View File

@ -17,11 +17,18 @@ type StepCreateVagrantfile struct {
OutputDir string OutputDir string
SyncedFolder string SyncedFolder string
GlobalID string GlobalID string
SourceBox string
BoxName string BoxName string
} }
var DEFAULT_TEMPLATE = `Vagrant.configure("2") do |config| var DEFAULT_TEMPLATE = `Vagrant.configure("2") do |config|
config.vm.box = "{{.BoxName}}" config.vm.define "source", autostart: false do |source|
source.vm.box = "{{.SourceBox}}"
end
config.vm.define "output" do |output|
output.vm.box = "{{.BoxName}}"
output.vm.box_url = "file://package.box"
end
{{ if ne .SyncedFolder "" -}} {{ if ne .SyncedFolder "" -}}
config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant" config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant"
{{- else -}} {{- else -}}
@ -31,6 +38,7 @@ end`
type VagrantfileOptions struct { type VagrantfileOptions struct {
SyncedFolder string SyncedFolder string
SourceBox string
BoxName string BoxName string
} }
@ -57,6 +65,7 @@ func (s *StepCreateVagrantfile) createVagrantfile() (string, error) {
opts := &VagrantfileOptions{ opts := &VagrantfileOptions{
SyncedFolder: s.SyncedFolder, SyncedFolder: s.SyncedFolder,
BoxName: s.BoxName, BoxName: s.BoxName,
SourceBox: s.SourceBox,
} }
err = tpl.Execute(templateFile, opts) err = tpl.Execute(templateFile, opts)

View File

@ -20,6 +20,7 @@ func TestStepCreateVagrantfile_Impl(t *testing.T) {
func TestCreateFile(t *testing.T) { func TestCreateFile(t *testing.T) {
testy := StepCreateVagrantfile{ testy := StepCreateVagrantfile{
OutputDir: "./", OutputDir: "./",
SourceBox: "apples",
BoxName: "bananas", BoxName: "bananas",
} }
templatePath, err := testy.createVagrantfile() templatePath, err := testy.createVagrantfile()
@ -30,7 +31,13 @@ func TestCreateFile(t *testing.T) {
contents, err := ioutil.ReadFile(templatePath) contents, err := ioutil.ReadFile(templatePath)
actual := string(contents) actual := string(contents)
expected := `Vagrant.configure("2") do |config| expected := `Vagrant.configure("2") do |config|
config.vm.box = "bananas" config.vm.define "source", autostart: false do |source|
source.vm.box = "apples"
end
config.vm.define "output" do |output|
output.vm.box = "bananas"
output.vm.box_url = "file://package.box"
end
config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder ".", "/vagrant", disabled: true
end` end`
if ok := strings.Compare(actual, expected); ok != 0 { if ok := strings.Compare(actual, expected); ok != 0 {
@ -51,7 +58,13 @@ func TestCreateFile_customSync(t *testing.T) {
contents, err := ioutil.ReadFile(templatePath) contents, err := ioutil.ReadFile(templatePath)
actual := string(contents) actual := string(contents)
expected := `Vagrant.configure("2") do |config| expected := `Vagrant.configure("2") do |config|
config.vm.box = "" config.vm.define "source", autostart: false do |source|
source.vm.box = ""
end
config.vm.define "output" do |output|
output.vm.box = ""
output.vm.box_url = "file://package.box"
end
config.vm.synced_folder "myfolder/foldertimes", "/vagrant" config.vm.synced_folder "myfolder/foldertimes", "/vagrant"
end` end`
if ok := strings.Compare(actual, expected); ok != 0 { if ok := strings.Compare(actual, expected); ok != 0 {

View File

@ -20,7 +20,8 @@ func (s *StepUp) Run(ctx context.Context, state multistep.StateBag) multistep.St
ui.Say("Calling Vagrant Up...") ui.Say("Calling Vagrant Up...")
var args []string // start only the source box
args := []string{"source"}
if s.GlobalID != "" { if s.GlobalID != "" {
args = append(args, s.GlobalID) args = append(args, s.GlobalID)
} }