-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1726937
commit 500d543
Showing
9 changed files
with
221 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -108,4 +108,5 @@ | |
"bucket", | ||
], | ||
}, | ||
models.Space.TAR: {"model": models.TAR, "form": forms.TARForm, "fields": []}, | ||
} |
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
67 changes: 67 additions & 0 deletions
67
storage_service/locations/migrations/0027_tape_archive_space.py
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,67 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Migration to add a Tape Archive Space to the Storage Service.""" | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""Entry point for Tape Archive Space migration.""" | ||
|
||
dependencies = [("locations", "0026_update_package_status")] | ||
operations = [ | ||
migrations.CreateModel( | ||
name="TAR", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "Tape Archive (TAR) on Local Filesystem", | ||
}, | ||
), | ||
migrations.AlterField( | ||
model_name="space", | ||
name="access_protocol", | ||
field=models.CharField( | ||
choices=[ | ||
(b"ARKIVUM", "Arkivum"), | ||
(b"DV", "Dataverse"), | ||
(b"DC", "DuraCloud"), | ||
(b"DSPACE", "DSpace via SWORD2 API"), | ||
(b"DSPC_RST", "DSpace via REST API"), | ||
(b"FEDORA", "FEDORA via SWORD2"), | ||
(b"GPG", "GPG encryption on Local Filesystem"), | ||
(b"FS", "Local Filesystem"), | ||
(b"LOM", "LOCKSS-o-matic"), | ||
(b"NFS", "NFS"), | ||
(b"PIPE_FS", "Pipeline Local Filesystem"), | ||
(b"SWIFT", "Swift"), | ||
(b"S3", "S3"), | ||
(b"TAR", "Tape Archive (TAR) on Local Filesystem"), | ||
], | ||
help_text="How the space can be accessed.", | ||
max_length=8, | ||
verbose_name="Access protocol", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="tar", | ||
name="space", | ||
field=models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="locations.Space", | ||
to_field=b"uuid", | ||
), | ||
), | ||
] |
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
Empty file.
85 changes: 85 additions & 0 deletions
85
storage_service/locations/models/location_helpers/helpers.py
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,85 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import absolute_import | ||
|
||
import logging | ||
import os | ||
import shutil | ||
import subprocess | ||
import tarfile | ||
|
||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class TARException(Exception): | ||
pass | ||
|
||
|
||
def _abort_create_tar(path, tarpath): | ||
fail_msg = _( | ||
"Failed to create a tarfile at %(tarpath)s for dir at %(path)s" | ||
% {"tarpath": tarpath, "path": path} | ||
) | ||
LOGGER.error(fail_msg) | ||
raise TARException(fail_msg) | ||
|
||
|
||
def create_tar(path): | ||
"""Create a tarfile from the directory at ``path`` and overwrite | ||
``path`` with that tarfile. | ||
""" | ||
path = path.rstrip("/") | ||
tarpath = "{}.tar".format(path) | ||
changedir = os.path.dirname(tarpath) | ||
source = os.path.basename(path) | ||
cmd = ["tar", "-C", changedir, "-cf", tarpath, source] | ||
LOGGER.info( | ||
"creating archive of %s at %s, relative to %s", source, tarpath, changedir | ||
) | ||
try: | ||
subprocess.check_output(cmd) | ||
except (OSError, subprocess.CalledProcessError): | ||
_abort_create_tar(path, tarpath) | ||
|
||
# Providing the TAR is successfully created then remove the original. | ||
if os.path.isfile(tarpath) and tarfile.is_tarfile(tarpath): | ||
try: | ||
shutil.rmtree(path) | ||
except OSError: | ||
# Remove a file-path as We're likely packaging a file, e.g. 7z. | ||
os.remove(path) | ||
os.rename(tarpath, path) | ||
else: | ||
_abort_create_tar(path, tarpath) | ||
try: | ||
assert tarfile.is_tarfile(path) | ||
assert not os.path.exists(tarpath) | ||
except AssertionError: | ||
_abort_create_tar(path, tarpath) | ||
|
||
|
||
def _abort_extract_tar(tarpath, newtarpath, err): | ||
fail_msg = _( | ||
"Failed to extract %(tarpath)s: %(error)s" % {"tarpath": tarpath, "error": err} | ||
) | ||
LOGGER.error(fail_msg) | ||
os.rename(newtarpath, tarpath) | ||
raise TARException(fail_msg) | ||
|
||
|
||
def extract_tar(tarpath): | ||
"""Extract tarfile at ``path`` to a directory at ``path``.""" | ||
newtarpath = "{}.tar".format(tarpath) | ||
os.rename(tarpath, newtarpath) | ||
changedir = os.path.dirname(newtarpath) | ||
cmd = ["tar", "-xf", newtarpath, "-C", changedir] | ||
try: | ||
subprocess.check_output(cmd) | ||
except (OSError, subprocess.CalledProcessError) as err: | ||
_abort_extract_tar(tarpath, newtarpath, err) | ||
# TODO: GPG treats this differently because it only ever expects to | ||
# TAR a directory but we actually want to TAR file-types as well. | ||
os.remove(newtarpath) |
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,42 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Tape Archive Space created for the Norwegian Health Archive.""" | ||
|
||
from __future__ import absolute_import | ||
|
||
|
||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from .location import Location | ||
from .location_helpers.helpers import create_tar, extract_tar | ||
|
||
|
||
class TAR(models.Model): | ||
"""Space for storing packages as a Tape Archive File.""" | ||
|
||
# Package will use this attribute to determine whether the Space | ||
# is for storing Tape Archive objects. | ||
packaged_space = True | ||
|
||
space = models.OneToOneField("Space", to_field="uuid", on_delete=models.CASCADE) | ||
|
||
class Meta: | ||
verbose_name = _("Tape Archive (TAR) on Local Filesystem") | ||
app_label = _("locations") | ||
|
||
ALLOWED_LOCATION_PURPOSE = [Location.AIP_STORAGE, Location.REPLICATOR] | ||
|
||
def move_to_storage_service(self, src_path, dest_path, dest_space): | ||
""" Moves src_path to dest_space.staging_path/dest_path. """ | ||
self.space.create_local_directory(dest_path) | ||
self.space.move_rsync(src_path, dest_path, try_mv_local=True) | ||
extract_tar(dest_path) | ||
|
||
def move_from_storage_service(self, src_path, dest_path, package=None): | ||
""" Moves self.staging_path/src_path to dest_path. """ | ||
self.space.create_local_directory(dest_path) | ||
self.space.move_rsync(src_path, dest_path) | ||
create_tar(dest_path) | ||
if package.should_have_pointer_file(): | ||
"""Update the pointer file to represent the TAR packaging.""" |