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
|
import os, time, types, socket, shutil, glob, logging
from autotest_lib.client.common_lib import error, logging_manager
from autotest_lib.server import utils, autotest
from autotest_lib.server.hosts import remote
def make_ssh_command(user="root", port=22, opts='', connect_timeout=30):
base_command = ("/usr/bin/ssh -a -x %s -o BatchMode=yes "
"-o ConnectTimeout=%d -o ServerAliveInterval=300 "
"-l %s -p %d")
assert isinstance(connect_timeout, (int, long))
assert connect_timeout > 0 # can't disable the timeout
return base_command % (opts, connect_timeout, user, port)
# import site specific Host class
SiteHost = utils.import_site_class(
__file__, "autotest_lib.server.hosts.site_host", "SiteHost",
remote.RemoteHost)
# this constant can be passed to run() to tee stdout/stdout to the logging
# module.
TEE_TO_LOGS = object()
class AbstractSSHHost(SiteHost):
""" This class represents a generic implementation of most of the
framework necessary for controlling a host via ssh. It implements
almost all of the abstract Host methods, except for the core
Host.run method. """
def _initialize(self, hostname, user="root", port=22, password="",
*args, **dargs):
super(AbstractSSHHost, self)._initialize(hostname=hostname,
*args, **dargs)
self.ip = socket.getaddrinfo(self.hostname, None)[0][4][0]
self.user = user
self.port = port
self.password = password
def _encode_remote_paths(self, paths):
""" Given a list of file paths, encodes it as a single remote path, in
the style used by rsync and scp. """
escaped_paths = [utils.scp_remote_escape(path) for path in paths]
return '%s@%s:"%s"' % (self.user, self.hostname,
" ".join(paths))
def _make_rsync_cmd(self, sources, dest, delete_dest, preserve_symlinks):
""" Given a list of source paths and a destination path, produces the
appropriate rsync command for copying them. Remote paths must be
pre-encoded. """
ssh_cmd = make_ssh_command(self.user, self.port)
if delete_dest:
delete_flag = "--delete"
else:
delete_flag = ""
if preserve_symlinks:
symlink_flag = ""
else:
symlink_flag = "-L"
command = "rsync %s %s --timeout=1800 --rsh='%s' -az %s %s"
return command % (symlink_flag, delete_flag, ssh_cmd,
" ".join(sources), dest)
def _make_scp_cmd(self, sources, dest):
""" Given a list of source paths and a destination path, produces the
appropriate scp command for encoding it. Remote paths must be
pre-encoded. """
command = "scp -rpq -P %d %s '%s'"
return command % (self.port, " ".join(sources), dest)
def _make_rsync_compatible_globs(self, path, is_local):
""" Given an rsync-style path, returns a list of globbed paths
that will hopefully provide equivalent behaviour for scp. Does not
support the full range of rsync pattern matching behaviour, only that
exposed in the get/send_file interface (trailing slashes).
The is_local param is flag indicating if the paths should be
interpreted as local or remote paths. """
# non-trailing slash paths should just work
if len(path) == 0 or path[-1] != "/":
return [path]
# make a function to test if a pattern matches any files
if is_local:
def glob_matches_files(path):
return len(glob.glob(path)) > 0
else:
def glob_matches_files(path):
result = self.run("ls \"%s\"" % utils.sh_escape(path),
ignore_status=True)
return result.exit_status == 0
# take a set of globs that cover all files, and see which are needed
patterns = ["*", ".[!.]*"]
patterns = [p for p in patterns if glob_matches_files(path + p)]
# convert them into a set of paths suitable for the commandline
path = utils.sh_escape(path)
if is_local:
return ["\"%s\"%s" % (path, pattern) for pattern in patterns]
else:
return ["\"%s\"" % (path + pattern) for pattern in patterns]
def _make_rsync_compatible_source(self, source, is_local):
""" Applies the same logic as _make_rsync_compatible_globs, but
applies it to an entire list of sources, producing a new list of
sources, properly quoted. """
return sum((self._make_rsync_compatible_globs(path, is_local)
for path in source), [])
def _set_umask_perms(self, dest):
"""Given a destination file/dir (recursively) set the permissions on
all the files and directories to the max allowed by running umask."""
# now this looks strange but I haven't found a way in Python to _just_
# get the umask, apparently the only option is to try to set it
umask = os.umask(0)
os.umask(umask)
max_privs = 0777 & ~umask
def set_file_privs(filename):
file_stat = os.stat(filename)
file_privs = max_privs
# if the original file permissions do not have at least one
# executable bit then do not set it anywhere
if not file_stat.st_mode & 0111:
file_privs &= ~0111
os.chmod(filename, file_privs)
# try a bottom-up walk so changes on directory permissions won't cut
# our access to the files/directories inside it
for root, dirs, files in os.walk(dest, topdown=False):
# when setting the privileges we emulate the chmod "X" behaviour
# that sets to execute only if it is a directory or any of the
# owner/group/other already has execute right
for dirname in dirs:
os.chmod(os.path.join(root, dirname), max_privs)
for filename in files:
set_file_privs(os.path.join(root, filename))
# now set privs for the dest itself
if os.path.isdir(dest):
os.chmod(dest, max_privs)
else:
set_file_privs(dest)
def get_file(self, source, dest, delete_dest=False, preserve_perm=True,
preserve_symlinks=False):
"""
Copy files from the remote host to a local path.
Directories will be copied recursively.
If a source component is a directory with a trailing slash,
the content of the directory will be copied, otherwise, the
directory itself and its content will be copied. This
behavior is similar to that of the program 'rsync'.
Args:
source: either
1) a single file or directory, as a string
2) a list of one or more (possibly mixed)
files or directories
dest: a file or a directory (if source contains a
directory or more than one element, you must
supply a directory dest)
delete_dest: if this is true, the command will also clear
out any old files at dest that are not in the
source
preserve_perm: tells get_file() to try to preserve the sources
permissions on files and dirs
preserve_symlinks: try to preserve symlinks instead of
transforming them into files/dirs on copy
Raises:
AutoservRunError: the scp command failed
"""
if isinstance(source, basestring):
source = [source]
dest = os.path.abspath(dest)
try:
remote_source = self._encode_remote_paths(source)
local_dest = utils.sh_escape(dest)
rsync = self._make_rsync_cmd([remote_source], local_dest,
delete_dest, preserve_symlinks)
utils.run(rsync)
except error.CmdError, e:
logging.warn("warning: rsync failed with: %s", e)
logging.info("attempting to copy with scp instead")
# scp has no equivalent to --delete, just drop the entire dest dir
if delete_dest and os.path.isdir(dest):
shutil.rmtree(dest)
os.mkdir(dest)
remote_source = self._make_rsync_compatible_source(source, False)
if remote_source:
remote_source = self._encode_remote_paths(remote_source)
local_dest = utils.sh_escape(dest)
scp = self._make_scp_cmd([remote_source], local_dest)
try:
utils.run(scp)
except error.CmdError, e:
raise error.AutoservRunError(e.args[0], e.args[1])
if not preserve_perm:
# we have no way to tell scp to not try to preserve the
# permissions so set them after copy instead.
# for rsync we could use "--no-p --chmod=ugo=rwX" but those
# options are only in very recent rsync versions
self._set_umask_perms(dest)
def send_file(self, source, dest, delete_dest=False,
preserve_symlinks=False):
"""
Copy files from a local path to the remote host.
Directories will be copied recursively.
If a source component is a directory with a trailing slash,
the content of the directory will be copied, otherwise, the
directory itself and its content will be copied. This
behavior is similar to that of the program 'rsync'.
Args:
source: either
1) a single file or directory, as a string
2) a list of one or more (possibly mixed)
files or directories
dest: a file or a directory (if source contains a
directory or more than one element, you must
supply a directory dest)
delete_dest: if this is true, the command will also clear
out any old files at dest that are not in the
source
preserve_symlinks: controls if symlinks on the source will be
copied as such on the destination or transformed into the
referenced file/directory
Raises:
AutoservRunError: the scp command failed
"""
if isinstance(source, basestring):
source = [source]
remote_dest = self._encode_remote_paths([dest])
try:
local_sources = [utils.sh_escape(path) for path in source]
rsync = self._make_rsync_cmd(local_sources, remote_dest,
delete_dest, preserve_symlinks)
utils.run(rsync)
except error.CmdError, e:
logging.warn("warning: rsync failed with: %s", e)
logging.info("attempting to copy with scp instead")
# scp has no equivalent to --delete, just drop the entire dest dir
if delete_dest:
is_dir = self.run("ls -d %s/" % remote_dest,
ignore_status=True).exit_status == 0
if is_dir:
cmd = "rm -rf %s && mkdir %s"
cmd %= (remote_dest, remote_dest)
self.run(cmd)
local_sources = self._make_rsync_compatible_source(source, True)
if local_sources:
scp = self._make_scp_cmd(local_sources, remote_dest)
try:
utils.run(scp)
except error.CmdError, e:
raise error.AutoservRunError(e.args[0], e.args[1])
self.run('find "%s" -type d -print0 | xargs -0r chmod o+rx' % dest)
self.run('find "%s" -type f -print0 | xargs -0r chmod o+r' % dest)
if self.target_file_owner:
self.run('chown -R %s %s' % (self.target_file_owner, dest))
def ssh_ping(self, timeout=60):
try:
self.run("true", timeout=timeout, connect_timeout=timeout)
logging.info("ssh_ping of %s completed sucessfully", self.hostname)
except error.AutoservSSHTimeout:
msg = "ssh ping timed out (timeout = %d)" % timeout
raise error.AutoservSSHTimeout(msg)
except error.AutoservSshPermissionDeniedError:
#let AutoservSshPermissionDeniedError be visible to the callers
raise
except error.AutoservRunError, e:
# convert the generic AutoservRunError into something more
# specific for this context
raise error.AutoservSshPingHostError(e.description + '\n' +
repr(e.result_obj))
def is_up(self):
"""
Check if the remote host is up.
Returns:
True if the remote host is up, False otherwise
"""
try:
self.ssh_ping()
except error.AutoservError:
return False
else:
return True
def wait_up(self, timeout=None):
"""
Wait until the remote host is up or the timeout expires.
In fact, it will wait until an ssh connection to the remote
host can be established, and getty is running.
Args:
timeout: time limit in seconds before returning even
if the host is not up.
Returns:
True if the host was found to be up, False otherwise
"""
if timeout:
end_time = time.time() + timeout
while not timeout or time.time() < end_time:
if self.is_up():
try:
if self.are_wait_up_processes_up():
return True
except error.AutoservError:
pass
time.sleep(1)
return False
def wait_down(self, timeout=None, warning_timer=None):
"""
Wait until the remote host is down or the timeout expires.
In fact, it will wait until an ssh connection to the remote
host fails.
Args:
timeout: time limit in seconds before returning even
if the host is still up.
warning_timer: time limit in seconds that will generate
a warning if the host is not down yet.
Returns:
True if the host was found to be down, False otherwise
"""
current_time = time.time()
if timeout:
end_time = current_time + timeout
if warning_timer:
warn_time = current_time + warning_timer
while not timeout or current_time < end_time:
if not self.is_up():
return True
if warning_timer and current_time > warn_time:
self.record("WARN", None, "shutdown",
"Shutdown took longer than %ds" % warning_timer)
# Print the warning only once.
warning_timer = None
# If a machine is stuck switching runlevels
# This may cause the machine to reboot.
self.run('kill -HUP 1', ignore_status=True)
time.sleep(1)
current_time = time.time()
return False
# tunable constants for the verify & repair code
AUTOTEST_GB_DISKSPACE_REQUIRED = 20
def verify(self):
super(AbstractSSHHost, self).verify_hardware()
logging.info('Pinging host ' + self.hostname)
self.ssh_ping()
if self.is_shutting_down():
raise error.AutoservHostIsShuttingDownError("Host is shutting down")
super(AbstractSSHHost, self).verify_software()
try:
autodir = autotest._get_autodir(self)
if autodir:
self.check_diskspace(autodir,
self.AUTOTEST_GB_DISKSPACE_REQUIRED)
except error.AutoservHostError:
raise # only want to raise if it's a space issue
except Exception:
pass # autotest dir may not exist, etc. ignore
def _get_stream_tee_file(self, stream, level, verbose):
if stream is not TEE_TO_LOGS:
return stream
if not verbose:
return None
return logging_manager.LoggingFile(level=level)
|