mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 1 | from autotest_lib.client.common_lib import utils, error, global_config |
jadmanski | c6136e9 | 2010-07-19 16:41:49 +0000 | [diff] [blame] | 2 | from autotest_lib.server import autotest, utils as server_utils |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 3 | from autotest_lib.server.hosts import site_factory, ssh_host, serial |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 4 | from autotest_lib.server.hosts import logfile_monitor |
| 5 | |
| 6 | DEFAULT_FOLLOW_PATH = '/var/log/kern.log' |
| 7 | DEFAULT_PATTERNS_PATH = 'console_patterns' |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 8 | SSH_ENGINE = global_config.global_config.get_config_value('AUTOSERV', |
| 9 | 'ssh_engine', |
| 10 | type=str) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 11 | |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 12 | # for tracking which hostnames have already had job_start called |
| 13 | _started_hostnames = set() |
| 14 | |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 15 | def create_host( |
jadmanski | cb4aac7 | 2008-11-24 22:31:38 +0000 | [diff] [blame] | 16 | hostname, auto_monitor=True, follow_paths=None, pattern_paths=None, |
| 17 | netconsole=False, **args): |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 18 | # by default assume we're using SSH support |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 19 | if SSH_ENGINE == 'paramiko': |
| 20 | from autotest_lib.server.hosts import paramiko_host |
| 21 | classes = [paramiko_host.ParamikoHost] |
| 22 | elif SSH_ENGINE == 'raw_ssh': |
| 23 | classes = [ssh_host.SSHHost] |
| 24 | else: |
| 25 | raise error.AutoServError("Unknown SSH engine %s. Please verify the " |
| 26 | "value of the configuration key 'ssh_engine' " |
| 27 | "on autotest's global_config.ini file." % |
| 28 | SSH_ENGINE) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 29 | |
jadmanski | c6136e9 | 2010-07-19 16:41:49 +0000 | [diff] [blame] | 30 | # by default mix in run_test support |
| 31 | classes.append(autotest.AutotestHostMixin) |
| 32 | |
jadmanski | cb4aac7 | 2008-11-24 22:31:38 +0000 | [diff] [blame] | 33 | # if the user really wants to use netconsole, let them |
| 34 | if netconsole: |
| 35 | classes.append(netconsole.NetconsoleHost) |
| 36 | |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 37 | if auto_monitor: |
| 38 | # use serial console support if it's available |
| 39 | conmux_args = {} |
| 40 | for key in ("conmux_server", "conmux_attach"): |
| 41 | if key in args: |
| 42 | conmux_args[key] = args[key] |
| 43 | if serial.SerialHost.host_is_supported(hostname, **conmux_args): |
| 44 | classes.append(serial.SerialHost) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 45 | else: |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 46 | # no serial available, fall back to direct dmesg logging |
| 47 | if follow_paths is None: |
| 48 | follow_paths = [DEFAULT_FOLLOW_PATH] |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 49 | else: |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 50 | follow_paths = list(follow_paths) + [DEFAULT_FOLLOW_PATH] |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 51 | |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 52 | if pattern_paths is None: |
| 53 | pattern_paths = [DEFAULT_PATTERNS_PATH] |
| 54 | else: |
| 55 | pattern_paths = ( |
| 56 | list(pattern_paths) + [DEFAULT_PATTERNS_PATH]) |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 57 | |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 58 | logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin( |
| 59 | follow_paths, pattern_paths) |
| 60 | classes.append(logfile_monitor_class) |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 61 | |
| 62 | elif follow_paths: |
| 63 | logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin( |
| 64 | follow_paths, pattern_paths) |
| 65 | classes.append(logfile_monitor_class) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 66 | |
| 67 | # do any site-specific processing of the classes list |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 68 | site_factory.postprocess_classes(classes, hostname, |
jadmanski | ea90226 | 2008-10-16 15:51:00 +0000 | [diff] [blame] | 69 | auto_monitor=auto_monitor, **args) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 70 | |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 71 | hostname, args['user'], args['password'], args['port'] = \ |
| 72 | server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port) |
Eric Li | 10222b8 | 2010-11-24 09:33:15 -0800 | [diff] [blame] | 73 | |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 74 | # create a custom host class for this machine and return an instance of it |
| 75 | host_class = type("%s_host" % hostname, tuple(classes), {}) |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 76 | host_instance = host_class(hostname, **args) |
| 77 | |
| 78 | # call job_start if this is the first time this host is being used |
| 79 | if hostname not in _started_hostnames: |
| 80 | host_instance.job_start() |
| 81 | _started_hostnames.add(hostname) |
| 82 | |
| 83 | return host_instance |