vzdump: make guest include logic testable

As a first step to make the whole guest include logic more testable the
part from the API endpoint has been moved to its own method with as
little changes as possible.

Everything concerning `all` and `exclude` logic is still in the
PVE::VZDump->exec_backup() method.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
This commit is contained in:
Aaron Lauterer 2020-06-08 15:00:34 +02:00 committed by Thomas Lamprecht
parent 9c3a51ee22
commit 5c4da4c3e8
2 changed files with 43 additions and 30 deletions

View File

@ -69,39 +69,15 @@ __PACKAGE__->register_method ({
return 'OK' if $param->{node} && $param->{node} ne $nodename;
my $cmdline = PVE::VZDump::Common::command_line($param);
my @vmids;
# convert string lists to arrays
if ($param->{pool}) {
@vmids = @{PVE::API2Tools::get_resource_pool_guest_members($param->{pool})};
} else {
@vmids = PVE::Tools::split_list(extract_param($param, 'vmid'));
}
my ($vmids, $skiplist) = PVE::VZDump::get_included_guests($param);
if($param->{stop}){
PVE::VZDump::stop_running_backups();
return 'OK' if !scalar(@vmids);
return 'OK' if !scalar(@{$vmids});
}
my $skiplist = [];
if (!$param->{all}) {
if (!$param->{node} || $param->{node} eq $nodename) {
my $vmlist = PVE::Cluster::get_vmlist();
my @localvmids = ();
foreach my $vmid (@vmids) {
my $d = $vmlist->{ids}->{$vmid};
if ($d && ($d->{node} ne $nodename)) {
push @$skiplist, $vmid;
} else {
push @localvmids, $vmid;
}
}
@vmids = @localvmids;
# silent exit if specified VMs run on other nodes
return "OK" if !scalar(@vmids);
}
$param->{vmids} = PVE::VZDump::check_vmids(@vmids)
}
# silent exit if specified VMs run on other nodes
return "OK" if !scalar(@{$vmids}) && !$param->{all};
my @exclude = PVE::Tools::split_list(extract_param($param, 'exclude'));
$param->{exclude} = PVE::VZDump::check_vmids(@exclude);
@ -118,7 +94,7 @@ __PACKAGE__->register_method ({
}
die "you can only backup a single VM with option --stdout\n"
if $param->{stdout} && scalar(@vmids) != 1;
if $param->{stdout} && scalar(@{$vmids}) != 1;
$rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
if $param->{storage};
@ -167,7 +143,7 @@ __PACKAGE__->register_method ({
}
my $taskid;
$taskid = $vmids[0] if scalar(@vmids) == 1;
$taskid = ${$vmids}[0] if scalar(@{$vmids}) == 1;
return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
}});

View File

@ -21,6 +21,8 @@ use PVE::RPCEnvironment;
use PVE::Storage;
use PVE::VZDump::Common;
use PVE::VZDump::Plugin;
use PVE::Tools qw(extract_param);
use PVE::API2Tools;
my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs);
@ -1167,4 +1169,39 @@ sub stop_running_backups {
}
}
sub get_included_guests {
my ($job) = @_;
my $nodename = PVE::INotify::nodename();
my $vmids = [];
# convert string lists to arrays
if ($job->{pool}) {
$vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool});
} else {
$vmids = [ PVE::Tools::split_list(extract_param($job, 'vmid')) ];
}
my $skiplist = [];
if (!$job->{all}) {
if (!$job->{node} || $job->{node} eq $nodename) {
my $vmlist = PVE::Cluster::get_vmlist();
my $localvmids = [];
foreach my $vmid (@{$vmids}) {
my $d = $vmlist->{ids}->{$vmid};
if ($d && ($d->{node} ne $nodename)) {
push @{$skiplist}, $vmid;
} else {
push @{$localvmids}, $vmid;
}
}
$vmids = $localvmids;
}
$job->{vmids} = PVE::VZDump::check_vmids(@{$vmids})
}
return ($vmids, $skiplist);
}
1;