Add cleanup function to close SSHAgent

This commit is contained in:
Tarrant 2015-03-20 18:18:35 -07:00
parent 951f4201f7
commit 05407296c6
4 changed files with 21 additions and 3 deletions

View File

@ -60,6 +60,7 @@ func (p *ResourceProvisioner) copyFiles(conf *helper.SSHConfig, src, dst string)
if err != nil {
return err
}
defer config.CleanupConfig()
// Wait and retry until we establish the SSH connection
var comm *helper.SSHCommunicator

View File

@ -172,6 +172,7 @@ func (p *ResourceProvisioner) runScripts(
if err != nil {
return err
}
defer config.CleanupConfig()
o.Output(fmt.Sprintf(
"Connecting to remote host via SSH...\n"+

View File

@ -97,6 +97,10 @@ type Config struct {
// NoPty, if true, will not request a pty from the remote end.
NoPty bool
// SSHAgentConn is a pointer to the UNIX connection for talking with the
// ssh-agent.
SSHAgentConn net.Conn
}
// New creates a new packer.Communicator implementation over SSH. This takes

View File

@ -103,6 +103,9 @@ func safeDuration(dur string, defaultDur time.Duration) time.Duration {
// PrepareConfig is used to turn the *SSHConfig provided into a
// usable *Config for client initialization.
func PrepareConfig(conf *SSHConfig) (*Config, error) {
var conn net.Conn
var err error
sshConf := &ssh.ClientConfig{
User: conf.User,
}
@ -113,7 +116,7 @@ func PrepareConfig(conf *SSHConfig) (*Config, error) {
return nil, fmt.Errorf("SSH Requested but SSH_AUTH_SOCK not-specified")
}
conn, err := net.Dial("unix", sshAuthSock)
conn, err = net.Dial("unix", sshAuthSock)
if err != nil {
return nil, fmt.Errorf("Error connecting to SSH_AUTH_SOCK: %v", err)
}
@ -164,8 +167,17 @@ func PrepareConfig(conf *SSHConfig) (*Config, error) {
}
host := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
config := &Config{
SSHConfig: sshConf,
Connection: ConnectFunc("tcp", host),
SSHConfig: sshConf,
Connection: ConnectFunc("tcp", host),
SSHAgentConn: conn,
}
return config, nil
}
func (c *Config) CleanupConfig() error {
if c.SSHAgentConn != nil {
return c.SSHAgentConn.Close()
}
return nil
}