line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yars::Routes; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: set up the routes for Yars. |
4
|
|
|
|
|
|
|
our $VERSION = '1.28'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
23
|
|
|
23
|
|
10883
|
use strict; |
|
23
|
|
|
|
|
84
|
|
|
23
|
|
|
|
|
623
|
|
8
|
23
|
|
|
23
|
|
121
|
use warnings; |
|
23
|
|
|
|
|
57
|
|
|
23
|
|
|
|
|
615
|
|
9
|
23
|
|
|
23
|
|
282
|
use Mojo::ByteStream qw/b/; |
|
23
|
|
|
|
|
47
|
|
|
23
|
|
|
|
|
1088
|
|
10
|
23
|
|
|
23
|
|
134
|
use Clustericious::Log; |
|
23
|
|
|
|
|
48
|
|
|
23
|
|
|
|
|
201
|
|
11
|
23
|
|
|
23
|
|
16911
|
use File::Path qw/mkpath/; |
|
23
|
|
|
|
|
50
|
|
|
23
|
|
|
|
|
1003
|
|
12
|
23
|
|
|
23
|
|
126
|
use File::Temp; |
|
23
|
|
|
|
|
63
|
|
|
23
|
|
|
|
|
1671
|
|
13
|
23
|
|
|
23
|
|
690
|
use Clustericious::RouteBuilder; |
|
23
|
|
|
|
|
45803
|
|
|
23
|
|
|
|
|
160
|
|
14
|
23
|
|
|
23
|
|
6095
|
use if $^O ne 'MSWin32', 'Filesys::Df' => qw/df/; |
|
23
|
|
|
|
|
61
|
|
|
23
|
|
|
|
|
185
|
|
15
|
23
|
|
|
23
|
|
33504
|
use List::Util qw/shuffle/; |
|
23
|
|
|
|
|
52
|
|
|
23
|
|
|
|
|
1127
|
|
16
|
23
|
|
|
23
|
|
651
|
use List::MoreUtils qw/uniq/; |
|
23
|
|
|
|
|
163656
|
|
|
23
|
|
|
|
|
227
|
|
17
|
23
|
|
|
23
|
|
15851
|
use Digest::file qw/digest_file_hex/; |
|
23
|
|
|
|
|
25920
|
|
|
23
|
|
|
|
|
1234
|
|
18
|
23
|
|
|
23
|
|
167
|
use File::Basename qw/basename/; |
|
23
|
|
|
|
|
156
|
|
|
23
|
|
|
|
|
1245
|
|
19
|
23
|
|
|
23
|
|
136
|
use JSON::MaybeXS qw( encode_json ); |
|
23
|
|
|
|
|
47
|
|
|
23
|
|
|
|
|
952
|
|
20
|
23
|
|
|
23
|
|
604
|
use Yars::Util qw( format_tx_error ); |
|
23
|
|
|
|
|
75
|
|
|
23
|
|
|
|
|
2288
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
BEGIN { |
23
|
23
|
50
|
|
23
|
|
90933
|
if($^O eq 'MSWin32') |
24
|
|
|
|
|
|
|
{ |
25
|
|
|
|
|
|
|
# Filesys::Df is not available for MSWin32, |
26
|
|
|
|
|
|
|
# so we use Filesys::DfPortable on that platform |
27
|
0
|
|
|
|
|
0
|
require Filesys::DfPortable; |
28
|
|
|
|
|
|
|
*df = sub { |
29
|
0
|
|
|
|
|
0
|
my $df = Filesys::DfPortable::dfportable(@_); |
30
|
0
|
|
|
|
|
0
|
$df->{used} = $df->{bused}; |
31
|
0
|
|
|
|
|
0
|
$df; |
32
|
0
|
|
|
|
|
0
|
}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
get '/' => sub { shift->render(text => "welcome to Yars", format => 'txt', status => 200 ) } => 'index'; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
get '/file/#filename/:md5' => [ md5 => qr/[a-f0-9]{32}/ ] => \&_get; |
41
|
|
|
|
|
|
|
get '/file/:md5/#filename' => [ md5 => qr/[a-f0-9]{32}/ ] => \&_get => "file"; |
42
|
|
|
|
|
|
|
sub _get { |
43
|
662
|
|
|
662
|
|
2746872
|
my $c = shift; |
44
|
662
|
|
|
|
|
3207
|
my $filename = $c->stash("filename"); |
45
|
662
|
|
|
|
|
9161
|
my $md5 = $c->stash("md5"); |
46
|
|
|
|
|
|
|
|
47
|
662
|
100
|
|
|
|
6977
|
return _head($c, @_) if $c->req->method eq 'HEAD'; |
48
|
|
|
|
|
|
|
|
49
|
562
|
|
|
|
|
12693
|
my $url = $c->tools->server_for($md5); |
50
|
562
|
100
|
|
|
|
2960
|
if ($url ne $c->tools->server_url) { |
51
|
157
|
|
|
|
|
1155
|
TRACE "$md5 should be on $url"; |
52
|
|
|
|
|
|
|
# but check our local stash first, just in case. |
53
|
157
|
100
|
|
|
|
90519
|
_get_from_local_stash($c,$filename,$md5) and return; |
54
|
113
|
|
|
|
|
641
|
$c->res->headers->add("X-Yars-Cache" => 0); |
55
|
113
|
|
|
|
|
5697
|
return $c->render_moved("$url/file/$md5/$filename"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
405
|
|
|
|
|
2064
|
my $dir = $c->tools->storage_path($md5); |
59
|
405
|
100
|
|
|
|
17764
|
-r "$dir/$filename" or do { |
60
|
|
|
|
|
|
|
return |
61
|
25
|
|
66
|
|
|
155
|
_get_from_local_stash( $c, $filename, $md5 ) |
62
|
|
|
|
|
|
|
|| _redirect_to_remote_stash( $c, $filename, $md5 ) |
63
|
|
|
|
|
|
|
|| $c->reply->not_found; |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
|
66
|
380
|
100
|
100
|
|
|
2904
|
if($c->config->download_md5_verify(default => 1) || !$c->req->headers->header('X-Yars-Skip-Verify')) { |
67
|
379
|
|
|
|
|
26617
|
my $computed = digest_file_hex("$dir/$filename",'MD5'); |
68
|
379
|
100
|
|
|
|
85277
|
unless($computed eq $md5) { |
69
|
2
|
|
|
|
|
21
|
WARN "Content mismatch, possible disk corruption ($filename), $md5 != $computed"; |
70
|
2
|
|
|
|
|
2181
|
return $c->render(text => "content-mismatch", status => 500); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
378
|
100
|
|
|
|
1881
|
if ($c->req->headers->header('X-Yars-Use-X-Accel')) { |
75
|
1
|
|
|
|
|
36
|
return _x_accel_redirect($c, "$dir/$filename", $md5); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
377
|
|
|
|
|
15126
|
$c->res->headers->add("Content-MD5", $c->tools->hex2b64($md5)); |
79
|
377
|
|
|
|
|
6937
|
$c->app->static->paths([$dir])->serve($c,$filename); |
80
|
377
|
|
|
|
|
194899
|
_set_static_headers($c,"$dir/$filename"); |
81
|
377
|
|
|
|
|
1422
|
$c->rendered; |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _x_accel_redirect |
85
|
|
|
|
|
|
|
{ |
86
|
1
|
|
|
1
|
|
4
|
my ($c, $localfile, $md5) = @_; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
6
|
my $b64 = $c->tools->hex2b64($md5); |
89
|
1
|
|
|
|
|
6
|
$c->res->headers->add("Content-MD5", $b64); |
90
|
1
|
|
|
|
|
32
|
my $types = $c->app->types; |
91
|
1
|
50
|
|
|
|
19
|
my $type = $localfile =~ /\.(\w+)$/ ? $types->type($1) : undef; |
92
|
1
|
|
33
|
|
|
16
|
$c->res->headers->content_type($type || $types->type('bin')); |
93
|
1
|
|
|
|
|
25
|
$c->res->headers->add('X-Accel-Redirect', "/static$localfile"); |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
30
|
return $c->render(status => 200, text => ''); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _set_static_headers { |
99
|
|
|
|
|
|
|
# Based on Mojolicious::Static. Probably should support if-modified..? |
100
|
402
|
|
|
402
|
|
910
|
my $c = shift; |
101
|
402
|
|
|
|
|
927
|
my $filepath = shift; |
102
|
402
|
|
|
|
|
9705
|
my ($size, $modified) = (stat $filepath)[7, 9]; |
103
|
402
|
|
|
|
|
1718
|
my $rsh = $c->res->headers; |
104
|
402
|
|
|
|
|
7132
|
$rsh->content_length($size); |
105
|
402
|
|
|
|
|
3490
|
$rsh->last_modified(Mojo::Date->new($modified)); |
106
|
402
|
|
|
|
|
10449
|
$rsh->accept_ranges('bytes'); |
107
|
402
|
|
|
|
|
3200
|
my $types = $c->app->types; |
108
|
402
|
100
|
|
|
|
4042
|
my $type = $filepath =~ /\.(\w+)$/ ? $types->type($1) : undef; |
109
|
402
|
|
66
|
|
|
1554
|
$c->res->headers->content_type($type || $types->type('bin')); |
110
|
402
|
|
|
|
|
12940
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _head { |
116
|
100
|
|
|
100
|
|
1819
|
my $c = shift; |
117
|
100
|
|
|
|
|
338
|
my $filename = $c->stash("filename"); |
118
|
100
|
|
|
|
|
1021
|
my $md5 = $c->stash("md5"); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Just check the local stash and return? |
121
|
100
|
100
|
|
|
|
953
|
my $check_stash = $c->req->headers->header("X-Yars-Check-Stash") ? 1 : 0; |
122
|
100
|
|
|
|
|
3072
|
my $url; |
123
|
100
|
100
|
|
|
|
639
|
$url = $c->tools->server_for($md5) unless $check_stash; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Check the local stash if we are asked to, or if it doesn't belong here. |
126
|
100
|
100
|
100
|
|
|
585
|
if ($check_stash or $url ne $c->tools->server_url) { |
127
|
70
|
100
|
|
|
|
553
|
if (my $found_dir = $c->tools->local_stashed_dir($filename,$md5)) { |
128
|
9
|
|
|
|
|
61
|
_set_static_headers($c,"$found_dir/$filename"); |
129
|
9
|
|
|
|
|
49
|
return $c->render(status => 200, text => 'found'); |
130
|
|
|
|
|
|
|
} |
131
|
61
|
100
|
|
|
|
542
|
return $c->reply->not_found if $check_stash; |
132
|
15
|
|
|
|
|
173
|
return $c->render_moved("$url/file/$md5/$filename"); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# It belongs here. But it might still be stashed locally or remotely. |
136
|
30
|
|
|
|
|
158
|
my $dir = $c->tools->storage_path($md5); |
137
|
30
|
100
|
|
|
|
1297
|
my $found_dir = -r "$dir/$filename" ? $dir : undef; |
138
|
30
|
|
66
|
|
|
199
|
$found_dir ||= $c->tools->local_stashed_dir( $filename, $md5 ); |
139
|
30
|
100
|
|
|
|
146
|
return if _redirect_to_remote_stash($c, $filename, $md5 ); |
140
|
28
|
100
|
|
|
|
177
|
return $c->reply->not_found unless $found_dir; |
141
|
16
|
|
|
|
|
121
|
_set_static_headers($c,"$found_dir/$filename"); |
142
|
16
|
|
|
|
|
73
|
$c->render( status => 200, text => 'found' ); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub _get_from_local_stash { |
146
|
182
|
|
|
182
|
|
648
|
my ($c,$filename,$md5) = @_; |
147
|
|
|
|
|
|
|
# If this is stashed locally, serve it and return true. |
148
|
|
|
|
|
|
|
# Otherwise return false. |
149
|
182
|
100
|
|
|
|
1324
|
my $dir = $c->tools->local_stashed_dir($filename,$md5) or return 0; |
150
|
|
|
|
|
|
|
|
151
|
44
|
50
|
33
|
|
|
313
|
if($c->config->download_md5_verify(default => 1) || !$c->req->headers->header('X-Yars-Skip-Verify')) { |
152
|
44
|
|
|
|
|
2817
|
my $computed = digest_file_hex("$dir/$filename",'MD5'); |
153
|
44
|
50
|
|
|
|
6286
|
unless($computed eq $md5) { |
154
|
0
|
|
|
|
|
0
|
WARN "Content mismatch, possible disk corruption ($filename), $md5 != $computed"; |
155
|
0
|
|
|
|
|
0
|
return $c->render(text => "content-mismatch", status => 500); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
44
|
50
|
|
|
|
195
|
if ($c->req->headers->header('X-Yars-Use-X-Accel')) { |
160
|
0
|
|
|
|
|
0
|
return _x_accel_redirect($c, "$dir/$filename", $md5); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
44
|
|
|
|
|
1420
|
$c->res->headers->add("Content-MD5", $c->tools->hex2b64($md5)); |
164
|
44
|
|
|
|
|
689
|
$c->app->static->paths([$dir])->serve($c,$filename); |
165
|
44
|
|
|
|
|
20407
|
$c->rendered; |
166
|
44
|
|
|
|
|
55818
|
return 1; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _redirect_to_remote_stash { |
170
|
55
|
|
|
55
|
|
206
|
my ($c,$filename,$digest) = @_; |
171
|
55
|
|
|
|
|
288
|
DEBUG "Checking remote stashes"; |
172
|
55
|
100
|
|
|
|
66738
|
if (my $server = $c->tools->remote_stashed_server($filename,$digest)) { |
173
|
5
|
|
|
|
|
30
|
$c->res->headers->location("$server/file/$digest/$filename"); |
174
|
5
|
|
|
|
|
174
|
$c->res->headers->content_length(0); |
175
|
5
|
|
|
|
|
132
|
$c->rendered(307); |
176
|
5
|
|
|
|
|
7713
|
return 1; |
177
|
|
|
|
|
|
|
}; |
178
|
50
|
|
|
|
|
558
|
return 0; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
put '/file/#filename/:md5' => { md5 => 'calculate' } => sub { |
183
|
|
|
|
|
|
|
my $c = shift; |
184
|
|
|
|
|
|
|
my $filename = $c->stash('filename'); |
185
|
|
|
|
|
|
|
my $md5 = $c->stash('md5'); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
my $asset = $c->req->content->asset; |
188
|
|
|
|
|
|
|
my $digest; |
189
|
|
|
|
|
|
|
if ($asset->isa("Mojo::Asset::File")) { |
190
|
|
|
|
|
|
|
TRACE "Received file asset with size ".$asset->size; |
191
|
|
|
|
|
|
|
$digest = digest_file_hex($asset->path,'MD5'); |
192
|
|
|
|
|
|
|
TRACE "Md5 of ".$asset->path." is $digest"; |
193
|
|
|
|
|
|
|
} else { |
194
|
|
|
|
|
|
|
TRACE "Received memory asset with size ".$asset->size; |
195
|
|
|
|
|
|
|
$digest = b($asset->slurp)->md5_sum->to_string; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
$md5 = $digest if $md5 eq 'calculate'; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
if ($digest ne $md5) { |
201
|
|
|
|
|
|
|
WARN "md5 mismatch : $md5 != $digest for $filename which isa ".(ref $asset); |
202
|
|
|
|
|
|
|
return $c->render(text => "incorrect digest, $md5!=$digest", status => 400); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
if ($c->req->headers->header('X-Yars-Stash')) { |
206
|
|
|
|
|
|
|
DEBUG "Stashing a file that is not ours : $digest $filename"; |
207
|
|
|
|
|
|
|
_stash_locally($c, $filename, $digest, $asset) and return; |
208
|
|
|
|
|
|
|
return $c->reply->exception("Cannot stash $filename locally"); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
DEBUG "Received NoStash for $filename" if $c->req->headers->header('X-Yars-NoStash'); |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $assigned_server = $c->tools->server_for($digest); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
if ( $assigned_server ne $c->tools->server_url ) { |
216
|
|
|
|
|
|
|
TRACE "assigned $assigned_server != ".$c->tools->server_url; |
217
|
|
|
|
|
|
|
return _proxy_to( $c, $assigned_server, $filename, $digest, $asset, 0 ) |
218
|
|
|
|
|
|
|
|| _stash_locally( $c, $filename, $digest, $asset ) |
219
|
|
|
|
|
|
|
|| _stash_remotely( $c, $filename, $digest, $asset ) |
220
|
|
|
|
|
|
|
|| $c->render(status => 507, text => "Unable to proxy or stash"); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
my $assigned_disk = $c->tools->disk_for($digest); |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
DEBUG "Received $filename assigned to $assigned_server ($assigned_disk), this is ".$c->tools->server_url; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
unless (-d $assigned_disk) { |
228
|
|
|
|
|
|
|
INFO "$assigned_disk does not exist, creating it now"; |
229
|
|
|
|
|
|
|
mkdir $assigned_disk or WARN "Failed to mkdir $assigned_disk : $!"; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
if ( $c->tools->disk_is_up($assigned_disk) ) { |
232
|
|
|
|
|
|
|
my $assigned_path = $c->tools->storage_path($digest, $assigned_disk); |
233
|
|
|
|
|
|
|
my $abs_path = join '/', $assigned_path, $filename; |
234
|
|
|
|
|
|
|
my $location = $c->url_for("file", md5 => $digest, filename => $filename)->to_abs; |
235
|
|
|
|
|
|
|
if (-e $abs_path) { |
236
|
|
|
|
|
|
|
TRACE "Found another file at $abs_path, comparing content"; |
237
|
|
|
|
|
|
|
my $old_md5 = digest_file_hex($abs_path,"MD5"); |
238
|
|
|
|
|
|
|
if ($old_md5 eq $digest) { |
239
|
|
|
|
|
|
|
if ($c->tools->content_is_same($abs_path,$asset)) { |
240
|
|
|
|
|
|
|
$c->res->headers->location($location); |
241
|
|
|
|
|
|
|
return $c->render(status => 200, text => 'exists'); |
242
|
|
|
|
|
|
|
} else { |
243
|
|
|
|
|
|
|
WARN "Same md5, but different content for $filename"; |
244
|
|
|
|
|
|
|
return $c->render(status => 409, text => 'md5 collision'); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
DEBUG "md5 of content in $abs_path was incorrect; replacing corrupt file" |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
if (my $existing = _other_files_in_path( $assigned_path ) ) { |
250
|
|
|
|
|
|
|
if (_make_link($existing,"$assigned_path/$filename")) { |
251
|
|
|
|
|
|
|
$c->res->headers->location($location); |
252
|
|
|
|
|
|
|
return $c->render(status => 201, text => 'ok'); # CREATED |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
if (_atomic_write( $assigned_path , $filename, $asset ) ) { |
256
|
|
|
|
|
|
|
# Normal situation. |
257
|
|
|
|
|
|
|
$c->res->headers->location($location); |
258
|
|
|
|
|
|
|
return $c->render(status => 201, text => 'ok'); # CREATED |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
} else { |
261
|
|
|
|
|
|
|
DEBUG "Disk $assigned_disk is not up"; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
# Local designated disk is down. |
265
|
|
|
|
|
|
|
_stash_locally( $c, $filename, $digest, $asset ) |
266
|
|
|
|
|
|
|
or _stash_remotely( $c, $filename, $digest, $asset ) |
267
|
|
|
|
|
|
|
or $c->render(status => 507, text => "Unable to proxy or stash"); |
268
|
|
|
|
|
|
|
}; |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub _other_files_in_path { |
271
|
218
|
|
|
218
|
|
553
|
my $path = shift; |
272
|
218
|
100
|
|
|
|
5381
|
opendir( DR, $path ) or return; |
273
|
1
|
|
|
|
|
4
|
my $found; |
274
|
1
|
|
|
|
|
31
|
while ( $_ = readdir DR ) { |
275
|
1
|
50
|
|
|
|
5
|
next if /^\.\.?$/; |
276
|
1
|
|
|
|
|
3
|
$found = $_; |
277
|
1
|
|
|
|
|
2
|
last; |
278
|
|
|
|
|
|
|
} |
279
|
1
|
|
|
|
|
43
|
closedir DR; |
280
|
1
|
50
|
|
|
|
5
|
return unless $found; |
281
|
1
|
|
|
|
|
7
|
return "$path/$found"; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub _make_link { |
285
|
1
|
|
|
1
|
|
3
|
my ($old,$new) = @_; |
286
|
1
|
|
|
|
|
6
|
DEBUG "Making a hard link for $new"; |
287
|
1
|
|
|
|
|
1186
|
my $status = link($old,$new); |
288
|
1
|
50
|
|
|
|
6
|
WARN "Failed to link $old to $new : $!" unless $status; |
289
|
1
|
|
|
|
|
3
|
return $status; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
sub _proxy_to { |
293
|
107
|
|
|
107
|
|
386
|
my ($c, $url,$filename,$digest,$asset,$temporary) = @_; |
294
|
|
|
|
|
|
|
# Proxy a file to another url. |
295
|
|
|
|
|
|
|
# On success, render the response and return true. |
296
|
|
|
|
|
|
|
# On failure, return false. |
297
|
107
|
|
|
|
|
206
|
my $res; |
298
|
107
|
50
|
|
|
|
1207
|
DEBUG "Proxying file $filename with md5 $digest to $url/file/$filename/$digest" |
299
|
|
|
|
|
|
|
. ( $temporary ? " temporarily" : "" ); |
300
|
107
|
50
|
|
|
|
201782
|
my $headers = $temporary ? { 'X-Yars-Stash' => 1 } : {}; |
301
|
107
|
|
|
|
|
855
|
$headers->{"Content-MD5"} = $c->tools->hex2b64($digest); |
302
|
107
|
|
|
|
|
377
|
$headers->{Connection} = "Close"; |
303
|
107
|
|
|
|
|
564
|
my $tx = $c->tools->_ua->build_tx(PUT => "$url/file/$filename/$digest", $headers ); |
304
|
107
|
|
|
|
|
30492
|
$tx->req->content->asset($asset); |
305
|
107
|
|
|
|
|
1817
|
$tx = $c->tools->_ua->start($tx); |
306
|
107
|
100
|
|
|
|
442174
|
if ($res = $tx->success) { |
307
|
63
|
|
|
|
|
1869
|
my $headers = $c->res->headers; |
308
|
63
|
|
|
|
|
1271
|
$headers->location($tx->res->headers->location); |
309
|
63
|
50
|
|
|
|
1822
|
$headers->add("X-Yars-Cache" => 0) unless $temporary; |
310
|
63
|
|
|
|
|
1016
|
$c->render(status => $tx->res->code, text => 'ok'); |
311
|
63
|
|
|
|
|
113073
|
return 1; |
312
|
|
|
|
|
|
|
} |
313
|
44
|
|
|
|
|
1220
|
ERROR "failed to proxy $filename to $url : " . format_tx_error($tx->error); |
314
|
44
|
|
|
|
|
30656
|
return 0; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
sub _atomic_write { |
318
|
276
|
|
|
276
|
|
811
|
my ($dir, $filename, $asset) = @_; |
319
|
276
|
|
|
|
|
1788
|
TRACE "Writing $dir/$filename"; |
320
|
|
|
|
|
|
|
# Write a file atomically. Return 1 on success, 0 on failure. |
321
|
276
|
|
|
|
|
168480
|
my $failed; |
322
|
276
|
|
|
|
|
655
|
eval { |
323
|
276
|
|
|
|
|
317807
|
mkpath $dir; # dies on error |
324
|
276
|
50
|
|
|
|
2583
|
$asset->move_to("$dir/$filename") or LOGDIE "failed to write $dir/$filename: $!"; |
325
|
276
|
50
|
|
|
|
51010
|
}; if($@) { |
326
|
0
|
|
|
|
|
0
|
WARN "Could not write $dir/$filename : $@"; |
327
|
0
|
|
|
|
|
0
|
$failed = 1; |
328
|
|
|
|
|
|
|
}; |
329
|
276
|
50
|
|
|
|
877
|
return 0 if $failed; |
330
|
276
|
|
|
|
|
1646
|
TRACE "Wrote $dir/$filename"; |
331
|
276
|
|
|
|
|
168547
|
return 1; |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
sub _stash_locally { |
335
|
59
|
|
|
59
|
|
218
|
my ($c, $filename,$digest, $asset) = @_; |
336
|
|
|
|
|
|
|
# Stash this file on a local disk. |
337
|
|
|
|
|
|
|
# Returns false or renders the response. |
338
|
|
|
|
|
|
|
|
339
|
59
|
50
|
|
|
|
261
|
return 0 if $c->req->headers->header('X-Yars-NoStash'); |
340
|
|
|
|
|
|
|
|
341
|
59
|
|
|
|
|
2037
|
DEBUG "Stashing $filename locally"; |
342
|
59
|
|
|
|
|
63033
|
my $assigned_root = $c->tools->disk_for($digest); |
343
|
59
|
|
100
|
|
|
373
|
$assigned_root //= ''; |
344
|
59
|
|
|
|
|
110
|
my $wrote; |
345
|
59
|
|
|
|
|
382
|
for my $root (shuffle($c->tools->disk_roots)) { |
346
|
73
|
|
|
|
|
363
|
TRACE "Trying $root (assigned : $assigned_root)"; |
347
|
73
|
100
|
100
|
|
|
41834
|
next if $assigned_root && ($root eq $assigned_root); |
348
|
68
|
100
|
|
|
|
494
|
unless ($c->tools->disk_is_up($root)) { |
349
|
9
|
|
|
|
|
50
|
DEBUG "local disk $root is down, cannot stash $filename there."; |
350
|
9
|
|
|
|
|
8954
|
next; |
351
|
|
|
|
|
|
|
} |
352
|
59
|
|
|
|
|
11339
|
my $dir = $c->tools->storage_path( $digest, $root ); |
353
|
59
|
50
|
|
|
|
356
|
_atomic_write( $dir, $filename, $asset ) and do { |
354
|
59
|
|
|
|
|
129
|
$wrote = $root; |
355
|
59
|
|
|
|
|
145
|
last; |
356
|
|
|
|
|
|
|
}; |
357
|
0
|
|
|
|
|
0
|
TRACE "write failed"; |
358
|
|
|
|
|
|
|
} |
359
|
59
|
50
|
|
|
|
221
|
WARN "Help, all my disks are unwriteable!" unless $wrote; |
360
|
|
|
|
|
|
|
# I'm not dead yet! It's only a flesh wound! |
361
|
59
|
50
|
|
|
|
190
|
return 0 unless $wrote; |
362
|
59
|
|
|
|
|
306
|
my $location = $c->url_for("file", md5 => $digest, filename => $filename)->to_abs; |
363
|
59
|
|
|
|
|
41847
|
$c->res->headers->location($location); |
364
|
59
|
|
|
|
|
1828
|
$c->render(status => 201, text => 'ok'); # CREATED |
365
|
59
|
|
|
|
|
93422
|
DEBUG "Stashed $filename ($digest) locally on $wrote"; |
366
|
59
|
|
|
|
|
61462
|
return 1; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
sub _stash_remotely { |
370
|
0
|
|
|
0
|
|
0
|
my ($c, $filename,$digest,$asset) = @_; |
371
|
|
|
|
|
|
|
# Stash this file on a remote disk. |
372
|
|
|
|
|
|
|
# Returns false or renders the response. |
373
|
|
|
|
|
|
|
|
374
|
0
|
0
|
|
|
|
0
|
return 0 if $c->req->headers->header('X-Yars-NoStash'); |
375
|
|
|
|
|
|
|
|
376
|
0
|
|
|
|
|
0
|
DEBUG "Stashing $filename remotely."; |
377
|
0
|
|
|
|
|
0
|
my $assigned_server = $c->tools->server_for($digest); |
378
|
0
|
|
|
|
|
0
|
for my $server (shuffle($c->tools->server_urls)) { |
379
|
0
|
0
|
|
|
|
0
|
next if $server eq $c->tools->server_url; |
380
|
0
|
0
|
|
|
|
0
|
next if $server eq $assigned_server; |
381
|
0
|
0
|
|
|
|
0
|
_proxy_to( $c, $server, $filename, $digest, $asset, 1 ) and return 1; |
382
|
|
|
|
|
|
|
} |
383
|
0
|
|
|
|
|
0
|
return 0; |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
del '/file/#filename/:md5' => [ md5 => qr/[a-f0-9]{32}/ ] => \&_del; |
388
|
|
|
|
|
|
|
del '/file/:md5/#filename' => [ md5 => qr/[a-f0-9]{32}/ ] => \&_del; |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
sub _del { |
391
|
30
|
|
|
30
|
|
126688
|
my $c = shift; |
392
|
30
|
|
|
|
|
155
|
my $md5 = $c->stash("md5"); |
393
|
30
|
|
|
|
|
417
|
my $filename = $c->stash('filename'); |
394
|
30
|
|
|
|
|
454
|
TRACE "Delete request for $filename, $md5"; |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# Delete locally or proxy the delete if it is stashed somewhere else. |
397
|
|
|
|
|
|
|
|
398
|
30
|
|
|
|
|
21423
|
my $server = $c->tools->server_for($md5); |
399
|
30
|
100
|
|
|
|
176
|
if ($server eq $c->tools->server_url) { |
400
|
20
|
|
|
|
|
100
|
DEBUG "This is our file, we will delete it."; |
401
|
20
|
|
|
|
|
22862
|
my $dir = $c->tools->storage_path( $md5 ); |
402
|
20
|
100
|
|
|
|
1334
|
if (-r "$dir/$filename") { |
403
|
19
|
50
|
|
|
|
3054
|
unlink "$dir/$filename" or return $c->reply->exception($!); |
404
|
19
|
|
|
|
|
201
|
$c->tools->cleanup_tree($dir); |
405
|
19
|
|
|
|
|
134
|
return $c->render(status => 200, text =>'ok'); |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
1
|
|
|
|
|
8
|
$server = $c->tools->remote_stashed_server($filename, $md5); |
409
|
1
|
50
|
|
|
|
7
|
return $c->reply->not_found unless $server; |
410
|
|
|
|
|
|
|
# otherwise fall through... |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
11
|
100
|
|
|
|
68
|
if (my $dir = $c->tools->local_stashed_dir($filename,$md5)) { |
414
|
2
|
50
|
|
|
|
258
|
unlink "$dir/$filename" or return $c->reply->exception($!); |
415
|
2
|
|
|
|
|
20
|
$c->tools->cleanup_tree($dir); |
416
|
2
|
|
|
|
|
12
|
return $c->render(status => 200, text =>'ok'); |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
9
|
|
|
|
|
70
|
DEBUG "Proxying delete to $server"; |
420
|
9
|
|
|
|
|
10452
|
my $tx = $c->tools->_ua->delete("$server/file/$md5/$filename"); |
421
|
9
|
50
|
|
|
|
41919
|
if (my $res = $tx->success) { |
422
|
9
|
|
|
|
|
252
|
return $c->render(status => 200, text => "ok"); |
423
|
|
|
|
|
|
|
} else { |
424
|
0
|
|
|
|
|
|
my $error = $tx->error; |
425
|
0
|
|
|
|
|
|
my ($msg,$code) = ($error->{message}, $error->{code}); |
426
|
0
|
0
|
|
|
|
|
return $c->render(status => $code, text => $msg) if $code; |
427
|
0
|
|
|
|
|
|
return $c->reply->exception("Error deleting from $server ".format_tx_error($tx->error)); |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
}; |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
get '/disk/usage' => sub { |
433
|
|
|
|
|
|
|
my $c = shift; |
434
|
|
|
|
|
|
|
my $count = $c->param("count") ? 1 : 0; |
435
|
|
|
|
|
|
|
if ( my $server = $c->param('server') ) { |
436
|
|
|
|
|
|
|
if ( $c->tools->server_exists($server) |
437
|
|
|
|
|
|
|
and $c->tools->server_url ne $server ) { |
438
|
|
|
|
|
|
|
return $c->redirect_to("$server/disk/usage?count=$count"); |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
my %r; |
443
|
|
|
|
|
|
|
for my $disk ($c->tools->disk_roots) { |
444
|
|
|
|
|
|
|
if (defined( my $df = df($disk))) { |
445
|
|
|
|
|
|
|
$r{$disk} = { |
446
|
|
|
|
|
|
|
'1K-blocks' => $df->{blocks}, |
447
|
|
|
|
|
|
|
blocks_used => $df->{used}, |
448
|
|
|
|
|
|
|
blocks_avail => $df->{bavail}, |
449
|
|
|
|
|
|
|
space => $c->tools->human_size($df->{blocks}*1024), |
450
|
|
|
|
|
|
|
space_used => $c->tools->human_size($df->{used}*1024), |
451
|
|
|
|
|
|
|
space_avail => $c->tools->human_size($df->{bavail}*1024), |
452
|
|
|
|
|
|
|
percent_used => sprintf('%02d',(100*($df->{blocks} - $df->{bavail})/($df->{blocks}))).'%', |
453
|
|
|
|
|
|
|
}; |
454
|
|
|
|
|
|
|
} else { |
455
|
|
|
|
|
|
|
WARN "Error getting usage for disk $disk" if -d $disk; |
456
|
|
|
|
|
|
|
DEBUG "$disk does not exist" unless -d $disk; |
457
|
|
|
|
|
|
|
} |
458
|
|
|
|
|
|
|
$r{$disk}{count} = $c->tools->count_files($disk) if $count; |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
return $c->render(autodata => \%r) unless $c->param('all'); |
461
|
|
|
|
|
|
|
my %all = ( $c->tools->server_url => \%r ); |
462
|
|
|
|
|
|
|
for my $server ($c->tools->server_urls) { |
463
|
|
|
|
|
|
|
next if exists $all{$server}; |
464
|
|
|
|
|
|
|
my $tx = $c->tools->_ua->get("$server/disk/usage?count=$count"); |
465
|
|
|
|
|
|
|
my $res = $tx->success or do { |
466
|
|
|
|
|
|
|
$all{$server} = 'down'; |
467
|
|
|
|
|
|
|
next; |
468
|
|
|
|
|
|
|
}; |
469
|
|
|
|
|
|
|
$all{$server} = $res->json; |
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
return $c->render(autodata => \%all); |
472
|
|
|
|
|
|
|
}; |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
post '/disk/status' => sub { |
476
|
|
|
|
|
|
|
my $c = shift; |
477
|
|
|
|
|
|
|
my $got = $c->parse_autodata; |
478
|
|
|
|
|
|
|
my $root = $got->{root} || $got->{disk}; |
479
|
|
|
|
|
|
|
my $state = $got->{state} or return $c->reply->exception("no state found in request"); |
480
|
|
|
|
|
|
|
my $server = $got->{server}; |
481
|
|
|
|
|
|
|
if ($server && $server ne $c->tools->server_url) { |
482
|
|
|
|
|
|
|
unless ($c->tools->server_exists($server)) { |
483
|
|
|
|
|
|
|
return $c->render( status => 400, text => "Server $server does not exist" ); |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
WARN "Sending ".$c->req->body; |
486
|
|
|
|
|
|
|
my $tx = $c->tools->_ua->post("$server/disk/status", $c->req->headers->to_hash, ''.$c->req->body ); |
487
|
|
|
|
|
|
|
return $c->render( text => $tx->success ? $tx->res->body : 'failed '.format_tx_error($tx->error) ); |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
$c->tools->disk_is_local($root) or return $c->render->exception("Disk $root is not on ".$c->tools->server_url); |
490
|
|
|
|
|
|
|
my $success; |
491
|
|
|
|
|
|
|
for ($state) { |
492
|
|
|
|
|
|
|
/down/ and $success = $c->tools->mark_disk_down($root); |
493
|
|
|
|
|
|
|
/up/ and $success = $c->tools->mark_disk_up($root); |
494
|
|
|
|
|
|
|
} |
495
|
|
|
|
|
|
|
$c->render(text => $success ? "ok" : "failed" ); |
496
|
|
|
|
|
|
|
}; |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
post '/check/manifest' => sub { |
500
|
|
|
|
|
|
|
my $c = shift; |
501
|
|
|
|
|
|
|
my $got = $c->parse_autodata; |
502
|
|
|
|
|
|
|
my $files = $got->{files} || []; |
503
|
|
|
|
|
|
|
if (my $manifest = $got->{manifest}) { |
504
|
|
|
|
|
|
|
for my $line (split /\n/, $manifest) { |
505
|
|
|
|
|
|
|
my ($md5,$filename) = split /\s+/, $line; |
506
|
|
|
|
|
|
|
push @$files, +{ md5 => $md5, filename => $filename }; |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
} |
509
|
|
|
|
|
|
|
my %ret = ( missing => [], found => [] ); |
510
|
|
|
|
|
|
|
my %remote; |
511
|
|
|
|
|
|
|
for my $entry (@$files) { |
512
|
|
|
|
|
|
|
my ($filename,$md5) = @$entry{qw/filename md5/}; |
513
|
|
|
|
|
|
|
next unless $md5 && $md5 =~ /^[0-9a-fA-F]+$/; |
514
|
|
|
|
|
|
|
next unless $filename && $filename =~ /\w/; |
515
|
|
|
|
|
|
|
$filename = basename($filename); |
516
|
|
|
|
|
|
|
next if $filename =~ m[/]; |
517
|
|
|
|
|
|
|
TRACE "checking for $md5 and $filename"; |
518
|
|
|
|
|
|
|
my $server = $c->tools->server_for($md5); |
519
|
|
|
|
|
|
|
if ($server eq $c->tools->server_url) { |
520
|
|
|
|
|
|
|
my $dir = $c->tools->storage_path($md5); |
521
|
|
|
|
|
|
|
my $which = -r "$dir/$filename" ? "found" : "missing"; |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
if ($which eq 'found' && $c->param('show_corrupt')) { |
524
|
|
|
|
|
|
|
# Check md5, and maybe set $which to "corrupt". |
525
|
|
|
|
|
|
|
my $computed_md5 = digest_file_hex("$dir/$filename",'MD5'); |
526
|
|
|
|
|
|
|
if ($computed_md5 ne $md5) { |
527
|
|
|
|
|
|
|
$which = 'corrupt'; |
528
|
|
|
|
|
|
|
$md5 = $computed_md5; |
529
|
|
|
|
|
|
|
} |
530
|
|
|
|
|
|
|
} |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
push @{ $ret{$which} }, { filename => $filename, md5 => $md5 }; |
533
|
|
|
|
|
|
|
} else { |
534
|
|
|
|
|
|
|
push @{ $remote{$server} }, { filename => $filename, md5 => $md5 }; |
535
|
|
|
|
|
|
|
} |
536
|
|
|
|
|
|
|
} |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
for my $server (keys %remote) { |
539
|
|
|
|
|
|
|
TRACE "Looking for manifest files on $server"; |
540
|
|
|
|
|
|
|
my $content = encode_json { files => $remote{$server} }; |
541
|
|
|
|
|
|
|
my $tx = $c->tools->_ua->post( |
542
|
|
|
|
|
|
|
"$server/check/manifest?show_found=1&show_corrupt=".($c->param("show_corrupt")//''), |
543
|
|
|
|
|
|
|
{ "Content-type" => "application/json", "Connection" => "Close" }, $content ); |
544
|
|
|
|
|
|
|
if (my $res = $tx->success) { |
545
|
|
|
|
|
|
|
my $got = $res->json; |
546
|
|
|
|
|
|
|
push @{ $ret{missing} }, @{ $got->{missing} }; |
547
|
|
|
|
|
|
|
push @{ $ret{found} }, @{ $got->{found} }; |
548
|
|
|
|
|
|
|
push @{ $ret{corrupt} }, @{ $got->{corrupt} || [] } if $c->param("show_corrupt"); |
549
|
|
|
|
|
|
|
} else { |
550
|
|
|
|
|
|
|
ERROR "Failed to connect to $server"; |
551
|
|
|
|
|
|
|
push @{ $ret{missing} }, @{ $remote{$server} }; |
552
|
|
|
|
|
|
|
} |
553
|
|
|
|
|
|
|
} |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
# Check stashes for missing ones to be sure. |
556
|
|
|
|
|
|
|
my $missing = $ret{missing}; |
557
|
|
|
|
|
|
|
my @are_missing; |
558
|
|
|
|
|
|
|
my @not_missing; |
559
|
|
|
|
|
|
|
for my $m (@$missing) { |
560
|
|
|
|
|
|
|
my $found = $c->tools->local_stashed_dir( $m->{filename}, $m->{md5} ) |
561
|
|
|
|
|
|
|
|| $c->tools->remote_stashed_server( $m->{filename}, $m->{md5} ); |
562
|
|
|
|
|
|
|
if ($found) { |
563
|
|
|
|
|
|
|
push @not_missing, $m; |
564
|
|
|
|
|
|
|
} else { |
565
|
|
|
|
|
|
|
push @are_missing, $m; |
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
} |
568
|
|
|
|
|
|
|
if (@not_missing) { |
569
|
|
|
|
|
|
|
push @{ $ret{found} }, @not_missing; |
570
|
|
|
|
|
|
|
$ret{missing} = \@are_missing; |
571
|
|
|
|
|
|
|
} |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
$ret{found} = scalar @{ $ret{found} } unless $c->param("show_found"); |
574
|
|
|
|
|
|
|
$c->render(autodata => \%ret); |
575
|
|
|
|
|
|
|
}; |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
get '/servers/status' => sub { |
579
|
|
|
|
|
|
|
my $c = shift; |
580
|
|
|
|
|
|
|
my %disks = |
581
|
|
|
|
|
|
|
map { $_ => $c->tools->disk_is_up_verified($_) ? "up" : "down" } |
582
|
|
|
|
|
|
|
$c->tools->disk_roots; |
583
|
|
|
|
|
|
|
my %all; |
584
|
|
|
|
|
|
|
$all{$c->tools->server_url} = \%disks; |
585
|
|
|
|
|
|
|
for my $server ($c->tools->server_urls) { |
586
|
|
|
|
|
|
|
next if exists($all{$server}); |
587
|
|
|
|
|
|
|
my $tx = $c->tools->_ua->get("$server/server/status"); |
588
|
|
|
|
|
|
|
if (my $res = $tx->success) { |
589
|
|
|
|
|
|
|
$all{$server} = $res->json; |
590
|
|
|
|
|
|
|
} else { |
591
|
|
|
|
|
|
|
WARN "Could not reach $server : ".format_tx_error($tx->error); |
592
|
|
|
|
|
|
|
$all{$server} = "down"; |
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
} |
595
|
|
|
|
|
|
|
$c->render(autodata => \%all); |
596
|
|
|
|
|
|
|
}; |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
get '/server/status' => sub { |
600
|
|
|
|
|
|
|
my $c = shift; |
601
|
|
|
|
|
|
|
my %disks = |
602
|
|
|
|
|
|
|
map { $_ => $c->tools->disk_is_up_verified($_) ? "up" : "down" } |
603
|
|
|
|
|
|
|
$c->tools->disk_roots; |
604
|
|
|
|
|
|
|
$c->render(autodata => \%disks); |
605
|
|
|
|
|
|
|
}; |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
get '/bucket_map' => sub { |
609
|
|
|
|
|
|
|
my $c = shift; |
610
|
|
|
|
|
|
|
$c->render(autodata => $c->tools->bucket_map) |
611
|
|
|
|
|
|
|
}; |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
get '/bucket/usage' => sub { |
615
|
|
|
|
|
|
|
my $c = shift; |
616
|
|
|
|
|
|
|
if ( my $server = $c->param('server') ) { |
617
|
|
|
|
|
|
|
if ( $c->tools->server_exists($server) |
618
|
|
|
|
|
|
|
and $c->tools->server_url ne $server ) { |
619
|
|
|
|
|
|
|
return $c->redirect_to("$server/bucket/usage"); |
620
|
|
|
|
|
|
|
} |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
my %used; |
623
|
|
|
|
|
|
|
my %assigned = $c->tools->local_buckets; |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
# NB: this assumes homogeneous buckets and doesn't |
626
|
|
|
|
|
|
|
# work for > 256 buckets. |
627
|
|
|
|
|
|
|
my $bucket_size = 1; |
628
|
|
|
|
|
|
|
for (keys %assigned) { |
629
|
|
|
|
|
|
|
for (@{ $assigned{$_} }) { |
630
|
|
|
|
|
|
|
$bucket_size = length($_) if length($_) > $bucket_size; |
631
|
|
|
|
|
|
|
} |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
for my $disk ($c->tools->disk_roots) { |
635
|
|
|
|
|
|
|
my @dirs = map /\/([0-9a-f]+)$/, glob "$disk/*"; |
636
|
|
|
|
|
|
|
my @buckets = uniq map substr($_,0,$bucket_size), @dirs; |
637
|
|
|
|
|
|
|
$used{$disk} = \@buckets; |
638
|
|
|
|
|
|
|
} |
639
|
|
|
|
|
|
|
$c->render(autodata => { used => \%used, assigned => \%assigned } ); |
640
|
|
|
|
|
|
|
}; |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
1; |
644
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
__END__ |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
=pod |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
=encoding UTF-8 |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
=head1 NAME |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
Yars::Routes - set up the routes for Yars. |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
=head1 VERSION |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
version 1.28 |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
=head1 SYNOPSIS |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
% curl http://localhost:9001/file/764efa883dda1e11db47671c4a3bbd9e/test_file1 |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
=head1 DESCRIPTION |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
This document provides information on the Yars specific REST API (the |
666
|
|
|
|
|
|
|
HTTP "routes") provided by Yars servers. For a Perl interface to this |
667
|
|
|
|
|
|
|
API, see L<Yars::Client>. For a command line interface see |
668
|
|
|
|
|
|
|
L<yarsclient>. For the generic L<Clustericious> REST API that comes |
669
|
|
|
|
|
|
|
with all L<Clustericious> services, see |
670
|
|
|
|
|
|
|
L<Clustericious::RouteBuilder::Common>. |
671
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=head1 ROUTES |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
=head2 GET / |
675
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
Get a welcome message. This is usually simply the text string "welcome |
677
|
|
|
|
|
|
|
to Yars". |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
=head2 GET /file/#filename/:md5, GET /:md5/#filename |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
Retrieve a file with the given name and md5. |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
You can also make a HEAD request on the same route to determine if the |
684
|
|
|
|
|
|
|
file is available without making the yars server send the file. |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
=head2 PUT /file/#filename/#md5 |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
PUT a file with the given name and md5. |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
=head2 DELETE /file/#filename/:md5, /file/:md5/#filename |
691
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
Delete a file with the given name and md5. |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=head2 GET /disk/usage |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
Get a summary of the disk usage. |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
Send the CGI parameters count=1 to also count the files. |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
=head2 POST /disk/status |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
Mark disks up or down. Send the disk root and state (up or down) |
703
|
|
|
|
|
|
|
as JSON encoded in the body. |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
=head2 POST /check/manifest |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
Given JSON with 'manifest' which is a return-delimited string of |
708
|
|
|
|
|
|
|
filenames and md5s (like the output of md5sum), check each file for |
709
|
|
|
|
|
|
|
existence on the server (or proxy to the right server) |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=head2 GET /servers/status |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
Get the status of all the disks on all the servers/ |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
=head2 GET /server/status |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
Get the status of just this server. |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
=head2 GET /bucket_map |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
Get a mapping from buckets to hosts. |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
=head2 GET /bucket/usage |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
Find the disk usage per bucket. |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=head1 SEE ALSO |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
L<Yars>, L<Yars::Client>, L<yarsclient>, L<Clustericious> |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=head1 AUTHOR |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
Original author: Marty Brandon |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt> |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
Contributors: |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
Brian Duggan |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
Curt Tilmes |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
This software is copyright (c) 2013 by NASA GSFC. |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
748
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
=cut |