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 |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 9 | from autotest_lib.cli import topic_common, host |
| 10 | from autotest_lib.server import hosts |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import error |
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): |
| 23 | pass |
| 24 | |
| 25 | |
| 26 | class site_host_create(site_host, host.host_create): |
| 27 | """ |
| 28 | site_host_create subclasses host_create in host.py. |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | def _execute_add_one_host(self, host): |
| 33 | # Always add the hosts as locked to avoid the host |
| 34 | # being picked up by the scheduler before it's ACL'ed |
| 35 | self.data['locked'] = True |
| 36 | self.execute_rpc('add_host', hostname=host, |
| 37 | status="Ready", **self.data) |
| 38 | # If there are labels avaliable for host, use them. |
| 39 | host_info = self.host_info_map[host] |
| 40 | if host_info.labels: |
| 41 | labels = list(set(self.labels[:] + host_info.labels)) |
| 42 | else: |
| 43 | labels = self.labels[:] |
| 44 | # Now add the platform label |
| 45 | if self.platform: |
| 46 | labels.append(self.platform) |
| 47 | elif host_info.platform: |
| 48 | # If a platform was not provided and we were able to retrieve it |
| 49 | # from the host, use the retrieved platform. |
| 50 | labels.append(host_info.platform) |
| 51 | if len(labels): |
| 52 | self.execute_rpc('host_add_labels', id=host, labels=labels) |
| 53 | |
| 54 | |
| 55 | def execute(self): |
| 56 | # Check to see if the platform or any other labels can be grabbed from |
| 57 | # the hosts. |
| 58 | self.host_info_map = {} |
| 59 | for host in self.hosts: |
| 60 | try: |
Simran Basi | be7b65b | 2012-11-21 09:33:27 -0800 | [diff] [blame] | 61 | if utils.ping(host, tries=1, deadline=1) == 0: |
| 62 | ssh_host = hosts.create_host(host) |
| 63 | host_info = host_information(host, |
| 64 | ssh_host.get_platform(), |
| 65 | ssh_host.get_labels()) |
| 66 | else: |
| 67 | # Can't ping the host, use default information. |
| 68 | host_info = host_information(host, None, []) |
| 69 | except (socket.gaierror, error.AutoservRunError, |
| 70 | error.AutoservSSHTimeout): |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 71 | # 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] | 72 | # reach due to hostname/address issues or if the host is down. |
Simran Basi | c6f1f7a | 2012-10-16 10:47:46 -0700 | [diff] [blame] | 73 | host_info = host_information(host, None, []) |
| 74 | self.host_info_map[host] = host_info |
| 75 | # We need to check if these labels & ACLs exist, |
| 76 | # and create them if not. |
| 77 | if self.platform: |
| 78 | self.check_and_create_items('get_labels', 'add_label', |
| 79 | [self.platform], |
| 80 | platform=True) |
| 81 | else: |
| 82 | # No platform was provided so check and create the platform label |
| 83 | # for each host. |
| 84 | platforms = [] |
| 85 | for host_info in self.host_info_map.values(): |
| 86 | if host_info.platform and host_info.platform not in platforms: |
| 87 | platforms.append(host_info.platform) |
| 88 | if len(platforms): |
| 89 | self.check_and_create_items('get_labels', 'add_label', |
| 90 | platforms, |
| 91 | platform=True) |
| 92 | labels_to_check_and_create = self.labels[:] |
| 93 | for host_info in self.host_info_map.values(): |
| 94 | labels_to_check_and_create = (host_info.labels + |
| 95 | labels_to_check_and_create) |
| 96 | if labels_to_check_and_create: |
| 97 | self.check_and_create_items('get_labels', 'add_label', |
| 98 | labels_to_check_and_create, |
| 99 | platform=False) |
| 100 | |
| 101 | if self.acls: |
| 102 | self.check_and_create_items('get_acl_groups', |
| 103 | 'add_acl_group', |
| 104 | self.acls) |
| 105 | |
| 106 | success = self.site_create_hosts_hook() |
| 107 | |
| 108 | if len(success): |
| 109 | for acl in self.acls: |
| 110 | self.execute_rpc('acl_group_add_hosts', id=acl, hosts=success) |
| 111 | |
| 112 | if not self.locked: |
| 113 | for host in success: |
| 114 | self.execute_rpc('modify_host', id=host, locked=False) |
| 115 | return success |
| 116 | |
| 117 | |
| 118 | class host_information(object): |
| 119 | """Store host information so we don't have to keep looking it up.""" |
| 120 | |
| 121 | |
| 122 | def __init__(self, hostname, platform, labels): |
| 123 | self.hostname = hostname |
| 124 | self.platform = platform |
| 125 | self.labels = labels |
| 126 | |
| 127 | |
| 128 | # Any classes we don't override in host should be copied automatically |
| 129 | for cls in [getattr(host, n) for n in dir(host) if not n.startswith("_")]: |
| 130 | if not inspect.isclass(cls): |
| 131 | continue |
| 132 | cls_name = cls.__name__ |
| 133 | site_cls_name = 'site_' + cls_name |
| 134 | if hasattr(sys.modules[__name__], site_cls_name): |
| 135 | continue |
| 136 | bases = (site_host, cls) |
| 137 | members = {'__doc__': cls.__doc__} |
| 138 | site_cls = new.classobj(site_cls_name, bases, members) |
| 139 | setattr(sys.modules[__name__], site_cls_name, site_cls) |