Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / buildbot / lab_status.py
1 # Copyright (c) 2013 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 """Module containing utils to get and check lab status."""
6
7 import json
8 import logging
9 import re
10 import time
11 import urllib
12
13 from chromite.buildbot import constants
14
15
16 logger = logging.getLogger('chromite')
17
18
19 class LabIsDownException(Exception):
20   """Raised when the Lab is Down."""
21   pass
22
23
24 class BoardIsDisabledException(Exception):
25   """Raised when a certain board is disabled in the lab."""
26   pass
27
28
29 def GetLabStatus(max_attempts=5):
30   """Grabs the current lab status and message.
31
32   Adopted from autotest/files/client/common_lib/site_utils.py
33
34   Args:
35     max_attempts: max attempts to hit the lab status url.
36
37   Returns:
38     a dict with keys 'lab_is_up' and 'message'. lab_is_up points
39     to a boolean and message points to a string.
40   """
41   status_url = constants.LAB_STATUS_URL
42   result = {'lab_is_up': True, 'message': ''}
43   retry_waittime = 1
44   for _ in range(max_attempts):
45     try:
46       response = urllib.urlopen(status_url)
47     except IOError as e:
48       logger.log(logging.WARNING,
49                'Error occurred when grabbing the lab status: %s.', e)
50       time.sleep(retry_waittime)
51       continue
52     # Check for successful response code.
53     code = response.getcode()
54     if code == 200:
55       data = json.load(response)
56       result['lab_is_up'] = data['general_state'] in ('open', 'throttled')
57       result['message'] = data['message']
58       return result
59     else:
60       logger.log(logging.WARNING,
61                  'Get HTTP code %d when grabbing the lab status from %s',
62                  code, status_url)
63       time.sleep(retry_waittime)
64   # We go ahead and say the lab is open if we can't get the status.
65   logger.log(logging.WARNING, 'Could not get a status from %s', status_url)
66   return result
67
68
69 def CheckLabStatus(board=None):
70   """Check if the lab is up and if we can schedule suites to run.
71
72   Also checks if the lab is disabled for that particular board, and if so
73   will raise an error to prevent new suites from being scheduled for that
74   board. Adopted from autotest/files/client/common_lib/site_utils.py
75
76   Args:
77     board: board name that we want to check the status of.
78
79   Raises:
80     LabIsDownException if the lab is not up.
81     BoardIsDisabledException if the desired board is currently disabled.
82   """
83   # First check if the lab is up.
84   lab_status = GetLabStatus()
85   if not lab_status['lab_is_up']:
86     raise LabIsDownException('Chromium OS Lab is currently not up: '
87                                    '%s.' % lab_status['message'])
88
89   # Check if the board we wish to use is disabled.
90   # Lab messages should be in the format of:
91   # Lab is 'status' [boards not to be ran] (comment). Example:
92   # Lab is Open [stumpy, kiev, x86-alex] (power_resume rtc causing duts to go
93   # down)
94   boards_are_disabled = re.search('\[(.*)\]', lab_status['message'])
95   if board and boards_are_disabled:
96     if board in boards_are_disabled.group(1):
97       raise BoardIsDisabledException('Chromium OS Lab is '
98           'currently not allowing suites to be scheduled on board '
99           '%s: %s' % (board, lab_status['message']))
100   return