Exemple #1
0
    def _deploy(self, sourceUrl=None, entrypoint=None, config={}):
        if not self.resources:
            return

        if self.backend == "cloudfunction":
            resp = self.versioned_clients.cloudfunctions.execute(
                "get", parent_key="name", parent_schema=self.cloudfunction)
            if not resp:
                raise ValueError(f"Function {self.cloudfunction} not found")
            target = resp["httpsTrigger"]["url"]
            service_account = resp["serviceAccountEmail"]

        if self.backend == "cloudrun":
            target = get_cloudrun_url(self.versioned_clients.run, self.name)
            config = GConfig(config=config)
            if config.cloudrun and config.cloudrun.get("service-account"):
                service_account = config.cloudrun.get("service-account")
            elif config.scheduler and config.scheduler.get("serviceAccount"):
                service_account = config.scheduler.get("serviceAccount")
            else:
                raise ValueError(
                    "Service account not found in cloudrun. You can set `serviceAccount` field in config.json under `scheduler`"
                )
        log.info("deploying scheduled jobs......")
        for job_name, job in self.resources.items():
            job["job_json"]["httpTarget"]["uri"] = target
            job["job_json"]["httpTarget"]["oidcToken"][
                "serviceAccountEmail"] = service_account

            self.deploy_job(job_name, job["job_json"])
Exemple #2
0
    def _deploy_subscription(self, topic_name, topic, config={}):
        sub_name = f"{self.name}-{topic_name}"
        log.info(f"deploying pubsub subscription {sub_name}......")
        if self.backend == "cloudrun":
            push_url = get_cloudrun_url(self.versioned_clients.run, self.name)
        else:
            push_url = get_cloudfunction_url(
                self.versioned_clients.cloudfunctions, self.name)

        gconfig = GConfig(config=config)
        if gconfig.pubsub and gconfig.pubsub.get("serviceAccountEmail"):
            service_account = gconfig.pubsub.get("serviceAccountEmail")
        elif (self.backend == "cloudrun" and gconfig.cloudrun
              and gconfig.cloudrun.get("service-account")):
            service_account = gconfig.cloudrun.get("service-account")
        elif (self.backend.startswith("cloudfunction")
              and gconfig.cloudfunction
              and gconfig.pubsub.get("serviceAccountEmail")):
            service_account = gconfig.pubsub.get("serviceAccountEmail")
        else:
            raise ValueError(
                "Service account not found in cloudrun or cloudfunction. You can set `serviceAccountEmail` field in config.json under `pubsub`"
            )
        req_body = {
            "name": sub_name,
            "topic": f"projects/{topic['project']}/topics/{topic_name}",
            "filter": topic["filter"] or "",
            "pushConfig":
            {} if topic["config"].get("enableExactlyOnceDelivery", None) else {
                "pushEndpoint": push_url,
                "oidcToken": {
                    "serviceAccountEmail": service_account,
                    "audience": push_url,
                },
            },
            **topic["config"],
        }
        create_pubsub_subscription(
            client=self.versioned_clients.pubsub,
            sub_name=sub_name,
            req_body=req_body,
        )
Exemple #3
0
 def _deploy(self, source=None, entrypoint=None, config={}):
     if (
         self.routes_type != "apigateway"
         and self.backend.startswith("cloudfunction")
         and self.versioned_clients.cloudfunctions == "v1"
     ):
         raise ValueError(
             f"Cloudfunctions v1 backend is not supported for routes_type {self.routes_type}"
         )
     if len(self.resources) == 0 or self.routes_type != "apigateway":
         return
     log.info("deploying api......")
     if self.backend.startswith("cloudfunction"):
         base_url = get_cloudfunction_url(
             self.versioned_clients.cloudfunctions, self.name
         )
     if self.backend == "cloudrun":
         base_url = get_cloudrun_url(self.versioned_clients.run, self.name)
     self.generate_openapi_spec(base_url)
     try:
         resp = self.versioned_clients.apigateway_api.execute(
             "create", params={"apiId": self.name}
         )
         self.versioned_clients.apigateway_api.wait_for_operation(resp["name"])
     except HttpError as e:
         if e.resp.status == 409:
             log.info("api already deployed")
         else:
             raise e
     goblet_config = GConfig()
     config = {
         "openapiDocuments": [
             {
                 "document": {
                     "path": f"{get_g_dir()}/{self.name}_openapi_spec.yml",
                     "contents": base64.b64encode(
                         open(
                             f"{get_g_dir()}/{self.name}_openapi_spec.yml", "rb"
                         ).read()
                     ).decode("utf-8"),
                 }
             }
         ],
         **(goblet_config.apiConfig or {}),
     }
     try:
         config_version_name = self.name
         self.versioned_clients.apigateway_configs.execute(
             "create",
             params={"apiConfigId": self.name, "body": config},
             parent_schema="projects/{project_id}/locations/global/apis/"
             + self.name,
         )
     except HttpError as e:
         if e.resp.status == 409:
             log.info("updating api endpoints")
             configs = self.versioned_clients.apigateway_configs.execute(
                 "list",
                 parent_schema="projects/{project_id}/locations/global/apis/"
                 + self.name,
             )
             # TODO: use hash
             version = len(configs["apiConfigs"])
             config_version_name = f"{self.name}-v{version}"
             self.versioned_clients.apigateway_configs.execute(
                 "create",
                 parent_schema="projects/{project_id}/locations/global/apis/"
                 + self.name,
                 params={"apiConfigId": config_version_name, "body": config},
             )
         else:
             raise e
     gateway = {
         "apiConfig": f"projects/{get_default_project()}/locations/global/apis/{self.name}/configs/{config_version_name}",
     }
     try:
         gateway_resp = self.versioned_clients.apigateway.execute(
             "create", params={"gatewayId": self.name, "body": gateway}
         )
     except HttpError as e:
         if e.resp.status == 409:
             log.info("updating gateway")
             gateway_resp = self.versioned_clients.apigateway.execute(
                 "patch",
                 parent_key="name",
                 parent_schema="projects/{project_id}/locations/{location_id}/gateways/"
                 + self.name,
                 params={"updateMask": "apiConfig", "body": gateway},
             )
         else:
             raise e
     if gateway_resp:
         self.versioned_clients.apigateway.wait_for_operation(gateway_resp["name"])
     log.info("api successfully deployed...")
     gateway_resp = self.versioned_clients.apigateway.execute(
         "get",
         parent_key="name",
         parent_schema="projects/{project_id}/locations/{location_id}/gateways/"
         + self.name,
     )
     log.info(f"api endpoint is {gateway_resp['defaultHostname']}")
     return