[infra] gcb: Add cancel.py

Also recognize CANCELLED status in wait_for_build.
This commit is contained in:
Oliver Chang 2017-03-15 18:57:58 -07:00
parent 7df64d4ac4
commit 3a746bab8a
2 changed files with 48 additions and 3 deletions

41
infra/gcb/cancel.py Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/python2
"""Cancels project build on Google Cloud Builder.
Usage: cancel.py <build_id>
"""
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] + " <build_id>\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()

View File

@ -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)