2019-01-11 17:06:15 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-02-07 15:39:56 -05:00
|
|
|
"log"
|
2019-01-11 17:06:15 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2019-02-07 15:39:56 -05:00
|
|
|
type StepCreateVagrantfile struct {
|
2019-01-11 17:06:15 -05:00
|
|
|
Template string
|
|
|
|
OutputDir string
|
|
|
|
SyncedFolder string
|
2019-01-25 15:32:44 -05:00
|
|
|
GlobalID string
|
2019-07-09 18:26:04 -04:00
|
|
|
BoxName string
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var DEFAULT_TEMPLATE = `Vagrant.configure("2") do |config|
|
|
|
|
config.vm.box = "{{.BoxName}}"
|
|
|
|
{{ if ne .SyncedFolder "" -}}
|
|
|
|
config.vm.synced_folder "{{.SyncedFolder}}", "/vagrant"
|
|
|
|
{{- else -}}
|
|
|
|
config.vm.synced_folder ".", "/vagrant", disabled: true
|
|
|
|
{{- end}}
|
|
|
|
end`
|
|
|
|
|
|
|
|
type VagrantfileOptions struct {
|
|
|
|
SyncedFolder string
|
|
|
|
BoxName string
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:39:56 -05:00
|
|
|
func (s *StepCreateVagrantfile) createVagrantfile() (string, error) {
|
|
|
|
tplPath := filepath.Join(s.OutputDir, "Vagrantfile")
|
2019-01-11 17:06:15 -05:00
|
|
|
templateFile, err := os.Create(tplPath)
|
|
|
|
if err != nil {
|
|
|
|
retErr := fmt.Errorf("Error creating vagrantfile %s", err.Error())
|
|
|
|
return "", retErr
|
|
|
|
}
|
|
|
|
|
|
|
|
var tpl *template.Template
|
|
|
|
if s.Template == "" {
|
|
|
|
// Generate vagrantfile template based on our default
|
|
|
|
tpl = template.Must(template.New("VagrantTpl").Parse(DEFAULT_TEMPLATE))
|
|
|
|
} else {
|
|
|
|
// Read in the template from provided file.
|
|
|
|
tpl, err = template.ParseFiles(s.Template)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &VagrantfileOptions{
|
|
|
|
SyncedFolder: s.SyncedFolder,
|
2019-07-09 18:26:04 -04:00
|
|
|
BoxName: s.BoxName,
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tpl.Execute(templateFile, opts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
abspath, err := filepath.Abs(tplPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return abspath, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateVagrantfile) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-01-25 15:32:44 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
// Skip the initialize step if we're trying to launch from a global ID.
|
|
|
|
if s.GlobalID != "" {
|
|
|
|
ui.Say("Using a global-id; skipping Vagrant init in this directory...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:39:56 -05:00
|
|
|
ui.Say("Creating a Vagrantfile in the build directory...")
|
|
|
|
vagrantfilePath, err := s.createVagrantfile()
|
2019-01-25 15:32:44 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-02-07 15:39:56 -05:00
|
|
|
log.Printf("Created vagrantfile at %s", vagrantfilePath)
|
2019-01-25 15:32:44 -05:00
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-02-07 15:39:56 -05:00
|
|
|
func (s *StepCreateVagrantfile) Cleanup(state multistep.StateBag) {
|
2019-01-11 17:06:15 -05:00
|
|
|
}
|