From 878ffeaf39e5fd4aed412eba63a07fc01f86e3d6 Mon Sep 17 00:00:00 2001 From: Peter Jung Date: Thu, 15 Feb 2024 13:23:11 +0100 Subject: [PATCH] Add support for gcp secrets and labels (#11) * Add support for gcp secrets and labels --- examples/cloud_deployment/gcp/deploy.py | 15 +++++++++++---- .../deploy/gcp/deploy.py | 8 ++++++-- .../deploy/gcp/utils.py | 10 ++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/cloud_deployment/gcp/deploy.py b/examples/cloud_deployment/gcp/deploy.py index 69648d4a..5388a0de 100644 --- a/examples/cloud_deployment/gcp/deploy.py +++ b/examples/cloud_deployment/gcp/deploy.py @@ -1,6 +1,6 @@ +import getpass import os -from prediction_market_agent_tooling.config import APIKeys from prediction_market_agent_tooling.deploy.gcp.deploy import ( deploy_to_gcp, remove_deployed_gcp_function, @@ -13,13 +13,20 @@ if __name__ == "__main__": current_dir = os.path.dirname(os.path.realpath(__file__)) fname = deploy_to_gcp( - requirements_file=f"{current_dir}/../../../pyproject.toml", + requirements_file=None, extra_deps=[ - "git+https://github.com/gnosis/prediction-market-agent-tooling.git" + "git+https://github.com/gnosis/prediction-market-agent-tooling.git@main" ], function_file=f"{current_dir}/agent.py", market_type=MarketType.MANIFOLD, - api_keys={"MANIFOLD_API_KEY": APIKeys().manifold_api_key}, + labels={ + "owner": getpass.getuser() + }, # Only lowercase letters, numbers, hyphens and underscores are allowed. + env_vars={"EXAMPLE_ENV_VAR": "Gnosis"}, + # You can allow the cloud function to access secrets by adding the role: `gcloud projects add-iam-policy-binding ${GCP_PROJECT_ID} --member=serviceAccount:${GCP_SVC_ACC} --role=roles/container.admin`. + secrets={ + "MANIFOLD_API_KEY": f"JUNG_PERSONAL_GMAIL_MANIFOLD_API_KEY:latest" + }, # Must be in the format "env_var_in_container => secret_name:version", you can create secrets using `gcloud secrets create --labels owner= ` command. memory=512, ) diff --git a/prediction_market_agent_tooling/deploy/gcp/deploy.py b/prediction_market_agent_tooling/deploy/gcp/deploy.py index adfe44bf..b86c09de 100644 --- a/prediction_market_agent_tooling/deploy/gcp/deploy.py +++ b/prediction_market_agent_tooling/deploy/gcp/deploy.py @@ -25,7 +25,9 @@ def deploy_to_gcp( function_file: str, requirements_file: t.Optional[str], extra_deps: list[str], - api_keys: dict[str, str], + labels: dict[str, str], + env_vars: dict[str, str], + secrets: dict[str, str], market_type: MarketType, memory: int, # in MB ) -> str: @@ -66,7 +68,9 @@ def deploy_to_gcp( gcp_function_name=gcp_fname, source=tempdir, entry_point="main", # TODO check this function exists in main.py - api_keys=api_keys, + labels=labels, + env_vars=env_vars, + secrets=secrets, memory=memory, ) subprocess.run(cmd, shell=True) diff --git a/prediction_market_agent_tooling/deploy/gcp/utils.py b/prediction_market_agent_tooling/deploy/gcp/utils.py index b298e3f7..d0f9c373 100644 --- a/prediction_market_agent_tooling/deploy/gcp/utils.py +++ b/prediction_market_agent_tooling/deploy/gcp/utils.py @@ -11,7 +11,9 @@ def gcloud_deploy_cmd( gcp_function_name: str, source: str, entry_point: str, - api_keys: dict[str, str], + labels: dict[str, str], + env_vars: dict[str, str], + secrets: dict[str, str], memory: int, # in MB ) -> str: cmd = ( @@ -25,8 +27,12 @@ def gcloud_deploy_cmd( f"--memory {memory}MB " f"--no-allow-unauthenticated " ) - for k, v in api_keys.items(): + for k, v in labels.items(): + cmd += f"--update-labels {k}={v} " + for k, v in env_vars.items(): cmd += f"--set-env-vars {k}={v} " + for k, v in secrets.items(): + cmd += f"--set-secrets {k}={v} " return cmd