Merge pull request #9577 from hashicorp/fix_9564

improve validation and error handling around synced_folder
This commit is contained in:
Megan Marsh 2020-07-15 09:58:14 -07:00 committed by GitHub
commit 3fa4499f91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -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,6 +243,18 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}
}
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 {
return nil, warnings, errs
}

View File

@ -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 ")

View File

@ -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