From 259be1946eeadef02badb3c8c88f4c4d41ccc74c Mon Sep 17 00:00:00 2001 From: Brian Hannafious Date: Wed, 24 Jan 2018 17:23:11 -0800 Subject: [PATCH] use gcloud command to deploy GCF --- scripts/deploy_gcf.py | 67 ++++++------------------------------------- 1 file changed, 9 insertions(+), 58 deletions(-) diff --git a/scripts/deploy_gcf.py b/scripts/deploy_gcf.py index d6be7b06f6..d92bcfe4bd 100755 --- a/scripts/deploy_gcf.py +++ b/scripts/deploy_gcf.py @@ -3,7 +3,7 @@ This script manages the deployment of Google Cloud Functions. """ -import os, sys, time, io, zipfile, random, string, binascii, datetime, argparse, base64 +import os, sys, time, io, zipfile, random, string, binascii, datetime, argparse, base64, subprocess import boto3 import google.cloud.storage @@ -16,11 +16,6 @@ class GCPClient(ClientWithProject): SCOPE = ["https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/cloudruntimeconfig"] -class GoogleCloudFunctionsConnection(JSONConnection): - API_BASE_URL = "https://cloudfunctions.googleapis.com" - API_VERSION = "v1beta2" - API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}" - class GoogleRuntimeConfigConnection(JSONConnection): API_BASE_URL = "https://runtimeconfig.googleapis.com" API_VERSION = "v1beta1" @@ -38,7 +33,6 @@ class GoogleRuntimeConfigConnection(JSONConnection): gcp_client = GCPClient() gcp_client._http.adapters["https://"].max_retries = Retry(status_forcelist={503, 504}) grtc_conn = GoogleRuntimeConfigConnection(client=gcp_client) -gcf_conn = GoogleCloudFunctionsConnection(client=gcp_client) gcf_ns = f"projects/{gcp_client.project}/locations/{gcp_region}/functions" boto3_session = boto3.session.Session() @@ -66,54 +60,11 @@ class GoogleRuntimeConfigConnection(JSONConnection): except google.cloud.exceptions.Conflict: grtc_conn.api_request("PUT", f"/{var_ns}/{k}", data=dict(name=f"{var_ns}/{k}", value=b64v)) -try: - now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") - deploy_filename = "{}-deploy-{}-{}.zip".format(args.gcf_name, now, binascii.hexlify(os.urandom(4)).decode()) - deploy_blob = gs.bucket(os.environ["DSS_GS_BUCKET_TEST_FIXTURES"]).blob(deploy_filename) - with io.BytesIO() as buf: - with zipfile.ZipFile(buf, 'w', compression=zipfile.ZIP_DEFLATED) as zbuf: - for root, dirs, files in os.walk(args.src_dir): - for f in files: - archive_path = os.path.relpath(os.path.join(root, f), args.src_dir) - if archive_path.startswith("node_modules"): - continue - print("Adding", archive_path) - zbuf.write(os.path.join(root, f), archive_path) - zbuf.close() - deploy_blob.upload_from_string(buf.getvalue()) - print("Uploaded", deploy_blob) - - gcf_config = { - "name": f"{gcf_ns}/{args.gcf_name}", - "entryPoint": args.entry_point, - "timeout": "60s", - "availableMemoryMb": 256, - "sourceArchiveUrl": f"gs://{deploy_blob.bucket.name}/{deploy_blob.name}", - "eventTrigger": { - "eventType": "providers/cloud.storage/eventTypes/object.change", - "resource": "projects/_/buckets/" + os.environ['DSS_GS_BUCKET'] - } - } - - try: - print(gcf_conn.api_request("POST", f"/{gcf_ns}", data=gcf_config)) - except google.cloud.exceptions.Conflict: - print(gcf_conn.api_request("PUT", f"/{gcf_ns}/{args.gcf_name}", data=gcf_config)) - - sys.stderr.write("Waiting for deployment...") - sys.stderr.flush() - for t in range(90): - if gcf_conn.api_request("GET", f"/{gcf_ns}/{args.gcf_name}")["status"] != "DEPLOYING": - break - sys.stderr.write(".") - sys.stderr.flush() - time.sleep(1) - else: - sys.exit("Timeout while waiting for GCF deployment to complete") - sys.stderr.write("done\n") - - res = gcf_conn.api_request("GET", f"/{gcf_ns}/{args.gcf_name}") - print(res) - assert res["status"] == "READY" -finally: - deploy_blob.delete() +subprocess.run([ + 'gcloud', 'beta', 'functions', 'deploy', args.gcf_name, + '--timeout=%i' % 60, + '--memory=%i' % 256, + '--source=%s' % args.src_dir, + '--entry-point=%s' % args.entry_point, + '--trigger-bucket=%s' % os.environ["DSS_GS_BUCKET"], +], timeout=240)