From 52d6146a8ba6d889ccd69ea46850bfa91a0e6438 Mon Sep 17 00:00:00 2001 From: Mike Aizatsky Date: Fri, 10 Mar 2017 11:54:37 -0800 Subject: [PATCH] [infra] v0 gcb building script (#446) Doesn't perform the upload, but is supposed to build all sanitizer configuration. I was primarily testing it on json. --- infra/gcb/.gitignore | 1 + infra/gcb/build.py | 127 +++++++++++++++++++++++++++++++++++++ infra/gcb/requirements.txt | 3 + 3 files changed, 131 insertions(+) create mode 100644 infra/gcb/.gitignore create mode 100755 infra/gcb/build.py create mode 100644 infra/gcb/requirements.txt diff --git a/infra/gcb/.gitignore b/infra/gcb/.gitignore new file mode 100644 index 000000000..4d3dae74c --- /dev/null +++ b/infra/gcb/.gitignore @@ -0,0 +1 @@ +default/ diff --git a/infra/gcb/build.py b/infra/gcb/build.py new file mode 100755 index 000000000..8a9c4722e --- /dev/null +++ b/infra/gcb/build.py @@ -0,0 +1,127 @@ +#!/usr/bin/python2 + +"""Starts project build on Google Cloud Builder. + +Usage: build.py +""" + +import datetime +import os +import pprint +import sys +import yaml + +from oauth2client.client import GoogleCredentials +from googleapiclient.discovery import build + + +CONFIGURATIONS = { + 'sanitizer-address' : [ 'SANITIZER=address' ], + 'sanitizer-memory' : [ 'SANITIZER=memory' ], + 'sanitizer-undefined' : [ 'SANITIZER=undefined' ], + } + + +def usage(): + sys.stderr.write( + "Usage: " + sys.argv[0] + " \n") + exit(1) + + +def load_project_yaml(project_dir): + project_name = os.path.basename(project_dir) + project_yaml_path = os.path.join(project_dir, 'project.yaml') + with open(project_yaml_path) as f: + project_yaml = yaml.safe_load(f) + project_yaml.setdefault('name', project_name) + project_yaml.setdefault('image', + 'gcr.io/clusterfuzz-external/oss-fuzz/' + project_name) + return project_yaml + + +def get_build_steps(project_yaml): + name = project_yaml['name'] + image = project_yaml['image'] + print "Building " + image + + ts = datetime.datetime.now().strftime('%Y%m%d%H%M') + + build_steps = [ + { + 'name': 'gcr.io/cloud-builders/docker', + 'args': [ + 'build', + '-t', + image, + '.', + ], + 'dir': 'projects/' + name, + }, + { + 'name': image, + 'args': [ 'srcmap' ], + 'env': [ 'OSSFUZZ_REVISION=$REVISION_ID' ], + }, + ] + + for sanitizer in project_yaml["sanitizers"]: + env = CONFIGURATIONS["sanitizer-" + sanitizer] + out = '/workspace/out/' + sanitizer + zip_file = name + "-" + sanitizer + "-" + ts + ".zip" + + build_steps.extend([ + {'name': image, + 'env' : env, + 'args': [ + 'bash', + '-c', + 'cd /src/{1} && compile && mkdir -p {0} && cp -Rv /out/* {0}/'.format(out, name), + ], + }, + {'name': image, + 'args': [ + 'bash', + '-c', + 'cd {0} && zip -r {1} *'.format(out, zip_file) + ], + }]) + + return build_steps + + +def main(): + if len(sys.argv) != 2: + usage() + + project_dir = sys.argv[1] + project_yaml = load_project_yaml(project_dir) + + options = {} + if "GCB_OPTIONS" in os.environ: + options = yaml.safe_load(os.environ["GCB_OPTIONS"]) + print "Using options", options + + + build_body = { + 'source': { + 'repoSource': { + 'branchName': 'master', + 'projectId': 'clusterfuzz-external', + 'repoName': 'oss-fuzz', + }, + }, + 'steps': get_build_steps(project_yaml), + 'timeout': str(4 * 3600) + 's', + 'options': options, + 'images': [ project_yaml['image'] ], + } + + credentials = GoogleCredentials.get_application_default() + cloudbuild = build('cloudbuild', 'v1', credentials=credentials) + pp = pprint.PrettyPrinter(indent=4) + pp.pprint(build_body) + pp.pprint(cloudbuild.projects().builds().create(projectId='clusterfuzz-external', body=build_body).execute()) + + +if __name__ == "__main__": + main() diff --git a/infra/gcb/requirements.txt b/infra/gcb/requirements.txt new file mode 100644 index 000000000..e8128a8ba --- /dev/null +++ b/infra/gcb/requirements.txt @@ -0,0 +1,3 @@ +google-api-python-client +PyYAML +