blob: af25cd1c4f4445b25dcb3de7b2d1ea64a75e46f9 [file] [log] [blame]
Fang Dengd1c2b732013-08-20 12:59:46 -07001"""Provides a factory method to create a host object."""
2
3
Aviv Keshetc5947fa2013-09-04 14:06:29 -07004from autotest_lib.client.common_lib import error, global_config
jadmanskic6136e92010-07-19 16:41:49 +00005from autotest_lib.server import autotest, utils as server_utils
Fang Deng0ca40e22013-08-27 17:47:44 -07006from autotest_lib.server.hosts import site_factory, cros_host, ssh_host, serial
mblighe48bcfb2008-11-11 17:09:44 +00007from autotest_lib.server.hosts import logfile_monitor
8
Fang Dengd1c2b732013-08-20 12:59:46 -07009
mblighe48bcfb2008-11-11 17:09:44 +000010DEFAULT_FOLLOW_PATH = '/var/log/kern.log'
11DEFAULT_PATTERNS_PATH = 'console_patterns'
mbligh55552bf2009-11-06 03:12:38 +000012SSH_ENGINE = global_config.global_config.get_config_value('AUTOSERV',
13 'ssh_engine',
14 type=str)
jadmanskid60321a2008-10-28 20:32:05 +000015# for tracking which hostnames have already had job_start called
16_started_hostnames = set()
17
Fang Dengd1c2b732013-08-20 12:59:46 -070018
mblighe48bcfb2008-11-11 17:09:44 +000019def create_host(
Dale Curtis3693fc82011-03-01 17:41:37 -080020 hostname, auto_monitor=False, follow_paths=None, pattern_paths=None,
jadmanskicb4aac72008-11-24 22:31:38 +000021 netconsole=False, **args):
Fang Dengd1c2b732013-08-20 12:59:46 -070022 """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 Deng96667ca2013-08-01 17:46:18 -070045
46 # TODO(fdeng): this method should should dynamically discover
47 # and allocate host types, crbug.com/273843
Fang Deng0ca40e22013-08-27 17:47:44 -070048 classes = [cros_host.CrosHost]
jadmanski635b06f2008-09-05 20:26:44 +000049 # by default assume we're using SSH support
mbligh55552bf2009-11-06 03:12:38 +000050 if SSH_ENGINE == 'paramiko':
51 from autotest_lib.server.hosts import paramiko_host
Fang Deng96667ca2013-08-01 17:46:18 -070052 classes.append(paramiko_host.ParamikoHost)
mbligh55552bf2009-11-06 03:12:38 +000053 elif SSH_ENGINE == 'raw_ssh':
Fang Deng96667ca2013-08-01 17:46:18 -070054 classes.append(ssh_host.SSHHost)
mbligh55552bf2009-11-06 03:12:38 +000055 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)
jadmanski635b06f2008-09-05 20:26:44 +000060
jadmanskic6136e92010-07-19 16:41:49 +000061 # by default mix in run_test support
62 classes.append(autotest.AutotestHostMixin)
63
jadmanskicb4aac72008-11-24 22:31:38 +000064 # if the user really wants to use netconsole, let them
65 if netconsole:
66 classes.append(netconsole.NetconsoleHost)
67
jadmanski54f90af2008-10-10 16:20:55 +000068 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)
jadmanski635b06f2008-09-05 20:26:44 +000076 else:
jadmanskiaf959a42008-11-11 18:34:43 +000077 # no serial available, fall back to direct dmesg logging
78 if follow_paths is None:
79 follow_paths = [DEFAULT_FOLLOW_PATH]
jadmanski54f90af2008-10-10 16:20:55 +000080 else:
jadmanskiaf959a42008-11-11 18:34:43 +000081 follow_paths = list(follow_paths) + [DEFAULT_FOLLOW_PATH]
mblighe48bcfb2008-11-11 17:09:44 +000082
jadmanskiaf959a42008-11-11 18:34:43 +000083 if pattern_paths is None:
84 pattern_paths = [DEFAULT_PATTERNS_PATH]
85 else:
86 pattern_paths = (
87 list(pattern_paths) + [DEFAULT_PATTERNS_PATH])
mblighe48bcfb2008-11-11 17:09:44 +000088
jadmanskiaf959a42008-11-11 18:34:43 +000089 logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
90 follow_paths, pattern_paths)
91 classes.append(logfile_monitor_class)
mblighe48bcfb2008-11-11 17:09:44 +000092
93 elif follow_paths:
94 logfile_monitor_class = logfile_monitor.NewLogfileMonitorMixin(
95 follow_paths, pattern_paths)
96 classes.append(logfile_monitor_class)
jadmanski635b06f2008-09-05 20:26:44 +000097
98 # do any site-specific processing of the classes list
jadmanskid60321a2008-10-28 20:32:05 +000099 site_factory.postprocess_classes(classes, hostname,
jadmanskiea902262008-10-16 15:51:00 +0000100 auto_monitor=auto_monitor, **args)
jadmanski635b06f2008-09-05 20:26:44 +0000101
Eric Li861b2d52011-02-04 14:50:35 -0800102 hostname, args['user'], args['password'], args['port'] = \
103 server_utils.parse_machine(hostname, ssh_user, ssh_pass, ssh_port)
Fang Dengd1c2b732013-08-20 12:59:46 -0700104 args['ssh_verbosity_flag'] = ssh_verbosity_flag
Aviv Keshetc5947fa2013-09-04 14:06:29 -0700105 args['ssh_options'] = ssh_options
106
Eric Li10222b82010-11-24 09:33:15 -0800107
jadmanski635b06f2008-09-05 20:26:44 +0000108 # create a custom host class for this machine and return an instance of it
109 host_class = type("%s_host" % hostname, tuple(classes), {})
jadmanskid60321a2008-10-28 20:32:05 +0000110 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