Skip to content

Commit

Permalink
[daemon] Handle forced stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
luis4a0 committed Jan 29, 2021
1 parent 83c747e commit 853800c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,8 @@ try // clang-format on
std::function<grpc::Status(VirtualMachine&)> operation;
if (request->cancel_shutdown())
operation = std::bind(&Daemon::cancel_vm_shutdown, this, std::placeholders::_1);
else if (request->force_stop())
operation = std::bind(&Daemon::switch_off_vm, this, std::placeholders::_1);
else
operation = std::bind(&Daemon::shutdown_vm, this, std::placeholders::_1,
std::chrono::minutes(request->time_minutes()));
Expand Down Expand Up @@ -2405,6 +2407,26 @@ grpc::Status mp::Daemon::shutdown_vm(VirtualMachine& vm, const std::chrono::mill
return grpc::Status::OK;
}

grpc::Status mp::Daemon::switch_off_vm(VirtualMachine& vm)
{
const auto& name = vm.vm_name;
const auto& state = vm.current_state();

using St = VirtualMachine::State;
const auto skip_states = {St::off, St::stopped};

if (std::none_of(cbegin(skip_states), cend(skip_states), [&state](const auto& st) { return state == st; }))
{
delayed_shutdown_instances.erase(name);

vm.stop(true);
}
else
mpl::log(mpl::Level::debug, category, fmt::format("instance \"{}\" does not need stopping", name));

return grpc::Status::OK;
}

grpc::Status mp::Daemon::cancel_vm_shutdown(const VirtualMachine& vm)
{
auto it = delayed_shutdown_instances.find(vm.vm_name);
Expand Down
3 changes: 2 additions & 1 deletion src/daemon/daemon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2020 Canonical, Ltd.
* Copyright (C) 2017-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -147,6 +147,7 @@ public slots:
std::promise<grpc::Status>* status_promise, bool start);
grpc::Status reboot_vm(VirtualMachine& vm);
grpc::Status shutdown_vm(VirtualMachine& vm, const std::chrono::milliseconds delay);
grpc::Status switch_off_vm(VirtualMachine& vm);
grpc::Status cancel_vm_shutdown(const VirtualMachine& vm);
grpc::Status cmd_vms(const std::vector<std::string>& tgts, std::function<grpc::Status(VirtualMachine&)> cmd);
void install_sshfs(VirtualMachine* vm, const std::string& name);
Expand Down

0 comments on commit 853800c

Please sign in to comment.