Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import common |
| 6 | import inspect, new, socket, sys |
| 7 | |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 8 | from autotest_lib.client.bin import utils |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 9 | from autotest_lib.cli import host, rpc |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 10 | from autotest_lib.server import hosts |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error, host_protections |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 12 | |
| 13 | |
| 14 | # In order for hosts to work correctly, some of its variables must be setup. |
| 15 | hosts.factory.ssh_user = 'root' |
| 16 | hosts.factory.ssh_pass = '' |
| 17 | hosts.factory.ssh_port = 22 |
Fang Deng | 6bee3de | 2013-09-04 20:17:26 -0700 | [diff] [blame] | 18 | hosts.factory.ssh_verbosity_flag = '' |
| 19 | hosts.factory.ssh_options = '' |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class site_host(host.host): |
Justin Giorgi | d76ed50 | 2016-06-21 10:27:52 -0700 | [diff] [blame^] | 23 | """Required by atest's site logic.""" |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 24 | pass |
| 25 | |
| 26 | |
| 27 | class site_host_create(site_host, host.host_create): |
| 28 | """ |
| 29 | site_host_create subclasses host_create in host.py. |
| 30 | """ |
| 31 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 32 | @classmethod |
| 33 | def construct_without_parse( |
| 34 | cls, web_server, hosts, platform=None, |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 35 | locked=False, lock_reason='', labels=[], acls=[], |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 36 | protection=host_protections.Protection.NO_PROTECTION): |
| 37 | """Construct an site_host_create object and fill in data from args. |
| 38 | |
| 39 | Do not need to call parse after the construction. |
| 40 | |
| 41 | Return an object of site_host_create ready to execute. |
| 42 | |
| 43 | @param web_server: A string specifies the autotest webserver url. |
| 44 | It is needed to setup comm to make rpc. |
| 45 | @param hosts: A list of hostnames as strings. |
| 46 | @param platform: A string or None. |
| 47 | @param locked: A boolean. |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 48 | @param lock_reason: A string. |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 49 | @param labels: A list of labels as strings. |
| 50 | @param acls: A list of acls as strings. |
| 51 | @param protection: An enum defined in host_protections. |
| 52 | """ |
| 53 | obj = cls() |
| 54 | obj.web_server = web_server |
| 55 | try: |
| 56 | # Setup stuff needed for afe comm. |
| 57 | obj.afe = rpc.afe_comm(web_server) |
| 58 | except rpc.AuthError, s: |
| 59 | obj.failure(str(s), fatal=True) |
| 60 | obj.hosts = hosts |
| 61 | obj.platform = platform |
| 62 | obj.locked = locked |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 63 | if locked and lock_reason.strip(): |
| 64 | obj.data['lock_reason'] = lock_reason.strip() |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 65 | obj.labels = labels |
| 66 | obj.acls = acls |
| 67 | if protection: |
| 68 | obj.data['protection'] = protection |
Kevin Cheng | bdedeb1 | 2016-01-12 16:28:43 -0800 | [diff] [blame] | 69 | # TODO(kevcheng): Update the admin page to take in serials? |
| 70 | obj.serials = None |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 71 | return obj |
| 72 | |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 73 | |
| 74 | def _execute_add_one_host(self, host): |
| 75 | # Always add the hosts as locked to avoid the host |
Jiaxi Luo | b0e6c60 | 2014-05-13 16:06:24 -0700 | [diff] [blame] | 76 | # being picked up by the scheduler before it's ACL'ed. |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 77 | self.data['locked'] = True |
Matthew Sartori | 6818633 | 2015-04-27 17:19:53 -0700 | [diff] [blame] | 78 | if not self.locked: |
| 79 | self.data['lock_reason'] = 'Forced lock on device creation' |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 80 | self.execute_rpc('add_host', hostname=host, |
| 81 | status="Ready", **self.data) |
| 82 | # If there are labels avaliable for host, use them. |
| 83 | host_info = self.host_info_map[host] |
Jiaxi Luo | b0e6c60 | 2014-05-13 16:06:24 -0700 | [diff] [blame] | 84 | labels = set(self.labels) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 85 | if host_info.labels: |
Jiaxi Luo | b0e6c60 | 2014-05-13 16:06:24 -0700 | [diff] [blame] | 86 | labels.update(host_info.labels) |
| 87 | # Now add the platform label. |
| 88 | # If a platform was not provided and we were able to retrieve it |
| 89 | # from the host, use the retrieved platform. |
| 90 | platform = self.platform if self.platform else host_info.platform |
| 91 | if platform: |
| 92 | labels.add(platform) |
| 93 | |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 94 | if len(labels): |
Jiaxi Luo | b0e6c60 | 2014-05-13 16:06:24 -0700 | [diff] [blame] | 95 | self.execute_rpc('host_add_labels', id=host, labels=list(labels)) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 96 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 97 | if self.serials: |
Justin Giorgi | d76ed50 | 2016-06-21 10:27:52 -0700 | [diff] [blame^] | 98 | self.execute_rpc('set_host_attribute', hostname=host, |
| 99 | attribute='serials', value=','.join(self.serials)) |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 100 | |
| 101 | def execute(self): |
| 102 | # Check to see if the platform or any other labels can be grabbed from |
| 103 | # the hosts. |
| 104 | self.host_info_map = {} |
| 105 | for host in self.hosts: |
| 106 | try: |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 107 | if utils.ping(host, tries=1, deadline=1) == 0: |
Kevin Cheng | 49f7b81 | 2015-12-15 15:24:23 -0800 | [diff] [blame] | 108 | if self.serials and len(self.serials) > 1: |
| 109 | host_dut = hosts.create_testbed( |
| 110 | host, adb_serials=self.serials) |
| 111 | else: |
Kevin Cheng | d7ef5d3 | 2016-01-08 16:19:28 -0800 | [diff] [blame] | 112 | adb_serial = None |
| 113 | if self.serials: |
| 114 | adb_serial = self.serials[0] |
| 115 | host_dut = hosts.create_host(host, |
| 116 | adb_serial=adb_serial) |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 117 | host_info = host_information(host, |
Kevin Cheng | 49f7b81 | 2015-12-15 15:24:23 -0800 | [diff] [blame] | 118 | host_dut.get_platform(), |
| 119 | host_dut.get_labels()) |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 120 | else: |
| 121 | # Can't ping the host, use default information. |
| 122 | host_info = host_information(host, None, []) |
| 123 | except (socket.gaierror, error.AutoservRunError, |
| 124 | error.AutoservSSHTimeout): |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 125 | # We may be adding a host that does not exist yet or we can't |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 126 | # reach due to hostname/address issues or if the host is down. |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 127 | host_info = host_information(host, None, []) |
| 128 | self.host_info_map[host] = host_info |
| 129 | # We need to check if these labels & ACLs exist, |
| 130 | # and create them if not. |
| 131 | if self.platform: |
| 132 | self.check_and_create_items('get_labels', 'add_label', |
| 133 | [self.platform], |
| 134 | platform=True) |
| 135 | else: |
| 136 | # No platform was provided so check and create the platform label |
| 137 | # for each host. |
| 138 | platforms = [] |
| 139 | for host_info in self.host_info_map.values(): |
| 140 | if host_info.platform and host_info.platform not in platforms: |
| 141 | platforms.append(host_info.platform) |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 142 | if platforms: |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 143 | self.check_and_create_items('get_labels', 'add_label', |
| 144 | platforms, |
| 145 | platform=True) |
| 146 | labels_to_check_and_create = self.labels[:] |
| 147 | for host_info in self.host_info_map.values(): |
| 148 | labels_to_check_and_create = (host_info.labels + |
| 149 | labels_to_check_and_create) |
| 150 | if labels_to_check_and_create: |
| 151 | self.check_and_create_items('get_labels', 'add_label', |
| 152 | labels_to_check_and_create, |
| 153 | platform=False) |
| 154 | |
| 155 | if self.acls: |
| 156 | self.check_and_create_items('get_acl_groups', |
| 157 | 'add_acl_group', |
| 158 | self.acls) |
| 159 | |
Jiaxi Luo | c342f9f | 2014-05-19 16:22:03 -0700 | [diff] [blame] | 160 | return self._execute_add_hosts() |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 161 | |
| 162 | |
| 163 | class host_information(object): |
| 164 | """Store host information so we don't have to keep looking it up.""" |
| 165 | |
| 166 | |
| 167 | def __init__(self, hostname, platform, labels): |
| 168 | self.hostname = hostname |
| 169 | self.platform = platform |
| 170 | self.labels = labels |
| 171 | |
| 172 | |
| 173 | # Any classes we don't override in host should be copied automatically |
| 174 | for cls in [getattr(host, n) for n in dir(host) if not n.startswith("_")]: |
| 175 | if not inspect.isclass(cls): |
| 176 | continue |
| 177 | cls_name = cls.__name__ |
| 178 | site_cls_name = 'site_' + cls_name |
| 179 | if hasattr(sys.modules[__name__], site_cls_name): |
| 180 | continue |
| 181 | bases = (site_host, cls) |
| 182 | members = {'__doc__': cls.__doc__} |
| 183 | site_cls = new.classobj(site_cls_name, bases, members) |
| 184 | setattr(sys.modules[__name__], site_cls_name, site_cls) |