/
remote.go
113 lines (85 loc) · 1.82 KB
/
remote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"net"
"net/rpc"
"path"
)
type PipesReadWrite struct {
Stdin io.ReadCloser
Stdout io.WriteCloser
}
func (x *PipesReadWrite) Read(p []byte) (n int, err error) {
return x.Stdin.Read(p)
}
func (x *PipesReadWrite) Write(p []byte) (n int, err error) {
return x.Stdout.Write(p)
}
func (x *PipesReadWrite) Close() error {
if err := x.Stdin.Close(); err != nil {
return err
}
if err := x.Stdout.Close(); err != nil {
return err
}
return nil
}
func RemoteConnect(socketfile string) (io.ReadWriteCloser, error) {
if options.Remote == "" {
if len(socketfile) == 0 {
socketfile = path.Join(options.Base, "run", "autobuild.sock")
}
cl, err := net.Dial("unix", socketfile)
if err != nil {
return nil, err
}
if err := RemoteSendCredentials(cl.(*net.UnixConn)); err != nil {
return nil, err
}
return cl, nil
} else {
// Connect to the remote using ssh
scmd := "autobuild connect"
if len(socketfile) != 0 {
scmd = scmd + " " + socketfile
}
cmd := MakeCommand("ssh", options.Remote, scmd)
inp, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
outp, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
rw := &PipesReadWrite{
Stdin: outp,
Stdout: inp,
}
if err := cmd.Start(); err != nil {
return nil, err
}
return rw, nil
}
return nil, nil
}
func RemoteCall(method string, args interface{}, reply interface{}) error {
rwc, err := RemoteConnect("")
if err != nil {
if options.Verbose {
fmt.Printf("Failed to connect: %s\n", err)
}
return err
}
c := rpc.NewClient(rwc)
if options.Verbose {
fmt.Printf("Connected to remote daemon\n")
}
if err := c.Call(method, args, reply); err != nil {
fmt.Printf("Failed to call remote method %v: %s\n", method, err)
return err
}
c.Close()
return nil
}