2018-01-31 21:08:24 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (c) 2018 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Script that triggers and waits for tasks on android-compile.skia.org"""
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
import json
|
2018-09-27 18:19:14 +00:00
|
|
|
import math
|
2018-01-31 21:08:24 +00:00
|
|
|
import optparse
|
2018-09-27 18:19:14 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2018-01-31 21:08:24 +00:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
INFRA_BOTS_DIR = os.path.abspath(os.path.realpath(os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)), os.pardir)))
|
|
|
|
sys.path.insert(0, INFRA_BOTS_DIR)
|
2019-10-28 13:56:15 +00:00
|
|
|
import git_utils
|
2018-09-27 18:19:14 +00:00
|
|
|
import utils
|
2018-01-31 21:08:24 +00:00
|
|
|
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
ANDROID_COMPILE_BUCKET = 'android-compile-tasks'
|
2018-01-31 21:08:24 +00:00
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
GS_RETRIES = 5
|
|
|
|
GS_RETRY_WAIT_BASE = 15
|
|
|
|
|
|
|
|
POLLING_FREQUENCY_SECS = 10
|
2019-06-26 23:03:27 +00:00
|
|
|
DEADLINE_SECS = 2* 60 * 60 # 2 hours.
|
2018-01-31 21:08:24 +00:00
|
|
|
|
2018-06-19 17:48:18 +00:00
|
|
|
INFRA_FAILURE_ERROR_MSG = (
|
|
|
|
'\n\n'
|
2019-05-09 14:42:21 +00:00
|
|
|
'Your run failed due to unknown infrastructure failures.\n'
|
|
|
|
'Please contact rmistry@ or the trooper from '
|
2020-03-02 18:19:02 +00:00
|
|
|
'http://tree-status.skia.org/trooper\n'
|
2019-05-09 14:42:21 +00:00
|
|
|
'Sorry for the inconvenience!\n'
|
2018-06-19 17:48:18 +00:00
|
|
|
)
|
|
|
|
|
2018-01-31 21:08:24 +00:00
|
|
|
|
|
|
|
class AndroidCompileException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
def _create_task_dict(options):
|
|
|
|
"""Creates a dict representation of the requested task."""
|
2018-01-31 21:08:24 +00:00
|
|
|
params = {}
|
2018-11-26 17:36:34 +00:00
|
|
|
params['lunch_target'] = options.lunch_target
|
|
|
|
params['mmma_targets'] = options.mmma_targets
|
2018-09-27 18:19:14 +00:00
|
|
|
params['issue'] = options.issue
|
|
|
|
params['patchset'] = options.patchset
|
|
|
|
params['hash'] = options.hash
|
|
|
|
return params
|
2018-01-31 21:08:24 +00:00
|
|
|
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
def _get_gs_bucket():
|
|
|
|
"""Returns the Google storage bucket with the gs:// prefix."""
|
|
|
|
return 'gs://%s' % ANDROID_COMPILE_BUCKET
|
|
|
|
|
|
|
|
|
|
|
|
def _write_to_storage(task):
|
|
|
|
"""Writes the specified compile task to Google storage."""
|
|
|
|
with utils.tmp_dir():
|
|
|
|
json_file = os.path.join(os.getcwd(), _get_task_file_name(task))
|
|
|
|
with open(json_file, 'w') as f:
|
|
|
|
json.dump(task, f)
|
|
|
|
subprocess.check_call(['gsutil', 'cp', json_file, '%s/' % _get_gs_bucket()])
|
|
|
|
print 'Created %s/%s' % (_get_gs_bucket(), os.path.basename(json_file))
|
|
|
|
|
|
|
|
|
|
|
|
def _get_task_file_name(task):
|
|
|
|
"""Returns the file name of the compile task. Eg: ${issue}-${patchset}.json"""
|
2018-11-27 19:12:10 +00:00
|
|
|
return '%s-%s-%s.json' % (task['lunch_target'], task['issue'],
|
|
|
|
task['patchset'])
|
2018-09-27 18:19:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Checks to see if task already exists in Google storage.
|
|
|
|
# If the task has completed then the Google storage file is deleted.
|
|
|
|
def _does_task_exist_in_storage(task):
|
|
|
|
"""Checks to see if the corresponding file of the task exists in storage.
|
|
|
|
|
|
|
|
If the file exists and the task has already completed then the storage file is
|
|
|
|
deleted and False is returned.
|
|
|
|
"""
|
|
|
|
gs_file = '%s/%s' % (_get_gs_bucket(), _get_task_file_name(task))
|
2018-01-31 21:08:24 +00:00
|
|
|
try:
|
2018-09-27 18:19:14 +00:00
|
|
|
output = subprocess.check_output(['gsutil', 'cat', gs_file])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
print 'Task does not exist in Google storage'
|
|
|
|
return False
|
|
|
|
taskJSON = json.loads(output)
|
|
|
|
if taskJSON.get('done'):
|
|
|
|
print 'Task exists in Google storage and has completed.'
|
|
|
|
print 'Deleting it so that a new run can be scheduled.'
|
|
|
|
subprocess.check_call(['gsutil', 'rm', gs_file])
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
print 'Tasks exists in Google storage and is still running.'
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _trigger_task(options):
|
|
|
|
"""Triggers a task on the compile server by creating a file in storage."""
|
|
|
|
task = _create_task_dict(options)
|
|
|
|
# Check to see if file already exists in Google Storage.
|
|
|
|
if not _does_task_exist_in_storage(task):
|
|
|
|
_write_to_storage(task)
|
|
|
|
return task
|
|
|
|
|
|
|
|
|
2019-10-28 13:56:15 +00:00
|
|
|
def _add_cl_comment(issue, comment):
|
|
|
|
# Depot tools needs a checkout to use "git cl" even though we are just adding
|
|
|
|
# a comment to a change unrelated to the checkout.
|
|
|
|
with git_utils.NewGitCheckout(repository=utils.SKIA_REPO) as checkout:
|
|
|
|
add_comment_cmd = ['git', 'cl', 'comments', '-i', str(issue), '-a', comment]
|
|
|
|
subprocess.check_call(add_comment_cmd)
|
|
|
|
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
def trigger_and_wait(options):
|
|
|
|
"""Triggers a task on the compile server and waits for it to complete."""
|
|
|
|
task = _trigger_task(options)
|
|
|
|
print 'Android Compile Task for %d/%d has been successfully added to %s.' % (
|
|
|
|
options.issue, options.patchset, ANDROID_COMPILE_BUCKET)
|
|
|
|
print '%s will be polled every %d seconds.' % (ANDROID_COMPILE_BUCKET,
|
|
|
|
POLLING_FREQUENCY_SECS)
|
|
|
|
|
|
|
|
# Now poll the Google storage file till the task completes or till deadline
|
|
|
|
# is hit.
|
2018-01-31 21:08:24 +00:00
|
|
|
time_started_polling = time.time()
|
|
|
|
while True:
|
|
|
|
if (time.time() - time_started_polling) > DEADLINE_SECS:
|
|
|
|
raise AndroidCompileException(
|
|
|
|
'Task did not complete in the deadline of %s seconds.' % (
|
|
|
|
DEADLINE_SECS))
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
# Get the status of the task.
|
|
|
|
gs_file = '%s/%s' % (_get_gs_bucket(), _get_task_file_name(task))
|
|
|
|
|
|
|
|
for retry in range(GS_RETRIES):
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(['gsutil', 'cat', gs_file])
|
|
|
|
ret = json.loads(output)
|
2018-10-02 13:09:02 +00:00
|
|
|
break
|
2020-01-09 18:33:34 +00:00
|
|
|
except (ValueError, subprocess.CalledProcessError) as e:
|
|
|
|
if e.__class__ == ValueError:
|
|
|
|
print ('Received output "%s" that could not be converted to '
|
|
|
|
'json: %s' % (output, e))
|
|
|
|
elif e.__class__ == subprocess.CalledProcessError:
|
|
|
|
print e
|
2018-09-27 18:19:14 +00:00
|
|
|
if retry == (GS_RETRIES-1):
|
|
|
|
print '%d retries did not help' % GS_RETRIES
|
|
|
|
raise
|
|
|
|
waittime = GS_RETRY_WAIT_BASE * math.pow(2, retry)
|
|
|
|
print 'Retry in %d seconds.' % waittime
|
|
|
|
time.sleep(waittime)
|
|
|
|
|
|
|
|
if ret.get('infra_failure'):
|
2019-05-09 14:42:21 +00:00
|
|
|
if ret.get('error'):
|
|
|
|
raise AndroidCompileException('Run failed with:\n\n%s\n' % ret['error'])
|
|
|
|
else:
|
|
|
|
# Use a general purpose error message.
|
|
|
|
raise AndroidCompileException(INFRA_FAILURE_ERROR_MSG)
|
2018-01-31 21:08:24 +00:00
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
if ret.get('done'):
|
|
|
|
if not ret.get('is_master_branch', True):
|
2018-02-14 13:24:27 +00:00
|
|
|
print 'The Android Framework Compile bot only works for patches and'
|
|
|
|
print 'hashes from the master branch.'
|
|
|
|
return 0
|
2018-09-27 18:19:14 +00:00
|
|
|
elif ret['withpatch_success']:
|
2018-01-31 21:08:24 +00:00
|
|
|
print 'Your run was successfully completed.'
|
2018-09-27 18:19:14 +00:00
|
|
|
print 'With patch logs are here: %s' % ret['withpatch_log']
|
2018-01-31 21:08:24 +00:00
|
|
|
return 0
|
2018-09-27 18:19:14 +00:00
|
|
|
elif ret['nopatch_success']:
|
2018-01-31 21:08:24 +00:00
|
|
|
raise AndroidCompileException('The build with the patch failed and the '
|
|
|
|
'build without the patch succeeded. This means that the patch '
|
2018-02-05 18:37:29 +00:00
|
|
|
'causes Android to fail compilation.\n\n'
|
|
|
|
'With patch logs are here: %s\n\n'
|
2019-05-09 12:26:24 +00:00
|
|
|
'No patch logs are here: %s\n\n'
|
|
|
|
'You can force sync of the checkout if needed here: %s\n\n' % (
|
|
|
|
ret['withpatch_log'], ret['nopatch_log'],
|
|
|
|
'https://skia-android-compile.corp.goog/'))
|
2018-01-31 21:08:24 +00:00
|
|
|
else:
|
2019-10-28 13:56:15 +00:00
|
|
|
msg = ('FYI: Both with patch and no patch builds of the %s bot '
|
|
|
|
'failed.\nThis could mean that the Android tree is currently '
|
|
|
|
'broken and infra is investigating.\nMarking this bot as '
|
|
|
|
'successful to not block the CQ.\n\n'
|
|
|
|
'With patch logs are here: %s\n'
|
|
|
|
'No patch logs are here: %s\n\n') % (
|
|
|
|
options.builder_name, ret['withpatch_log'],
|
|
|
|
ret['nopatch_log'])
|
|
|
|
_add_cl_comment(task['issue'], msg)
|
|
|
|
print msg
|
2018-01-31 21:08:24 +00:00
|
|
|
return 0
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
# Print status of the task.
|
|
|
|
print 'Task: %s\n' % pretty_task_str(ret)
|
2018-01-31 21:08:24 +00:00
|
|
|
time.sleep(POLLING_FREQUENCY_SECS)
|
|
|
|
|
|
|
|
|
2018-09-27 18:19:14 +00:00
|
|
|
def pretty_task_str(task):
|
|
|
|
status = 'Not picked up by server yet'
|
|
|
|
if task.get('task_id'):
|
|
|
|
status = 'Running withpatch compilation'
|
|
|
|
if task.get('withpatch_log'):
|
|
|
|
status = 'Running nopatch compilation'
|
|
|
|
return '[id: %s, checkout: %s, status: %s]' % (
|
|
|
|
task.get('task_id'), task.get('checkout'), status)
|
|
|
|
|
|
|
|
|
2018-01-31 21:08:24 +00:00
|
|
|
def main():
|
|
|
|
option_parser = optparse.OptionParser()
|
2018-11-26 17:36:34 +00:00
|
|
|
option_parser.add_option(
|
|
|
|
'', '--lunch_target', type=str, default='',
|
|
|
|
help='The lunch target the android compile bot should build with.')
|
|
|
|
option_parser.add_option(
|
|
|
|
'', '--mmma_targets', type=str, default='',
|
|
|
|
help='The comma-separated mmma targets the android compile bot should '
|
|
|
|
'build.')
|
2018-01-31 21:08:24 +00:00
|
|
|
option_parser.add_option(
|
|
|
|
'', '--issue', type=int, default=0,
|
|
|
|
help='The Gerrit change number to get the patch from.')
|
|
|
|
option_parser.add_option(
|
|
|
|
'', '--patchset', type=int, default=0,
|
|
|
|
help='The Gerrit change patchset to use.')
|
|
|
|
option_parser.add_option(
|
|
|
|
'', '--hash', type=str, default='',
|
|
|
|
help='The Skia repo hash to compile against.')
|
2019-10-28 13:56:15 +00:00
|
|
|
option_parser.add_option(
|
|
|
|
'', '--builder_name', type=str, default='',
|
|
|
|
help='The builder that triggered this run.')
|
2018-01-31 21:08:24 +00:00
|
|
|
options, _ = option_parser.parse_args()
|
2018-09-27 18:19:14 +00:00
|
|
|
sys.exit(trigger_and_wait(options))
|
2018-01-31 21:08:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|