From 380eccc3b98a8290992d47baedb8b21596c50d22 Mon Sep 17 00:00:00 2001 From: Allison Larson Date: Wed, 21 Aug 2024 13:37:26 -0700 Subject: [PATCH] bsd/nfs: Check the status of nfsd before update/restart --- plugins/hosts/bsd/cap/nfs.rb | 18 ++++++++++++++-- plugins/hosts/bsd/plugin.rb | 5 +++++ test/unit/plugins/hosts/bsd/cap/nfs_test.rb | 23 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/plugins/hosts/bsd/cap/nfs.rb b/plugins/hosts/bsd/cap/nfs.rb index 053b37c054a..60794d85d7e 100644 --- a/plugins/hosts/bsd/cap/nfs.rb +++ b/plugins/hosts/bsd/cap/nfs.rb @@ -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") @@ -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) @@ -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 @@ -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) diff --git a/plugins/hosts/bsd/plugin.rb b/plugins/hosts/bsd/plugin.rb index 1e95198549e..8bbb1c06cc2 100644 --- a/plugins/hosts/bsd/plugin.rb +++ b/plugins/hosts/bsd/plugin.rb @@ -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 diff --git a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb index 877ff218e59..7744e38e540 100644 --- a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb @@ -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"). @@ -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