2018-12-11 22:01:50 +01:00
|
|
|
package ssh
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-22 19:41:12 +01:00
|
|
|
"time"
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/problame/go-netssh"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/transport"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SSHStdinserverConnecter struct {
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Port uint16
|
|
|
|
IdentityFile string
|
|
|
|
TransportOpenCommand []string
|
|
|
|
SSHCommand string
|
|
|
|
Options []string
|
|
|
|
dialTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func SSHStdinserverConnecterFromConfig(in *config.SSHStdinserverConnect) (c *SSHStdinserverConnecter, err error) {
|
|
|
|
|
|
|
|
c = &SSHStdinserverConnecter{
|
|
|
|
Host: in.Host,
|
|
|
|
User: in.User,
|
|
|
|
Port: in.Port,
|
|
|
|
IdentityFile: in.IdentityFile,
|
|
|
|
SSHCommand: in.SSHCommand,
|
|
|
|
Options: in.Options,
|
|
|
|
dialTimeout: in.DialTimeout,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (c *SSHStdinserverConnecter) Connect(dialCtx context.Context) (transport.Wire, error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
|
|
|
|
var endpoint netssh.Endpoint
|
|
|
|
if err := copier.Copy(&endpoint, c); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
dialCtx, dialCancel := context.WithTimeout(dialCtx, c.dialTimeout) // context.TODO tied to error handling below
|
|
|
|
defer dialCancel()
|
|
|
|
nconn, err := netssh.Dial(dialCtx, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
if err == context.DeadlineExceeded {
|
|
|
|
err = errors.Errorf("dial_timeout of %s exceeded", c.dialTimeout)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
return nconn, nil
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|