14325: Configurable SSH target port for cloud VMs.
[arvados.git] / lib / dispatchcloud / ssh_executor / executor.go
index 4b2478e94046feec8b3fdbbc5b10e8ab8b43e154..d0fb54c54cd932df806e0129a0f92d78cd3a9999 100644 (file)
@@ -36,9 +36,10 @@ func New(t cloud.ExecutorTarget) *Executor {
 //
 // An Executor must not be copied.
 type Executor struct {
-       target  cloud.ExecutorTarget
-       signers []ssh.Signer
-       mtx     sync.RWMutex // controls access to instance after creation
+       target     cloud.ExecutorTarget
+       targetPort string
+       signers    []ssh.Signer
+       mtx        sync.RWMutex // controls access to instance after creation
 
        client      *ssh.Client
        clientErr   error
@@ -67,6 +68,17 @@ func (exr *Executor) SetTarget(t cloud.ExecutorTarget) {
        exr.target = t
 }
 
+// SetTargetPort sets the default port (name or number) to connect
+// to. This is used only when the address returned by the target's
+// Address() method does not specify a port. If the given port is
+// empty (or SetTargetPort is not called at all), the default port is
+// "ssh".
+func (exr *Executor) SetTargetPort(port string) {
+       exr.mtx.Lock()
+       defer exr.mtx.Unlock()
+       exr.targetPort = port
+}
+
 // Target returns the current target.
 func (exr *Executor) Target() cloud.ExecutorTarget {
        exr.mtx.RLock()
@@ -167,6 +179,14 @@ func (exr *Executor) setupSSHClient() (*ssh.Client, error) {
        if addr == "" {
                return nil, errors.New("instance has no address")
        }
+       if h, p, err := net.SplitHostPort(addr); err != nil || p == "" {
+               // Target address does not specify a port.  Use
+               // targetPort, or "ssh".
+               if p = exr.targetPort; p == "" {
+                       p = "ssh"
+               }
+               addr = net.JoinHostPort(h, p)
+       }
        var receivedKey ssh.PublicKey
        client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
                User: "root",