Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 1 | """Provides a factory method to create a host object.""" |
| 2 | |
| 3 | |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 4 | from autotest_lib.client.common_lib import error, global_config |
jadmanski | c6136e9 | 2010-07-19 16:41:49 +0000 | [diff] [blame] | 5 | from autotest_lib.server import autotest, utils as server_utils |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame^] | 6 | from autotest_lib.server.hosts import site_factory, cros_host, ssh_host, serial |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 7 | from autotest_lib.server.hosts import logfile_monitor |
| 8 | |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 9 | |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 10 | DEFAULT_FOLLOW_PATH = '/var/log/kern.log' |
| 11 | DEFAULT_PATTERNS_PATH = 'console_patterns' |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 12 | SSH_ENGINE = global_config.global_config.get_config_value('AUTOSERV', |
| 13 | 'ssh_engine', |
| 14 | type=str) |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 15 | # for tracking which hostnames have already had job_start called |
| 16 | _started_hostnames = set() |
| 17 | |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 18 | |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 19 | def create_host( |
Dale Curtis | 3693fc8 | 2011-03-01 17:41:37 -0800 | [diff] [blame] | 20 | hostname, auto_monitor=False, follow_paths=None, pattern_paths=None, |
jadmanski | cb4aac7 | 2008-11-24 22:31:38 +0000 | [diff] [blame] | 21 | netconsole=False, **args): |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 22 | """Create a host object. |
| 23 | |
| 24 | This method mixes host classes that are needed into a new subclass |
| 25 | and creates a instance of the new class. |
| 26 | |
| 27 | @param hostname: A string representing the host name of the device. |
| 28 | @param auto_monitor: A boolean value, if True, will try to mix |
| 29 | SerialHost in. If the host supports use as SerialHost, |
| 30 | will not mix in LogfileMonitorMixin anymore. |
| 31 | If the host doesn't support it, will |
| 32 | fall back to direct demesg logging and mix |
| 33 | LogfileMonitorMixin in. |
| 34 | @param follow_paths: A list, passed to LogfileMonitorMixin, |
| 35 | remote paths to monitor. |
| 36 | @param pattern_paths: A list, passed to LogfileMonitorMixin, |
| 37 | local paths to alert pattern definition files. |
| 38 | @param netconsole: A boolean value, if True, will mix NetconsoleHost in. |
| 39 | @param args: Args that will be passed to the constructor of |
| 40 | the new host class. |
| 41 | |
| 42 | @returns: A host object which is an instance of the newly created |
| 43 | host class. |
| 44 | """ |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 45 | |
| 46 | # TODO(fdeng): this method should should dynamically discover |
| 47 | # and allocate host types, crbug.com/273843 |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame^] | 48 | classes = [cros_host.CrosHost] |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 49 | # by default assume we're using SSH support |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 50 | if SSH_ENGINE == 'paramiko': |
| 51 | from autotest_lib.server.hosts import paramiko_host |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 52 | classes.append(paramiko_host.ParamikoHost) |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 53 | elif SSH_ENGINE == 'raw_ssh': |
Fang Deng | 96667ca | 2013-08-01 17:46:18 -0700 | [diff] [blame] | 54 | classes.append(ssh_host.SSHHost) |
mbligh | 55552bf | 2009-11-06 03:12:38 +0000 | [diff] [blame] | 55 | else: |
| 56 | raise error.AutoServError("Unknown SSH engine %s. Please verify the " |
| 57 | "value of the configuration key 'ssh_engine' " |
| 58 | "on autotest's global_config.ini file." % |
| 59 | SSH_ENGINE) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 60 | |
jadmanski | c6136e9 | 2010-07-19 16:41:49 +0000 | [diff] [blame] | 61 | # by default mix in run_test support |
| 62 | classes.append(autotest.AutotestHostMixin) |
| 63 | |
jadmanski | cb4aac7 | 2008-11-24 22:31:38 +0000 | [diff] [blame] | 64 | # if the user really wants to use netconsole, let them |
| 65 | if netconsole: |
| 66 | classes.append(netconsole.NetconsoleHost) |
| 67 | |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 68 | if auto_monitor: |
| 69 | # use serial console support if it's available |
| 70 | conmux_args = {} |
| 71 | for key in ("conmux_server", "conmux_attach"): |
| 72 | if key in args: |
| 73 | conmux_args[key] = args[key] |
| 74 | if serial.SerialHost.host_is_supported(hostname, **conmux_args): |
| 75 | classes.append(serial.SerialHost) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 76 | else: |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 77 | # no serial available, fall back to direct dmesg logging |
| 78 | if follow_paths is None: |
| 79 | follow_paths = [DEFAULT_FOLLOW_PATH] |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 80 | else: |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 81 | follow_paths = list(follow_paths) + [DEFAULT_FOLLOW_PATH] |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 82 | |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 83 | if pattern_paths is None: |
| 84 | pattern_paths = [DEFAULT_PATTERNS_PATH] |
| 85 | else: |
| 86 | pattern_paths = ( |
| 87 | list(pattern_paths) + [DEFAULT_PATTERNS_PATH]) |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 88 | |
jadmanski | af959a4 | 2008-11-11 18:34:43 +0000 | [diff] [blame] | 89 | logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin( |
| 90 | follow_paths, pattern_paths) |
| 91 | classes.append(logfile_monitor_class) |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 92 | |
| 93 | elif follow_paths: |
| 94 | logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin( |
| 95 | follow_paths, pattern_paths) |
| 96 | classes.append(logfile_monitor_class) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 97 | |
| 98 | # do any site-specific processing of the classes list |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 99 | site_factory.postprocess_classes(classes, hostname, |
jadmanski | ea90226 | 2008-10-16 15:51:00 +0000 | [diff] [blame] | 100 | auto_monitor=auto_monitor, **args) |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 101 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 102 | hostname, args['user'], args['password'], args['port'] = \ |
| 103 | server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port) |
Fang Deng | d1c2b73 | 2013-08-20 12:59:46 -0700 | [diff] [blame] | 104 | args['ssh_verbosity_flag'] = ssh_verbosity_flag |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 105 | args['ssh_options'] = ssh_options |
| 106 | |
Eric Li | 10222b8 | 2010-11-24 09:33:15 -0800 | [diff] [blame] | 107 | |
jadmanski | 635b06f | 2008-09-05 20:26:44 +0000 | [diff] [blame] | 108 | # create a custom host class for this machine and return an instance of it |
| 109 | host_class = type("%s_host" % hostname, tuple(classes), {}) |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 110 | host_instance = host_class(hostname, **args) |
| 111 | |
| 112 | # call job_start if this is the first time this host is being used |
| 113 | if hostname not in _started_hostnames: |
| 114 | host_instance.job_start() |
| 115 | _started_hostnames.add(hostname) |
| 116 | |
| 117 | return host_instance |