-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kedro Viz Static Website Hosting on GCP (#1711)
* CLI command kedro viz build added * Lint fix * lint fix * Lint fix * add mypy ignore * Missing build file added * Lint error fix * BaseDeployer class added * Unused code removed * Fix lint issue * azure deploy initial draft Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * added base_deployer * add deployer factory * partial working draft Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * Test and comments of deployers updated * test draft Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix lint Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * remove circular dependency Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix lint Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * revert back consent Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * initial draft Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * minor updates Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * update pytests Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * add pytest for azure shareableviz Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * refactor and add timeout Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * refactor cli Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * update pytest Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * add release note Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix flaky test Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix PR comments and flaky test Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * testing flaky c y test Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * remove flaky test Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * add pytest for gcp Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix gcp pytest coverage Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * fix lint Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * update pytest Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> * revert file permission change --------- Signed-off-by: ravi-kumar-pilla <ravi_kumar_pilla@mckinsey.com> Co-authored-by: Jitendra Gundaniya <jitendra_gundaniya@mckinsey.com> Co-authored-by: rashidakanchwala <rashida_kanchwala@mckinsey.com>
- Loading branch information
1 parent
25a7aff
commit 8f5a152
Showing
10 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""`kedro_viz.integrations.deployment.gcp_deployer` defines | ||
deployment class for Google Cloud Storage Bucket""" | ||
import glob | ||
import logging | ||
import mimetypes | ||
from pathlib import Path | ||
|
||
import fsspec | ||
|
||
from kedro_viz import __version__ | ||
from kedro_viz.integrations.deployment.base_deployer import BaseDeployer | ||
|
||
_GCP_PROTOCOL = "gcs" | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GCPDeployer(BaseDeployer): | ||
"""A class to handle the deployment of Kedro-viz to Google Cloud Storage Bucket. | ||
Attributes: | ||
_endpoint (str): GCP endpoint of the hosted site. | ||
_bucket_name (str): Name of the GCP storage bucket. | ||
_path (str): GCP protocol with bucket name. | ||
_fs (fsspec.filesystem): Filesystem for GCP protocol. | ||
""" | ||
|
||
def __init__(self, endpoint, bucket_name): | ||
"""Initialize GCPDeployer with endpoint and bucket name. | ||
Args: | ||
endpoint (str): GCP endpoint of the hosted site. | ||
bucket_name (str): Name of the GCP storage bucket. | ||
""" | ||
super().__init__() | ||
self._endpoint = endpoint | ||
self._bucket_name = bucket_name | ||
self._path = f"{_GCP_PROTOCOL}://{bucket_name}" | ||
self._fs = fsspec.filesystem(_GCP_PROTOCOL) | ||
|
||
def _upload_static_files(self, html_dir: Path): | ||
logger.debug("Uploading static html files to %s.", self._path) | ||
try: | ||
file_list = glob.glob(f"{str(html_dir)}/**/*", recursive=True) | ||
|
||
for local_file_path in file_list: | ||
content_type, _ = mimetypes.guess_type(local_file_path) | ||
|
||
# ignore directories | ||
if content_type is None: # pragma: no cover | ||
continue | ||
|
||
relative_path = local_file_path[len(str(html_dir)) + 1 :] | ||
remote_file_path = f"{self._path}/{relative_path}" | ||
|
||
# Read the contents of the local file | ||
with open(local_file_path, "rb") as file: | ||
content = file.read() | ||
|
||
self._fs.write_bytes( | ||
path=remote_file_path, | ||
value=content, | ||
content_type=content_type, | ||
) | ||
|
||
self._ingest_heap_analytics() | ||
|
||
except Exception as exc: # pragma: no cover | ||
logger.exception("Upload failed: %s ", exc) | ||
raise exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,5 +60,6 @@ | |
], | ||
"aws": ["s3fs>=2021.4"], | ||
"azure": ["adlfs>=2021.4"], | ||
"gcp": ["gcsfs>=2021.4"], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pytest | ||
|
||
from kedro_viz import __version__ | ||
from kedro_viz.integrations.deployment.gcp_deployer import GCPDeployer | ||
|
||
|
||
# Test the GCPDeployer class | ||
@pytest.fixture | ||
def endpoint(): | ||
return "http://34.120.87.227/" | ||
|
||
|
||
@pytest.fixture | ||
def bucket_name(): | ||
return "test-bucket" | ||
|
||
|
||
@pytest.fixture | ||
def mock_file_system(mocker): | ||
yield mocker.patch("fsspec.filesystem") | ||
|
||
|
||
class TestGCPDeployer: | ||
def test_deploy(self, endpoint, bucket_name, mocker): | ||
deployer = GCPDeployer(endpoint, bucket_name) | ||
|
||
mocker.patch.object(deployer, "_upload_api_responses") | ||
mocker.patch.object(deployer, "_upload_static_files") | ||
mocker.patch.object(deployer, "_upload_deploy_viz_metadata_file") | ||
|
||
deployer.deploy() | ||
|
||
deployer._upload_api_responses.assert_called_once() | ||
deployer._upload_static_files.assert_called_once() | ||
deployer._upload_deploy_viz_metadata_file.assert_called_once() | ||
|
||
def test_upload_static_files( | ||
self, endpoint, bucket_name, tmp_path, mocker, mock_file_system | ||
): | ||
deployer = GCPDeployer(endpoint, bucket_name) | ||
mock_ingest_heap_analytics = mocker.patch.object( | ||
deployer, "_ingest_heap_analytics" | ||
) | ||
mock_html_content = "<html><body>Test Content</body></html>" | ||
|
||
# Create a temporary HTML file with some test content | ||
temp_file_path = tmp_path / "test_file.html" | ||
with open(temp_file_path, "w", encoding="utf-8") as temp_file: | ||
temp_file.write(mock_html_content) | ||
|
||
with mocker.patch("mimetypes.guess_type", return_value=("text/html", None)): | ||
deployer._upload_static_files(tmp_path) | ||
deployer._fs.write_bytes.assert_called_once_with( | ||
path=f"gcs://{bucket_name}/test_file.html", | ||
value=mock_html_content.encode("utf-8"), | ||
content_type="text/html", | ||
) | ||
|
||
mock_ingest_heap_analytics.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters