api: factor out common vm_is_paused code in private helper

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2020-10-16 17:51:24 +02:00
parent 11c601e9ad
commit 009cbf61bc

View File

@ -2401,6 +2401,16 @@ __PACKAGE__->register_method({
return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
}});
my sub vm_is_paused {
my ($vmid) = @_;
my $qmpstatus = eval {
PVE::QemuConfig::assert_config_exists_on_node($vmid);
mon_cmd($vmid, "query-status");
};
warn "$@\n" if $@;
return $qmpstatus && $qmpstatus->{status} eq "paused";
}
__PACKAGE__->register_method({
name => 'vm_shutdown',
path => '{vmid}/status/shutdown',
@ -2469,13 +2479,7 @@ __PACKAGE__->register_method({
#
# checking the qmp status here to get feedback to the gui/cli/api
# and the status query should not take too long
my $qmpstatus = eval {
PVE::QemuConfig::assert_config_exists_on_node($vmid);
mon_cmd($vmid, "query-status");
};
my $err = $@ if $@;
if (!$err && $qmpstatus->{status} eq "paused") {
if (vm_is_paused($vmid)) {
if ($param->{forceStop}) {
warn "VM is paused - stop instead of shutdown\n";
$shutdown = 0;
@ -2551,15 +2555,7 @@ __PACKAGE__->register_method({
my $node = extract_param($param, 'node');
my $vmid = extract_param($param, 'vmid');
my $qmpstatus = eval {
PVE::QemuConfig::assert_config_exists_on_node($vmid);
mon_cmd($vmid, "query-status");
};
my $err = $@ if $@;
if (!$err && $qmpstatus->{status} eq "paused") {
die "VM is paused - cannot shutdown\n";
}
die "VM is paused - cannot shutdown\n" if vm_is_paused($vmid);
die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);