communicator/ssh: fix panic when client is nil on reconnect

This commit is contained in:
Mitchell Hashimoto 2013-08-12 15:22:31 -07:00
parent 656de901ca
commit 90974a4733
1 changed files with 11 additions and 2 deletions

View File

@ -205,9 +205,14 @@ func (c *comm) Download(string, io.Writer) error {
panic("not implemented yet") panic("not implemented yet")
} }
func (c *comm) newSession() (*ssh.Session, error) { func (c *comm) newSession() (session *ssh.Session, err error) {
log.Println("opening new ssh session") log.Println("opening new ssh session")
session, err := c.client.NewSession() if c.client == nil {
err = errors.New("client not available")
} else {
session, err = c.client.NewSession()
}
if err != nil { if err != nil {
log.Printf("ssh session open error: '%s', attempting reconnect", err) log.Printf("ssh session open error: '%s', attempting reconnect", err)
if err := c.reconnect(); err != nil { if err := c.reconnect(); err != nil {
@ -225,6 +230,10 @@ func (c *comm) reconnect() (err error) {
c.conn.Close() c.conn.Close()
} }
// Set the conn and client to nil since we'll recreate it
c.conn = nil
c.client = nil
log.Printf("reconnecting to TCP connection for SSH") log.Printf("reconnecting to TCP connection for SSH")
c.conn, err = c.config.Connection() c.conn, err = c.config.Connection()
if err != nil { if err != nil {