Skip to content

Commit

Permalink
bsd/nfs: Check the status of nfsd before update/restart
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonlarson committed Aug 21, 2024
1 parent 282b469 commit 380eccc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions plugins/hosts/bsd/cap/nfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Cap
class NFS
def self.nfs_export(environment, ui, id, ips, folders)
nfs_exports_template = environment.host.capability(:nfs_exports_template)
nfs_restart_command = environment.host.capability(:nfs_restart_command)
nfs_status_command = environment.host.capability(:nfs_status_command)
nfs_update_command = environment.host.capability(:nfs_update_command)
logger = Log4r::Logger.new("vagrant::hosts::bsd")

Expand Down Expand Up @@ -117,8 +119,12 @@ def self.nfs_export(environment, ui, id, ips, folders)
"#{sudo_command}/usr/bin/tee -a /etc/exports >/dev/null")
end

# Attempt to update nfsd
system(*nfs_update_command)
# Check if nfsd is running, and update or restart depending on the result
if nfs_running?(nfs_status_command)
system(*nfs_update_command)
else
system(*nfs_restart_command)
end
end

def self.nfs_exports_template(environment)
Expand Down Expand Up @@ -158,6 +164,10 @@ def self.nfs_prune(environment, ui, valid_ids)
raise Vagrant::Errors::NFSCantReadExports
end

def self.nfs_running?(check_command)
Vagrant::Util::Subprocess.execute(*check_command).exit_code == 0
end

def self.nfs_restart_command(environment)
["sudo", "nfsd", "restart"]
end
Expand All @@ -166,6 +176,10 @@ def self.nfs_update_command(environment)
["sudo", "nfsd", "update"]
end

def self.nfs_status_command(environment)
["sudo", "nfsd", "status"]
end

protected

def self.nfs_cleanup(id)
Expand Down
5 changes: 5 additions & 0 deletions plugins/hosts/bsd/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Plugin < Vagrant.plugin("2")
Cap::NFS
end

host_capability("bsd", "nfs_status_command") do
require_relative "cap/nfs"
Cap::NFS
end

host_capability("bsd", "resolve_host_path") do
require_relative "cap/path"
Cap::Path
Expand Down
23 changes: 23 additions & 0 deletions test/unit/plugins/hosts/bsd/cap/nfs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
allow(described_class).to receive(:sleep)
allow(described_class).to receive(:nfs_cleanup)
allow(described_class).to receive(:system)
allow(described_class).to receive(:nfs_running?).and_return(true)
allow(File).to receive(:writable?).with("/etc/exports")

allow(Vagrant::Util::Subprocess).to receive(:execute).with("nfsd", "checkexports").
Expand Down Expand Up @@ -49,5 +50,27 @@
described_class.nfs_export(environment, ui, id, ips, folders)
end
end

context "when nfsd is not running" do
before {
allow(described_class).to receive(:nfs_running?).and_return(false)
}
it "should restart nfsd" do
expect(host).to receive(:capability).with(:nfs_restart_command).and_return(["restart"])
expect(described_class).to receive(:system).with("restart")
described_class.nfs_export(environment, ui, id, ips, folders)
end
end

context "when nfsd is running" do
before {
allow(described_class).to receive(:nfs_running?).and_return(true)
}
it "should update nfsd" do
expect(host).to receive(:capability).with(:nfs_update_command).and_return(["update"])
expect(described_class).to receive(:system).with("update")
described_class.nfs_export(environment, ui, id, ips, folders)
end
end
end
end

0 comments on commit 380eccc

Please sign in to comment.