From 339a3f50ef386608a3b613d0a8be8611c8ef408c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Pe=C3=B1aranda?= Date: Fri, 28 May 2021 15:57:37 -0300 Subject: [PATCH] [lxd] Implement forced stop. --- .../backends/lxd/lxd_virtual_machine.cpp | 33 +++++++++++++++++-- .../backends/lxd/lxd_virtual_machine.h | 4 +-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/platform/backends/lxd/lxd_virtual_machine.cpp b/src/platform/backends/lxd/lxd_virtual_machine.cpp index 0ac41ba6bb..d79ae983a0 100644 --- a/src/platform/backends/lxd/lxd_virtual_machine.cpp +++ b/src/platform/backends/lxd/lxd_virtual_machine.cpp @@ -223,8 +223,35 @@ void mp::LXDVirtualMachine::start() update_state(); } -void mp::LXDVirtualMachine::stop() +void mp::LXDVirtualMachine::stop(bool force) { + if (force) + { + auto present_state = current_state(); + + if (present_state != State::stopped && present_state != State::off) + { + const QJsonObject state_json{{"action", "stop"}, {"force", true}}; + + auto state_task = lxd_request(manager, "PUT", state_url(), state_json, 5000); + + try + { + lxd_wait(manager, base_url, state_task, 60000); + } + catch (const LXDNotFoundException&) + { + } + + state = State::stopped; + } + + if (update_shutdown_status) + update_state(); + + return; + } + std::unique_lock lock{state_mutex}; auto present_state = current_state(); @@ -255,9 +282,9 @@ void mp::LXDVirtualMachine::stop() update_state(); } -void mp::LXDVirtualMachine::shutdown() +void mp::LXDVirtualMachine::shutdown(bool force) { - stop(); + stop(force); } void mp::LXDVirtualMachine::suspend() diff --git a/src/platform/backends/lxd/lxd_virtual_machine.h b/src/platform/backends/lxd/lxd_virtual_machine.h index 8c1c29adb6..8b00c60ad9 100644 --- a/src/platform/backends/lxd/lxd_virtual_machine.h +++ b/src/platform/backends/lxd/lxd_virtual_machine.h @@ -36,9 +36,9 @@ class LXDVirtualMachine final : public BaseVirtualMachine LXDVirtualMachine(const VirtualMachineDescription& desc, VMStatusMonitor& monitor, NetworkAccessManager* manager, const QUrl& base_url, const QString& bridge_name); ~LXDVirtualMachine() override; - void stop() override; + void stop(bool force = false) override; void start() override; - void shutdown() override; + void shutdown(bool force = false) override; void suspend() override; State current_state() override; int ssh_port() override;