From 3a746bab8a6eb58043328df54ed47bbf6cccd5e7 Mon Sep 17 00:00:00 2001 From: Oliver Chang Date: Wed, 15 Mar 2017 18:57:58 -0700 Subject: [PATCH] [infra] gcb: Add cancel.py Also recognize CANCELLED status in wait_for_build. --- infra/gcb/cancel.py | 41 +++++++++++++++++++++++++++++++++++++ infra/gcb/wait_for_build.py | 10 ++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100755 infra/gcb/cancel.py diff --git a/infra/gcb/cancel.py b/infra/gcb/cancel.py new file mode 100755 index 000000000..a999c5e8e --- /dev/null +++ b/infra/gcb/cancel.py @@ -0,0 +1,41 @@ +#!/usr/bin/python2 + +"""Cancels project build on Google Cloud Builder. + +Usage: cancel.py +""" + +import base64 +import collections +import datetime +import os +import subprocess +import sys +import time +import urllib +import yaml + +from oauth2client.client import GoogleCredentials +from oauth2client.service_account import ServiceAccountCredentials +from googleapiclient.discovery import build + +def usage(): + sys.stderr.write( + "Usage: " + sys.argv[0] + " \n") + exit(1) + + +def main(): + if len(sys.argv) != 2: + usage() + + build_id = sys.argv[1] + + credentials = GoogleCredentials.get_application_default() + cloudbuild = build('cloudbuild', 'v1', credentials=credentials) + print cloudbuild.projects().builds().cancel( + projectId='clusterfuzz-external', id=build_id, body={}).execute() + + +if __name__ == "__main__": + main() diff --git a/infra/gcb/wait_for_build.py b/infra/gcb/wait_for_build.py index b00b12399..a6e868527 100755 --- a/infra/gcb/wait_for_build.py +++ b/infra/gcb/wait_for_build.py @@ -12,7 +12,6 @@ import datetime from googleapiclient.discovery import build from oauth2client.client import GoogleCredentials - POLL_INTERVAL = 15 cloudbuild = None @@ -29,7 +28,12 @@ def get_build(build_id, cloudbuild): def wait_for_build(build_id): - global cloudbuild + DONE_STATUSES = [ + 'SUCCESS', + 'FAILURE', + 'INTERNAL_ERROR', + 'CANCELLED', + ] status = None while True: @@ -40,7 +44,7 @@ def wait_for_build(build_id): print datetime.datetime.now(), current_status sys.stdout.flush() status = current_status - if status == 'SUCCESS' or status == 'FAILURE' or status == 'INTERNAL_ERROR': + if status in DONE_STATUSES: return status == 'SUCCESS' time.sleep(POLL_INTERVAL)