[pve-devel] [PATCH pve-manager 1/2] api2: add suspendall endpoint

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Feb 5 13:40:10 CET 2021


On 05.02.21 11:35, Hannes Laimer wrote:

Missing reference to bug #804 which this patch addresses.
https://pve.proxmox.com/wiki/Developer_Documentation#Commits_and_Commit_Messages

Mentioning that this is mostly a 1:1 copy of the stop-, start-, all
methods would also be good; improves confidence in code and explains
why some parts seem so familiar ;-)


Looks OK code wise, but lots of code deduplication potential with the
other "start/stop/.. all" endpoints and IMO some missing thoughts on
the used API privileges.

> Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
> ---
>  PVE/API2/Nodes.pm | 113 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 113 insertions(+)
> 
> diff --git a/PVE/API2/Nodes.pm b/PVE/API2/Nodes.pm
> index 8172231e..0c11fe35 100644
> --- a/PVE/API2/Nodes.pm
> +++ b/PVE/API2/Nodes.pm
> @@ -1943,6 +1943,119 @@ __PACKAGE__->register_method ({
>  	return $rpcenv->fork_worker('stopall', undef, $authuser, $code);
>      }});
>  
> +my $create_suspend_worker = sub {
> +    my ($nodename, $type, $vmid, $down_timeout) = @_;
> +
> +    my $upid;
> +    if ($type eq 'qemu') {
> +	return if !PVE::QemuServer::check_running($vmid, 1);
> +	my $timeout =  defined($down_timeout) ? int($down_timeout) : 60*3;
> +	print STDERR "Suspending VM $vmid (timeout = $timeout seconds)\n";
> +	$upid = PVE::API2::Qemu->vm_suspend({node => $nodename, vmid => $vmid});

You could avoid the $upid and rather just return here

> +    } else {
> +	die "suspension is only supported on VMs, not on '$type'\n";


As we won't get other suspendable types in the foreseeable future we could
make this an early check.

> +    }
> +
> +    return $upid;
> +};
> +
> +__PACKAGE__->register_method ({
> +    name => 'suspendall',
> +    path => 'suspendall',
> +    method => 'POST',
> +    protected => 1,
> +    permissions => {
> +	check => ['perm', '/', [ 'VM.PowerMgmt' ]],

In contrast to the pretty much state less start and stop, supension results in
saving states to disks, so not sure if just having VM.PowerMgmt is enough?

Or at least I see no reasoning telling so in the (rather non existent) commit
message..

> +    },
> +    proxyto => 'node',
> +    description => "Suspend all VMs.",

Suspend all or a specific set of VMs.

> +    parameters => {
> +    	additionalProperties => 0,
> +	properties => {
> +	    node => get_standard_option('pve-node'),
> +	    vms => {
> +		description => "Only consider Guests with these IDs.",
> +		type => 'string',  format => 'pve-vmid-list',
> +		optional => 1,
> +	    },
> +	},
> +    },
> +    returns => {
> +	type => 'string',
> +    },
> +    code => sub {
> +	my ($param) = @_;
> +
> +	my $rpcenv = PVE::RPCEnvironment::get();
> +	my $authuser = $rpcenv->get_user();
> +
> +	my $nodename = $param->{node};
> +	$nodename = PVE::INotify::nodename() if $nodename eq 'localhost';
> +
> +	my $code = sub {
> +
> +	    $rpcenv->{type} = 'priv'; # to start tasks in background
> +
> +	    my $stopList = &$get_start_stop_list($nodename, undef, $param->{vms});

Use new call syntax for code references in new code:

$get_start_stop_list->(...)

> +
> +	    my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
> +	    my $datacenterconfig = cfs_read_file('datacenter.cfg');
> +	    # if not set by user spawn max cpu count number of workers
> +	    my $maxWorkers =  $datacenterconfig->{max_workers} || $cpuinfo->{cpus};

IIRC, above lines are now duplicated two or three times, refactoring it
out into a separate method would avoid code duplication and reduce the
length of this scope.

> +
> +	    foreach my $order (sort {$b <=> $a} keys %$stopList) {
> +		my $vmlist = $stopList->{$order};
> +		my $workers = {};
> +
> +		my $finish_worker = sub {
> +		    my $pid = shift;
> +		    my $d = $workers->{$pid};
> +		    return if !$d;
> +		    delete $workers->{$pid};
> +
> +		    syslog('info', "end task $d->{upid}");
> +		};
> +
> +		foreach my $vmid (sort {$b <=> $a} keys %$vmlist) {
> +		    my $d = $vmlist->{$vmid};
> +		    my $upid;
> +		    eval { $upid = &$create_suspend_worker($nodename, $d->{type}, $vmid, $d->{down}); };

above could be shorter, i.e., write as:

my $upid = eval { $create_suspend_worker->($nodename, $d->{type}, $vmid, $d->{down}) };

> +		    warn $@ if $@;
> +		    next if !$upid;
> +
> +		    my $res = PVE::Tools::upid_decode($upid, 1);
> +		    next if !$res;
> +
> +		    my $pid = $res->{pid};
> +
> +		    $workers->{$pid} = { type => $d->{type}, upid => $upid, vmid => $vmid };
> +		    while (scalar(keys %$workers) >= $maxWorkers) {
> +			foreach my $p (keys %$workers) {
> +			    if (!PVE::ProcFSTools::check_process_running($p)) {
> +				&$finish_worker($p);
> +			    }
> +			}
> +			sleep(1);
> +		    }
> +		}
> +		while (scalar(keys %$workers)) {
> +		    foreach my $p (keys %$workers) {
> +			if (!PVE::ProcFSTools::check_process_running($p)) {
> +			    &$finish_worker($p);
> +			}
> +		    }


we have that worker loop twice time per "stop/start/.. all" endpoint,
would be nice to be factored out in a sub which takes the hash reference
to $workers and the code from finish_worker inlined, as that is also
duplicated a few times.

> +		    sleep(1);
> +		}
> +	    }
> +
> +	    syslog('info', "all VMs suspended");
> +
> +	    return;
> +	};
> +
> +	return $rpcenv->fork_worker('suspendall', undef, $authuser, $code);
> +    }});
> +
>  my $create_migrate_worker = sub {
>      my ($nodename, $type, $vmid, $target, $with_local_disks) = @_;
>  
> 






More information about the pve-devel mailing list