forked from pulp/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migrate endpoint to move artifacts to another storage backend
fixes: pulp#3358
- Loading branch information
Showing
10 changed files
with
336 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added new `/migrate/` endpoint to Domains that allows for migrating artifacts from one storage backend to another. |
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,60 @@ | ||
import logging | ||
from gettext import gettext as _ | ||
|
||
from django.utils.timezone import now | ||
from pulpcore.app.models import Artifact, Domain, storage, ProgressReport | ||
from pulpcore.app.serializers import DomainBackendMigratorSerializer | ||
from pulpcore.app.util import get_domain | ||
from pulpcore.constants import TASK_STATES | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def migrate_backend(data): | ||
""" | ||
Copy the artifacts from the current storage backend to a new one. Then update backend settings. | ||
Args: | ||
data (str): encrypted json string of the new storage backend settings | ||
""" | ||
data = DomainBackendMigratorSerializer.decrypt_data(data) | ||
domain = get_domain() | ||
old_storage = domain.get_storage() | ||
new_storage = Domain(**data).get_storage() | ||
|
||
artifacts = Artifact.objects.filter(pulp_domain=domain) | ||
date = now() | ||
|
||
with ProgressReport( | ||
message=_("Migrating Artifacts"), code="migrate", total=artifacts.count() | ||
) as pb: | ||
while True: | ||
for digest in pb.iter(artifacts.values_list("sha256", flat=True)): | ||
filename = storage.get_artifact_path(digest) | ||
if not new_storage.exists(filename): | ||
new_storage.save(filename, old_storage.open(filename)) | ||
# Handle new artifacts saved by the content app | ||
artifacts = Artifact.objects.filter(pulp_domain=domain, pulp_created__gte=date) | ||
if count := artifacts.count(): | ||
pb.total += count | ||
pb.save() | ||
date = now() | ||
continue | ||
break | ||
|
||
# Update the current domain to the new storage backend settings | ||
msg = _("Update Domain({domain})'s Backend Settings").format(domain=domain.name) | ||
with ProgressReport(message=msg, code="update", total=1) as pb: | ||
# Special handling for default domain | ||
if domain.name == "default": | ||
msg = _( | ||
"PLEASE UPDATE THE CONFIG FILE WITH THE NEW STORAGE SETTINGS BEFORE RESTARTING PULP!" | ||
) | ||
_logger.warning(msg) | ||
ProgressReport(message=msg, code="URGENT", state=TASK_STATES.SKIPPED).save() | ||
pb.state = TASK_STATES.SKIPPED | ||
else: | ||
domain.storage_class = data["storage_class"] | ||
domain.storage_settings = data["storage_settings"] | ||
domain.save(update_fields=["storage_class", "storage_settings"]) | ||
pb.increment() |
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
Oops, something went wrong.