shahar1 commented on code in PR #55919: URL: https://github.com/apache/airflow/pull/55919#discussion_r2373104414
########## providers/google/src/airflow/providers/google/cloud/bundles/gcs.py: ########## @@ -0,0 +1,163 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +from pathlib import Path + +import structlog +from google.api_core.exceptions import NotFound + +from airflow.dag_processing.bundles.base import BaseDagBundle +from airflow.exceptions import AirflowException +from airflow.providers.google.cloud.hooks.gcs import GCSHook +from airflow.providers.google.common.hooks.base_google import GoogleBaseHook + + +class GCSDagBundle(BaseDagBundle): + """ + GCS DAG bundle - exposes a directory in GCS as a DAG bundle. + + This allows Airflow to load DAGs directly from a GCS bucket. + + :param gcp_conn_id: Airflow connection ID for GCS. Defaults to GoogleBaseHook.default_conn_name. + :param bucket_name: The name of the GCS bucket containing the DAG files. + :param prefix: Optional subdirectory within the GCS bucket where the DAGs are stored. + If None, DAGs are assumed to be at the root of the bucket (Optional). + """ + + supports_versioning = False + + def __init__( + self, + *, + gcp_conn_id: str = GoogleBaseHook.default_conn_name, + bucket_name: str, + prefix: str = "", + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.gcp_conn_id = gcp_conn_id + self.bucket_name = bucket_name + self.prefix = prefix + # Local path where GCS DAGs are downloaded + self.gcs_dags_dir: Path = self.base_dir + + log = structlog.get_logger(__name__) + self._log = log.bind( + bundle_name=self.name, + version=self.version, + bucket_name=self.bucket_name, + prefix=self.prefix, + gcp_conn_id=self.gcp_conn_id, + ) + self._gcs_hook: GCSHook | None = None + + def _initialize(self): + with self.lock(): + if not self.gcs_dags_dir.exists(): + self._log.info("Creating local DAGs directory: %s", self.gcs_dags_dir) + os.makedirs(self.gcs_dags_dir) + + if not self.gcs_dags_dir.is_dir(): + raise AirflowException(f"Local DAGs path: {self.gcs_dags_dir} is not a directory.") + + try: + self.gcs_hook.get_bucket(bucket_name=self.bucket_name) + except NotFound: + raise AirflowException(f"GCS bucket '{self.bucket_name}' does not exist.") + + if self.prefix: + # don't check when prefix is "" + if not self.gcs_hook.list(bucket_name=self.bucket_name, prefix=self.prefix): + raise AirflowException( + f"GCS prefix 'gs://{self.bucket_name}/{self.prefix}' does not exist." + ) + self.refresh() + + def initialize(self) -> None: + self._initialize() + super().initialize() + + @property + def gcs_hook(self): + if self._gcs_hook is None: + try: + self._gcs_hook: GCSHook = GCSHook(gcp_conn_id=self.gcp_conn_id) # Initialize GCS hook. + except AirflowException as e: + self._log.warning("Could not create GCSHook for connection %s: %s", self.gcp_conn_id, e) + return self._gcs_hook + + def __repr__(self): + return ( + f"<GCSDagBundle(" + f"name={self.name!r}, " + f"bucket_name={self.bucket_name!r}, " + f"prefix={self.prefix!r}, " + f"version={self.version!r}" + f")>" + ) + + def get_current_version(self) -> str | None: + """Return the current version of the DAG bundle. Currently not supported.""" + return None + + @property + def path(self) -> Path: + """Return the local path to the DAG files.""" + return self.gcs_dags_dir # Path where DAGs are downloaded. + + def refresh(self) -> None: + """Refresh the DAG bundle by re-downloading the DAGs from GCS.""" + if self.version: + raise AirflowException("Refreshing a specific version is not supported") + + with self.lock(): + self._log.debug( + "Downloading DAGs from gs://%s/%s to %s", self.bucket_name, self.prefix, self.gcs_dags_dir + ) + self.gcs_hook.sync_to_local_dir( + bucket_name=self.bucket_name, + prefix=self.prefix, + local_dir=self.gcs_dags_dir, + delete_stale=True, + ) + + def view_url(self, version: str | None = None) -> str | None: Review Comment: I prefer to remove it from all bundles altogether (git, S3, and GCS) after Airflow 3.1 becomes the min. supported version. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
