Skip to content

Commit

Permalink
60s wait for command
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Nov 22, 2022
1 parent 931c5df commit 999ada2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli/hancock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {
}
app := cli.NewApp()
app.Name = "hancock"
app.Version = "20221106"
app.Version = "20221122"
app.Usage = "Deploy and run command on remote instances with built-in nami, joker"
app.Authors = []*cli.Author{
{
Expand Down
39 changes: 39 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2020-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package hancock

import (
"net"
"time"
)

type Conn struct {
net.Conn
Timeout int
}

func (c *Conn) Read(b []byte) (int, error) {
if err := c.Conn.SetDeadline(time.Now().Add(time.Duration(c.Timeout) * time.Second)); err != nil {
return 0, err
}
return c.Conn.Read(b)
}

func (c *Conn) Write(b []byte) (int, error) {
if err := c.Conn.SetDeadline(time.Now().Add(time.Duration(c.Timeout) * time.Second)); err != nil {
return 0, err
}
return c.Conn.Write(b)
}
10 changes: 8 additions & 2 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package hancock

import (
"io"
"net"
"os"
"path/filepath"
"strings"
"time"

"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -47,16 +49,20 @@ func NewInstance(server, user, password string, privateKey []byte) (*Instance, e
l = append(l, ssh.PublicKeys(signer))
}

conn, err := net.DialTimeout("tcp", server, 60*time.Second)
if err != nil {
return nil, err
}
config := &ssh.ClientConfig{
User: user,
Auth: l,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
// Timeout: 10 * time.Second,
}
client, err := ssh.Dial("tcp", server, config)
c, chans, reqs, err := ssh.NewClientConn(&Conn{Conn: conn, Timeout: 60}, server, config)
if err != nil {
return nil, err
}
client := ssh.NewClient(c, chans, reqs)
return &Instance{
Client: client,
}, nil
Expand Down

0 comments on commit 999ada2

Please # to comment.