Create storage client only once for build status updates. (#2756)

Should fix https://github.com/google/oss-fuzz/issues/2755.
This commit is contained in:
Abhishek Arya 2019-08-23 13:43:10 -07:00 committed by GitHub
parent 8799e89e3a
commit 32dd91bc40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 33 deletions

View File

@ -3,16 +3,13 @@
import datetime import datetime
import os import os
import sys import sys
import jinja2
import json import json
import tempfile import tempfile
import time import time
import dateutil.parser import dateutil.parser
from oauth2client.client import GoogleCredentials from oauth2client.client import GoogleCredentials
import googleapiclient
from googleapiclient.discovery import build as gcb_build from googleapiclient.discovery import build as gcb_build
from google.cloud import logging
from google.cloud import storage from google.cloud import storage
import build_and_run_coverage import build_and_run_coverage
@ -25,6 +22,18 @@ RETRY_COUNT = 3
RETRY_WAIT = 5 RETRY_WAIT = 5
MAX_BUILD_RESULTS = 2000 MAX_BUILD_RESULTS = 2000
BUILDS_PAGE_SIZE = 256 BUILDS_PAGE_SIZE = 256
BADGE_IMAGE_TYPES = {'svg': 'image/svg+xml', 'png': 'image/png'}
_client = None
def _get_storage_client():
"""Return storage client."""
if not _client:
global _client
_client = storage.Client()
return _client
def usage(): def usage():
@ -50,8 +59,7 @@ def upload_status(successes, failures, status_filename):
'last_updated': datetime.datetime.utcnow().ctime() 'last_updated': datetime.datetime.utcnow().ctime()
} }
storage_client = storage.Client() bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
bucket = storage_client.get_bucket(STATUS_BUCKET)
blob = bucket.blob(status_filename) blob = bucket.blob(status_filename)
blob.cache_control = 'no-cache' blob.cache_control = 'no-cache'
blob.upload_from_string(json.dumps(data), content_type='application/json') blob.upload_from_string(json.dumps(data), content_type='application/json')
@ -67,7 +75,7 @@ def find_last_build(builds, project, build_tag_suffix):
builds = builds.get(tag) builds = builds.get(tag)
if not builds: if not builds:
print >>sys.stderr, 'Failed to find builds with tag', tag print >> sys.stderr, 'Failed to find builds with tag', tag
return None return None
for build in builds: for build in builds:
@ -83,10 +91,9 @@ def find_last_build(builds, project, build_tag_suffix):
finish_time = dateutil.parser.parse(build['finishTime'], ignoretz=True) finish_time = dateutil.parser.parse(build['finishTime'], ignoretz=True)
if (datetime.datetime.utcnow() - finish_time >= if (datetime.datetime.utcnow() - finish_time >=
datetime.timedelta(minutes=DELAY_MINUTES)): datetime.timedelta(minutes=DELAY_MINUTES)):
storage_client = storage.Client() status_bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
gcb_bucket = _get_storage_client().get_bucket(
status_bucket = storage_client.get_bucket(STATUS_BUCKET) build_project.GCB_LOGS_BUCKET)
gcb_bucket = storage_client.get_bucket(build_project.GCB_LOGS_BUCKET)
log_name = 'log-{0}.txt'.format(build['id']) log_name = 'log-{0}.txt'.format(build['id'])
log = gcb_bucket.blob(log_name) log = gcb_bucket.blob(log_name)
dest_log = status_bucket.blob(log_name) dest_log = status_bucket.blob(log_name)
@ -142,8 +149,7 @@ def get_builds(cloudbuild):
return builds return builds
def update_build_status( def update_build_status(builds, projects, build_tag_suffix, status_filename):
builds, projects, build_tag_suffix, status_filename):
successes = [] successes = []
failures = [] failures = []
@ -189,26 +195,17 @@ def update_build_badges(builds, projects, build_tag, coverage_tag):
print("[badge] {}: {}".format(project, badge)) print("[badge] {}: {}".format(project, badge))
storage_client = storage.Client() for extension, mime_type in BADGE_IMAGE_TYPES.items():
status_bucket = storage_client.get_bucket(STATUS_BUCKET)
# Supported image types for badges
image_types = {
'svg': 'image/svg+xml',
'png': 'image/png'
}
for extension, mime_type in image_types.items():
badge_name = '{badge}.{extension}'.format( badge_name = '{badge}.{extension}'.format(
badge=badge, extension=extension) badge=badge, extension=extension)
# Retrieve the image relative to this script's location # Retrieve the image relative to this script's location
badge_file = os.path.join( badge_file = os.path.join(SCRIPT_DIR, 'badge_images', badge_name)
SCRIPT_DIR, 'badge_images', badge_name)
# The uploaded blob name should look like `badges/project.png` # The uploaded blob name should look like `badges/project.png`
blob_name = '{badge_dir}/{project_name}.{extension}'.format( blob_name = '{badge_dir}/{project_name}.{extension}'.format(
badge_dir=BADGE_DIR, project_name=project, badge_dir=BADGE_DIR, project_name=project, extension=extension)
extension=extension)
status_bucket = _get_storage_client().get_bucket(STATUS_BUCKET)
badge_blob = status_bucket.blob(blob_name) badge_blob = status_bucket.blob(blob_name)
badge_blob.upload_from_filename(badge_file, content_type=mime_type) badge_blob.upload_from_filename(badge_file, content_type=mime_type)
@ -224,15 +221,22 @@ def main():
cloudbuild = gcb_build('cloudbuild', 'v1', credentials=credentials) cloudbuild = gcb_build('cloudbuild', 'v1', credentials=credentials)
builds = get_builds(cloudbuild) builds = get_builds(cloudbuild)
update_build_status(builds, projects, build_project.FUZZING_BUILD_TAG, update_build_status(
status_filename='status.json') builds,
update_build_status(builds, projects, projects,
build_and_run_coverage.COVERAGE_BUILD_TAG, build_project.FUZZING_BUILD_TAG,
status_filename='status-coverage.json') status_filename='status.json')
update_build_status(
builds,
projects,
build_and_run_coverage.COVERAGE_BUILD_TAG,
status_filename='status-coverage.json')
update_build_badges(builds, projects, update_build_badges(
build_tag=build_project.FUZZING_BUILD_TAG, builds,
coverage_tag=build_and_run_coverage.COVERAGE_BUILD_TAG) projects,
build_tag=build_project.FUZZING_BUILD_TAG,
coverage_tag=build_and_run_coverage.COVERAGE_BUILD_TAG)
if __name__ == '__main__': if __name__ == '__main__':