- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathremote-run
executable file
·473 lines (387 loc) · 17.7 KB
/
remote-run
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env python3
# remote-run - Runs a command on another machine, for testing -----*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2018 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ----------------------------------------------------------------------------
importargparse
importos
importposixpath
importsubprocess
importsys
importshutil
defquote(arg):
returnrepr(arg)
classCommandRunner(object):
def__init__(self):
self.verbose=False
self.dry_run=False
self.ignore_rsync_failure=False
@staticmethod
def_dirnames(files):
returnsorted(set(posixpath.dirname(f) forfinfiles))
defpopen(self, command, **kwargs):
ifself.verbose:
print(' '.join(command), file=sys.stderr)
ifself.dry_run:
returnNone
returnsubprocess.Popen(command, **kwargs)
defmkdirs_remote(self, directories):
ifdirectories:
mkdir_command= ['/bin/mkdir', '-p'] +directories
self.run_remote(mkdir_command)
defsend(self, input_prefix, remote_prefix, local_to_remote_files):
# Prepare the remote directory structure.
self.mkdirs_remote([remote_prefix])
self.run_rsync_to(input_prefix, local_to_remote_files, remote_prefix)
deffetch(self, output_prefix, remote_prefix, remote_to_local_files):
# Prepare the local directory structure.
mkdir_command= ['/bin/mkdir', '-p', output_prefix]
ifself.verbose:
print(' '.join(mkdir_command), file=sys.stderr)
ifnotself.dry_run:
subprocess.check_call(mkdir_command)
self.run_rsync_from(remote_prefix, remote_to_local_files, output_prefix)
# Recover from random and transient errors that occur when SSHing to devices, in particular devicecompute
@staticmethod
defshould_remote_command_retry(stderr):
if"banner line contains invalid characters"instderr:
returnTrue
if"Connection to localhost closed by remote host"instderr:
returnTrue
if"kex_exchange_identification: Connection closed by remote host"instderr:
returnTrue
if"rsync error: unexplained error"instderr:
returnTrue
# Fallthrough. The error is not known and shouldn't be retried
returnFalse
defrun_remote(self, command, remote_env={}):
env_strings= ['{0}={1}'.format(k,v) fork,vinsorted(remote_env.items())]
remote_invocation=self.remote_invocation(
['/usr/bin/env'] +env_strings+command)
attempt=1
remote_proc=None
whileattempt<=3:
remote_proc=self.popen(
remote_invocation,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
ifself.dry_run:
return
stdout, stderr=remote_proc.communicate()
stdout=stdout.decode(encoding='utf-8', errors='replace')
stderr=stderr.decode(encoding='utf-8', errors='replace')
# Print stdout to screen
print(stdout, end='')
# This is a transient and random error, and if this occurs, we should simply retry our ssh command.
ifself.should_remote_command_retry(stderr):
attempt+=1
continue
print(stderr, end='', file=sys.stderr)
# Process error code
ifremote_proc.returncode:
# FIXME: We may still want to fetch the output files to see what
# went wrong.
sys.exit(remote_proc.returncode)
else:
# Nothing went wrong. Return
return
ifattempt>3andremote_procisnotNone:
sys.exit(remote_proc.returncode)
defrun_rsync(self, invocation, sources):
attempt=1
rsync_proc=None
whileattempt<=3:
rsync_proc=self.popen(
invocation,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
ifself.dry_run:
return
sources='\n'.join(sources)
ifself.verbose:
print(sources, file=sys.stderr)
stdout, stderr=rsync_proc.communicate(sources.encode('utf-8'))
stdout=stdout.decode(encoding='utf-8', errors='replace')
stderr=stderr.decode(encoding='utf-8', errors='replace')
# Print stdout to screen
print(stdout, end='')
# This is a transient and random error, and if this occurs, we should simply retry our ssh command.
ifself.should_remote_command_retry(stderr):
attempt+=1
continue
print(stderr, end='', file=sys.stderr)
# Process error code
ifrsync_proc.returncode:
ifself.ignore_rsync_failure:
return
else:
sys.exit(rsync_proc.returncode)
else:
# Nothing went wrong. Return
return
ifattempt>3andrsync_procisnotNone:
sys.exit(rsync_proc.returncode)
defrun_rsync_to(self, prefix, sources, dest):
self.run_rsync(self.rsync_to_invocation(prefix, dest), sources)
defrun_rsync_from(self, prefix, sources, dest):
self.run_rsync(self.rsync_from_invocation(prefix, dest), sources)
classRemoteCommandRunner(CommandRunner):
def__init__(self, host, identity_path, ssh_options, config_file):
if':'inhost:
(self.remote_host, self.port) =host.rsplit(':', 1)
else:
self.remote_host=host
self.port=None
self.identity_path=identity_path
self.ssh_options=ssh_options
self.config_file=config_file
defcommon_options(self, port_flag):
port_option= [port_flag, self.port] ifself.portelse []
config_option= ['-F', self.config_file] ifself.config_fileelse []
identity_option= (
['-i', self.identity_path] ifself.identity_pathelse [])
# Interleave '-o' with each custom option.
# From https://stackoverflow.com/a/8168526,
# with explanatory help from
# https://spapas.github.io/2016/04/27/python-nested-list-comprehensions/
extra_options= [argforoptioninself.ssh_options
forargin ["-o", option]]
returnport_option+identity_option+config_option+extra_options
defremote_invocation(self, command):
return (['/usr/bin/ssh', '-n'] +
self.common_options(port_flag='-p') +
[self.remote_host, '--'] +
[quote(arg) forargincommand])
defrsync_invocation(self, source, dest):
return ['/usr/bin/rsync', '-arRz', '--files-from=-',
'-e', ' '.join([quote(x) forxin
['/usr/bin/ssh'] +
self.common_options(port_flag='-p')]),
source,
dest]
defrsync_from_invocation(self, source, dest):
returnself.rsync_invocation(self.remote_host+':'+source, dest)
defrsync_to_invocation(self, source, dest):
returnself.rsync_invocation(source, self.remote_host+':'+dest)
classLocalCommandRunner(CommandRunner):
defremote_invocation(self, command):
returncommand
defrsync_invocation(self, source, dest):
return ['/usr/bin/rsync', '-arz', '--files-from=-', source, dest]
defrsync_from_invocation(self, source, dest):
returnself.rsync_invocation(source, dest)
defrsync_to_invocation(self, source, dest):
returnself.rsync_invocation(source, dest)
defstrip_sep(name):
lsep=len(posixpath.sep)
whilename.startswith(posixpath.sep):
name=name[lsep:]
returnname
classRemotePathSet(object):
def__init__(self):
self.inputs=set()
self.nodir_inputs=set()
self.existing_outputs=set()
self.outputs=set()
self.existing_nodir_outputs=set()
self.nodir_outputs=set()
classPrefixProcessor(object):
def__init__(self,
input_prefix, output_prefix,
remote_dir, remote_input_prefix,
remote_output_prefix,
path_set):
assertnotremote_input_prefix.startswith('..')
assertnotremote_output_prefix.startswith('..')
self.input_prefix=input_prefix
self.output_prefix=output_prefix
self.remote_dir=remote_dir
self.remote_input_prefix=remote_input_prefix
self.remote_output_prefix=remote_output_prefix
self.path_set=path_set
ifself.input_prefix:
whileself.input_prefix.endswith(posixpath.sep):
self.input_prefix=self.input_prefix[:-len(posixpath.sep)]
ifself.output_prefix:
whileself.output_prefix.endswith(posixpath.sep):
self.output_prefix=self.output_prefix[:-len(posixpath.sep)]
ifself.input_prefix:
split=posixpath.split(self.input_prefix)
self.input_prefix_split= (len(self.input_prefix), split[0], split[1])
else:
self.input_prefix_split= (0, '', '')
ifself.output_prefix:
split=posixpath.split(self.output_prefix)
self.output_prefix_split= (len(self.output_prefix),
split[0],
split[1])
else:
self.output_prefix_split= (0, '', '')
defprocess_one(self, orig_name):
iplen, ipfxdir, ipfx=self.input_prefix_split
oplen, opfxdir, opfx=self.output_prefix_split
ifiplenandorig_name.startswith(self.input_prefix):
name=orig_name[iplen:]
ifnotname.startswith(posixpath.sep):
name=ipfx+name
self.path_set.nodir_inputs.add(name)
else:
name=strip_sep(name)
self.path_set.inputs.add(name)
returnposixpath.join(self.remote_dir,
self.remote_input_prefix,
name)
ifoplenandorig_name.startswith(self.output_prefix):
name=orig_name[oplen:]
ifnotname.startswith(posixpath.sep):
name=opfx+name
self.path_set.nodir_outputs.add(name)
ifos.path.exists(orig_name):
self.path_set.existing_nodir_outputs.add(name)
else:
name=strip_sep(name)
self.path_set.outputs.add(name)
ifos.path.exists(orig_name):
self.path_set.existing_outputs.add(name)
returnposixpath.join(self.remote_dir,
self.remote_output_prefix,
name)
returnorig_name
classArgumentProcessor(PrefixProcessor):
def__init__(self,
input_prefix, output_prefix,
remote_dir, remote_input_prefix, remote_output_prefix,
path_set, arguments):
super().__init__(input_prefix, output_prefix,
remote_dir, remote_input_prefix, remote_output_prefix,
path_set)
self.original_args=arguments
defprocess_args(self):
self.args= []
forarginself.original_args:
self.args.append(self.process_one(arg))
classEnvVarProcessor(PrefixProcessor):
def__init__(self,
input_prefix, output_prefix,
remote_dir, remote_input_prefix, remote_output_prefix,
path_set, environment):
super().__init__(input_prefix, output_prefix,
remote_dir, remote_input_prefix, remote_output_prefix,
path_set)
self.original_env=environment
defprocess_env(self):
self.env=dict()
forkey, valueinself.original_env.items():
self.env[key] =self.process_one(value)
defcollect_remote_env(local_env=os.environ, prefix='REMOTE_RUN_CHILD_'):
returndict((key[len(prefix):], value)
forkey, valueinlocal_env.items() ifkey.startswith(prefix))
defmain():
parser=argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
help='print commands as they are run')
parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
help="print the commands that would have been run, but "
"don't actually run them")
parser.add_argument('--remote-dir', required=True, metavar='PATH',
help='(required) a writable temporary path on the '
'remote machine')
parser.add_argument('--input-prefix',
help='arguments matching this prefix will be uploaded')
parser.add_argument('--output-prefix',
help='arguments matching this prefix will be both '
'uploaded and downloaded')
parser.add_argument('--remote-input-prefix', default='input',
help='input arguments use this prefix on the remote '
'machine')
parser.add_argument('--remote-output-prefix', default='output',
help='output arguments use this prefix on the remote '
'machine')
parser.add_argument('-i', '--identity', dest='identity', metavar='FILE',
help='an SSH identity file (private key) to use')
parser.add_argument('-F', '--config-file', dest='config_file', metavar='FILE',
help='an SSH configuration file')
parser.add_argument('-o', '--ssh-option', action='append', default=[],
dest='ssh_options', metavar='OPTION',
help='extra SSH config options (man ssh_config)')
parser.add_argument('--debug-as-local', action='store_true',
help='run commands locally instead of over SSH, for '
'debugging purposes. The "host" argument is '
'omitted.')
parser.add_argument('--ignore-rsync-failure', action='store_true',
help='ignore rsync failures, for debugging.')
parser.add_argument('host',
help='the host to connect to, in the form '
'[user@]host[:port]')
parser.add_argument('command', nargs=argparse.REMAINDER,
help='the command to run', metavar='command...')
args=parser.parse_args()
ifargs.debug_as_local:
runner=LocalCommandRunner()
args.command.insert(0, args.host)
delargs.host
else:
runner=RemoteCommandRunner(args.host,
args.identity,
args.ssh_options,
args.config_file)
runner.dry_run=args.dry_run
runner.verbose=args.verboseorargs.dry_run
runner.ignore_rsync_failure=args.ignore_rsync_failure
assertnotargs.remote_dir=='/'
path_set=RemotePathSet()
argproc=ArgumentProcessor(args.input_prefix,
args.output_prefix,
args.remote_dir,
posixpath.normpath(args.remote_input_prefix),
posixpath.normpath(args.remote_output_prefix),
path_set,
args.command)
argproc.process_args()
envproc=EnvVarProcessor(args.input_prefix,
args.output_prefix,
args.remote_dir,
posixpath.normpath(args.remote_input_prefix),
posixpath.normpath(args.remote_output_prefix),
path_set,
collect_remote_env())
envproc.process_env()
input_dir=posixpath.join(args.remote_dir, args.remote_input_prefix)
output_dir=posixpath.join(args.remote_dir, args.remote_output_prefix)
ifargs.output_prefix:
runner.run_remote(['/bin/rm', '-rf', output_dir])
dirs=set()
foroutputinpath_set.outputs:
dirs.add(posixpath.join(output_dir, posixpath.dirname(output)))
foroutputinpath_set.nodir_outputs:
dirs.add(posixpath.join(output_dir, posixpath.dirname(output)))
runner.mkdirs_remote(list(dirs))
ifpath_set.inputs:
runner.send(args.input_prefix, input_dir, path_set.inputs)
ifpath_set.nodir_inputs:
runner.send(posixpath.dirname(args.input_prefix),
input_dir, path_set.nodir_inputs)
ifpath_set.existing_outputs:
runner.send(args.output_prefix, output_dir, path_set.existing_outputs)
ifpath_set.existing_nodir_outputs:
runner.send(posixpath.dirname(args.output_prefix),
output_dir, path_set.existing_nodir_outputs)
runner.run_remote(argproc.args, envproc.env)
ifpath_set.outputs:
runner.fetch(args.output_prefix, output_dir, path_set.outputs)
ifpath_set.nodir_outputs:
runner.fetch(posixpath.dirname(args.output_prefix),
output_dir, path_set.nodir_outputs)
if__name__=="__main__":
main()