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