guests/bsd: Centralize logic for halting guests
This commit is contained in:
parent
3174c0079b
commit
07e6d0e707
|
@ -1,10 +1,10 @@
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module GuestNetBSD
|
module GuestBSD
|
||||||
module Cap
|
module Cap
|
||||||
class Halt
|
class Halt
|
||||||
def self.halt(machine)
|
def self.halt(machine)
|
||||||
begin
|
begin
|
||||||
machine.communicate.sudo("/sbin/shutdown -p -h now")
|
machine.communicate.sudo("/sbin/shutdown -p -h now", shell: "sh")
|
||||||
rescue IOError
|
rescue IOError
|
||||||
# Do nothing, because it probably means the machine shut down
|
# Do nothing, because it probably means the machine shut down
|
||||||
# and SSH connection was lost.
|
# and SSH connection was lost.
|
|
@ -11,6 +11,11 @@ module VagrantPlugins
|
||||||
Guest
|
Guest
|
||||||
end
|
end
|
||||||
|
|
||||||
|
guest_capability(:bsd, :halt) do
|
||||||
|
require_relative "cap/halt"
|
||||||
|
Cap::Halt
|
||||||
|
end
|
||||||
|
|
||||||
guest_capability(:bsd, :insert_public_key) do
|
guest_capability(:bsd, :insert_public_key) do
|
||||||
require_relative "cap/public_key"
|
require_relative "cap/public_key"
|
||||||
Cap::PublicKey
|
Cap::PublicKey
|
||||||
|
|
|
@ -4,7 +4,9 @@ module VagrantPlugins
|
||||||
class Halt
|
class Halt
|
||||||
def self.halt(machine)
|
def self.halt(machine)
|
||||||
begin
|
begin
|
||||||
machine.communicate.sudo("shutdown -h now")
|
# Darwin does not support the `-p` option like the rest of the
|
||||||
|
# BSD-based guests, so it needs its own cap.
|
||||||
|
machine.communicate.sudo("/sbin/shutdown -h now")
|
||||||
rescue IOError
|
rescue IOError
|
||||||
# Do nothing because SSH connection closed and it probably
|
# Do nothing because SSH connection closed and it probably
|
||||||
# means the VM just shut down really fast.
|
# means the VM just shut down really fast.
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
module VagrantPlugins
|
|
||||||
module GuestFreeBSD
|
|
||||||
module Cap
|
|
||||||
class Halt
|
|
||||||
def self.halt(machine)
|
|
||||||
begin
|
|
||||||
machine.communicate.sudo("shutdown -p now", { shell: "sh" })
|
|
||||||
rescue IOError
|
|
||||||
# Do nothing because SSH connection closed and it probably
|
|
||||||
# means the VM just shut down really fast.
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -21,11 +21,6 @@ module VagrantPlugins
|
||||||
Cap::ConfigureNetworks
|
Cap::ConfigureNetworks
|
||||||
end
|
end
|
||||||
|
|
||||||
guest_capability(:freebsd, :halt) do
|
|
||||||
require_relative "cap/halt"
|
|
||||||
Cap::Halt
|
|
||||||
end
|
|
||||||
|
|
||||||
guest_capability(:freebsd, :rsync_install) do
|
guest_capability(:freebsd, :rsync_install) do
|
||||||
require_relative "cap/rsync"
|
require_relative "cap/rsync"
|
||||||
Cap::RSync
|
Cap::RSync
|
||||||
|
|
|
@ -21,11 +21,6 @@ module VagrantPlugins
|
||||||
Cap::ConfigureNetworks
|
Cap::ConfigureNetworks
|
||||||
end
|
end
|
||||||
|
|
||||||
guest_capability(:netbsd, :halt) do
|
|
||||||
require_relative "cap/halt"
|
|
||||||
Cap::Halt
|
|
||||||
end
|
|
||||||
|
|
||||||
guest_capability(:netbsd, :rsync_install) do
|
guest_capability(:netbsd, :rsync_install) do
|
||||||
require_relative "cap/rsync"
|
require_relative "cap/rsync"
|
||||||
Cap::RSync
|
Cap::RSync
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
module VagrantPlugins
|
|
||||||
module GuestOpenBSD
|
|
||||||
module Cap
|
|
||||||
class Halt
|
|
||||||
def self.halt(machine)
|
|
||||||
begin
|
|
||||||
machine.communicate.sudo("shutdown -p -h now")
|
|
||||||
rescue IOError
|
|
||||||
# Do nothing, because it probably means the machine shut down
|
|
||||||
# and SSH connection was lost.
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -21,11 +21,6 @@ module VagrantPlugins
|
||||||
Cap::ConfigureNetworks
|
Cap::ConfigureNetworks
|
||||||
end
|
end
|
||||||
|
|
||||||
guest_capability(:openbsd, :halt) do
|
|
||||||
require_relative "cap/halt"
|
|
||||||
Cap::Halt
|
|
||||||
end
|
|
||||||
|
|
||||||
guest_capability(:openbsd, :rsync_install) do
|
guest_capability(:openbsd, :rsync_install) do
|
||||||
require_relative "cap/rsync"
|
require_relative "cap/rsync"
|
||||||
Cap::RSync
|
Cap::RSync
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
require_relative "../../../../base"
|
require_relative "../../../../base"
|
||||||
|
|
||||||
describe "VagrantPlugins::GuestFreeBSD::Cap::Halt" do
|
describe "VagrantPlugins::GuestBSD::Cap::Halt" do
|
||||||
let(:described_class) do
|
let(:caps) do
|
||||||
VagrantPlugins::GuestFreeBSD::Plugin
|
VagrantPlugins::GuestBSD::Plugin
|
||||||
.components
|
.components
|
||||||
.guest_capabilities[:freebsd]
|
.guest_capabilities[:bsd]
|
||||||
.get(:halt)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
|
@ -20,15 +19,17 @@ describe "VagrantPlugins::GuestFreeBSD::Cap::Halt" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".halt" do
|
describe ".halt" do
|
||||||
|
let(:cap) { caps.get(:halt) }
|
||||||
|
|
||||||
it "runs the shutdown command" do
|
it "runs the shutdown command" do
|
||||||
comm.expect_command("shutdown -p now")
|
comm.expect_command("/sbin/shutdown -p -h now")
|
||||||
described_class.halt(machine)
|
cap.halt(machine)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not raise an IOError" do
|
it "ignores an IOError" do
|
||||||
comm.stub_command("shutdown -p now", raise: IOError)
|
comm.stub_command("/sbin/shutdown -p -h now", raise: IOError)
|
||||||
expect {
|
expect {
|
||||||
described_class.halt(machine)
|
cap.halt(machine)
|
||||||
}.to_not raise_error
|
}.to_not raise_error
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -0,0 +1,36 @@
|
||||||
|
require_relative "../../../../base"
|
||||||
|
|
||||||
|
describe "VagrantPlugins::GuestDarwin::Cap::Halt" do
|
||||||
|
let(:caps) do
|
||||||
|
VagrantPlugins::GuestDarwin::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:darwin]
|
||||||
|
end
|
||||||
|
|
||||||
|
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("/sbin/shutdown -h now")
|
||||||
|
cap.halt(machine)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "ignores an IOError" do
|
||||||
|
comm.stub_command("/sbin/shutdown -h now", raise: IOError)
|
||||||
|
expect {
|
||||||
|
cap.halt(machine)
|
||||||
|
}.to_not raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue