From bb43b5cac4002547ac787bea61a534eb48c725a3 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 14 Jul 2020 11:34:40 -0700 Subject: [PATCH 1/2] improve validation and error handling around synced_folder --- builder/vagrant/builder.go | 5 +++++ builder/vagrant/driver_2_2.go | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index c5110ede6..5692383bc 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -242,6 +242,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { } } + if _, err := os.Stat(b.config.SyncedFolder); err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("synced_folder \"%s\" does not exist on the Packer host.", b.config.SyncedFolder)) + } + if errs != nil && len(errs.Errors) > 0 { return nil, warnings, errs } diff --git a/builder/vagrant/driver_2_2.go b/builder/vagrant/driver_2_2.go index 9e9679ff1..c1df9ea98 100644 --- a/builder/vagrant/driver_2_2.go +++ b/builder/vagrant/driver_2_2.go @@ -151,13 +151,21 @@ func (d *Vagrant_2_2_Driver) SSHConfig(id string) (*VagrantSSHConfig, error) { if id != "" { args = append(args, id) } - stdout, _, err := d.vagrantCmd(args...) sshConf := &VagrantSSHConfig{} + stdout, stderr, err := d.vagrantCmd(args...) + if stderr != "" { + err := fmt.Errorf("ssh-config command returned error: %s", stderr) + return sshConf, err + } lines := strings.Split(stdout, "\n") sshConf.Hostname = parseSSHConfig(lines, "HostName ") sshConf.User = parseSSHConfig(lines, "User ") sshConf.Port = parseSSHConfig(lines, "Port ") + if sshConf.Port == "" { + err := fmt.Errorf("error: SSH Port was not properly retrieved from SSHConfig.") + return sshConf, err + } sshConf.UserKnownHostsFile = parseSSHConfig(lines, "UserKnownHostsFile ") sshConf.IdentityFile = parseSSHConfig(lines, "IdentityFile ") sshConf.LogLevel = parseSSHConfig(lines, "LogLevel ") From a414e7cdb958ff1a3b5f2e542bad19664fe02b6a Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 14 Jul 2020 15:47:55 -0700 Subject: [PATCH 2/2] convert relative to absolute path in prepare --- builder/vagrant/builder.go | 16 ++++++++++++---- .../builder/vagrant/Config-not-required.mdx | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index 5692383bc..8bd38c379 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -106,7 +106,8 @@ type Config struct { // `{{ .BoxName }}`, `{{ .SyncedFolder }}`, and `{{.InsertKey}}`, which // correspond to the Packer options box_name, synced_folder, and insert_key. Template string `mapstructure:"template" required:"false"` - + // Path to the folder to be synced to the guest. The path can be absolute + // or relative to the directory Packer is being run from. SyncedFolder string `mapstructure:"synced_folder"` // Don't call "vagrant add" to add the box to your local environment; this // is necessary if you want to launch a box that is already added to your @@ -242,9 +243,16 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { } } - if _, err := os.Stat(b.config.SyncedFolder); err != nil { - errs = packer.MultiErrorAppend(errs, - fmt.Errorf("synced_folder \"%s\" does not exist on the Packer host.", b.config.SyncedFolder)) + if b.config.SyncedFolder != "" { + b.config.SyncedFolder, err = filepath.Abs(b.config.SyncedFolder) + if err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("unable to determine absolute path for synced_folder: %s", b.config.SyncedFolder)) + } + if _, err := os.Stat(b.config.SyncedFolder); err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("synced_folder \"%s\" does not exist on the Packer host.", b.config.SyncedFolder)) + } } if errs != nil && len(errs.Errors) > 0 { diff --git a/website/pages/partials/builder/vagrant/Config-not-required.mdx b/website/pages/partials/builder/vagrant/Config-not-required.mdx index 146655af7..4889f1d3e 100644 --- a/website/pages/partials/builder/vagrant/Config-not-required.mdx +++ b/website/pages/partials/builder/vagrant/Config-not-required.mdx @@ -51,7 +51,8 @@ `{{ .BoxName }}`, `{{ .SyncedFolder }}`, and `{{.InsertKey}}`, which correspond to the Packer options box_name, synced_folder, and insert_key. -- `synced_folder` (string) - Synced Folder +- `synced_folder` (string) - Path to the folder to be synced to the guest. The path can be absolute + or relative to the directory Packer is being run from. - `skip_add` (bool) - Don't call "vagrant add" to add the box to your local environment; this is necessary if you want to launch a box that is already added to your