mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 2 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 3 | import re, os, sys, traceback, subprocess, time, pickle, glob, tempfile |
| 4 | import logging, getpass |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 5 | from autotest_lib.server import installable_object, prebuild, utils |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import base_job, log, error, autotemp |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import global_config, packages |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import utils as client_utils |
mbligh | 3c7a150 | 2008-07-24 18:08:47 +0000 | [diff] [blame] | 9 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 10 | AUTOTEST_SVN = 'svn://test.kernel.org/autotest/trunk/client' |
| 11 | AUTOTEST_HTTP = 'http://test.kernel.org/svn/autotest/trunk/client' |
| 12 | |
| 13 | # Timeouts for powering down and up respectively |
| 14 | HALT_TIME = 300 |
mbligh | 07c1eac | 2007-11-05 18:39:29 +0000 | [diff] [blame] | 15 | BOOT_TIME = 1800 |
jadmanski | ec85914 | 2008-05-29 21:33:39 +0000 | [diff] [blame] | 16 | CRASH_RECOVERY_TIME = 9000 |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 17 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 18 | |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 19 | get_value = global_config.global_config.get_config_value |
| 20 | autoserv_prebuild = get_value('AUTOSERV', 'enable_server_prebuild', |
| 21 | type=bool, default=False) |
| 22 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 23 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 24 | class AutodirNotFoundError(Exception): |
| 25 | """No Autotest installation could be found.""" |
| 26 | |
| 27 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 28 | class BaseAutotest(installable_object.InstallableObject): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 29 | """ |
| 30 | This class represents the Autotest program. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 31 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 32 | Autotest is used to run tests automatically and collect the results. |
| 33 | It also supports profilers. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 34 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 35 | Implementation details: |
| 36 | This is a leaf class in an abstract class hierarchy, it must |
| 37 | implement the unimplemented methods in parent classes. |
| 38 | """ |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 39 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 40 | def __init__(self, host = None): |
| 41 | self.host = host |
| 42 | self.got = False |
| 43 | self.installed = False |
| 44 | self.serverdir = utils.get_server_dir() |
| 45 | super(BaseAutotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 46 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 47 | |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 48 | install_in_tmpdir = False |
| 49 | @classmethod |
| 50 | def set_install_in_tmpdir(cls, flag): |
| 51 | """ Sets a flag that controls whether or not Autotest should by |
| 52 | default be installed in a "standard" directory (e.g. |
| 53 | /home/autotest, /usr/local/autotest) or a temporary directory. """ |
| 54 | cls.install_in_tmpdir = flag |
| 55 | |
| 56 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 57 | @classmethod |
| 58 | def get_client_autodir_paths(cls, host): |
| 59 | return global_config.global_config.get_config_value( |
| 60 | 'AUTOSERV', 'client_autodir_paths', type=list) |
| 61 | |
| 62 | |
| 63 | @classmethod |
| 64 | def get_installed_autodir(cls, host): |
| 65 | """ |
| 66 | Find where the Autotest client is installed on the host. |
| 67 | @returns an absolute path to an installed Autotest client root. |
| 68 | @raises AutodirNotFoundError if no Autotest installation can be found. |
| 69 | """ |
| 70 | autodir = host.get_autodir() |
| 71 | if autodir: |
| 72 | logging.debug('Using existing host autodir: %s', autodir) |
| 73 | return autodir |
| 74 | |
| 75 | for path in Autotest.get_client_autodir_paths(host): |
| 76 | try: |
| 77 | autotest_binary = os.path.join(path, 'bin', 'autotest') |
| 78 | host.run('test -x %s' % utils.sh_escape(autotest_binary)) |
| 79 | logging.debug('Found existing autodir at %s', path) |
| 80 | return path |
| 81 | except error.AutoservRunError: |
| 82 | logging.debug('%s does not exist on %s', autotest_binary, |
| 83 | host.hostname) |
| 84 | raise AutodirNotFoundError |
| 85 | |
| 86 | |
| 87 | @classmethod |
| 88 | def get_install_dir(cls, host): |
| 89 | """ |
| 90 | Determines the location where autotest should be installed on |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 91 | host. If self.install_in_tmpdir is set, it will return a unique |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 92 | temporary directory that autotest can be installed in. Otherwise, looks |
| 93 | for an existing installation to use; if none is found, looks for a |
| 94 | usable directory in the global config client_autodir_paths. |
| 95 | """ |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 96 | try: |
lmr | 9dcf083 | 2009-12-08 21:28:55 +0000 | [diff] [blame] | 97 | install_dir = cls.get_installed_autodir(host) |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 98 | except AutodirNotFoundError: |
lmr | 9dcf083 | 2009-12-08 21:28:55 +0000 | [diff] [blame] | 99 | install_dir = cls._find_installable_dir(host) |
| 100 | |
| 101 | if cls.install_in_tmpdir: |
| 102 | return host.get_tmp_dir(parent=install_dir) |
| 103 | return install_dir |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | @classmethod |
| 107 | def _find_installable_dir(cls, host): |
| 108 | client_autodir_paths = cls.get_client_autodir_paths(host) |
| 109 | for path in client_autodir_paths: |
| 110 | try: |
| 111 | host.run('mkdir -p %s' % utils.sh_escape(path)) |
| 112 | return path |
| 113 | except error.AutoservRunError: |
| 114 | logging.debug('Failed to create %s', path) |
| 115 | raise error.AutoservInstallError( |
| 116 | 'Unable to find a place to install Autotest; tried %s', |
| 117 | ', '.join(client_autodir_paths)) |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 118 | |
| 119 | |
mbligh | 1b3b376 | 2008-09-25 02:46:34 +0000 | [diff] [blame] | 120 | @log.record |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 121 | def install(self, host=None, autodir=None): |
| 122 | self._install(host=host, autodir=autodir) |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 123 | |
| 124 | |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 125 | @log.record |
| 126 | def install_full_client(self, host=None, autodir=None): |
| 127 | self._install(host=host, autodir=autodir, use_autoserv=False, |
| 128 | use_packaging=False) |
| 129 | |
| 130 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 131 | def install_no_autoserv(self, host=None, autodir=None): |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 132 | self._install(host=host, autodir=autodir, use_autoserv=False) |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 133 | |
| 134 | |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 135 | def _install_using_packaging(self, host, autodir): |
| 136 | c = global_config.global_config |
jadmanski | 2315a7e | 2009-09-18 18:39:37 +0000 | [diff] [blame] | 137 | repos = c.get_config_value("PACKAGES", 'fetch_location', type=list, |
| 138 | default=[]) |
| 139 | repos.reverse() |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 140 | if not repos: |
| 141 | raise error.PackageInstallError("No repos to install an " |
| 142 | "autotest client from") |
| 143 | pkgmgr = packages.PackageManager(autodir, hostname=host.hostname, |
| 144 | repo_urls=repos, |
| 145 | do_locking=False, |
| 146 | run_function=host.run, |
| 147 | run_function_dargs=dict(timeout=600)) |
| 148 | # The packages dir is used to store all the packages that |
| 149 | # are fetched on that client. (for the tests,deps etc. |
| 150 | # too apart from the client) |
| 151 | pkg_dir = os.path.join(autodir, 'packages') |
| 152 | # clean up the autodir except for the packages directory |
| 153 | host.run('cd %s && ls | grep -v "^packages$"' |
| 154 | ' | xargs rm -rf && rm -rf .[^.]*' % autodir) |
| 155 | pkgmgr.install_pkg('autotest', 'client', pkg_dir, autodir, |
| 156 | preserve_install_dir=True) |
| 157 | self.installed = True |
| 158 | |
| 159 | |
| 160 | def _install_using_send_file(self, host, autodir): |
| 161 | dirs_to_exclude = set(["tests", "site_tests", "deps", "profilers"]) |
| 162 | light_files = [os.path.join(self.source_material, f) |
| 163 | for f in os.listdir(self.source_material) |
| 164 | if f not in dirs_to_exclude] |
| 165 | host.send_file(light_files, autodir, delete_dest=True) |
| 166 | |
| 167 | # create empty dirs for all the stuff we excluded |
| 168 | commands = [] |
| 169 | for path in dirs_to_exclude: |
| 170 | abs_path = os.path.join(autodir, path) |
| 171 | abs_path = utils.sh_escape(abs_path) |
| 172 | commands.append("mkdir -p '%s'" % abs_path) |
| 173 | commands.append("touch '%s'/__init__.py" % abs_path) |
| 174 | host.run(';'.join(commands)) |
| 175 | |
| 176 | |
| 177 | def _install(self, host=None, autodir=None, use_autoserv=True, |
| 178 | use_packaging=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 179 | """ |
| 180 | Install autotest. If get() was not called previously, an |
| 181 | attempt will be made to install from the autotest svn |
| 182 | repository. |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 183 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 184 | @param host A Host instance on which autotest will be installed |
| 185 | @param autodir Location on the remote host to install to |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 186 | @param use_autoserv Enable install modes that depend on the client |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 187 | running with the autoserv harness |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 188 | @param use_packaging Enable install modes that use the packaging system |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 189 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 190 | @exception AutoservError if a tarball was not specified and |
| 191 | the target host does not have svn installed in its path |
| 192 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 193 | if not host: |
| 194 | host = self.host |
| 195 | if not self.got: |
| 196 | self.get() |
| 197 | host.wait_up(timeout=30) |
| 198 | host.setup() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 199 | logging.info("Installing autotest on %s", host.hostname) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 200 | |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 201 | # set up the autotest directory on the remote machine |
| 202 | if not autodir: |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 203 | autodir = self.get_install_dir(host) |
| 204 | logging.info('Using installation dir %s', autodir) |
mbligh | 0562e65 | 2008-08-20 20:11:45 +0000 | [diff] [blame] | 205 | host.set_autodir(autodir) |
jadmanski | 3c23694 | 2009-03-04 17:51:26 +0000 | [diff] [blame] | 206 | host.run('mkdir -p %s' % utils.sh_escape(autodir)) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 207 | |
jadmanski | 1c3c07b | 2009-03-03 23:29:36 +0000 | [diff] [blame] | 208 | # make sure there are no files in $AUTODIR/results |
| 209 | results_path = os.path.join(autodir, 'results') |
jadmanski | 3c23694 | 2009-03-04 17:51:26 +0000 | [diff] [blame] | 210 | host.run('rm -rf %s/*' % utils.sh_escape(results_path), |
jadmanski | 1c3c07b | 2009-03-03 23:29:36 +0000 | [diff] [blame] | 211 | ignore_status=True) |
| 212 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 213 | # Fetch the autotest client from the nearest repository |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 214 | if use_packaging: |
| 215 | try: |
| 216 | self._install_using_packaging(host, autodir) |
| 217 | return |
| 218 | except global_config.ConfigError, e: |
| 219 | logging.info("Could not install autotest using the packaging " |
| 220 | "system: %s", e) |
| 221 | except (error.PackageInstallError, error.AutoservRunError), e: |
| 222 | logging.error("Could not install autotest from repos") |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 223 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 224 | # try to install from file or directory |
| 225 | if self.source_material: |
| 226 | if os.path.isdir(self.source_material): |
jadmanski | 27b5291 | 2009-08-14 17:09:15 +0000 | [diff] [blame] | 227 | c = global_config.global_config |
| 228 | supports_autoserv_packaging = c.get_config_value( |
| 229 | "PACKAGES", "serve_packages_from_autoserv", type=bool) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 230 | # Copy autotest recursively |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 231 | if supports_autoserv_packaging and use_autoserv: |
| 232 | self._install_using_send_file(host, autodir) |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 233 | else: |
mbligh | 89e258d | 2008-10-24 13:58:08 +0000 | [diff] [blame] | 234 | host.send_file(self.source_material, autodir, |
| 235 | delete_dest=True) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 236 | else: |
| 237 | # Copy autotest via tarball |
| 238 | e_msg = 'Installation method not yet implemented!' |
| 239 | raise NotImplementedError(e_msg) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 240 | logging.info("Installation of autotest completed") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 241 | self.installed = True |
| 242 | return |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 243 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 244 | # if that fails try to install using svn |
| 245 | if utils.run('which svn').exit_status: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 246 | raise error.AutoservError('svn not found on target machine: %s' |
| 247 | % host.name) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 248 | try: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 249 | host.run('svn checkout %s %s' % (AUTOTEST_SVN, autodir)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 250 | except error.AutoservRunError, e: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 251 | host.run('svn checkout %s %s' % (AUTOTEST_HTTP, autodir)) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 252 | logging.info("Installation of autotest completed") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 253 | self.installed = True |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 254 | |
| 255 | |
jadmanski | 7c7aff3 | 2009-03-25 22:43:07 +0000 | [diff] [blame] | 256 | def uninstall(self, host=None): |
| 257 | """ |
| 258 | Uninstall (i.e. delete) autotest. Removes the autotest client install |
| 259 | from the specified host. |
| 260 | |
| 261 | @params host a Host instance from which the client will be removed |
| 262 | """ |
| 263 | if not self.installed: |
| 264 | return |
| 265 | if not host: |
| 266 | host = self.host |
| 267 | autodir = host.get_autodir() |
| 268 | if not autodir: |
| 269 | return |
| 270 | |
| 271 | # perform the actual uninstall |
| 272 | host.run("rm -rf %s" % utils.sh_escape(autodir), ignore_status=True) |
| 273 | host.set_autodir(None) |
| 274 | self.installed = False |
| 275 | |
| 276 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 277 | def get(self, location = None): |
| 278 | if not location: |
| 279 | location = os.path.join(self.serverdir, '../client') |
| 280 | location = os.path.abspath(location) |
| 281 | # If there's stuff run on our client directory already, it |
| 282 | # can cause problems. Try giving it a quick clean first. |
| 283 | cwd = os.getcwd() |
| 284 | os.chdir(location) |
showard | 4b97607 | 2009-10-20 23:50:08 +0000 | [diff] [blame] | 285 | try: |
| 286 | utils.system('tools/make_clean', ignore_status=True) |
| 287 | finally: |
| 288 | os.chdir(cwd) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 289 | super(BaseAutotest, self).get(location) |
| 290 | self.got = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 291 | |
| 292 | |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 293 | def run(self, control_file, results_dir='.', host=None, timeout=None, |
| 294 | tag=None, parallel_flag=False, background=False, |
mbligh | 9de6ed7 | 2010-01-11 19:01:10 +0000 | [diff] [blame] | 295 | client_disconnect_timeout=1800): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 296 | """ |
| 297 | Run an autotest job on the remote machine. |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 298 | |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 299 | @param control_file: An open file-like-obj of the control file. |
| 300 | @param results_dir: A str path where the results should be stored |
| 301 | on the local filesystem. |
| 302 | @param host: A Host instance on which the control file should |
| 303 | be run. |
| 304 | @param timeout: Maximum number of seconds to wait for the run or None. |
| 305 | @param tag: Tag name for the client side instance of autotest. |
| 306 | @param parallel_flag: Flag set when multiple jobs are run at the |
| 307 | same time. |
| 308 | @param background: Indicates that the client should be launched as |
| 309 | a background job; the code calling run will be responsible |
| 310 | for monitoring the client and collecting the results. |
| 311 | @param client_disconnect_timeout: Seconds to wait for the remote host |
| 312 | to come back after a reboot. [default: 30 minutes] |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 313 | |
| 314 | @raises AutotestRunError: If there is a problem executing |
| 315 | the control file. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 316 | """ |
| 317 | host = self._get_host_and_setup(host) |
| 318 | results_dir = os.path.abspath(results_dir) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 319 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 320 | if tag: |
| 321 | results_dir = os.path.join(results_dir, tag) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 322 | |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 323 | atrun = _Run(host, results_dir, tag, parallel_flag, background) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 324 | self._do_run(control_file, results_dir, host, atrun, timeout, |
mbligh | 9de6ed7 | 2010-01-11 19:01:10 +0000 | [diff] [blame] | 325 | client_disconnect_timeout) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 326 | |
| 327 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 328 | def _get_host_and_setup(self, host): |
| 329 | if not host: |
| 330 | host = self.host |
| 331 | if not self.installed: |
| 332 | self.install(host) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 333 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 334 | host.wait_up(timeout=30) |
| 335 | return host |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 336 | |
| 337 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 338 | def _do_run(self, control_file, results_dir, host, atrun, timeout, |
mbligh | 9de6ed7 | 2010-01-11 19:01:10 +0000 | [diff] [blame] | 339 | client_disconnect_timeout): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 340 | try: |
| 341 | atrun.verify_machine() |
| 342 | except: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 343 | logging.error("Verify failed on %s. Reinstalling autotest", |
| 344 | host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 345 | self.install(host) |
| 346 | atrun.verify_machine() |
| 347 | debug = os.path.join(results_dir, 'debug') |
| 348 | try: |
| 349 | os.makedirs(debug) |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 350 | except Exception: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 351 | pass |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 352 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 353 | delete_file_list = [atrun.remote_control_file, |
| 354 | atrun.remote_control_file + '.state', |
| 355 | atrun.manual_control_file, |
| 356 | atrun.manual_control_file + '.state'] |
| 357 | cmd = ';'.join('rm -f ' + control for control in delete_file_list) |
| 358 | host.run(cmd, ignore_status=True) |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 359 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 360 | tmppath = utils.get(control_file) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 361 | |
jadmanski | cb0e161 | 2009-02-27 18:03:10 +0000 | [diff] [blame] | 362 | # build up the initialization prologue for the control file |
| 363 | prologue_lines = [] |
jadmanski | 23afbec | 2008-09-17 18:12:07 +0000 | [diff] [blame] | 364 | |
mbligh | 2f07683 | 2010-03-30 17:08:20 +0000 | [diff] [blame] | 365 | # Add the additional user arguments |
jadmanski | 808f4b1 | 2010-04-09 22:30:31 +0000 | [diff] [blame] | 366 | prologue_lines.append("args = %r\n" % self.job.args) |
mbligh | 2f07683 | 2010-03-30 17:08:20 +0000 | [diff] [blame] | 367 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 368 | # If the packaging system is being used, add the repository list. |
mbligh | ddc9a40 | 2010-01-15 20:33:34 +0000 | [diff] [blame] | 369 | repos = None |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 370 | try: |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 371 | c = global_config.global_config |
| 372 | repos = c.get_config_value("PACKAGES", 'fetch_location', type=list) |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 373 | repos.reverse() # high priority packages should be added last |
mbligh | 76d19f7 | 2008-10-15 16:24:43 +0000 | [diff] [blame] | 374 | pkgmgr = packages.PackageManager('autotest', hostname=host.hostname, |
| 375 | repo_urls=repos) |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 376 | prologue_lines.append('job.add_repository(%s)\n' % repos) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 377 | except global_config.ConfigError, e: |
mbligh | ddc9a40 | 2010-01-15 20:33:34 +0000 | [diff] [blame] | 378 | # If repos is defined packaging is enabled so log the error |
| 379 | if repos: |
| 380 | logging.error(e) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 381 | |
jadmanski | e2eef7b | 2009-03-03 23:55:13 +0000 | [diff] [blame] | 382 | # on full-size installs, turn on any profilers the server is using |
jadmanski | 27b5291 | 2009-08-14 17:09:15 +0000 | [diff] [blame] | 383 | if not atrun.background: |
jadmanski | e2eef7b | 2009-03-03 23:55:13 +0000 | [diff] [blame] | 384 | running_profilers = host.job.profilers.add_log.iteritems() |
| 385 | for profiler, (args, dargs) in running_profilers: |
| 386 | call_args = [repr(profiler)] |
| 387 | call_args += [repr(arg) for arg in args] |
| 388 | call_args += ["%s=%r" % item for item in dargs.iteritems()] |
| 389 | prologue_lines.append("job.profilers.add(%s)\n" |
| 390 | % ", ".join(call_args)) |
| 391 | cfile = "".join(prologue_lines) |
| 392 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 393 | cfile += open(tmppath).read() |
| 394 | open(tmppath, "w").write(cfile) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 395 | |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 396 | # Create and copy state file to remote_control_file + '.state' |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 397 | state_file = host.job.preprocess_client_state() |
mbligh | fbf73ae | 2009-12-19 05:22:42 +0000 | [diff] [blame] | 398 | host.send_file(state_file, atrun.remote_control_file + '.init.state') |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 399 | os.remove(state_file) |
| 400 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 401 | # Copy control_file to remote_control_file on the host |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 402 | host.send_file(tmppath, atrun.remote_control_file) |
| 403 | if os.path.abspath(tmppath) != os.path.abspath(control_file): |
| 404 | os.remove(tmppath) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 405 | |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 406 | atrun.execute_control( |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 407 | timeout=timeout, |
| 408 | client_disconnect_timeout=client_disconnect_timeout) |
jadmanski | 23afbec | 2008-09-17 18:12:07 +0000 | [diff] [blame] | 409 | |
| 410 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 411 | def run_timed_test(self, test_name, results_dir='.', host=None, |
jadmanski | c98c470 | 2009-01-05 15:50:06 +0000 | [diff] [blame] | 412 | timeout=None, *args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 413 | """ |
| 414 | Assemble a tiny little control file to just run one test, |
| 415 | and run it as an autotest client-side test |
| 416 | """ |
| 417 | if not host: |
| 418 | host = self.host |
| 419 | if not self.installed: |
| 420 | self.install(host) |
| 421 | opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()] |
| 422 | cmd = ", ".join([repr(test_name)] + map(repr, args) + opts) |
| 423 | control = "job.run_test(%s)\n" % cmd |
jadmanski | c98c470 | 2009-01-05 15:50:06 +0000 | [diff] [blame] | 424 | self.run(control, results_dir, host, timeout=timeout) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 425 | |
| 426 | |
jadmanski | c98c470 | 2009-01-05 15:50:06 +0000 | [diff] [blame] | 427 | def run_test(self, test_name, results_dir='.', host=None, *args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 428 | self.run_timed_test(test_name, results_dir, host, timeout=None, |
jadmanski | c98c470 | 2009-01-05 15:50:06 +0000 | [diff] [blame] | 429 | *args, **dargs) |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 430 | |
| 431 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 432 | class _Run(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 433 | """ |
| 434 | Represents a run of autotest control file. This class maintains |
| 435 | all the state necessary as an autotest control file is executed. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 436 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 437 | It is not intended to be used directly, rather control files |
| 438 | should be run using the run method in Autotest. |
| 439 | """ |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 440 | def __init__(self, host, results_dir, tag, parallel_flag, background): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 441 | self.host = host |
| 442 | self.results_dir = results_dir |
| 443 | self.env = host.env |
| 444 | self.tag = tag |
| 445 | self.parallel_flag = parallel_flag |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 446 | self.background = background |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 447 | self.autodir = Autotest.get_installed_autodir(self.host) |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 448 | control = os.path.join(self.autodir, 'control') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 449 | if tag: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 450 | control += '.' + tag |
| 451 | self.manual_control_file = control |
| 452 | self.remote_control_file = control + '.autoserv' |
lmr | 6d08b3c | 2009-11-18 19:26:38 +0000 | [diff] [blame] | 453 | self.config_file = os.path.join(self.autodir, 'global_config.ini') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 454 | |
| 455 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 456 | def verify_machine(self): |
| 457 | binary = os.path.join(self.autodir, 'bin/autotest') |
| 458 | try: |
| 459 | self.host.run('ls %s > /dev/null 2>&1' % binary) |
| 460 | except: |
lmr | d6d27ed | 2009-12-08 19:58:33 +0000 | [diff] [blame] | 461 | raise error.AutoservInstallError( |
| 462 | "Autotest does not appear to be installed") |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 463 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 464 | if not self.parallel_flag: |
| 465 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 466 | download = os.path.join(self.autodir, 'tests/download') |
| 467 | self.host.run('umount %s' % tmpdir, ignore_status=True) |
| 468 | self.host.run('umount %s' % download, ignore_status=True) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 469 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 470 | |
| 471 | def get_base_cmd_args(self, section): |
showard | 234b296 | 2009-07-28 20:02:30 +0000 | [diff] [blame] | 472 | args = ['--verbose'] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 473 | if section > 0: |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 474 | args.append('-c') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 475 | if self.tag: |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 476 | args.append('-t %s' % self.tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 477 | if self.host.job.use_external_logging(): |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 478 | args.append('-l') |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 479 | if self.host.hostname: |
| 480 | args.append('--hostname=%s' % self.host.hostname) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 481 | args.append('--user=%s' % self.host.job.user) |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 482 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 483 | args.append(self.remote_control_file) |
| 484 | return args |
| 485 | |
| 486 | |
| 487 | def get_background_cmd(self, section): |
| 488 | cmd = ['nohup', os.path.join(self.autodir, 'bin/autotest_client')] |
| 489 | cmd += self.get_base_cmd_args(section) |
| 490 | cmd.append('>/dev/null 2>/dev/null &') |
| 491 | return ' '.join(cmd) |
| 492 | |
| 493 | |
| 494 | def get_daemon_cmd(self, section, monitor_dir): |
| 495 | cmd = ['nohup', os.path.join(self.autodir, 'bin/autotestd'), |
| 496 | monitor_dir, '-H autoserv'] |
| 497 | cmd += self.get_base_cmd_args(section) |
| 498 | cmd.append('>/dev/null 2>/dev/null </dev/null &') |
| 499 | return ' '.join(cmd) |
| 500 | |
| 501 | |
| 502 | def get_monitor_cmd(self, monitor_dir, stdout_read, stderr_read): |
| 503 | cmd = [os.path.join(self.autodir, 'bin', 'autotestd_monitor'), |
| 504 | monitor_dir, str(stdout_read), str(stderr_read)] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 505 | return ' '.join(cmd) |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 506 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 507 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 508 | def get_client_log(self): |
| 509 | """Find what the "next" client.* prefix should be |
| 510 | |
| 511 | @returns A string of the form client.INTEGER that should be prefixed |
| 512 | to all client debug log files. |
| 513 | """ |
| 514 | max_digit = -1 |
| 515 | debug_dir = os.path.join(self.results_dir, 'debug') |
| 516 | client_logs = glob.glob(os.path.join(debug_dir, 'client.*.*')) |
| 517 | for log in client_logs: |
| 518 | _, number, _ = log.split('.', 2) |
| 519 | if number.isdigit(): |
| 520 | max_digit = max(max_digit, int(number)) |
| 521 | return 'client.%d' % (max_digit + 1) |
| 522 | |
| 523 | |
| 524 | def copy_client_config_file(self, client_log_prefix=None): |
| 525 | """ |
| 526 | Create and copy the client config file based on the server config. |
| 527 | |
| 528 | @param client_log_prefix: Optional prefix to prepend to log files. |
| 529 | """ |
| 530 | client_config_file = self._create_client_config_file(client_log_prefix) |
| 531 | self.host.send_file(client_config_file, self.config_file) |
| 532 | os.remove(client_config_file) |
| 533 | |
| 534 | |
| 535 | def _create_client_config_file(self, client_log_prefix=None): |
| 536 | """ |
| 537 | Create a temporary file with the [CLIENT] section configuration values |
| 538 | taken from the server global_config.ini. |
| 539 | |
| 540 | @param client_log_prefix: Optional prefix to prepend to log files. |
| 541 | |
| 542 | @return: Path of the temporary file generated. |
| 543 | """ |
| 544 | config = global_config.global_config.get_section_values('CLIENT') |
| 545 | if client_log_prefix: |
| 546 | config.set('CLIENT', 'default_logging_name', client_log_prefix) |
| 547 | return self._create_aux_file(config.write) |
| 548 | |
| 549 | |
| 550 | def _create_aux_file(self, func, *args): |
| 551 | """ |
| 552 | Creates a temporary file and writes content to it according to a |
| 553 | content creation function. The file object is appended to *args, which |
| 554 | is then passed to the content creation function |
| 555 | |
| 556 | @param func: Function that will be used to write content to the |
| 557 | temporary file. |
| 558 | @param *args: List of parameters that func takes. |
| 559 | @return: Path to the temporary file that was created. |
| 560 | """ |
| 561 | fd, path = tempfile.mkstemp(dir=self.host.job.tmpdir) |
| 562 | aux_file = os.fdopen(fd, "w") |
| 563 | try: |
| 564 | list_args = list(args) |
| 565 | list_args.append(aux_file) |
| 566 | func(*list_args) |
| 567 | finally: |
| 568 | aux_file.close() |
| 569 | return path |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 570 | |
| 571 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 572 | @staticmethod |
| 573 | def is_client_job_finished(last_line): |
| 574 | return bool(re.match(r'^END .*\t----\t----\t.*$', last_line)) |
| 575 | |
| 576 | |
| 577 | @staticmethod |
| 578 | def is_client_job_rebooting(last_line): |
| 579 | return bool(re.match(r'^\t*GOOD\t----\treboot\.start.*$', last_line)) |
| 580 | |
| 581 | |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 582 | def log_unexpected_abort(self, stderr_redirector): |
| 583 | stderr_redirector.flush_all_buffers() |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 584 | msg = "Autotest client terminated unexpectedly" |
| 585 | self.host.job.record("END ABORT", None, None, msg) |
| 586 | |
| 587 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 588 | def _execute_in_background(self, section, timeout): |
| 589 | full_cmd = self.get_background_cmd(section) |
| 590 | devnull = open(os.devnull, "w") |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 591 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 592 | self.copy_client_config_file(self.get_client_log()) |
| 593 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 594 | self.host.job.push_execution_context(self.results_dir) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 595 | try: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 596 | result = self.host.run(full_cmd, ignore_status=True, |
| 597 | timeout=timeout, |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 598 | stdout_tee=devnull, |
| 599 | stderr_tee=devnull) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 600 | finally: |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 601 | self.host.job.pop_execution_context() |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 602 | |
| 603 | return result |
| 604 | |
| 605 | |
| 606 | @staticmethod |
| 607 | def _strip_stderr_prologue(stderr): |
| 608 | """Strips the 'standard' prologue that get pre-pended to every |
| 609 | remote command and returns the text that was actually written to |
| 610 | stderr by the remote command.""" |
| 611 | stderr_lines = stderr.split("\n")[1:] |
| 612 | if not stderr_lines: |
| 613 | return "" |
| 614 | elif stderr_lines[0].startswith("NOTE: autotestd_monitor"): |
| 615 | del stderr_lines[0] |
| 616 | return "\n".join(stderr_lines) |
| 617 | |
| 618 | |
| 619 | def _execute_daemon(self, section, timeout, stderr_redirector, |
| 620 | client_disconnect_timeout): |
| 621 | monitor_dir = self.host.get_tmp_dir() |
| 622 | daemon_cmd = self.get_daemon_cmd(section, monitor_dir) |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 623 | |
| 624 | # grab the location for the server-side client log file |
| 625 | client_log_prefix = self.get_client_log() |
| 626 | client_log_path = os.path.join(self.results_dir, 'debug', |
| 627 | client_log_prefix + '.log') |
| 628 | client_log = open(client_log_path, 'w', 0) |
| 629 | self.copy_client_config_file(client_log_prefix) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 630 | |
| 631 | stdout_read = stderr_read = 0 |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 632 | self.host.job.push_execution_context(self.results_dir) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 633 | try: |
| 634 | self.host.run(daemon_cmd, ignore_status=True, timeout=timeout) |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 635 | disconnect_warnings = [] |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 636 | while True: |
| 637 | monitor_cmd = self.get_monitor_cmd(monitor_dir, stdout_read, |
| 638 | stderr_read) |
| 639 | try: |
| 640 | result = self.host.run(monitor_cmd, ignore_status=True, |
| 641 | timeout=timeout, |
| 642 | stdout_tee=client_log, |
| 643 | stderr_tee=stderr_redirector) |
| 644 | except error.AutoservRunError, e: |
| 645 | result = e.result_obj |
| 646 | result.exit_status = None |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 647 | disconnect_warnings.append(e.description) |
| 648 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 649 | stderr_redirector.log_warning( |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 650 | "Autotest client was disconnected: %s" % e.description, |
| 651 | "NETWORK") |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 652 | except error.AutoservSSHTimeout: |
| 653 | result = utils.CmdResult(monitor_cmd, "", "", None, 0) |
| 654 | stderr_redirector.log_warning( |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 655 | "Attempt to connect to Autotest client timed out", |
| 656 | "NETWORK") |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 657 | |
| 658 | stdout_read += len(result.stdout) |
| 659 | stderr_read += len(self._strip_stderr_prologue(result.stderr)) |
| 660 | |
| 661 | if result.exit_status is not None: |
| 662 | return result |
| 663 | elif not self.host.wait_up(client_disconnect_timeout): |
| 664 | raise error.AutoservSSHTimeout( |
| 665 | "client was disconnected, reconnect timed out") |
| 666 | finally: |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 667 | client_log.close() |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 668 | self.host.job.pop_execution_context() |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 669 | |
| 670 | |
| 671 | def execute_section(self, section, timeout, stderr_redirector, |
| 672 | client_disconnect_timeout): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 673 | logging.info("Executing %s/bin/autotest %s/control phase %d", |
| 674 | self.autodir, self.autodir, section) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 675 | |
| 676 | if self.background: |
| 677 | result = self._execute_in_background(section, timeout) |
| 678 | else: |
| 679 | result = self._execute_daemon(section, timeout, stderr_redirector, |
| 680 | client_disconnect_timeout) |
| 681 | |
| 682 | last_line = stderr_redirector.last_line |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 683 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 684 | # check if we failed hard enough to warrant an exception |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 685 | if result.exit_status == 1: |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 686 | err = error.AutotestRunError("client job was aborted") |
| 687 | elif not self.background and not result.stderr: |
| 688 | err = error.AutotestRunError( |
jadmanski | e413053 | 2009-03-17 18:01:28 +0000 | [diff] [blame] | 689 | "execute_section %s failed to return anything\n" |
| 690 | "stdout:%s\n" % (section, result.stdout)) |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 691 | else: |
| 692 | err = None |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 693 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 694 | # log something if the client failed AND never finished logging |
| 695 | if err and not self.is_client_job_finished(last_line): |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 696 | self.log_unexpected_abort(stderr_redirector) |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 697 | |
| 698 | if err: |
| 699 | raise err |
| 700 | else: |
| 701 | return stderr_redirector.last_line |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 702 | |
| 703 | |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 704 | def _wait_for_reboot(self, old_boot_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 705 | logging.info("Client is rebooting") |
| 706 | logging.info("Waiting for client to halt") |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 707 | if not self.host.wait_down(HALT_TIME, old_boot_id=old_boot_id): |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 708 | err = "%s failed to shutdown after %d" |
| 709 | err %= (self.host.hostname, HALT_TIME) |
| 710 | raise error.AutotestRunError(err) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 711 | logging.info("Client down, waiting for restart") |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 712 | if not self.host.wait_up(BOOT_TIME): |
| 713 | # since reboot failed |
| 714 | # hardreset the machine once if possible |
| 715 | # before failing this control file |
| 716 | warning = "%s did not come back up, hard resetting" |
| 717 | warning %= self.host.hostname |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 718 | logging.warning(warning) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 719 | try: |
| 720 | self.host.hardreset(wait=False) |
mbligh | d99d3b27 | 2008-12-22 14:41:27 +0000 | [diff] [blame] | 721 | except (AttributeError, error.AutoservUnsupportedError): |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 722 | warning = "Hard reset unsupported on %s" |
| 723 | warning %= self.host.hostname |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 724 | logging.warning(warning) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 725 | raise error.AutotestRunError("%s failed to boot after %ds" % |
| 726 | (self.host.hostname, BOOT_TIME)) |
| 727 | self.host.reboot_followup() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 728 | |
| 729 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 730 | def execute_control(self, timeout=None, client_disconnect_timeout=None): |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 731 | if not self.background: |
| 732 | collector = log_collector(self.host, self.tag, self.results_dir) |
| 733 | hostname = self.host.hostname |
| 734 | remote_results = collector.client_results_dir |
| 735 | local_results = collector.server_results_dir |
| 736 | self.host.job.add_client_log(hostname, remote_results, |
| 737 | local_results) |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame^] | 738 | job_record_context = self.host.job.get_record_context() |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 739 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 740 | section = 0 |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 741 | start_time = time.time() |
| 742 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 743 | logger = client_logger(self.host, self.tag, self.results_dir) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 744 | try: |
| 745 | while not timeout or time.time() < start_time + timeout: |
| 746 | if timeout: |
| 747 | section_timeout = start_time + timeout - time.time() |
| 748 | else: |
| 749 | section_timeout = None |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 750 | boot_id = self.host.get_boot_id() |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 751 | last = self.execute_section(section, section_timeout, |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 752 | logger, client_disconnect_timeout) |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 753 | if self.background: |
| 754 | return |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 755 | section += 1 |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 756 | if self.is_client_job_finished(last): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 757 | logging.info("Client complete") |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 758 | return |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 759 | elif self.is_client_job_rebooting(last): |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 760 | try: |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 761 | self._wait_for_reboot(boot_id) |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 762 | except error.AutotestRunError, e: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 763 | self.host.job.record("ABORT", None, "reboot", str(e)) |
| 764 | self.host.job.record("END ABORT", None, None, str(e)) |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 765 | raise |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 766 | continue |
| 767 | |
| 768 | # if we reach here, something unexpected happened |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 769 | self.log_unexpected_abort(logger) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 770 | |
| 771 | # give the client machine a chance to recover from a crash |
| 772 | self.host.wait_up(CRASH_RECOVERY_TIME) |
| 773 | msg = ("Aborting - unexpected final status message from " |
jadmanski | b1de549 | 2010-06-11 14:29:01 +0000 | [diff] [blame] | 774 | "client on %s: %s\n") % (self.host.hostname, last) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 775 | raise error.AutotestRunError(msg) |
| 776 | finally: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 777 | logger.close() |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 778 | if not self.background: |
| 779 | collector.collect_client_job_results() |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 780 | collector.remove_redundant_client_logs() |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 781 | state_file = os.path.basename(self.remote_control_file |
| 782 | + '.state') |
| 783 | state_path = os.path.join(self.results_dir, state_file) |
| 784 | self.host.job.postprocess_client_state(state_path) |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 785 | self.host.job.remove_client_log(hostname, remote_results, |
| 786 | local_results) |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame^] | 787 | job_record_context.restore() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 788 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 789 | # should only get here if we timed out |
| 790 | assert timeout |
| 791 | raise error.AutotestTimeoutError() |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 792 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 793 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 794 | class log_collector(object): |
| 795 | def __init__(self, host, client_tag, results_dir): |
| 796 | self.host = host |
| 797 | if not client_tag: |
| 798 | client_tag = "default" |
| 799 | self.client_results_dir = os.path.join(host.get_autodir(), "results", |
| 800 | client_tag) |
| 801 | self.server_results_dir = results_dir |
| 802 | |
| 803 | |
| 804 | def collect_client_job_results(self): |
| 805 | """ A method that collects all the current results of a running |
| 806 | client job into the results dir. By default does nothing as no |
| 807 | client job is running, but when running a client job you can override |
| 808 | this with something that will actually do something. """ |
| 809 | |
| 810 | # make an effort to wait for the machine to come up |
| 811 | try: |
| 812 | self.host.wait_up(timeout=30) |
| 813 | except error.AutoservError: |
| 814 | # don't worry about any errors, we'll try and |
| 815 | # get the results anyway |
| 816 | pass |
| 817 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 818 | # Copy all dirs in default to results_dir |
| 819 | try: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 820 | self.host.get_file(self.client_results_dir + '/', |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 821 | self.server_results_dir, preserve_symlinks=True) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 822 | except Exception: |
| 823 | # well, don't stop running just because we couldn't get logs |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 824 | e_msg = "Unexpected error copying test result logs, continuing ..." |
| 825 | logging.error(e_msg) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 826 | traceback.print_exc(file=sys.stdout) |
| 827 | |
| 828 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 829 | def remove_redundant_client_logs(self): |
| 830 | """Remove client.*.log files in favour of client.*.DEBUG files.""" |
| 831 | debug_dir = os.path.join(self.server_results_dir, 'debug') |
| 832 | debug_files = [f for f in os.listdir(debug_dir) |
| 833 | if re.search(r'^client\.\d+\.DEBUG$', f)] |
| 834 | for debug_file in debug_files: |
| 835 | log_file = debug_file.replace('DEBUG', 'log') |
| 836 | log_file = os.path.join(debug_dir, log_file) |
| 837 | if os.path.exists(log_file): |
| 838 | os.remove(log_file) |
| 839 | |
| 840 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 841 | # a file-like object for catching stderr from an autotest client and |
| 842 | # extracting status logs from it |
| 843 | class client_logger(object): |
| 844 | """Partial file object to write to both stdout and |
| 845 | the status log file. We only implement those methods |
| 846 | utils.run() actually calls. |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 847 | """ |
| 848 | status_parser = re.compile(r"^AUTOTEST_STATUS:([^:]*):(.*)$") |
| 849 | test_complete_parser = re.compile(r"^AUTOTEST_TEST_COMPLETE:(.*)$") |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 850 | fetch_package_parser = re.compile( |
| 851 | r"^AUTOTEST_FETCH_PACKAGE:([^:]*):([^:]*):(.*)$") |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 852 | extract_indent = re.compile(r"^(\t*).*$") |
jadmanski | efe4ebf | 2009-05-21 22:12:30 +0000 | [diff] [blame] | 853 | extract_timestamp = re.compile(r".*\ttimestamp=(\d+)\t.*$") |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 854 | |
| 855 | def __init__(self, host, tag, server_results_dir): |
| 856 | self.host = host |
| 857 | self.job = host.job |
| 858 | self.log_collector = log_collector(host, tag, server_results_dir) |
| 859 | self.leftover = "" |
| 860 | self.last_line = "" |
| 861 | self.logs = {} |
jadmanski | efe4ebf | 2009-05-21 22:12:30 +0000 | [diff] [blame] | 862 | |
| 863 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 864 | def _process_log_dict(self, log_dict): |
| 865 | log_list = log_dict.pop("logs", []) |
| 866 | for key in sorted(log_dict.iterkeys()): |
| 867 | log_list += self._process_log_dict(log_dict.pop(key)) |
| 868 | return log_list |
| 869 | |
| 870 | |
| 871 | def _process_logs(self): |
| 872 | """Go through the accumulated logs in self.log and print them |
| 873 | out to stdout and the status log. Note that this processes |
| 874 | logs in an ordering where: |
| 875 | |
| 876 | 1) logs to different tags are never interleaved |
| 877 | 2) logs to x.y come before logs to x.y.z for all z |
| 878 | 3) logs to x.y come before x.z whenever y < z |
| 879 | |
| 880 | Note that this will in general not be the same as the |
| 881 | chronological ordering of the logs. However, if a chronological |
| 882 | ordering is desired that one can be reconstructed from the |
| 883 | status log by looking at timestamp lines.""" |
| 884 | log_list = self._process_log_dict(self.logs) |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 885 | for entry in log_list: |
| 886 | self.job.record_entry(entry, log_in_subdir=False) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 887 | if log_list: |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 888 | self.last_line = log_list[-1].render() |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 889 | |
| 890 | |
| 891 | def _process_quoted_line(self, tag, line): |
| 892 | """Process a line quoted with an AUTOTEST_STATUS flag. If the |
| 893 | tag is blank then we want to push out all the data we've been |
| 894 | building up in self.logs, and then the newest line. If the |
| 895 | tag is not blank, then push the line into the logs for handling |
| 896 | later.""" |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 897 | entry = base_job.status_log_entry.parse(line) |
| 898 | if entry is None: |
| 899 | return # the line contains no status lines |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 900 | if tag == "": |
| 901 | self._process_logs() |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 902 | self.job.record_entry(entry, log_in_subdir=False) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 903 | self.last_line = line |
| 904 | else: |
| 905 | tag_parts = [int(x) for x in tag.split(".")] |
| 906 | log_dict = self.logs |
| 907 | for part in tag_parts: |
| 908 | log_dict = log_dict.setdefault(part, {}) |
| 909 | log_list = log_dict.setdefault("logs", []) |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 910 | log_list.append(entry) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 911 | |
| 912 | |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 913 | def _process_info_line(self, line): |
| 914 | """Check if line is an INFO line, and if it is, interpret any control |
| 915 | messages (e.g. enabling/disabling warnings) that it may contain.""" |
| 916 | match = re.search(r"^\t*INFO\t----\t----(.*)\t[^\t]*$", line) |
| 917 | if not match: |
| 918 | return # not an INFO line |
| 919 | for field in match.group(1).split('\t'): |
| 920 | if field.startswith("warnings.enable="): |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 921 | func = self.job.warning_manager.enable_warnings |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 922 | elif field.startswith("warnings.disable="): |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 923 | func = self.job.warning_manager.disable_warnings |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 924 | else: |
| 925 | continue |
| 926 | warning_type = field.split("=", 1)[1] |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 927 | func(warning_type) |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 928 | |
| 929 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 930 | def _process_line(self, line): |
| 931 | """Write out a line of data to the appropriate stream. Status |
| 932 | lines sent by autotest will be prepended with |
| 933 | "AUTOTEST_STATUS", and all other lines are ssh error |
| 934 | messages.""" |
| 935 | status_match = self.status_parser.search(line) |
| 936 | test_complete_match = self.test_complete_parser.search(line) |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 937 | fetch_package_match = self.fetch_package_parser.search(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 938 | if status_match: |
| 939 | tag, line = status_match.groups() |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 940 | self._process_info_line(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 941 | self._process_quoted_line(tag, line) |
| 942 | elif test_complete_match: |
jadmanski | fcc0d5d | 2009-02-12 21:52:54 +0000 | [diff] [blame] | 943 | self._process_logs() |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 944 | fifo_path, = test_complete_match.groups() |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 945 | try: |
| 946 | self.log_collector.collect_client_job_results() |
| 947 | self.host.run("echo A > %s" % fifo_path) |
| 948 | except Exception: |
| 949 | msg = "Post-test log collection failed, continuing anyway" |
| 950 | logging.exception(msg) |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 951 | elif fetch_package_match: |
| 952 | pkg_name, dest_path, fifo_path = fetch_package_match.groups() |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 953 | serve_packages = global_config.global_config.get_config_value( |
| 954 | "PACKAGES", "serve_packages_from_autoserv", type=bool) |
| 955 | if serve_packages and pkg_name.endswith(".tar.bz2"): |
| 956 | try: |
| 957 | self._send_tarball(pkg_name, dest_path) |
| 958 | except Exception: |
| 959 | msg = "Package tarball creation failed, continuing anyway" |
| 960 | logging.exception(msg) |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 961 | try: |
| 962 | self.host.run("echo B > %s" % fifo_path) |
| 963 | except Exception: |
| 964 | msg = "Package tarball installation failed, continuing anyway" |
| 965 | logging.exception(msg) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 966 | else: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 967 | logging.info(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 968 | |
| 969 | |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 970 | def _send_tarball(self, pkg_name, remote_dest): |
| 971 | name, pkg_type = self.job.pkgmgr.parse_tarball_name(pkg_name) |
| 972 | src_dirs = [] |
| 973 | if pkg_type == 'test': |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 974 | for test_dir in ['site_tests', 'tests']: |
| 975 | src_dir = os.path.join(self.job.clientdir, test_dir, name) |
| 976 | if os.path.exists(src_dir): |
| 977 | src_dirs += [src_dir] |
| 978 | if autoserv_prebuild: |
| 979 | prebuild.setup(self.job.clientdir, src_dir) |
| 980 | break |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 981 | elif pkg_type == 'profiler': |
| 982 | src_dirs += [os.path.join(self.job.clientdir, 'profilers', name)] |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 983 | if autoserv_prebuild: |
| 984 | prebuild.setup(self.job.clientdir, src_dir) |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 985 | elif pkg_type == 'dep': |
| 986 | src_dirs += [os.path.join(self.job.clientdir, 'deps', name)] |
| 987 | elif pkg_type == 'client': |
| 988 | return # you must already have a client to hit this anyway |
| 989 | else: |
| 990 | return # no other types are supported |
| 991 | |
| 992 | # iterate over src_dirs until we find one that exists, then tar it |
| 993 | for src_dir in src_dirs: |
| 994 | if os.path.exists(src_dir): |
| 995 | try: |
| 996 | logging.info('Bundling %s into %s', src_dir, pkg_name) |
| 997 | temp_dir = autotemp.tempdir(unique_id='autoserv-packager', |
| 998 | dir=self.job.tmpdir) |
| 999 | tarball_path = self.job.pkgmgr.tar_package( |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 1000 | pkg_name, src_dir, temp_dir.name, " .") |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1001 | self.host.send_file(tarball_path, remote_dest) |
| 1002 | finally: |
| 1003 | temp_dir.clean() |
| 1004 | return |
| 1005 | |
| 1006 | |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 1007 | def log_warning(self, msg, warning_type): |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1008 | """Injects a WARN message into the current status logging stream.""" |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 1009 | timestamp = int(time.time()) |
| 1010 | if self.job.warning_manager.is_valid(timestamp, warning_type): |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1011 | self.job.record('WARN', None, None, {}, msg) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1012 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1013 | |
| 1014 | def write(self, data): |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1015 | # now start processing the existing buffer and the new data |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1016 | data = self.leftover + data |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1017 | lines = data.split('\n') |
| 1018 | processed_lines = 0 |
| 1019 | try: |
| 1020 | # process all the buffered data except the last line |
| 1021 | # ignore the last line since we may not have all of it yet |
| 1022 | for line in lines[:-1]: |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1023 | self._process_line(line) |
| 1024 | processed_lines += 1 |
| 1025 | finally: |
| 1026 | # save any unprocessed lines for future processing |
| 1027 | self.leftover = '\n'.join(lines[processed_lines:]) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1028 | |
| 1029 | |
| 1030 | def flush(self): |
| 1031 | sys.stdout.flush() |
| 1032 | |
| 1033 | |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1034 | def flush_all_buffers(self): |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1035 | if self.leftover: |
| 1036 | self._process_line(self.leftover) |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1037 | self.leftover = "" |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1038 | self._process_logs() |
| 1039 | self.flush() |
| 1040 | |
| 1041 | |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1042 | def close(self): |
| 1043 | self.flush_all_buffers() |
| 1044 | |
| 1045 | |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 1046 | SiteAutotest = client_utils.import_site_class( |
| 1047 | __file__, "autotest_lib.server.site_autotest", "SiteAutotest", |
| 1048 | BaseAutotest) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 1049 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 1050 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 1051 | class Autotest(SiteAutotest): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1052 | pass |