Skip to content

Commit

Permalink
add ssh support
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Sep 6, 2017
1 parent 5c5dc74 commit e08aaf4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
24 changes: 24 additions & 0 deletions pkg/util/ssh/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ssh

import (
"context"
"fmt"
"os/exec"
)

// Exec executes the cmd on the remote node.
// Here we assume we can run with `ssh node cmd` directly
// TODO: add a SSH config?
func Exec(ctx context.Context, node string, cmd string) error {
return exec.CommandContext(ctx, "ssh", node, cmd).Run()
}

// Upload uploads files from local path to remote node path.
func Upload(ctx context.Context, localPath string, node string, remotePath string) error {
return exec.CommandContext(ctx, "scp", "-r", localPath, fmt.Sprintf("%s:%s", node, remotePath)).Run()
}

// Download downloads files from remote node path to local path.
func Download(ctx context.Context, localPath string, node string, remotePath string) error {
return exec.CommandContext(ctx, "scp", "-r", fmt.Sprintf("%s:%s", node, remotePath), localPath).Run()
}
56 changes: 56 additions & 0 deletions pkg/util/ssh/ssh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ssh

import (
"context"
"io/ioutil"
"os"
"path"
"testing"
)

func TestExec(t *testing.T) {
ctx := context.Background()
if err := Exec(ctx, "n1", "sleep 1"); err != nil {
t.Fatalf("ssh failed %v", err)
}

if err := Exec(ctx, "n1", "cat non_exist_file"); err == nil {
t.Fatal("ssh must fail")
}

if err := Exec(ctx, "n6", "sleep 1"); err == nil {
t.Fatal("ssh must fail")
}
}

func TestScp(t *testing.T) {
dir, err := ioutil.TempDir(".", "var")
if err != nil {
t.Fatal(err)
}
//defer os.RemoveAll(dir)

f, err := os.Create(path.Join(dir, "a.log"))
if err != nil {
t.Fatalf("create file failed %v", err)
}
f.WriteString("Hello world")
f.Close()

ctx := context.Background()
if err = Upload(ctx, path.Join(dir, "a.log"), "n1", "/tmp/b.log"); err != nil {
t.Fatalf("upload file failed %v", err)
}

if err = Download(ctx, path.Join(dir, "b.log"), "n1", "/tmp/b.log"); err != nil {
t.Fatalf("download file failed %v", err)
}

if err = Upload(ctx, dir, "n1", "/tmp/"); err != nil {
t.Fatalf("upload folder failed %v", err)
}

if err = Download(ctx, path.Join(dir, "sub"), "n1", path.Join("/tmp", path.Base(dir))); err != nil {
t.Fatalf("download foler failed %v", err)
}
}
3 changes: 3 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"strconv"
"testing"
"time"
)

func TestWget(t *testing.T) {
Expand Down Expand Up @@ -93,6 +94,8 @@ func TestDaemon(t *testing.T) {
t.Fatalf("stop daemon failed %v", err)
}

time.Sleep(time.Second)

if IsProcessExist(pid) {
t.Fatalf("pid %d must not exist", pid)
}
Expand Down

0 comments on commit e08aaf4

Please sign in to comment.