Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 1 | # Copyright (c) 2014 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 | |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 5 | import contextlib |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 6 | import getpass |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 7 | import subprocess |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 8 | import os |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 9 | |
| 10 | import common |
| 11 | from autotest_lib.server.hosts import ssh_host |
| 12 | from autotest_lib.client.common_lib import error |
| 13 | from autotest_lib.client.common_lib import global_config |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import utils |
Jakob Juelich | 0a33187 | 2014-09-25 14:45:43 -0700 | [diff] [blame] | 15 | from autotest_lib.server.cros.dynamic_suite import frontend_wrappers |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 16 | |
| 17 | |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 18 | @contextlib.contextmanager |
| 19 | def chdir(dirname=None): |
| 20 | """A context manager to help change directories. |
| 21 | |
| 22 | Will chdir into the provided dirname for the lifetime of the context and |
| 23 | return to cwd thereafter. |
| 24 | |
| 25 | @param dirname: The dirname to chdir into. |
| 26 | """ |
| 27 | curdir = os.getcwd() |
| 28 | try: |
| 29 | if dirname is not None: |
| 30 | os.chdir(dirname) |
| 31 | yield |
| 32 | finally: |
| 33 | os.chdir(curdir) |
| 34 | |
| 35 | |
| 36 | def local_runner(cmd, stream_output=False): |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 37 | """ |
| 38 | Runs a command on the local system as the current user. |
| 39 | |
| 40 | @param cmd: The command to run. |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 41 | @param stream_output: If True, streams the stdout of the process. |
| 42 | |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 43 | @returns: The output of cmd. |
| 44 | @raises CalledProcessError: If there was a non-0 return code. |
| 45 | """ |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 46 | if not stream_output: |
| 47 | return subprocess.check_output(cmd, shell=True) |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame^] | 48 | |
| 49 | proc = subprocess.Popen( |
| 50 | cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 51 | while proc.poll() is None: |
| 52 | print proc.stdout.readline().rstrip('\n') |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame^] | 53 | if proc.returncode !=0: |
| 54 | raise subprocess.CalledProcessError(proc.returncode, cmd) |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 55 | |
| 56 | |
| 57 | _host_objects = {} |
| 58 | |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 59 | def host_object_runner(host, **kwargs): |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 60 | """ |
| 61 | Returns a function that returns the output of running a command via a host |
| 62 | object. |
| 63 | |
| 64 | @param host: The host to run a command on. |
| 65 | @returns: A function that can invoke a command remotely. |
| 66 | """ |
| 67 | try: |
| 68 | host_object = _host_objects[host] |
| 69 | except KeyError: |
Alex Miller | 792e207 | 2014-02-18 21:00:20 -0800 | [diff] [blame] | 70 | username = global_config.global_config.get_config_value( |
| 71 | 'CROS', 'infrastructure_user') |
| 72 | host_object = ssh_host.SSHHost(host, user=username) |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 73 | _host_objects[host] = host_object |
| 74 | |
| 75 | def runner(cmd): |
| 76 | """ |
| 77 | Runs a command via a host object on the enclosed host. Translates |
| 78 | host.run errors to the subprocess equivalent to expose a common API. |
| 79 | |
| 80 | @param cmd: The command to run. |
| 81 | @returns: The output of cmd. |
| 82 | @raises CalledProcessError: If there was a non-0 return code. |
| 83 | """ |
| 84 | try: |
Alex Miller | 792e207 | 2014-02-18 21:00:20 -0800 | [diff] [blame] | 85 | return host_object.run(cmd).stdout |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 86 | except error.AutotestHostRunError as e: |
| 87 | exit_status = e.result_obj.exit_status |
| 88 | command = e.result_obj.command |
| 89 | raise subprocess.CalledProcessError(exit_status, command) |
| 90 | return runner |
| 91 | |
| 92 | |
Fang Deng | 51240a4 | 2015-12-17 14:52:52 -0800 | [diff] [blame] | 93 | def googlesh_runner(host, **kwargs): |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 94 | """ |
| 95 | Returns a function that return the output of running a command via shelling |
Fang Deng | 51240a4 | 2015-12-17 14:52:52 -0800 | [diff] [blame] | 96 | out to `googlesh`. |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 97 | |
| 98 | @param host: The host to run a command on |
| 99 | @returns: A function that can invoke a command remotely. |
| 100 | """ |
| 101 | def runner(cmd): |
| 102 | """ |
Fang Deng | 51240a4 | 2015-12-17 14:52:52 -0800 | [diff] [blame] | 103 | Runs a command via googlesh on the enclosed host. |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 104 | |
| 105 | @param cmd: The command to run. |
| 106 | @returns: The output of cmd. |
| 107 | @raises CalledProcessError: If there was a non-0 return code. |
| 108 | """ |
Fang Deng | 51240a4 | 2015-12-17 14:52:52 -0800 | [diff] [blame] | 109 | out = subprocess.check_output(['googlesh', '-s', '-uchromeos-test', |
| 110 | '-m%s' % host, '%s' % cmd]) |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 111 | return out |
| 112 | return runner |
| 113 | |
| 114 | |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 115 | def execute_command(host, cmd, **kwargs): |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 116 | """ |
| 117 | Executes a command on the host `host`. This an optimization that if |
| 118 | we're already chromeos-test, we can just ssh to the machine in question. |
| 119 | Or if we're local, we don't have to ssh at all. |
| 120 | |
| 121 | @param host: The hostname to execute the command on. |
| 122 | @param cmd: The command to run. Special shell syntax (such as pipes) |
| 123 | is allowed. |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 124 | @param kwargs: Key word arguments for the runner functions. |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 125 | @returns: The output of the command. |
| 126 | """ |
Dan Shi | f6c65bd | 2014-08-29 16:15:07 -0700 | [diff] [blame] | 127 | if utils.is_localhost(host): |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 128 | runner = local_runner |
| 129 | elif getpass.getuser() == 'chromeos-test': |
| 130 | runner = host_object_runner(host) |
| 131 | else: |
Fang Deng | 51240a4 | 2015-12-17 14:52:52 -0800 | [diff] [blame] | 132 | runner = googlesh_runner(host) |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 133 | |
Prashanth Balasubramanian | 3a9b9a1 | 2014-11-13 20:05:10 -0800 | [diff] [blame] | 134 | return runner(cmd, **kwargs) |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def _csv_to_list(s): |
| 138 | """ |
| 139 | Converts a list seperated by commas into a list of strings. |
| 140 | |
| 141 | >>> _csv_to_list('') |
| 142 | [] |
| 143 | >>> _csv_to_list('one') |
| 144 | ['one'] |
| 145 | >>> _csv_to_list('one, two,three') |
| 146 | ['one', 'two', 'three'] |
| 147 | """ |
| 148 | return [x.strip() for x in s.split(',') if x] |
| 149 | |
| 150 | |
| 151 | # The goal with these functions is to give you a list of hosts that are valid |
| 152 | # arguments to ssh. Note that this only really works since our instances use |
| 153 | # names that are findable by our default /etc/resolv.conf `search` domains, |
| 154 | # because all of our instances have names under .corp |
| 155 | def sam_servers(): |
| 156 | """ |
| 157 | Generate a list of all scheduler/afe instances of autotest. |
| 158 | |
| 159 | Note that we don't include the mysql database host if the database is split |
| 160 | from the rest of the system. |
| 161 | """ |
| 162 | sams_config = global_config.global_config.get_config_value( |
Alex Miller | b0b2d25 | 2014-06-25 17:17:01 -0700 | [diff] [blame] | 163 | 'CROS', 'sam_instances', default='') |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 164 | sams = _csv_to_list(sams_config) |
Alex Miller | b0b2d25 | 2014-06-25 17:17:01 -0700 | [diff] [blame] | 165 | return set(sams) |
| 166 | |
| 167 | |
| 168 | def extra_servers(): |
| 169 | """ |
| 170 | Servers that have an autotest checkout in /usr/local/autotest, but aren't |
| 171 | in any other list. |
| 172 | |
| 173 | @returns: A set of hosts. |
| 174 | """ |
| 175 | servers = global_config.global_config.get_config_value( |
| 176 | 'CROS', 'extra_servers', default='') |
| 177 | return set(_csv_to_list(servers)) |
| 178 | |
| 179 | |
| 180 | def test_instance(): |
| 181 | """ |
| 182 | A server that is set up to run tests of the autotest infrastructure. |
| 183 | |
| 184 | @returns: A hostname |
| 185 | """ |
| 186 | server = global_config.global_config.get_config_value( |
| 187 | 'CROS', 'test_instance', default='') |
| 188 | return server |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 189 | |
| 190 | |
| 191 | # The most reliable way to pull information about the state of the lab is to |
| 192 | # look at the global/shadow config on each server. The best way to do this is |
| 193 | # via the global_config module. Therefore, we invoke python on the remote end |
| 194 | # to call global_config to get whatever values we want. |
| 195 | _VALUE_FROM_CONFIG = ''' |
| 196 | cd /usr/local/autotest |
| 197 | python -c " |
| 198 | import common |
| 199 | from autotest_lib.client.common_lib import global_config |
| 200 | print global_config.global_config.get_config_value( |
| 201 | '%s', '%s', default='') |
| 202 | " |
| 203 | ''' |
| 204 | # There's possibly cheaper ways to do some of this, for example, we could scrape |
| 205 | # instance:13467 for the list of drones, but this way you can get the list of |
| 206 | # drones that is what should/will be running, and not what the scheduler thinks |
| 207 | # is running. (It could have kicked one out, or we could be bringing a new one |
| 208 | # into rotation.) So scraping the config on remote servers, while slow, gives |
| 209 | # us consistent logical results. |
| 210 | |
| 211 | |
| 212 | def _scrape_from_instances(section, key): |
| 213 | sams = sam_servers() |
| 214 | all_servers = set() |
| 215 | for sam in sams: |
| 216 | servers_csv = execute_command(sam, _VALUE_FROM_CONFIG % (section, key)) |
| 217 | servers = _csv_to_list(servers_csv) |
| 218 | for server in servers: |
| 219 | if server == 'localhost': |
| 220 | all_servers.add(sam) |
| 221 | else: |
| 222 | all_servers.add(server) |
| 223 | return all_servers |
| 224 | |
| 225 | |
| 226 | def database_servers(): |
| 227 | """ |
| 228 | Generate a list of all database servers running for instances of autotest. |
| 229 | |
| 230 | @returns: An iterable of all hosts. |
| 231 | """ |
| 232 | return _scrape_from_instances('AUTOTEST_WEB', 'host') |
| 233 | |
| 234 | |
| 235 | def drone_servers(): |
| 236 | """ |
| 237 | Generate a list of all drones used by all instances of autotest in |
| 238 | production. |
| 239 | |
Jakob Juelich | 0a33187 | 2014-09-25 14:45:43 -0700 | [diff] [blame] | 240 | @returns: An iterable of drone servers. |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 241 | """ |
| 242 | return _scrape_from_instances('SCHEDULER', 'drones') |
| 243 | |
| 244 | |
| 245 | def devserver_servers(): |
| 246 | """ |
| 247 | Generate a list of all devservers. |
| 248 | |
| 249 | @returns: An iterable of all hosts. |
| 250 | """ |
| 251 | zone = global_config.global_config.get_config_value( |
Hung-ying Tyan | cbdd198 | 2014-09-03 16:54:08 +0800 | [diff] [blame] | 252 | 'CLIENT', 'dns_zone') |
Alex Miller | 885543d | 2014-02-04 20:51:21 -0800 | [diff] [blame] | 253 | servers = _scrape_from_instances('CROS', 'dev_server_hosts') |
| 254 | # The default text we get back here isn't something you can ssh into unless |
| 255 | # you've set up your /etc/resolve.conf to automatically try .cros, so we |
| 256 | # append the zone to try and make this more in line with everything else. |
| 257 | return set([server+'.'+zone for server in servers]) |
Jakob Juelich | 0a33187 | 2014-09-25 14:45:43 -0700 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def shard_servers(): |
| 261 | """ |
| 262 | Generate a list of all shard servers. |
| 263 | |
| 264 | @returns: An iterable of all shard servers. |
| 265 | """ |
| 266 | shard_hostnames = set() |
| 267 | sams = sam_servers() |
| 268 | for sam in sams: |
| 269 | afe = frontend_wrappers.RetryingAFE(server=sam) |
| 270 | shards = afe.run('get_shards') |
| 271 | for shard in shards: |
| 272 | shard_hostnames.add(shard['hostname']) |
| 273 | |
| 274 | return list(shard_hostnames) |