Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Ersilia exit when trying to delete a model fetched using docker if Docker is not active #1447

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ersilia/cli/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def delete_cmd():
Delete all models:
$ ersilia delete --all
"""
def _delete(md, model_id):
md.delete(model_id)

def _delete_model_by_id(model_id):
md = ModelFullDeleter()
if md.needs_delete(model_id):
can_delete, reason = md.can_be_deleted(model_id)
if can_delete:
echo("Deleting model {0}".format(model_id))
_delete(md, model_id)
echo(
Expand All @@ -43,15 +46,12 @@ def _delete_model_by_id(model_id):
)
else:
echo(
":person_tipping_hand: Model {0} is not available locally. No delete is necessary".format(
f":person_tipping_hand: {reason}".format(
model_id
),
fg="yellow",
)

def _delete(md, model_id):
md.delete(model_id)

def _delete_all():
"""Function to delete all locally available models"""
model_catalog = ModelCatalog()
Expand Down
31 changes: 29 additions & 2 deletions ersilia/hub/delete/delete.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import shutil
import os.path
from typing import Tuple
from ... import ErsiliaBase
from ...utils.terminal import run_command
from ...utils.environment import Environment
from ...utils.conda import SimpleConda
from ...utils.system import is_inside_docker
from ..content.catalog import ModelCatalog
from ..content.card import ModelCard
from ...db.environments.localdb import EnvironmentDb
from ...db.hubdata.localslugs import SlugDb
from ...db.environments.managers import DockerManager
Expand Down Expand Up @@ -450,7 +452,7 @@ def delete(self, model_id: str):
)
)
dm = DockerManager(config_json=self.config_json)
if dm.is_active():
if dm.is_active(): # TODO This is hacky but is needed by ModelPreparer when model is fetched.
dm.delete_images(model_id)


Expand Down Expand Up @@ -535,7 +537,7 @@ def __init__(self, config_json=None, overwrite=True):
self.overwrite = overwrite
ErsiliaBase.__init__(self, config_json=config_json, credentials_json=None)

def needs_delete(self, model_id: str) -> bool:
def _needs_delete(self, model_id: str) -> bool:
"""
Checks if the model needs to be deleted.

Expand All @@ -557,6 +559,31 @@ def needs_delete(self, model_id: str) -> bool:
return True
return False

def can_be_deleted(self, model_id: str) -> Tuple[bool, str]:
"""
Checks if the model can be deleted.

Parameters
----------
model_id : str
Identifier of the model.

Returns
-------
bool
True if the model can be deleted, False otherwise.
"""
needs_delete = self._needs_delete(model_id)
mc = ModelCard(config_json=self.config_json).get(model_id)
model_source = ModelCatalog(config_json=self.config_json)._get_model_source(mc)
dm = DockerManager(config_json=self.config_json)
if needs_delete:
if model_source == "DockerHub" and not dm.is_active():
return False, "Model fetched through Docker but Docker engine is inactive."
return True, "Model can be deleted."
else:
return False, f"Model {model_id} is not available locally, no delete necessary."

def delete(self, model_id: str):
"""
Deletes all data related to the model.
Expand Down
Loading