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{
Template: b.config.Template,
SyncedFolder: b.config.SyncedFolder,
SourceBox: b.config.SourceBox,
BoxName: b.config.BoxName,
OutputDir: b.config.OutputDir,
GlobalID: b.config.GlobalID,

View File

@ -17,11 +17,18 @@ type StepCreateVagrantfile struct {
OutputDir string
SyncedFolder string
GlobalID string
SourceBox string
BoxName string
}
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 "" -}}
config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant"
{{- else -}}
@ -31,6 +38,7 @@ end`
type VagrantfileOptions struct {
SyncedFolder string
SourceBox string
BoxName string
}
@ -57,6 +65,7 @@ func (s *StepCreateVagrantfile) createVagrantfile() (string, error) {
opts := &VagrantfileOptions{
SyncedFolder: s.SyncedFolder,
BoxName: s.BoxName,
SourceBox: s.SourceBox,
}
err = tpl.Execute(templateFile, opts)

View File

@ -20,6 +20,7 @@ func TestStepCreateVagrantfile_Impl(t *testing.T) {
func TestCreateFile(t *testing.T) {
testy := StepCreateVagrantfile{
OutputDir: "./",
SourceBox: "apples",
BoxName: "bananas",
}
templatePath, err := testy.createVagrantfile()
@ -30,7 +31,13 @@ func TestCreateFile(t *testing.T) {
contents, err := ioutil.ReadFile(templatePath)
actual := string(contents)
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
end`
if ok := strings.Compare(actual, expected); ok != 0 {
@ -51,7 +58,13 @@ func TestCreateFile_customSync(t *testing.T) {
contents, err := ioutil.ReadFile(templatePath)
actual := string(contents)
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"
end`
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...")
var args []string
// start only the source box
args := []string{"source"}
if s.GlobalID != "" {
args = append(args, s.GlobalID)
}