blob: d27aff37297287b677c5cb5c2b129f470e7f8836 [file] [log] [blame]
Dan Shia1ecd5c2013-06-06 11:21:31 -07001# Copyright (c) 2013 The Chromium 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
Dan Shia1ecd5c2013-06-06 11:21:31 -07005
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -08006import httplib
7import json
Alex Millerdadc2c22013-07-08 15:21:21 -07008import logging
9import re
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080010import time
Paul Drewsbef578d2013-09-24 15:10:36 -070011import urllib2
Alex Millerdadc2c22013-07-08 15:21:21 -070012
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080013import common
Alex Millerdadc2c22013-07-08 15:21:21 -070014from autotest_lib.client.common_lib import base_utils, global_config
Dan Shia1ecd5c2013-06-06 11:21:31 -070015from autotest_lib.server.cros.dynamic_suite import constants
16
17
Alex Millerdadc2c22013-07-08 15:21:21 -070018_SHERIFF_JS = global_config.global_config.get_config_value(
19 'NOTIFICATIONS', 'sheriffs', default='')
Fang Deng3197b392013-06-26 11:42:02 -070020_LAB_SHERIFF_JS = global_config.global_config.get_config_value(
21 'NOTIFICATIONS', 'lab_sheriffs', default='')
Alex Millerdadc2c22013-07-08 15:21:21 -070022_CHROMIUM_BUILD_URL = global_config.global_config.get_config_value(
23 'NOTIFICATIONS', 'chromium_build_url', default='')
24
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080025LAB_GOOD_STATES = ('open', 'throttled')
26
27
J. Richard Barnetteabbe0962013-12-10 18:15:44 -080028class TestLabException(Exception):
29 """Exception raised when the Test Lab blocks a test or suite."""
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -080030 pass
31
32
33class ParseBuildNameException(Exception):
34 """Raised when ParseBuildName() cannot parse a build name."""
35 pass
36
37
38def ParseBuildName(name):
39 """Format a build name, given board, type, milestone, and manifest num.
40
41 @param name: a build name, e.g. 'x86-alex-release/R20-2015.0.0'
42
43 @return board: board the manifest is for, e.g. x86-alex.
44 @return type: one of 'release', 'factory', or 'firmware'
45 @return milestone: (numeric) milestone the manifest was associated with.
46 @return manifest: manifest number, e.g. '2015.0.0'
47
48 """
49 match = re.match(r'([\w-]+)-(\w+)/R(\d+)-([\d.ab-]+)', name)
50 if match and len(match.groups()) == 4:
51 return match.groups()
52 raise ParseBuildNameException('%s is a malformed build name.' % name)
53
Alex Millerdadc2c22013-07-08 15:21:21 -070054
Dan Shia1ecd5c2013-06-06 11:21:31 -070055def get_label_from_afe(hostname, label_prefix, afe):
56 """Retrieve a host's specific label from the AFE.
57
58 Looks for a host label that has the form <label_prefix>:<value>
59 and returns the "<value>" part of the label. None is returned
60 if there is not a label matching the pattern
61
62 @param hostname: hostname of given DUT.
63 @param label_prefix: prefix of label to be matched, e.g., |board:|
64 @param afe: afe instance.
65 @returns the label that matches the prefix or 'None'
66
67 """
68 labels = afe.get_labels(name__startswith=label_prefix,
69 host__hostname__in=[hostname])
70 if labels and len(labels) == 1:
71 return labels[0].name.split(label_prefix, 1)[1]
72
73
74def get_board_from_afe(hostname, afe):
75 """Retrieve given host's board from its labels in the AFE.
76
77 Looks for a host label of the form "board:<board>", and
78 returns the "<board>" part of the label. `None` is returned
79 if there is not a single, unique label matching the pattern.
80
81 @param hostname: hostname of given DUT.
82 @param afe: afe instance.
83 @returns board from label, or `None`.
84
85 """
86 return get_label_from_afe(hostname, constants.BOARD_PREFIX, afe)
87
88
89def get_build_from_afe(hostname, afe):
90 """Retrieve the current build for given host from the AFE.
91
92 Looks through the host's labels in the AFE to determine its build.
93
94 @param hostname: hostname of given DUT.
95 @param afe: afe instance.
96 @returns The current build or None if it could not find it or if there
97 were multiple build labels assigned to this host.
98
99 """
100 return get_label_from_afe(hostname, constants.VERSION_PREFIX, afe)
101
102
Fang Deng3197b392013-06-26 11:42:02 -0700103def get_sheriffs(lab_only=False):
Alex Millerdadc2c22013-07-08 15:21:21 -0700104 """
105 Polls the javascript file that holds the identity of the sheriff and
106 parses it's output to return a list of chromium sheriff email addresses.
107 The javascript file can contain the ldap of more than one sheriff, eg:
108 document.write('sheriff_one, sheriff_two').
109
Fang Deng3197b392013-06-26 11:42:02 -0700110 @param lab_only: if True, only pulls lab sheriff.
111 @return: A list of chroium.org sheriff email addresses to cc on the bug.
112 An empty list if failed to parse the javascript.
Alex Millerdadc2c22013-07-08 15:21:21 -0700113 """
114 sheriff_ids = []
Fang Deng3197b392013-06-26 11:42:02 -0700115 sheriff_js_list = _LAB_SHERIFF_JS.split(',')
116 if not lab_only:
117 sheriff_js_list.extend(_SHERIFF_JS.split(','))
118
119 for sheriff_js in sheriff_js_list:
Alex Millerdadc2c22013-07-08 15:21:21 -0700120 try:
121 url_content = base_utils.urlopen('%s%s'% (
122 _CHROMIUM_BUILD_URL, sheriff_js)).read()
123 except (ValueError, IOError) as e:
beeps4efdf032013-09-17 11:27:14 -0700124 logging.warning('could not parse sheriff from url %s%s: %s',
125 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Paul Drewsbef578d2013-09-24 15:10:36 -0700126 except (urllib2.URLError, httplib.HTTPException) as e:
127 logging.warning('unexpected error reading from url "%s%s": %s',
128 _CHROMIUM_BUILD_URL, sheriff_js, str(e))
Alex Millerdadc2c22013-07-08 15:21:21 -0700129 else:
130 ldaps = re.search(r"document.write\('(.*)'\)", url_content)
131 if not ldaps:
beeps4efdf032013-09-17 11:27:14 -0700132 logging.warning('Could not retrieve sheriff ldaps for: %s',
133 url_content)
Alex Millerdadc2c22013-07-08 15:21:21 -0700134 continue
135 sheriff_ids += ['%s@chromium.org' % alias.replace(' ', '')
136 for alias in ldaps.group(1).split(',')]
137 return sheriff_ids
beeps46dadc92013-11-07 14:07:10 -0800138
139
140def remote_wget(source_url, dest_path, ssh_cmd):
141 """wget source_url from localhost to dest_path on remote host using ssh.
142
143 @param source_url: The complete url of the source of the package to send.
144 @param dest_path: The path on the remote host's file system where we would
145 like to store the package.
146 @param ssh_cmd: The ssh command to use in performing the remote wget.
147 """
148 wget_cmd = ("wget -O - %s | %s 'cat >%s'" %
149 (source_url, ssh_cmd, dest_path))
150 base_utils.run(wget_cmd)
151
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800152
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800153_MAX_LAB_STATUS_ATTEMPTS = 5
154def _get_lab_status(status_url):
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800155 """Grabs the current lab status and message.
156
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800157 @returns The JSON object obtained from the given URL.
158
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800159 """
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800160 retry_waittime = 1
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800161 for _ in range(_MAX_LAB_STATUS_ATTEMPTS):
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800162 try:
163 response = urllib2.urlopen(status_url)
164 except IOError as e:
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800165 logging.debug('Error occurred when grabbing the lab status: %s.',
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800166 e)
167 time.sleep(retry_waittime)
168 continue
169 # Check for successful response code.
170 if response.getcode() == 200:
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800171 return json.load(response)
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800172 time.sleep(retry_waittime)
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800173 return None
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800174
175
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800176def _decode_lab_status(lab_status, build):
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800177 """Decode lab status, and report exceptions as needed.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800178
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800179 Take a deserialized JSON object from the lab status page, and
180 interpret it to determine the actual lab status. Raise
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800181 exceptions as required to report when the lab is down.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800182
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800183 @param build: build name that we want to check the status of.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800184
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800185 @raises TestLabException Raised if a request to test for the given
186 status and build should be blocked.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800187 """
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800188 # First check if the lab is up.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800189 if not lab_status['general_state'] in LAB_GOOD_STATES:
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800190 raise TestLabException('Chromium OS Test Lab is closed: '
191 '%s.' % lab_status['message'])
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800192
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800193 # Check if the build we wish to use is disabled.
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800194 # Lab messages should be in the format of:
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800195 # Lab is 'status' [regex ...] (comment)
196 # If the build name matches any regex, it will be blocked.
197 build_exceptions = re.search('\[(.*)\]', lab_status['message'])
198 if not build_exceptions:
199 return
200 for build_pattern in build_exceptions.group(1).split():
201 if re.search(build_pattern, build):
202 raise TestLabException('Chromium OS Test Lab is closed: '
203 '%s matches %s.' % (
204 build, build_pattern))
J. Richard Barnette3cbd76b2013-11-27 12:11:25 -0800205 return
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800206
207
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800208def check_lab_status(build):
209 """Check if the lab status allows us to schedule for a build.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800210
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800211 Checks if the lab is down, or if testing for the requested build
212 should be blocked.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800213
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800214 @param build: Name of the build to be scheduled for testing.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800215
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800216 @raises TestLabException Raised if a request to test for the given
217 status and build should be blocked.
J. Richard Barnette266da2a2013-11-27 15:09:55 -0800218
219 """
220 # Ensure we are trying to schedule on the actual lab.
221 test_server_name = global_config.global_config.get_config_value(
222 'SERVER', 'hostname')
223 if not test_server_name.startswith('cautotest'):
224 return
225
226 # Download the lab status from its home on the web.
227 status_url = global_config.global_config.get_config_value(
228 'CROS', 'lab_status_url')
229 json_status = _get_lab_status(status_url)
230 if json_status is None:
231 # We go ahead and say the lab is open if we can't get the status.
232 logging.warn('Could not get a status from %s', status_url)
233 return
J. Richard Barnetteabbe0962013-12-10 18:15:44 -0800234 _decode_lab_status(json_status, build)