forked from google/gapid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.go
361 lines (317 loc) · 9.39 KB
/
commands.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (C) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package remotessh
import (
"bufio"
"context"
"fmt"
"io"
"net"
"os"
"path"
"runtime"
"strings"
"sync"
"github.com/google/gapid/core/app/crash"
"github.com/google/gapid/core/event/task"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/shell"
"github.com/google/gapid/core/text"
"golang.org/x/crypto/ssh"
)
// remoteProcess is the interface to a running process, as started by a Target.
type remoteProcess struct {
session *ssh.Session
wg sync.WaitGroup
}
func (r *remoteProcess) Kill() error {
return r.session.Signal(ssh.SIGSEGV)
}
func (r *remoteProcess) Wait(ctx context.Context) error {
ret := r.session.Wait()
r.wg.Wait()
return ret
}
var _ shell.Process = (*remoteProcess)(nil)
type sshShellTarget struct{ b *binding }
// Start starts the given command in the remote shell.
func (t sshShellTarget) Start(cmd shell.Cmd) (shell.Process, error) {
session, err := t.b.connection.NewSession()
if err != nil {
return nil, err
}
p := &remoteProcess{
session: session,
wg: sync.WaitGroup{},
}
if cmd.Stdin != nil {
stdin, err := session.StdinPipe()
if err != nil {
return nil, err
}
crash.Go(func() {
defer stdin.Close()
io.Copy(stdin, cmd.Stdin)
})
}
if cmd.Stdout != nil {
stdout, err := session.StdoutPipe()
if err != nil {
return nil, err
}
p.wg.Add(1)
crash.Go(func() {
io.Copy(cmd.Stdout, stdout)
p.wg.Done()
})
}
if cmd.Stderr != nil {
stderr, err := session.StderrPipe()
if err != nil {
return nil, err
}
p.wg.Add(1)
crash.Go(func() {
io.Copy(cmd.Stderr, stderr)
p.wg.Done()
})
}
prefix := ""
if cmd.Dir != "" {
prefix += "cd " + cmd.Dir + "; "
}
for _, e := range cmd.Environment.Keys() {
if e != "" {
val := text.Quote([]string{cmd.Environment.Get(e)})[0]
prefix = prefix + strings.TrimSpace(e) + "=" + val + " "
}
}
for _, e := range t.b.env.Keys() {
if e != "" {
val := text.Quote([]string{t.b.env.Get(e)})[0]
prefix = prefix + strings.TrimSpace(e) + "=" + val + " "
}
}
val := prefix + cmd.Name + " " + strings.Join(cmd.Args, " ")
if err := session.Start(val); err != nil {
return nil, err
}
return p, nil
}
func (t sshShellTarget) String() string {
c := t.b.configuration
return c.User + "@" + c.Host + ": " + t.b.String()
}
// Shell implements the Device interface returning commands that will error if run.
func (b binding) Shell(name string, args ...string) shell.Cmd {
return shell.Command(name, args...).On(sshShellTarget{&b})
}
func (b binding) destroyPosixDirectory(ctx context.Context, dir string) {
_, _ = b.Shell("rm", "-rf", dir).Call(ctx)
}
func (b binding) createPosixTempDirectory(ctx context.Context) (string, func(context.Context), error) {
dir, err := b.Shell("mktemp", "-d").Call(ctx)
if err != nil {
return "", nil, err
}
return dir, func(ctx context.Context) { b.destroyPosixDirectory(ctx, dir) }, nil
}
func (b binding) createWindowsTempDirectory(ctx context.Context) (string, func(ctx context.Context), error) {
return "", nil, fmt.Errorf("Windows remote targets are not yet supported.")
}
// MakeTempDir creates a temporary directory on the remote machine. It returns the
// full path, and a function that can be called to clean up the directory.
func (b binding) MakeTempDir(ctx context.Context) (string, func(ctx context.Context), error) {
switch b.os {
case device.Linux, device.OSX:
return b.createPosixTempDirectory(ctx)
case device.Windows:
return b.createWindowsTempDirectory(ctx)
default:
panic(fmt.Errorf("Unsupported OS %v", b.os))
}
}
// WriteFile moves the contents of io.Reader into the given file on the remote machine.
// The file is given the mode as described by the unix filemode string.
func (b binding) WriteFile(ctx context.Context, contents io.Reader, mode os.FileMode, destPath string) error {
perm := fmt.Sprintf("%4o", mode.Perm())
_, err := b.Shell("cat", ">", destPath, "; chmod ", perm, " ", destPath).Read(contents).Call(ctx)
return err
}
// PushFile copies a file from a local path to the remote machine. Permissions are
// maintained across.
func (b binding) PushFile(ctx context.Context, source, dest string) error {
infile, err := os.Open(source)
if err != nil {
return err
}
permission, err := os.Stat(source)
if err != nil {
return err
}
mode := permission.Mode()
// If we are on windows pushing to Linux, we lose the executable
// bit, get it back.
if (b.os == device.Linux ||
b.os == device.OSX) &&
runtime.GOOS == "windows" {
mode |= 0550
}
return b.WriteFile(ctx, infile, mode, dest)
}
// doTunnel tunnels a single connection through the SSH connection.
func (b binding) doTunnel(ctx context.Context, local net.Conn, remotePort int) error {
remote, err := b.connection.Dial("tcp", fmt.Sprintf("localhost:%d", remotePort))
if err != nil {
local.Close()
return err
}
wg := sync.WaitGroup{}
copy := func(writer io.Writer, reader io.Reader) {
_, err := io.Copy(writer, reader)
if err != nil {
log.E(ctx, "Copy Error %s", err)
}
wg.Done()
}
wg.Add(2)
crash.Go(func() { copy(local, remote) })
crash.Go(func() { copy(remote, local) })
crash.Go(func() {
defer local.Close()
defer remote.Close()
wg.Wait()
})
return nil
}
// SetupLocalPort forwards a local TCP port to the remote machine on the remote port.
// The local port that was opened is returned.
func (b binding) SetupLocalPort(ctx context.Context, remotePort int) (int, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return 0, err
}
crash.Go(func() {
<-task.ShouldStop(ctx)
listener.Close()
})
crash.Go(func() {
defer listener.Close()
for {
local, err := listener.Accept()
if err != nil {
return
}
if err = b.doTunnel(ctx, local, remotePort); err != nil {
return
}
}
})
return listener.Addr().(*net.TCPAddr).Port, nil
}
// TempFile creates a temporary file on the given Device. It returns the
// path to the file, and a function that can be called to clean it up.
func (b binding) TempFile(ctx context.Context) (string, func(ctx context.Context), error) {
res, err := b.Shell("mktemp").Call(ctx)
if err != nil {
return "", nil, err
}
return res, func(ctx context.Context) {
b.Shell("rm", "-f", res).Call(ctx)
}, nil
}
// FileContents returns the contents of a given file on the Device.
func (b binding) FileContents(ctx context.Context, path string) (string, error) {
return b.Shell("cat", path).Call(ctx)
}
// RemoveFile removes the given file from the device
func (b binding) RemoveFile(ctx context.Context, path string) error {
_, err := b.Shell("rm", "-f", path).Call(ctx)
return err
}
// GetEnv returns the default environment for the Device.
func (b binding) GetEnv(ctx context.Context) (*shell.Env, error) {
env, err := b.Shell("env").Call(ctx)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(strings.NewReader(env))
e := shell.NewEnv()
for scanner.Scan() {
e.Add(scanner.Text())
}
return e, nil
}
// ListExecutables returns the executables in a particular directory as given by path
func (b binding) ListExecutables(ctx context.Context, inPath string) ([]string, error) {
if inPath == "" {
inPath = b.GetURIRoot()
}
files, err := b.Shell("find", `"`+inPath+`"`, "-mindepth", "1", "-maxdepth", "1", "-type", "f", "-executable", "-printf", `%f\\n`).Call(ctx)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(strings.NewReader(files))
out := []string{}
for scanner.Scan() {
_, file := path.Split(scanner.Text())
out = append(out, file)
}
return out, nil
}
// ListDirectories returns a list of directories rooted at a particular path
func (b binding) ListDirectories(ctx context.Context, inPath string) ([]string, error) {
if inPath == "" {
inPath = b.GetURIRoot()
}
dirs, err := b.Shell("find", `"`+inPath+`"`, "-mindepth", "1", "-maxdepth", "1", "-type", "d", "-printf", `%f\\n`).Call(ctx)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(strings.NewReader(dirs))
out := []string{}
for scanner.Scan() {
_, file := path.Split(scanner.Text())
out = append(out, file)
}
return out, nil
}
// IsFile returns true if the given path is a file
func (b binding) IsFile(ctx context.Context, inPath string) (bool, error) {
dir, err := b.IsDirectory(ctx, inPath)
if err == nil && dir {
return false, nil
}
_, err = b.Shell("stat", `"`+inPath+`"`).Call(ctx)
if err != nil {
return false, nil
}
return true, nil
}
// IsDirectory returns true if the given path is a directory
func (b binding) IsDirectory(ctx context.Context, inPath string) (bool, error) {
_, err := b.Shell("cd", `"`+inPath+`"`).Call(ctx)
if err != nil {
return false, nil
}
return true, nil
}
// GetWorkingDirectory returns the directory that this device considers CWD
func (b binding) GetWorkingDirectory(ctx context.Context) (string, error) {
return b.Shell("pwd").Call(ctx)
}
func (b binding) GetURIRoot() string {
return "/"
}