]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::API2::Storage::Status; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
7814e05f DM |
6 | use File::Path; |
7 | use File::Basename; | |
8 | use PVE::Tools; | |
9 | use PVE::INotify; | |
83d7192f | 10 | use PVE::Cluster; |
0ce8cadd | 11 | use PVE::RRD; |
b6cf0a66 DM |
12 | use PVE::Storage; |
13 | use PVE::API2::Storage::Content; | |
14 | use PVE::RESTHandler; | |
15 | use PVE::RPCEnvironment; | |
16 | use PVE::JSONSchema qw(get_standard_option); | |
17 | use PVE::Exception qw(raise_param_exc); | |
18 | ||
19 | use base qw(PVE::RESTHandler); | |
20 | ||
b6cf0a66 | 21 | __PACKAGE__->register_method ({ |
7dd31e68 | 22 | subclass => "PVE::API2::Storage::Content", |
b6cf0a66 | 23 | # set fragment delimiter (no subdirs) - we need that, because volume |
7dd31e68 FE |
24 | # IDs may contain a slash '/' |
25 | fragmentDelimiter => '', | |
b6cf0a66 DM |
26 | path => '{storage}/content', |
27 | }); | |
28 | ||
29 | __PACKAGE__->register_method ({ | |
7dd31e68 | 30 | name => 'index', |
b6cf0a66 DM |
31 | path => '', |
32 | method => 'GET', | |
33 | description => "Get status for all datastores.", | |
7dd31e68 | 34 | permissions => { |
5f642f73 DM |
35 | description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'", |
36 | user => 'all', | |
37 | }, | |
b6cf0a66 DM |
38 | protected => 1, |
39 | proxyto => 'node', | |
40 | parameters => { | |
41 | additionalProperties => 0, | |
42 | properties => { | |
43 | node => get_standard_option('pve-node'), | |
f3bd890d DM |
44 | storage => get_standard_option('pve-storage-id', { |
45 | description => "Only list status for specified storage", | |
46 | optional => 1, | |
47 | completion => \&PVE::Storage::complete_storage_enabled, | |
48 | }), | |
7dd31e68 | 49 | content => { |
b6cf0a66 | 50 | description => "Only list stores which support this content type.", |
583c2802 | 51 | type => 'string', format => 'pve-storage-content-list', |
b6cf0a66 | 52 | optional => 1, |
98437f4c | 53 | completion => \&PVE::Storage::complete_content_type, |
b6cf0a66 | 54 | }, |
283608f3 DM |
55 | enabled => { |
56 | description => "Only list stores which are enabled (not disabled in config).", | |
57 | type => 'boolean', | |
12c2fe32 DM |
58 | optional => 1, |
59 | default => 0, | |
283608f3 DM |
60 | }, |
61 | target => get_standard_option('pve-node', { | |
62 | description => "If target is different to 'node', we only lists shared storages which " . | |
63 | "content is accessible on this 'node' and the specified 'target' node.", | |
64 | optional => 1, | |
f3bd890d | 65 | completion => \&PVE::Cluster::get_nodelist, |
283608f3 | 66 | }), |
d347322a | 67 | 'format' => { |
856c54bd DC |
68 | description => "Include information about formats", |
69 | type => 'boolean', | |
70 | optional => 1, | |
71 | default => 0, | |
72 | }, | |
b6cf0a66 DM |
73 | }, |
74 | }, | |
75 | returns => { | |
76 | type => 'array', | |
77 | items => { | |
78 | type => "object", | |
d347322a DM |
79 | properties => { |
80 | storage => get_standard_option('pve-storage-id'), | |
81 | type => { | |
82 | description => "Storage type.", | |
83 | type => 'string', | |
84 | }, | |
85 | content => { | |
86 | description => "Allowed storage content types.", | |
87 | type => 'string', format => 'pve-storage-content-list', | |
88 | }, | |
89 | enabled => { | |
90 | description => "Set when storage is enabled (not disabled).", | |
91 | type => 'boolean', | |
92 | optional => 1, | |
93 | }, | |
94 | active => { | |
95 | description => "Set when storage is accessible.", | |
96 | type => 'boolean', | |
97 | optional => 1, | |
98 | }, | |
99 | shared => { | |
100 | description => "Shared flag from storage configuration.", | |
101 | type => 'boolean', | |
102 | optional => 1, | |
103 | }, | |
104 | total => { | |
105 | description => "Total storage space in bytes.", | |
106 | type => 'integer', | |
107 | renderer => 'bytes', | |
108 | optional => 1, | |
109 | }, | |
110 | used => { | |
111 | description => "Used storage space in bytes.", | |
112 | type => 'integer', | |
113 | renderer => 'bytes', | |
114 | optional => 1, | |
115 | }, | |
116 | avail => { | |
117 | description => "Available storage space in bytes.", | |
118 | type => 'integer', | |
119 | renderer => 'bytes', | |
120 | optional => 1, | |
121 | }, | |
122 | used_fraction => { | |
123 | description => "Used fraction (used/total).", | |
124 | type => 'number', | |
125 | renderer => 'fraction_as_percentage', | |
126 | optional => 1, | |
127 | }, | |
128 | }, | |
b6cf0a66 DM |
129 | }, |
130 | links => [ { rel => 'child', href => "{storage}" } ], | |
131 | }, | |
132 | code => sub { | |
133 | my ($param) = @_; | |
134 | ||
5f642f73 DM |
135 | my $rpcenv = PVE::RPCEnvironment::get(); |
136 | my $authuser = $rpcenv->get_user(); | |
137 | ||
283608f3 DM |
138 | my $localnode = PVE::INotify::nodename(); |
139 | ||
140 | my $target = $param->{target}; | |
141 | ||
142 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); | |
f6c03d36 | 143 | |
83d7192f | 144 | my $cfg = PVE::Storage::config(); |
b6cf0a66 | 145 | |
856c54bd | 146 | my $info = PVE::Storage::storage_info($cfg, $param->{content}, $param->{format}); |
b6cf0a66 | 147 | |
5f642f73 DM |
148 | raise_param_exc({ storage => "No such storage." }) |
149 | if $param->{storage} && !defined($info->{$param->{storage}}); | |
f6c03d36 | 150 | |
5f642f73 DM |
151 | my $res = {}; |
152 | my @sids = PVE::Storage::storage_ids($cfg); | |
153 | foreach my $storeid (@sids) { | |
d347322a DM |
154 | my $data = $info->{$storeid}; |
155 | next if !$data; | |
5f642f73 DM |
156 | my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ]; |
157 | next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1); | |
158 | next if $param->{storage} && $param->{storage} ne $storeid; | |
283608f3 DM |
159 | |
160 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
161 | ||
162 | next if $param->{enabled} && $scfg->{disable}; | |
f6c03d36 | 163 | |
283608f3 DM |
164 | if ($target) { |
165 | # check if storage content is accessible on local node and specified target node | |
166 | # we use this on the Clone GUI | |
167 | ||
168 | next if !$scfg->{shared}; | |
169 | next if !PVE::Storage::storage_check_node($cfg, $storeid, undef, 1); | |
170 | next if !PVE::Storage::storage_check_node($cfg, $storeid, $target, 1); | |
171 | } | |
172 | ||
d347322a DM |
173 | if ($data->{total}) { |
174 | $data->{used_fraction} = ($data->{used} // 0) / $data->{total}; | |
175 | } | |
176 | ||
177 | $res->{$storeid} = $data; | |
b6cf0a66 | 178 | } |
5f642f73 DM |
179 | |
180 | return PVE::RESTHandler::hash_to_array($res, 'storage'); | |
b6cf0a66 DM |
181 | }}); |
182 | ||
183 | __PACKAGE__->register_method ({ | |
184 | name => 'diridx', | |
7dd31e68 | 185 | path => '{storage}', |
b6cf0a66 DM |
186 | method => 'GET', |
187 | description => "", | |
7dd31e68 | 188 | permissions => { |
5f642f73 DM |
189 | check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1], |
190 | }, | |
b6cf0a66 DM |
191 | parameters => { |
192 | additionalProperties => 0, | |
193 | properties => { | |
194 | node => get_standard_option('pve-node'), | |
195 | storage => get_standard_option('pve-storage-id'), | |
196 | }, | |
197 | }, | |
198 | returns => { | |
199 | type => 'array', | |
200 | items => { | |
201 | type => "object", | |
202 | properties => { | |
203 | subdir => { type => 'string' }, | |
204 | }, | |
205 | }, | |
206 | links => [ { rel => 'child', href => "{subdir}" } ], | |
207 | }, | |
208 | code => sub { | |
209 | my ($param) = @_; | |
210 | ||
211 | my $res = [ | |
212 | { subdir => 'status' }, | |
213 | { subdir => 'content' }, | |
7814e05f | 214 | { subdir => 'upload' }, |
b6cf0a66 DM |
215 | { subdir => 'rrd' }, |
216 | { subdir => 'rrddata' }, | |
7dd31e68 FE |
217 | ]; |
218 | ||
b6cf0a66 DM |
219 | return $res; |
220 | }}); | |
221 | ||
222 | __PACKAGE__->register_method ({ | |
223 | name => 'read_status', | |
7dd31e68 | 224 | path => '{storage}/status', |
b6cf0a66 DM |
225 | method => 'GET', |
226 | description => "Read storage status.", | |
7dd31e68 | 227 | permissions => { |
5f642f73 DM |
228 | check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1], |
229 | }, | |
b6cf0a66 DM |
230 | protected => 1, |
231 | proxyto => 'node', | |
232 | parameters => { | |
233 | additionalProperties => 0, | |
234 | properties => { | |
235 | node => get_standard_option('pve-node'), | |
236 | storage => get_standard_option('pve-storage-id'), | |
237 | }, | |
238 | }, | |
239 | returns => { | |
240 | type => "object", | |
241 | properties => {}, | |
242 | }, | |
243 | code => sub { | |
244 | my ($param) = @_; | |
245 | ||
83d7192f | 246 | my $cfg = PVE::Storage::config(); |
b6cf0a66 DM |
247 | |
248 | my $info = PVE::Storage::storage_info($cfg, $param->{content}); | |
249 | ||
250 | my $data = $info->{$param->{storage}}; | |
251 | ||
252 | raise_param_exc({ storage => "No such storage." }) | |
253 | if !defined($data); | |
7dd31e68 | 254 | |
b6cf0a66 DM |
255 | return $data; |
256 | }}); | |
257 | ||
258 | __PACKAGE__->register_method ({ | |
259 | name => 'rrd', | |
7dd31e68 | 260 | path => '{storage}/rrd', |
b6cf0a66 DM |
261 | method => 'GET', |
262 | description => "Read storage RRD statistics (returns PNG).", | |
7dd31e68 | 263 | permissions => { |
5f642f73 DM |
264 | check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1], |
265 | }, | |
b6cf0a66 DM |
266 | protected => 1, |
267 | proxyto => 'node', | |
268 | parameters => { | |
269 | additionalProperties => 0, | |
270 | properties => { | |
271 | node => get_standard_option('pve-node'), | |
272 | storage => get_standard_option('pve-storage-id'), | |
273 | timeframe => { | |
274 | description => "Specify the time frame you are interested in.", | |
275 | type => 'string', | |
276 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
277 | }, | |
278 | ds => { | |
279 | description => "The list of datasources you want to display.", | |
280 | type => 'string', format => 'pve-configid-list', | |
281 | }, | |
282 | cf => { | |
283 | description => "The RRD consolidation function", | |
284 | type => 'string', | |
285 | enum => [ 'AVERAGE', 'MAX' ], | |
286 | optional => 1, | |
287 | }, | |
288 | }, | |
289 | }, | |
290 | returns => { | |
291 | type => "object", | |
292 | properties => { | |
293 | filename => { type => 'string' }, | |
294 | }, | |
295 | }, | |
296 | code => sub { | |
297 | my ($param) = @_; | |
298 | ||
0ce8cadd | 299 | return PVE::RRD::create_rrd_graph( |
7dd31e68 | 300 | "pve2-storage/$param->{node}/$param->{storage}", |
b6cf0a66 | 301 | $param->{timeframe}, $param->{ds}, $param->{cf}); |
b6cf0a66 DM |
302 | }}); |
303 | ||
304 | __PACKAGE__->register_method ({ | |
305 | name => 'rrddata', | |
7dd31e68 | 306 | path => '{storage}/rrddata', |
b6cf0a66 DM |
307 | method => 'GET', |
308 | description => "Read storage RRD statistics.", | |
7dd31e68 | 309 | permissions => { |
5f642f73 DM |
310 | check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1], |
311 | }, | |
b6cf0a66 DM |
312 | protected => 1, |
313 | proxyto => 'node', | |
314 | parameters => { | |
315 | additionalProperties => 0, | |
316 | properties => { | |
317 | node => get_standard_option('pve-node'), | |
318 | storage => get_standard_option('pve-storage-id'), | |
319 | timeframe => { | |
320 | description => "Specify the time frame you are interested in.", | |
321 | type => 'string', | |
322 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
323 | }, | |
324 | cf => { | |
325 | description => "The RRD consolidation function", | |
326 | type => 'string', | |
327 | enum => [ 'AVERAGE', 'MAX' ], | |
328 | optional => 1, | |
329 | }, | |
330 | }, | |
331 | }, | |
332 | returns => { | |
333 | type => "array", | |
334 | items => { | |
335 | type => "object", | |
336 | properties => {}, | |
337 | }, | |
338 | }, | |
339 | code => sub { | |
340 | my ($param) = @_; | |
341 | ||
0ce8cadd | 342 | return PVE::RRD::create_rrd_data( |
7dd31e68 FE |
343 | "pve2-storage/$param->{node}/$param->{storage}", |
344 | $param->{timeframe}, $param->{cf}); | |
b6cf0a66 | 345 | }}); |
7814e05f | 346 | |
7dd31e68 | 347 | # makes no sense for big images and backup files (because it |
1f6610f3 | 348 | # create a copy of the file). |
7814e05f DM |
349 | __PACKAGE__->register_method ({ |
350 | name => 'upload', | |
7dd31e68 | 351 | path => '{storage}/upload', |
7814e05f | 352 | method => 'POST', |
1f6610f3 | 353 | description => "Upload templates and ISO images.", |
7dd31e68 | 354 | permissions => { |
1f6610f3 | 355 | check => ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']], |
5f642f73 | 356 | }, |
7814e05f DM |
357 | protected => 1, |
358 | parameters => { | |
359 | additionalProperties => 0, | |
360 | properties => { | |
361 | node => get_standard_option('pve-node'), | |
362 | storage => get_standard_option('pve-storage-id'), | |
7dd31e68 | 363 | content => { |
7814e05f DM |
364 | description => "Content type.", |
365 | type => 'string', format => 'pve-storage-content', | |
366 | }, | |
7dd31e68 | 367 | filename => { |
7814e05f DM |
368 | description => "The name of the file to create.", |
369 | type => 'string', | |
370 | }, | |
7dd31e68 | 371 | tmpfilename => { |
5c0720d6 | 372 | description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trusted port on localhost.", |
7814e05f DM |
373 | type => 'string', |
374 | optional => 1, | |
375 | }, | |
376 | }, | |
377 | }, | |
378 | returns => { type => "string" }, | |
379 | code => sub { | |
380 | my ($param) = @_; | |
381 | ||
382 | my $rpcenv = PVE::RPCEnvironment::get(); | |
383 | ||
384 | my $user = $rpcenv->get_user(); | |
385 | ||
83d7192f | 386 | my $cfg = PVE::Storage::config(); |
7814e05f DM |
387 | |
388 | my $node = $param->{node}; | |
389 | my $scfg = PVE::Storage::storage_check_enabled($cfg, $param->{storage}, $node); | |
390 | ||
d68c7bca | 391 | die "can't upload to storage type '$scfg->{type}'\n" |
ee8c176d | 392 | if !defined($scfg->{path}); |
7814e05f DM |
393 | |
394 | my $content = $param->{content}; | |
395 | ||
396 | my $tmpfilename = $param->{tmpfilename}; | |
397 | die "missing temporary file name\n" if !$tmpfilename; | |
398 | ||
399 | my $size = -s $tmpfilename; | |
481f6177 | 400 | die "temporary file '$tmpfilename' does not exist\n" if !defined($size); |
7814e05f DM |
401 | |
402 | my $filename = $param->{filename}; | |
403 | ||
404 | chomp $filename; | |
405 | $filename =~ s/^.*[\/\\]//; | |
38e1eb3d | 406 | $filename =~ s/[^-a-zA-Z0-9_.]/_/g; |
7814e05f DM |
407 | |
408 | my $path; | |
409 | ||
410 | if ($content eq 'iso') { | |
4c693491 SR |
411 | if ($filename !~ m![^/]+$PVE::Storage::iso_extension_re$!) { |
412 | raise_param_exc({ filename => "missing '.iso' or '.img' extension" }); | |
7814e05f DM |
413 | } |
414 | $path = PVE::Storage::get_iso_dir($cfg, $param->{storage}); | |
415 | } elsif ($content eq 'vztmpl') { | |
030f6e1a EK |
416 | if ($filename !~ m![^/]+\.tar\.[gx]z$!) { |
417 | raise_param_exc({ filename => "missing '.tar.gz' or '.tar.xz' extension" }); | |
7814e05f | 418 | } |
4ea5bca4 | 419 | $path = PVE::Storage::get_vztmpl_dir($cfg, $param->{storage}); |
7814e05f | 420 | } else { |
1f6610f3 | 421 | raise_param_exc({ content => "upload content type '$content' not allowed" }); |
7814e05f DM |
422 | } |
423 | ||
d68c7bca | 424 | die "storage '$param->{storage}' does not support '$content' content\n" |
7814e05f DM |
425 | if !$scfg->{content}->{$content}; |
426 | ||
427 | my $dest = "$path/$filename"; | |
428 | my $dirname = dirname($dest); | |
429 | ||
5c0720d6 SR |
430 | # best effort to match apl_download behaviour |
431 | chmod 0644, $tmpfilename; | |
432 | ||
243f413a | 433 | # we simply overwrite the destination file if it already exists |
7814e05f DM |
434 | |
435 | my $cmd; | |
436 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
437 | my $remip = PVE::Cluster::remote_node_ip($node); | |
438 | ||
45c2ee35 | 439 | my @ssh_options = ('-o', 'BatchMode=yes'); |
7814e05f | 440 | |
53ec90e2 | 441 | my @remcmd = ('/usr/bin/ssh', @ssh_options, $remip, '--'); |
7814e05f | 442 | |
7dd31e68 | 443 | eval { |
7814e05f | 444 | # activate remote storage |
7dd31e68 FE |
445 | PVE::Tools::run_command([@remcmd, '/usr/sbin/pvesm', 'status', |
446 | '--storage', $param->{storage}]); | |
7814e05f | 447 | }; |
4af77132 | 448 | die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@; |
7814e05f | 449 | |
53ec90e2 | 450 | PVE::Tools::run_command([@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)], |
7814e05f DM |
451 | errmsg => "mkdir failed"); |
452 | ||
5c0720d6 | 453 | $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)]; |
7814e05f DM |
454 | } else { |
455 | PVE::Storage::activate_storage($cfg, $param->{storage}); | |
4ea5bca4 | 456 | File::Path::make_path($dirname); |
53ec90e2 | 457 | $cmd = ['cp', '--', $tmpfilename, $dest]; |
7814e05f DM |
458 | } |
459 | ||
7dd31e68 | 460 | my $worker = sub { |
7814e05f | 461 | my $upid = shift; |
7dd31e68 | 462 | |
7814e05f DM |
463 | print "starting file import from: $tmpfilename\n"; |
464 | print "target node: $node\n"; | |
465 | print "target file: $dest\n"; | |
466 | print "file size is: $size\n"; | |
467 | print "command: " . join(' ', @$cmd) . "\n"; | |
468 | ||
469 | eval { PVE::Tools::run_command($cmd, errmsg => 'import failed'); }; | |
470 | if (my $err = $@) { | |
471 | unlink $dest; | |
472 | die $err; | |
473 | } | |
474 | print "finished file import successfully\n"; | |
475 | }; | |
476 | ||
d6c9dc34 DM |
477 | my $upid = $rpcenv->fork_worker('imgcopy', undef, $user, $worker); |
478 | ||
479 | # apache removes the temporary file on return, so we need | |
480 | # to wait here to make sure the worker process starts and | |
481 | # opens the file before it gets removed. | |
482 | sleep(1); | |
483 | ||
484 | return $upid; | |
7814e05f | 485 | }}); |
7dd31e68 | 486 | |
b6cf0a66 | 487 | 1; |