Skip to content

Commit c12bc74

Browse files
committed
Add check to prevent users to deploy the same snapshot multiple times
1 parent e74e8af commit c12bc74

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/zenml/deployers/base_deployer.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,46 @@ def _check_deployment_snapshot(
247247
"lead to unexpected behavior and is not allowed."
248248
)
249249

250+
def _check_snapshot_already_deployed(
251+
self,
252+
snapshot: PipelineSnapshotResponse,
253+
new_deployment_id_or_name: Union[str, UUID],
254+
) -> None:
255+
"""Check if the snapshot is already deployed to another deployment.
256+
257+
Args:
258+
snapshot: The pipeline snapshot to check.
259+
new_deployment_id_or_name: The ID or name of the deployment that is
260+
being provisioned.
261+
262+
Raises:
263+
DeploymentAlreadyExistsError: if the snapshot is already deployed to
264+
another deployment.
265+
"""
266+
if snapshot.deployment and (
267+
isinstance(snapshot.deployment.id, UUID)
268+
and snapshot.deployment.id != new_deployment_id_or_name
269+
or (
270+
isinstance(snapshot.deployment.id, str)
271+
and snapshot.deployment.name != new_deployment_id_or_name
272+
)
273+
):
274+
raise DeploymentAlreadyExistsError(
275+
f"The pipeline snapshot with name or ID "
276+
f"'{snapshot.name or snapshot.id}' "
277+
f"already has an associated deployment: "
278+
f"'{snapshot.deployment.name or snapshot.deployment.id}'. "
279+
"You can try one of the following:\n"
280+
"1. Delete the existing deployment before provisioning "
281+
f"a new one: 'zenml deployment delete "
282+
f"{snapshot.deployment.name or snapshot.deployment.id}'\n"
283+
"2. Update the existing deployment with the snapshot: 'zenml "
284+
f"pipeline snapshot deploy {snapshot.name or snapshot.id} "
285+
f"--deployment {snapshot.deployment.name or snapshot.deployment.id}'\n"
286+
"3. Create and deploy a different snapshot: 'zenml snapshot "
287+
"create ...'\n"
288+
)
289+
250290
def _generate_auth_key(self, key_length: int = 32) -> str:
251291
"""Generate an authentication key.
252292
@@ -446,6 +486,8 @@ def provision_deployment(
446486
deployment_name_or_id, project=snapshot.project_id
447487
)
448488

489+
self._check_snapshot_already_deployed(snapshot, deployment.id)
490+
449491
logger.debug(
450492
f"Existing deployment found with name '{deployment.name}'"
451493
)
@@ -455,6 +497,10 @@ def provision_deployment(
455497
f"Deployment with ID '{deployment_name_or_id}' not found"
456498
)
457499

500+
self._check_snapshot_already_deployed(
501+
snapshot, deployment_name_or_id
502+
)
503+
458504
logger.debug(
459505
f"Creating new deployment {deployment_name_or_id} with "
460506
f"snapshot ID: {snapshot.id}"

0 commit comments

Comments
 (0)