Skip to content

Commit 9a110e0

Browse files
bors[bot]Chris Townsend
andcommitted
Merge #589
589: delayed_shutdown: Only broadcast `wall` messages if SSHSession is valid r=ricab a=townsend2010 Fixes #539 Co-authored-by: Chris Townsend <[email protected]>
2 parents 78e9367 + b2a6c1f commit 9a110e0

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

include/multipass/delayed_shutdown_timer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DelayedShutdownTimer : public QObject
3333
Q_OBJECT
3434

3535
public:
36-
DelayedShutdownTimer(VirtualMachine* virtual_machine, SSHSession&& session);
36+
DelayedShutdownTimer(VirtualMachine* virtual_machine, optional<SSHSession>&& session);
3737
~DelayedShutdownTimer();
3838

3939
void start(const std::chrono::milliseconds delay);
@@ -47,7 +47,7 @@ class DelayedShutdownTimer : public QObject
4747

4848
QTimer shutdown_timer;
4949
VirtualMachine* virtual_machine;
50-
SSHSession ssh_session;
50+
optional<SSHSession> ssh_session;
5151
std::chrono::milliseconds delay;
5252
std::chrono::milliseconds time_remaining;
5353
};

include/multipass/optional.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 Canonical, Ltd.
2+
* Copyright (C) 2018-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -13,8 +13,6 @@
1313
* You should have received a copy of the GNU General Public License
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*
16-
* Authored by: Alberto Aguirre <[email protected]>
17-
*
1816
*/
1917

2018
#ifndef MULTIPASS_OPTIONAL_H
@@ -25,17 +23,17 @@
2523
#include <optional>
2624
namespace multipass
2725
{
28-
using std::optional;
2926
using std::nullopt;
27+
using std::optional;
3028
}
3129

3230
#elif __has_include(<experimental/optional>)
3331

3432
#include <experimental/optional>
3533
namespace multipass
3634
{
37-
using std::experimental::optional;
3835
using std::experimental::nullopt;
36+
using std::experimental::optional;
3937
}
4038

4139
#else

src/daemon/daemon.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2018 Canonical, Ltd.
2+
* Copyright (C) 2017-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -1972,7 +1972,17 @@ grpc::Status mp::Daemon::shutdown_vm(VirtualMachine& vm, const std::chrono::mill
19721972
{
19731973
delayed_shutdown_instances.erase(name);
19741974

1975-
mp::SSHSession session{vm.ssh_hostname(), vm.ssh_port(), vm.ssh_username(), *config->ssh_key_provider};
1975+
mp::optional<mp::SSHSession> session;
1976+
try
1977+
{
1978+
session = mp::SSHSession{vm.ssh_hostname(), vm.ssh_port(), vm.ssh_username(), *config->ssh_key_provider};
1979+
}
1980+
catch (const std::exception& e)
1981+
{
1982+
mpl::log(mpl::Level::info, category,
1983+
fmt::format("Cannot open ssh session on \"{}\" shutdown: {}", name, e.what()));
1984+
}
1985+
19761986
auto& shutdown_timer = delayed_shutdown_instances[name] =
19771987
std::make_unique<DelayedShutdownTimer>(&vm, std::move(session));
19781988

src/daemon/delayed_shutdown_timer.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018 Canonical, Ltd.
2+
* Copyright (C) 2018-2019 Canonical, Ltd.
33
*
44
* This program is free software; you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -25,22 +25,27 @@ namespace mpl = multipass::logging;
2525

2626
namespace
2727
{
28-
void write_shutdown_message(mp::SSHSession& ssh_session, const std::chrono::minutes& time_left, const std::string& name)
28+
void write_shutdown_message(mp::optional<mp::SSHSession>& ssh_session, const std::chrono::minutes& time_left,
29+
const std::string& name)
2930
{
30-
if (time_left > std::chrono::milliseconds::zero())
31+
if (ssh_session)
3132
{
32-
ssh_session.exec(fmt::format("wall \"The system is going down for poweroff in {} minute{}, use 'multipass stop "
33-
"--cancel {}' to cancel the shutdown.\"",
34-
time_left.count(), time_left > std::chrono::minutes(1) ? "s" : "", name));
35-
}
36-
else
37-
{
38-
ssh_session.exec(fmt::format("wall The system is going down for poweroff now"));
33+
if (time_left > std::chrono::milliseconds::zero())
34+
{
35+
ssh_session->exec(
36+
fmt::format("wall \"The system is going down for poweroff in {} minute{}, use 'multipass stop "
37+
"--cancel {}' to cancel the shutdown.\"",
38+
time_left.count(), time_left > std::chrono::minutes(1) ? "s" : "", name));
39+
}
40+
else
41+
{
42+
ssh_session->exec(fmt::format("wall The system is going down for poweroff now"));
43+
}
3944
}
4045
}
4146
} // namespace
4247

43-
mp::DelayedShutdownTimer::DelayedShutdownTimer(VirtualMachine* virtual_machine, SSHSession&& session)
48+
mp::DelayedShutdownTimer::DelayedShutdownTimer(VirtualMachine* virtual_machine, mp::optional<SSHSession>&& session)
4449
: virtual_machine{virtual_machine}, ssh_session{std::move(session)}
4550
{
4651
}
@@ -49,8 +54,11 @@ mp::DelayedShutdownTimer::~DelayedShutdownTimer()
4954
{
5055
if (shutdown_timer.isActive())
5156
{
52-
// exit_code() is here to make sure the command finishes before continuing in the dtor
53-
ssh_session.exec("wall The system shutdown has been cancelled").exit_code();
57+
if (ssh_session)
58+
{
59+
// exit_code() is here to make sure the command finishes before continuing in the dtor
60+
ssh_session->exec("wall The system shutdown has been cancelled").exit_code();
61+
}
5462
mpl::log(mpl::Level::info, virtual_machine->vm_name, fmt::format("Cancelling delayed shutdown"));
5563
virtual_machine->state = VirtualMachine::State::running;
5664
}

0 commit comments

Comments
 (0)