Merge pull request #11482 from soapy1/systemd-cap-halt

Use systemd to halt system if available
This commit is contained in:
Sophia Castellarin 2020-04-06 10:18:53 -04:00 committed by GitHub
commit cf9a690bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 24 deletions

View File

@ -1,10 +1,18 @@
require 'vagrant/util/guest_inspection'
module VagrantPlugins
module GuestLinux
module Cap
class Halt
extend Vagrant::Util::GuestInspection::Linux
def self.halt(machine)
begin
machine.communicate.sudo("shutdown -h now")
if systemd?(machine.communicate)
machine.communicate.sudo("systemctl poweroff")
else
machine.communicate.sudo("shutdown -h now")
end
rescue IOError, Vagrant::Errors::SSHDisconnected
# Do nothing, because it probably means the machine shut down
# and SSH connection was lost.

View File

@ -10,34 +10,57 @@ describe "VagrantPlugins::GuestLinux::Cap::Halt" do
let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
allow(machine).to receive(:communicate).and_return(comm)
end
after do
comm.verify_expectations!
end
describe ".halt" do
let(:cap) { caps.get(:halt) }
it "runs the shutdown command" do
comm.expect_command("shutdown -h now")
cap.halt(machine)
context "systemd not enabled" do
before do
allow(machine).to receive(:communicate).and_return(comm)
allow(comm).to receive(:test).and_return(false)
end
it "does not raise an IOError" do
comm.stub_command("shutdown -h now", raise: IOError)
expect {
cap.halt(machine)
}.to_not raise_error
after do
comm.verify_expectations!
end
it "does not raise a SSHDisconnected" do
comm.stub_command("shutdown -h now", raise: Vagrant::Errors::SSHDisconnected)
expect {
describe ".halt" do
let(:cap) { caps.get(:halt) }
it "runs the shutdown command" do
comm.expect_command("shutdown -h now")
cap.halt(machine)
}.to_not raise_error
end
it "does not raise an IOError" do
comm.stub_command("shutdown -h now", raise: IOError)
expect {
cap.halt(machine)
}.to_not raise_error
end
it "does not raise a SSHDisconnected" do
comm.stub_command("shutdown -h now", raise: Vagrant::Errors::SSHDisconnected)
expect {
cap.halt(machine)
}.to_not raise_error
end
end
context "systemd enabled" do
before do
allow(machine).to receive(:communicate).and_return(comm)
allow(comm).to receive(:test).and_return(true)
end
after do
comm.verify_expectations!
end
describe ".halt" do
let(:cap) { caps.get(:halt) }
it "runs the shutdown command" do
comm.expect_command("systemctl poweroff")
cap.halt(machine)
end
end
end
end
end