summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org>2018-12-16 10:53:03 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2018-12-16 11:50:08 (GMT)
commitf9b3dff3aa426431d89d375c10383c0b0c9f76c7 (patch) (side-by-side diff)
tree48168227c5fed8b5314bf6f0af63d8e14f35edb9
parent441dccad18df38dae1623b567f1910ec6ffcc161 (diff)
downloadglacier-f9b3dff3aa426431d89d375c10383c0b0c9f76c7.tar.gz
glacier-f9b3dff3aa426431d89d375c10383c0b0c9f76c7.tar.bz2
Provide intermediate Glacier API
The class App::Glacier::Bre is a wrapper over Net::Amazon::Glacier that fixes several bugs in the parent class and provides graceful error handling using the App::Glacier::HttpCatch interface. For each Net::Amazon::Glacier method, it offers a corresponding method with capitalized name, that, instead of croaking on errors, sets up the error status and returns. Error status of the last method call is available via methods lasterr and last_error_message. Additionally, the methods of Net::Amazon::Glacier are available under their original names. * lib/App/Glacier/Bre.pm: New file. * lib/App/Glacier/Command.pm (glacier_eval,lasterr) (last_error_message): Remove. Use Bre API. * lib/App/Glacier/Command/CreateVault.pm: Use Bre API. * lib/App/Glacier/Command/DeleteFile.pm: Likewise. * lib/App/Glacier/Command/DeleteVault.pm: Likewise. * lib/App/Glacier/Command/Get.pm: Likewise. * lib/App/Glacier/Command/Jobs.pm: Likewise. * lib/App/Glacier/Command/ListVault.pm: Likewise. * lib/App/Glacier/Command/Periodic.pm: Likewise. * lib/App/Glacier/Command/Purge.pm: Likewise. * lib/App/Glacier/Command/Put.pm: Likewise. * lib/App/Glacier/Command/Sync.pm: Likewise. * lib/App/Glacier/Directory.pm: Likewise. * lib/App/Glacier/Job.pm: Use Bre API. Use the init method to initialize the job. * lib/App/Glacier/Job/ArchiveRetrieval.pm (init): New method. * lib/App/Glacier/Job/InventoryRetrieval.pm: Likewise.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--lib/App/Glacier/Bre.pm125
-rw-r--r--lib/App/Glacier/Command.pm55
-rw-r--r--lib/App/Glacier/Command/CreateVault.pm7
-rw-r--r--lib/App/Glacier/Command/DeleteFile.pm6
-rw-r--r--lib/App/Glacier/Command/DeleteVault.pm7
-rw-r--r--lib/App/Glacier/Command/Get.pm20
-rw-r--r--lib/App/Glacier/Command/Jobs.pm11
-rw-r--r--lib/App/Glacier/Command/ListVault.pm35
-rw-r--r--lib/App/Glacier/Command/Periodic.pm10
-rw-r--r--lib/App/Glacier/Command/Purge.pm7
-rw-r--r--lib/App/Glacier/Command/Put.pm30
-rw-r--r--lib/App/Glacier/Command/Sync.pm10
-rw-r--r--lib/App/Glacier/Directory.pm1
-rw-r--r--lib/App/Glacier/Job.pm50
-rw-r--r--lib/App/Glacier/Job/ArchiveRetrieval.pm33
-rw-r--r--lib/App/Glacier/Job/InventoryRetrieval.pm29
16 files changed, 277 insertions, 159 deletions
diff --git a/lib/App/Glacier/Bre.pm b/lib/App/Glacier/Bre.pm
new file mode 100644
index 0000000..0eff7a4
--- a/dev/null
+++ b/lib/App/Glacier/Bre.pm
@@ -0,0 +1,125 @@
+package App::Glacier::Bre;
+use strict;
+use warnings;
+use parent 'Net::Amazon::Glacier';
+use App::Glacier::HttpCatch;
+use Carp;
+use version 0.77;
+
+sub new {
+ my ($class, %opts) = @_;
+ my $region = delete $opts{region} or croak 'region must be supplied';
+ my $access = delete $opts{access} or croak 'access must be supplied';
+ my $secret = delete $opts{secret} or croak 'secret must be supplied';
+ $class->SUPER::new($region, $access, $secret);
+}
+
+# Fix bugs in Net::Amazon::Glacier 0.15
+if (version->parse($Net::Amazon::Glacier::VERSION) <= version->parse('0.15')) {
+ no strict 'refs';
+ *{__PACKAGE__.'::list_vaults'} = \&list_vaults_fixed;
+ *{__PACKAGE__.'::get_vault_notifications'} = \&get_vault_notifications_fixed;
+}
+
+sub _eval {
+ my $self = shift;
+ my $method = shift;
+ my $wantarray = wantarray;
+ my $ret = http_catch(sub {
+ $wantarray ? [ $self->${\$method}(@_) ]
+ : $self->${\$method}(@_)
+ },
+ err => \my %err,
+ args => \@_);
+ if (keys(%err)) {
+ $self->{_last_http_err} = \%err;
+ } else {
+ $self->{_last_http_err} = undef;
+ }
+ return (wantarray && ref($ret) eq 'ARRAY') ? @$ret : $ret;
+}
+
+sub lasterr {
+ my ($self, $key) = @_;
+ return undef unless defined $self->{_last_http_err};
+ return 1 unless defined $key;
+ return $self->{_last_http_err}{$key};
+}
+
+sub last_error_message {
+ my ($self) = @_;
+ return "No error" unless $self->lasterr;
+ return $self->lasterr('mesg') || $self->lasterr('text');
+}
+
+# list_vaults_fixed
+# A fixed version of Net::Amazon::Glacier::list_vaults. This version correctly
+# formats the query string in accordance with the specification[1].
+# The bug (along with the fix) was reported on December 15, 2018[2].
+# Hopefully it will be fixed in further versions of Net::Amazon::Glacier.
+# Until then, the initialization code above overrides installs this method
+# as list_vaults, if Net::Amazon::Glacier version 0.15 is in use.
+#
+# [1] https://docs.aws.amazon.com/amazonglacier/latest/dev/api-vaults-get.html
+# [2] https://rt.cpan.org/Public/Bug/Display.html?id=128029
+sub list_vaults_fixed {
+ my ($self) = @_;
+ my @vaults;
+
+# print "using fixed list_vaults\n";
+ my $marker;
+ do {
+ #10 is the default limit, send a marker if needed
+ my $res = $self->_send_receive(
+ GET => "/-/vaults?limit=10" . ($marker ? '&marker='.$marker : '')
+ );
+ # updated error severity
+ croak 'list_vaults failed with error ' . $res->status_line unless $res->is_success;
+ my $decoded = $self->_decode_and_handle_response( $res );
+
+ push @vaults, @{$decoded->{VaultList}};
+ $marker = $decoded->{Marker};
+ } while ( $marker );
+
+ return ( \@vaults );
+}
+
+# get_vault_notifications_fixed
+# A fixed version of Net::Amazon::Glacier::get_vault_notifications.
+# Fixes the use of HTTP method[1].
+#
+# The bug (along with the fix) was reported on December 15, 2018[2].
+# Hopefully it will be fixed in further versions of Net::Amazon::Glacier.
+# Until then, the initialization code above overrides installs this method
+# as get_vault_notifications, if Net::Amazon::Glacier version 0.15 is in use.
+#
+# [1] https://docs.aws.amazon.com/amazonglacier/latest/dev/api-vault-notifications-get.html
+# [2] https://rt.cpan.org/Public/Bug/Display.html?id=128028
+sub get_vault_notifications_fixed {
+ my ($self, $vault_name, $sns_topic, $events) = @_;
+
+ croak "no vault name given" unless $vault_name;
+
+ my $res = $self->_send_receive(
+ GET => "/-/vaults/$vault_name/notification-configuration",
+ );
+ # updated error severity
+ croak 'get_vault_notifications failed with error ' . $res->status_line
+ unless $res->is_success;
+
+ return $self->_decode_and_handle_response( $res );
+}
+
+
+our $AUTOLOAD;
+
+sub AUTOLOAD {
+ my $self = shift;
+ (my $meth = $AUTOLOAD) =~ s/.*:://;
+ if ($meth =~ s/^([A-Z])(.*)/\L$1\E$2/) {
+ return $self->_eval($meth, @_);
+ }
+ croak "unknown method $AUTOLOAD";
+}
+
+1;
diff --git a/lib/App/Glacier/Command.pm b/lib/App/Glacier/Command.pm
index 47bfce6..747abcb 100644
--- a/lib/App/Glacier/Command.pm
+++ b/lib/App/Glacier/Command.pm
@@ -9,8 +9,7 @@ use File::Basename;
use File::Spec;
use App::Glacier::EclatCreds;
use App::Glacier::Config;
-use Net::Amazon::Glacier;
-use App::Glacier::HttpCatch;
+use App::Glacier::Bre;
use App::Glacier::Timestamp;
use App::Glacier::Directory;
use App::Glacier::Roster;
@@ -151,11 +150,7 @@ sub new {
$self->{_config}->set(qw(glacier region), 'eu-west-1');
}
- $self->{_glacier} = new Net::Amazon::Glacier(
- $self->{_config}->get(qw(glacier region)),
- $self->{_config}->get(qw(glacier access)),
- $self->{_config}->get(qw(glacier secret))
- );
+ $self->{_glacier} = new App::Glacier::Bre(%{$self->config->as_hash('glacier')});
return $self;
}
@@ -201,13 +196,13 @@ sub jobdb {
sub describe_vault {
my ($self, $vault_name) = @_;
- my $res = $self->glacier_eval('describe_vault', $vault_name);
- if ($self->lasterr) {
- if ($self->lasterr('code') == 404) {
+ my $res = $self->glacier->Describe_vault($vault_name);
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
return undef;
} else {
$self->abend(EX_FAILURE, "can't list vault: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
}
return timestamp_deserialize($res);
@@ -227,10 +222,9 @@ sub directory {
return $self->{_dir}{$vault_name};
}
-sub config {
- my ($self) = @_;
- return $self->{_config};
-}
+sub config { shift->{_config} }
+
+sub glacier { shift->{_glacier} }
sub cfget {
my ($self, @path) = @_;
@@ -248,37 +242,6 @@ sub run {
$self->abend(EX_SOFTWARE, "command not implemented");
}
-sub glacier_eval {
- my $self = shift;
- my $method = shift;
- my $wantarray = wantarray;
- my $ret = http_catch(sub {
- $wantarray ? [ $self->{_glacier}->${\$method}(@_) ]
- : $self->{_glacier}->${\$method}(@_)
- },
- err => \my %err,
- args => \@_);
- if (keys(%err)) {
- $self->{_last_http_err} = \%err;
- } else {
- $self->{_last_http_err} = undef;
- }
- return (wantarray && ref($ret) eq 'ARRAY') ? @$ret : $ret;
-}
-
-sub lasterr {
- my ($self, $key) = @_;
- return undef unless defined $self->{_last_http_err};
- return 1 unless defined $key;
- return $self->{_last_http_err}{$key};
-}
-
-sub last_error_message {
- my ($self) = @_;
- return "No error" unless $self->lasterr;
- return $self->lasterr('mesg') || $self->lasterr('text');
-}
-
sub getyn {
my $self = shift;
my $in;
diff --git a/lib/App/Glacier/Command/CreateVault.pm b/lib/App/Glacier/Command/CreateVault.pm
index ce96480..9bb5be0 100644
--- a/lib/App/Glacier/Command/CreateVault.pm
+++ b/lib/App/Glacier/Command/CreateVault.pm
@@ -30,9 +30,10 @@ sub run {
$self->abend(EX_USAGE, "only one argument expected")
unless $self->command_line == 1;
my $vault_name = ($self->command_line)[0];
- $self->glacier_eval('create_vault', $vault_name);
- if ($self->lasterr) {
- $self->abend(EX_FAILURE, "can't create: ", $self->last_error_message);
+ $self->glacier->Create_vault($vault_name);
+ if ($self->glacier->lasterr) {
+ $self->abend(EX_FAILURE, "can't create: ",
+ $self->glacier->last_error_message);
}
}
diff --git a/lib/App/Glacier/Command/DeleteFile.pm b/lib/App/Glacier/Command/DeleteFile.pm
index e6a127e..074835e 100644
--- a/lib/App/Glacier/Command/DeleteFile.pm
+++ b/lib/App/Glacier/Command/DeleteFile.pm
@@ -40,11 +40,11 @@ sub run {
my $error = 0;
my $success = 0;
foreach my $ref (@{$self->get_vault_inventory($vault_name, @argv)}) {
- $self->glacier_eval('delete_archive', $vault_name, $ref->{ArchiveId});
- if ($self->lasterr) {
+ $self->glacier->Delete_archive($vault_name, $ref->{ArchiveId});
+ if ($self->glacier->lasterr) {
$self->error(EX_FAILURE,
"can't remove file \"$ref->{FileName};$ref->FileVersion}\":",
- $self->last_error_message);
+ $self->glacier->last_error_message);
$error++;
} else {
$dir->delete_version($ref->{FileName}, $ref->{FileVersion});
diff --git a/lib/App/Glacier/Command/DeleteVault.pm b/lib/App/Glacier/Command/DeleteVault.pm
index 94246e2..6f94ccd 100644
--- a/lib/App/Glacier/Command/DeleteVault.pm
+++ b/lib/App/Glacier/Command/DeleteVault.pm
@@ -30,9 +30,10 @@ sub run {
$self->abend(EX_USAGE, "one argument expected")
unless $self->command_line == 1;
my $vault_name = ($self->command_line)[0];
- $self->glacier_eval('delete_vault', $vault_name);
- if ($self->lasterr) {
- $self->abend(EX_FAILURE, "can't delete: ", $self->last_error_message);
+ $self->glacier->Delete_vault($vault_name);
+ if ($self->glacier->lasterr) {
+ $self->abend(EX_FAILURE, "can't delete: ",
+ $self->glacier->last_error_message);
} else {
my $dir = $self->directory($vault_name);
$dir->drop
diff --git a/lib/App/Glacier/Command/Get.pm b/lib/App/Glacier/Command/Get.pm
index 0fb1afb..0d17659 100644
--- a/lib/App/Glacier/Command/Get.pm
+++ b/lib/App/Glacier/Command/Get.pm
@@ -219,11 +219,11 @@ sub _download_simple {
};
return if $self->dry_run;
my $fd = $self->_open_output($localname);
- my ($res, $tree_hash) = $self->glacier_eval('get_job_output',
- $job->vault, $job->id);
- if ($self->lasterr) {
+ my ($res, $tree_hash) = $self->glacier->Get_job_output($job->vault,
+ $job->id);
+ if ($self->glacier->lasterr) {
$self->abend(EX_FAILURE, "downoad failed: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
syswrite($fd, $res);
close($fd);
@@ -287,17 +287,17 @@ sub _download_multipart {
my $range = 'bytes=' . $off . '-' . ($off + $part_size - 1);
my ($res, $hash);
for (my $try = 0;;) {
- ($res, $hash) = $self->glacier_eval('get_job_output',
- $job->vault,
- $job->id, $range);
- if ($self->lasterr) {
+ ($res, $hash) =
+ $self->glacier->Get_job_output($job->vault,
+ $job->id, $range);
+ if ($self->glacier->lasterr) {
if (++$try < $retries) {
$self->debug(1, "part $part_idx: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
$self->debug(1, "retrying");
} else {
$self->error("failed to download part $part_idx: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
return 0;
}
} else {
diff --git a/lib/App/Glacier/Command/Jobs.pm b/lib/App/Glacier/Command/Jobs.pm
index c4f244c..736d713 100644
--- a/lib/App/Glacier/Command/Jobs.pm
+++ b/lib/App/Glacier/Command/Jobs.pm
@@ -107,7 +107,6 @@ sub new {
sub run {
my $self = shift;
-# my $res = $self->glacier_eval('list_jobs');
$self->list($self->command_line);
}
@@ -131,11 +130,9 @@ sub list {
return;
}
- my $res = $self->glacier_eval('describe_job',
- $vault,
- $descr->{JobId});
- if ($self->lasterr) {
- if ($self->lasterr('code') == 404) {
+ my $res = $self->glacier->Describe_job($vault, $descr->{JobId});
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
$self->debug(1, "deleting expired $key $vault " .
($descr->{JobDescription} || $descr->{Action}) .
$descr->{JobId});
@@ -143,7 +140,7 @@ sub list {
return;
} else {
$self->error("can't describe job $descr->{JobId}: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
} elsif (ref($res) ne 'HASH') {
croak "describe_job returned wrong datatype (".ref($res).") for \"$descr->{JobId}\"";
diff --git a/lib/App/Glacier/Command/ListVault.pm b/lib/App/Glacier/Command/ListVault.pm
index 47b56c9..951eca2 100644
--- a/lib/App/Glacier/Command/ListVault.pm
+++ b/lib/App/Glacier/Command/ListVault.pm
@@ -217,10 +217,10 @@ sub get_vault_list {
if ($glob->is_literal) {
return [$self->describe_vault(@_)];
} else {
- my $res = $self->glacier_eval('list_vaults');
- if ($self->lasterr) {
+ my $res = $self->glacier->List_vaults();
+ if ($self->glacier->lasterr) {
$self->abend(EX_FAILURE, "can't list vaults: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
return [map { timestamp_deserialize($_) }
$glob->filter(sub {
@@ -233,12 +233,12 @@ sub get_vault_list {
sub list_vaults {
my ($self, $ref) = @_;
- foreach my $v (defined($self->{_options}{sort}) ?
- sort {
- &{$self->{_options}{sort}}
+ foreach my $v (defined($self->{_options}{sort})
+ ? sort {
+ &{$self->{_options}{sort}}
($self->{_options}{r} ? ($b, $a) : ($a, $b))
- } @$ref
- : @$ref) {
+ } @$ref
+ : @$ref) {
$self->show_vault($v);
}
}
@@ -260,11 +260,20 @@ sub format_size {
sub show_vault {
my ($self, $vault) = @_;
if ($self->{_options}{l}) {
- printf("%8s % 8u %s %-24s\n",
- $self->format_size($vault->{SizeInBytes}),
- $vault->{NumberOfArchives},
- $vault->{CreationDate}->canned_format($self->{_options}{time_style}),
- $vault->{VaultName});
+ if (defined($vault->{LastInventoryDate})) {
+ printf("%s % 8u %s %-24s\n",
+ $self->format_size($vault->{SizeInBytes}),
+ $vault->{NumberOfArchives},
+ $vault->{CreationDate}->canned_format($self->{_options}{time_style}),
+ $vault->{VaultName});
+ } else {
+ printf("%*s %8s %s %-24s\n",
+ length($self->format_size(0)),
+ "N/A",
+ "N/A",
+ $vault->{CreationDate}->canned_format($self->{_options}{time_style}),
+ $vault->{VaultName});
+ }
} else {
print $vault->{VaultName},"\n";
}
diff --git a/lib/App/Glacier/Command/Periodic.pm b/lib/App/Glacier/Command/Periodic.pm
index 638639a..ee72bbe 100644
--- a/lib/App/Glacier/Command/Periodic.pm
+++ b/lib/App/Glacier/Command/Periodic.pm
@@ -42,18 +42,16 @@ sub run {
$db->delete($key) unless $self->dry_run;
}
- my $res = $self->glacier_eval('describe_job',
- $vault,
- $descr->{JobId});
- if ($self->lasterr) {
- if ($self->lasterr('code') == 404) {
+ my $res = $self->glacier->Describe_job($vault, $descr->{JobId});
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
$self->debug(1, "deleting expired $key $vault " .
($descr->{JobDescription} || $descr->{Action}) .
$descr->{JobId});
$db->delete($key) unless $self->dry_run;
} else {
$self->error("can't describe job $descr->{JobId}: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
} elsif (ref($res) ne 'HASH') {
croak "describe_job returned wrong datatype (".ref($res).") for \"$descr->{JobId}\"";
diff --git a/lib/App/Glacier/Command/Purge.pm b/lib/App/Glacier/Command/Purge.pm
index a7a9eeb..6b9d40c 100644
--- a/lib/App/Glacier/Command/Purge.pm
+++ b/lib/App/Glacier/Command/Purge.pm
@@ -75,11 +75,10 @@ sub run {
foreach my $arch (@{$info}) {
$self->debug(1, "deleting $file;$ver");
return if $self->dry_run;
- $self->glacier_eval('delete_archive',
- $vault_name, $arch->{ArchiveId});
- if ($self->lasterr) {
+ $self->glacier->Delete_archive($vault_name, $arch->{ArchiveId});
+ if ($self->glacier->lasterr) {
$self->error(EX_FAILURE, "can't remove file \"$file;$ver\":",
- $self->last_error_message);
+ $self->glacier->last_error_message);
$error++;
} else {
$dir->delete_version($file, $ver);
diff --git a/lib/App/Glacier/Command/Put.pm b/lib/App/Glacier/Command/Put.pm
index 43e3724..304f889 100644
--- a/lib/App/Glacier/Command/Put.pm
+++ b/lib/App/Glacier/Command/Put.pm
@@ -165,22 +165,20 @@ sub _upload_simple {
prefix => $localname,
show_none => 1)
unless $self->{_options}{quiet};
- my $archive_id = $self->glacier_eval('upload_archive',
- $vaultname,
- $localname,
- $remotename);
+ my $archive_id = $self->glacier->Upload_archive($vaultname, $localname,
+ $remotename);
$p->finish('uploaded') if $p;
- if ($self->lasterr) {
+ if ($self->glacier->lasterr) {
$self->abend(EX_FAILURE, "upload failed: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
return $archive_id;
}
sub _upload_multipart {
my ($self, $vaultname, $localname, $remotename) = @_;
- my $glacier = $self->{_glacier};
+ my $glacier = $self->glacier;
use threads;
use threads::shared;
@@ -247,21 +245,21 @@ sub _upload_multipart {
my $res;
for (my $try = 0;;) {
- $res = $self->glacier_eval(
- 'multipart_upload_upload_part',
+ $res = $glacier->Multipart_upload_upload_part(
$vaultname,
$upload_id,
$part_size,
$part_idx,
- \$part);
- if ($self->lasterr) {
+ \$part
+ );
+ if ($glacier->lasterr) {
if (++$try < $retries) {
$self->debug(1, "part $part_idx: ",
- $self->last_error_message);
+ $glacier->last_error_message);
$self->debug(1, "retrying");
} else {
$self->error("failed to upload part $part_idx: ",
- $self->last_error_message);
+ $glacier->last_error_message);
return 0;
}
} else {
@@ -285,15 +283,15 @@ sub _upload_multipart {
# Capture archive id or error code
$self->debug(2, "finalizing the upload");
- my $archive_id = $self->glacier_eval('multipart_upload_complete',
+ my $archive_id = $glacier->Multipart_upload_complete(
$vaultname, $upload_id,
\@part_hashes,
$archive_size);
- if ($self->lasterr) {
+ if ($glacier->lasterr) {
$glacier->multipart_upload_abort($vaultname, $upload_id);
$self->abend(EX_FAILURE, "upload failed: ",
- $self->last_error_message);
+ $glacier->last_error_message);
}
# Check if we have a valid $archive_id
diff --git a/lib/App/Glacier/Command/Sync.pm b/lib/App/Glacier/Command/Sync.pm
index 1a1fcaf..4904fa6 100644
--- a/lib/App/Glacier/Command/Sync.pm
+++ b/lib/App/Glacier/Command/Sync.pm
@@ -77,13 +77,13 @@ sub sync {
$self, $vault_name,
invalidate => $opts{force});
if ($job->is_completed) {
- my $res = $self->glacier_eval('get_job_output', $vault_name, $job->id);
- if ($self->lasterr) {
- if ($self->lasterr('code') == 404 && !$opts{force}) {
+ my $res = $self->glacier->Get_job_output($vault_name, $job->id);
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404 && !$opts{force}) {
if ($opts{restart}) {
$self->abend(EX_FAILURE,
"unexpected error after restart:",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
# Job expired, delete it
# ('mesg' => 'The job ID was not found...)
@@ -92,7 +92,7 @@ sub sync {
} else {
# FIXME
$self->abend(EX_FAILURE, "can't list vault $vault_name: ",
- $self->last_error_message);
+ $self->glacier->last_error_message);
}
}
$res = decode_json($res);
diff --git a/lib/App/Glacier/Directory.pm b/lib/App/Glacier/Directory.pm
index d2b4571..7271423 100644
--- a/lib/App/Glacier/Directory.pm
+++ b/lib/App/Glacier/Directory.pm
@@ -12,7 +12,6 @@ use constant DB_INFO_KEY => ';00INFO';
sub new {
my ($class, $backend, $vault, %opts) = @_;
my $ttl = delete $opts{ttl};
- # my $test = delete $opts{test};
(my $vault_name = $vault) =~
s/([^A-Za-z_0-9\.-])/sprintf("%%%02X", ord($1))/gex;
map { $opts{$_} =~ s/\$(?:vault|\{vault\})/$vault_name/g } keys %opts;
diff --git a/lib/App/Glacier/Job.pm b/lib/App/Glacier/Job.pm
index d6b6820..ae12b22 100644
--- a/lib/App/Glacier/Job.pm
+++ b/lib/App/Glacier/Job.pm
@@ -11,7 +11,7 @@ use App::Glacier::Timestamp;
sub new {
croak "bad number of arguments" unless $#_ >= 4;
- my ($class, $cmd, $vault, $key, $init, %opts) = @_;
+ my ($class, $cmd, $vault, $key, %opts) = @_;
my $invalidate = delete $opts{invalidate};
my $ttl = delete $opts{ttl};
@@ -20,7 +20,6 @@ sub new {
}
return bless { _cmd => $cmd,
- _init => $init,
_vault => $vault,
_key => $key,
_job => undef,
@@ -36,9 +35,13 @@ sub fromdb {
_job => $job }, $class;
}
+sub command { shift->{_cmd} }
+sub glacier { shift->command->glacier }
+sub vault { shift->{_vault} };
+
sub _get_db {
my ($self) = @_;
- return $self->{_cmd}->jobdb();
+ return $self->command->jobdb();
}
sub _get_job {
@@ -56,21 +59,7 @@ sub _get_job {
my $job = $db->retrieve($self->{_key}) unless $self->{_invalidate};
if (!$job) {
$self->debug(2, "initiating job $self->{_key}");
- my $jid = $self->{_cmd}->glacier_eval(@{$self->{_init}});
- if ($self->{_cmd}->lasterr) {
- if ($self->{_cmd}->lasterr('code') == 404) {
- $self->{_cmd}->abend(EX_TEMPFAIL,
- $self->{_cmd}->last_error_message
- . "\n"
- . "Try again later or use the --cached option to see the cached content.")
- } else {
- $self->{_cmd}->abend(EX_FAILURE,
- "can't create job: ",
- $self->{_cmd}->lasterr('code'),
- $self->{_cmd}->last_error_message);
- }
- }
- $job = { JobId => $jid, Completed => 0 };
+ $job = { JobId => $self->init, Completed => 0 };
$db->store($self->{_key}, $job);
}
@@ -78,18 +67,17 @@ sub _get_job {
|| ($self->{_ttl}
&& (time - $job->{CompletionDate}->epoch) > $self->{_ttl})) {
$self->debug(2, "checking status of job $self->{_key}");
- my $res = $self->{_cmd}->glacier_eval('describe_job',
- $self->{_vault},
- $job->{JobId});
- if ($self->{_cmd}->lasterr) {
- if ($self->{_cmd}->lasterr('code') == 404) {
+ my $res = $self->glacier->Describe_job($self->{_vault},
+ $job->{JobId});
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
$self->debug(2, "job $self->{_key} expired");
$db->delete($self->{_key});
return $self->_get_job;
} else {
- $self->{_cmd}->abend(EX_UNAVAILABLE,
+ $self->command->abend(EX_UNAVAILABLE,
"can't describe job $job->{JobId}: ",
- $self->{_cmd}->last_error_message);
+ $self->glacier->last_error_message);
}
} elsif (ref($res) ne 'HASH') {
croak "describe_job returned wrong datatype (".ref($res).") for \"$job->{JobId}\"";
@@ -105,10 +93,7 @@ sub _get_job {
return $self->{_job};
}
-sub debug {
- my $self = shift;
- $self->{_cmd}->debug(@_);
-}
+sub debug { my $self = shift->command->debug(@_) }
sub id {
my $self = shift;
@@ -140,11 +125,6 @@ sub status {
return wantarray ? ($status, $self->get('StatusMessage')) : $status;
}
-sub vault {
- my $self = shift;
- return $self->{_vault};
-}
-
sub delete {
my $self = shift;
if (my $cache = $self->cache_file) {
@@ -161,7 +141,7 @@ sub cache_file {
my $aid = $self->get('ArchiveId') or return;
my $vault = $self->get('VaultARN') or return;
$vault =~ s{.*:vaults/}{};
- return $self->{_cmd}->archive_cache_filename($vault, $aid);
+ return $self->command->archive_cache_filename($vault, $aid);
}
1;
diff --git a/lib/App/Glacier/Job/ArchiveRetrieval.pm b/lib/App/Glacier/Job/ArchiveRetrieval.pm
index 660c4c9..dae6ac9 100644
--- a/lib/App/Glacier/Job/ArchiveRetrieval.pm
+++ b/lib/App/Glacier/Job/ArchiveRetrieval.pm
@@ -2,8 +2,8 @@ package App::Glacier::Job::ArchiveRetrieval;
use strict;
use warnings;
-require App::Glacier::Job;
use parent qw(App::Glacier::Job);
+use App::Glacier::Core;
use Carp;
# new(CMD, VAULT, ARCHIVE[, description => DESCR, OPTS...])
@@ -11,11 +11,36 @@ sub new {
croak "bad number of arguments" if $#_ < 3;
my ($class, $cmd, $vault, $archive, %opts) = @_;
my $descr = delete $opts{description};
- return $class->SUPER::new(
+ my $self = $class->SUPER::new(
$cmd,
$vault,
$vault . ':' . $archive,
- [ 'initiate_archive_retrieval', $vault, $archive, $descr ],
%opts
- );
+ );
+ $self->{_archive} = $archive;
+ $self->{_descr} = $descr;
+ return $self;
}
+
+sub init {
+ my $self = shift;
+ my $jid = $self->glacier->Initiate_archive_retrieval($self->vault,
+ $self->{_archive},
+ $self->{_descr});
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
+ $self->command->abend(EX_TEMPFAIL,
+ $self->glacier->last_error_message
+ . "\n"
+ . "Try again later or use the --cached option to see the cached content.")
+ } else {
+ $self->command->abend(EX_FAILURE,
+ "can't create job: ",
+ $self->command->lasterr('code'),
+ $self->command->last_error_message);
+ }
+ }
+ return $jid;
+}
+
+1;
diff --git a/lib/App/Glacier/Job/InventoryRetrieval.pm b/lib/App/Glacier/Job/InventoryRetrieval.pm
index 28aca70..e73546e 100644
--- a/lib/App/Glacier/Job/InventoryRetrieval.pm
+++ b/lib/App/Glacier/Job/InventoryRetrieval.pm
@@ -2,8 +2,8 @@ package App::Glacier::Job::InventoryRetrieval;
use strict;
use warnings;
-require App::Glacier::Job;
use parent qw(App::Glacier::Job);
+use App::Glacier::Core;
use Carp;
# new(CMD, VAULT)
@@ -12,9 +12,32 @@ sub new {
my ($class, $cmd, $vault, %opts) = @_;
return $class->SUPER::new(
$cmd, $vault, $vault,
- [ 'initiate_inventory_retrieval', $vault, 'JSON',
- "Inventory retrieval for vault $vault" ],
ttl => $cmd->cfget(qw(database inv ttl)),
%opts);
}
+sub init {
+ my $self = shift;
+ my $jid = $self->glacier->Initiate_inventory_retrieval(
+ $self->vault,
+ 'JSON',
+ "Inventory retrieval for vault ".$self->vault
+ );
+ if ($self->glacier->lasterr) {
+ if ($self->glacier->lasterr('code') == 404) {
+ $self->command->abend(EX_TEMPFAIL,
+ $self->glacier->last_error_message
+ . "\n"
+ . "Try again later or use the --cached option to see the cached content.")
+ } else {
+ $self->command->abend(EX_FAILURE,
+ "can't create job: ",
+ $self->command->lasterr('code'),
+ $self->command->last_error_message);
+ }
+ }
+ return $jid;
+}
+
+1;
+

Return to:

Send suggestions and report system problems to the System administrator.