2019-01-11 17:06:15 -05:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Vagrant already sets up ssh on the guests; our job is to find out what
|
|
|
|
// it did. We can do that with the ssh-config command. Example output:
|
|
|
|
|
|
|
|
// $ vagrant ssh-config
|
|
|
|
// Host default
|
|
|
|
// HostName 172.16.41.194
|
|
|
|
// User vagrant
|
|
|
|
// Port 22
|
|
|
|
// UserKnownHostsFile /dev/null
|
|
|
|
// StrictHostKeyChecking no
|
|
|
|
// PasswordAuthentication no
|
|
|
|
// IdentityFile /Users/mmarsh/Projects/vagrant-boxes/ubuntu/.vagrant/machines/default/vmware_fusion/private_key
|
|
|
|
// IdentitiesOnly yes
|
|
|
|
// LogLevel FATAL
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
type StepSSHConfig struct {
|
|
|
|
GlobalID string
|
|
|
|
}
|
2019-01-11 17:06:15 -05:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepSSHConfig) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-01-11 17:06:15 -05:00
|
|
|
driver := state.Get("driver").(VagrantDriver)
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
sshConfig, err := driver.SSHConfig(s.GlobalID)
|
2019-01-11 17:06:15 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Comm.SSHHost = sshConfig.Hostname
|
|
|
|
port, err := strconv.Atoi(sshConfig.Port)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
config.Comm.SSHPort = port
|
|
|
|
|
2019-06-21 19:27:34 -04:00
|
|
|
if config.Comm.SSHUsername != "" {
|
|
|
|
// If user has set the username within the communicator, use the
|
|
|
|
// auth provided there.
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
config.Comm.SSHPrivateKeyFile = sshConfig.IdentityFile
|
|
|
|
config.Comm.SSHUsername = sshConfig.User
|
|
|
|
|
2019-01-11 17:06:15 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSSHConfig) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|