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
|
2018-01-31 21:08:24 +00:00
|
|
|
import requests
|
2018-09-27 18:19:14 +00:00
|
|
|
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)
|
|
|
|
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
|
2018-04-16 01:19:33 +00:00
|
|
|
DEADLINE_SECS = 60 * 60 # 60 minutes.
|
2018-01-31 21:08:24 +00:00
|
|
|
|
2018-06-19 17:48:18 +00:00
|
|
|
INFRA_FAILURE_ERROR_MSG = (
|
|
|
|
'\n\n'
|
|
|
|
'Your run failed due to infra failures. '
|
|
|
|
'It could be due to any of the following:\n\n'
|
|
|
|
'* Need to rebase\n\n'
|
|
|
|
'* Failure when running "python -c from gn import gn_to_bp"\n\n'
|
|
|
|
'* Problem with syncing Android repository.\n\n'
|
|
|
|
'See go/skia-android-framework-compile-bot-cloud-logs-errors for the '
|
|
|
|
'compile server\'s logs.'
|
|
|
|
)
|
|
|
|
|
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-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"""
|
|
|
|
return '%s-%s.json' % (task['issue'], task['patchset'])
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
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])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise AndroidCompileException('The %s file no longer exists.' % gs_file)
|
|
|
|
try:
|
|
|
|
ret = json.loads(output)
|
2018-10-02 13:09:02 +00:00
|
|
|
break
|
2018-09-27 18:19:14 +00:00
|
|
|
except ValueError, e:
|
|
|
|
print 'Received output that could not be converted to json: %s' % output
|
|
|
|
print e
|
|
|
|
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'):
|
2018-06-19 17:48:18 +00:00
|
|
|
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'
|
|
|
|
'No patch logs are here: %s\n\n' % (
|
2018-09-27 18:19:14 +00:00
|
|
|
ret['withpatch_log'], ret['nopatch_log']))
|
2018-01-31 21:08:24 +00:00
|
|
|
else:
|
|
|
|
print ('Both with patch and no patch builds failed. This means that the'
|
|
|
|
' Android tree is currently broken. Marking this bot as '
|
|
|
|
'successful')
|
2018-09-27 18:19:14 +00:00
|
|
|
print 'With patch logs are here: %s' % ret['withpatch_log']
|
|
|
|
print 'No patch logs are here: %s' % ret['nopatch_log']
|
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()
|
|
|
|
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.')
|
|
|
|
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()
|