blob: e9c5e6bbe6b6768df9dc0f205ed75fd666f71205 [file] [log] [blame]
Simran Basic6f1f7a2012-10-16 10:47:46 -07001# 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
5import common
6import inspect, new, socket, sys
7
8from autotest_lib.cli import topic_common, host
9from autotest_lib.server import hosts
10
11
12# In order for hosts to work correctly, some of its variables must be setup.
13hosts.factory.ssh_user = 'root'
14hosts.factory.ssh_pass = ''
15hosts.factory.ssh_port = 22
16
17
18class site_host(host.host):
19 pass
20
21
22class site_host_create(site_host, host.host_create):
23 """
24 site_host_create subclasses host_create in host.py.
25 """
26
27
28 def _execute_add_one_host(self, host):
29 # Always add the hosts as locked to avoid the host
30 # being picked up by the scheduler before it's ACL'ed
31 self.data['locked'] = True
32 self.execute_rpc('add_host', hostname=host,
33 status="Ready", **self.data)
34 # If there are labels avaliable for host, use them.
35 host_info = self.host_info_map[host]
36 if host_info.labels:
37 labels = list(set(self.labels[:] + host_info.labels))
38 else:
39 labels = self.labels[:]
40 # Now add the platform label
41 if self.platform:
42 labels.append(self.platform)
43 elif host_info.platform:
44 # If a platform was not provided and we were able to retrieve it
45 # from the host, use the retrieved platform.
46 labels.append(host_info.platform)
47 if len(labels):
48 self.execute_rpc('host_add_labels', id=host, labels=labels)
49
50
51 def execute(self):
52 # Check to see if the platform or any other labels can be grabbed from
53 # the hosts.
54 self.host_info_map = {}
55 for host in self.hosts:
56 try:
57 ssh_host = hosts.create_host(host)
58 host_info = host_information(host,
59 ssh_host.get_platform(),
60 ssh_host.get_labels())
61 except socket.gaierror:
62 # We may be adding a host that does not exist yet or we can't
63 # reach due to network issues. I.E. the labadmins are adding
64 # hosts not yet actually in the lab.
65 host_info = host_information(host, None, [])
66 self.host_info_map[host] = host_info
67 # We need to check if these labels & ACLs exist,
68 # and create them if not.
69 if self.platform:
70 self.check_and_create_items('get_labels', 'add_label',
71 [self.platform],
72 platform=True)
73 else:
74 # No platform was provided so check and create the platform label
75 # for each host.
76 platforms = []
77 for host_info in self.host_info_map.values():
78 if host_info.platform and host_info.platform not in platforms:
79 platforms.append(host_info.platform)
80 if len(platforms):
81 self.check_and_create_items('get_labels', 'add_label',
82 platforms,
83 platform=True)
84 labels_to_check_and_create = self.labels[:]
85 for host_info in self.host_info_map.values():
86 labels_to_check_and_create = (host_info.labels +
87 labels_to_check_and_create)
88 if labels_to_check_and_create:
89 self.check_and_create_items('get_labels', 'add_label',
90 labels_to_check_and_create,
91 platform=False)
92
93 if self.acls:
94 self.check_and_create_items('get_acl_groups',
95 'add_acl_group',
96 self.acls)
97
98 success = self.site_create_hosts_hook()
99
100 if len(success):
101 for acl in self.acls:
102 self.execute_rpc('acl_group_add_hosts', id=acl, hosts=success)
103
104 if not self.locked:
105 for host in success:
106 self.execute_rpc('modify_host', id=host, locked=False)
107 return success
108
109
110class host_information(object):
111 """Store host information so we don't have to keep looking it up."""
112
113
114 def __init__(self, hostname, platform, labels):
115 self.hostname = hostname
116 self.platform = platform
117 self.labels = labels
118
119
120# Any classes we don't override in host should be copied automatically
121for cls in [getattr(host, n) for n in dir(host) if not n.startswith("_")]:
122 if not inspect.isclass(cls):
123 continue
124 cls_name = cls.__name__
125 site_cls_name = 'site_' + cls_name
126 if hasattr(sys.modules[__name__], site_cls_name):
127 continue
128 bases = (site_host, cls)
129 members = {'__doc__': cls.__doc__}
130 site_cls = new.classobj(site_cls_name, bases, members)
131 setattr(sys.modules[__name__], site_cls_name, site_cls)