]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Status.pm
api: status: import run_command
[pve-storage.git] / PVE / API2 / Storage / Status.pm
index 50013a313805d1fc2db9e4eb18b1e4c312124916..2800ad23c3e633e56b5f6807b7f02a7cc11df1bd 100644 (file)
@@ -3,20 +3,22 @@ package PVE::API2::Storage::Status;
 use strict;
 use warnings;
 
-use File::Path;
 use File::Basename;
-use PVE::Tools;
-use PVE::INotify;
+use File::Path;
+
 use PVE::Cluster;
+use PVE::Exception qw(raise_param_exc);
+use PVE::INotify;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RESTHandler;
+use PVE::RPCEnvironment;
 use PVE::RRD;
-use PVE::Storage;
+use PVE::Tools qw(run_command);
+
 use PVE::API2::Storage::Content;
-use PVE::API2::Storage::PruneBackups;
 use PVE::API2::Storage::FileRestore;
-use PVE::RESTHandler;
-use PVE::RPCEnvironment;
-use PVE::JSONSchema qw(get_standard_option);
-use PVE::Exception qw(raise_param_exc);
+use PVE::API2::Storage::PruneBackups;
+use PVE::Storage;
 
 use base qw(PVE::RESTHandler);
 
@@ -378,9 +380,11 @@ __PACKAGE__->register_method ({
            content => {
                description => "Content type.",
                type => 'string', format => 'pve-storage-content',
+               enum => ['iso', 'vztmpl'],
            },
            filename => {
-               description => "The name of the file to create.",
+               description => "The name of the file to create. Caution: This will be normalized!",
+               maxLength => 255,
                type => 'string',
            },
            tmpfilename => {
@@ -444,6 +448,7 @@ __PACKAGE__->register_method ({
        # we simply overwrite the destination file if it already exists
 
        my $cmd;
+       my $err_cmd;
        if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
            my $remip = PVE::Cluster::remote_node_ip($node);
 
@@ -451,21 +456,23 @@ __PACKAGE__->register_method ({
 
            my @remcmd = ('/usr/bin/ssh', @ssh_options, $remip, '--');
 
-           eval {
-               # activate remote storage
-               PVE::Tools::run_command([@remcmd, '/usr/sbin/pvesm', 'status',
-                                        '--storage', $param->{storage}]);
+           eval { # activate remote storage
+               run_command([@remcmd, '/usr/sbin/pvesm', 'status', '--storage', $param->{storage}]);
            };
            die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
 
-           PVE::Tools::run_command([@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)],
-                                   errmsg => "mkdir failed");
+           run_command(
+               [@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)],
+               errmsg => "mkdir failed",
+           );
  
            $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
+           $err_cmd = [@remcmd, 'unlink', '--', $dest];
        } else {
            PVE::Storage::activate_storage($cfg, $param->{storage});
            File::Path::make_path($dirname);
            $cmd = ['cp', '--', $tmpfilename, $dest];
+           $err_cmd = ['unlink', '--', $dest];
        }
 
        my $worker = sub {
@@ -477,9 +484,9 @@ __PACKAGE__->register_method ({
            print "file size is: $size\n";
            print "command: " . join(' ', @$cmd) . "\n";
 
-           eval { PVE::Tools::run_command($cmd, errmsg => 'import failed'); };
+           eval { run_command($cmd, errmsg => 'import failed'); };
            if (my $err = $@) {
-               unlink $dest;
+               eval { run_command($err_cmd) };
                die $err;
            }
            print "finished file import successfully\n";
@@ -525,6 +532,7 @@ __PACKAGE__->register_method({
            },
            filename => {
                description => "The name of the file to create. Caution: This will be normalized!",
+               maxLength => 255,
                type => 'string',
            },
            checksum => {
@@ -607,7 +615,9 @@ __PACKAGE__->register_method({
            PVE::Tools::download_file_from_url("$path/$filename", $url, $opts);
        };
 
-       return $rpcenv->fork_worker('download', $filename, $user, $worker);
+       my $worker_id = PVE::Tools::encode_text($filename); # must not pass : or the like as w-ID
+
+       return $rpcenv->fork_worker('download', $worker_id, $user, $worker);
     }});
 
 1;