From a00483debabc6b3f1027c30f345dae800a044a26 Mon Sep 17 00:00:00 2001 From: Allison Larson Date: Tue, 20 Aug 2024 11:06:35 -0700 Subject: [PATCH 1/3] Move nfs mounting message within block --- plugins/synced_folders/nfs/synced_folder.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/synced_folders/nfs/synced_folder.rb b/plugins/synced_folders/nfs/synced_folder.rb index 4d4d0a686a0..44efcf37a46 100644 --- a/plugins/synced_folders/nfs/synced_folder.rb +++ b/plugins/synced_folders/nfs/synced_folder.rb @@ -103,12 +103,12 @@ def enable(machine, folders, nfsopts) mount_folders = {} folders.each do |id, opts| mount_folders[id] = opts.dup if opts[:guestpath] - end - machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry", - guestpath: opts[:guestpath], - hostpath: opts[:hostpath] - )) + machine.ui.detail(I18n.t("vagrant.actions.vm.nfs.mounting_entry", + guestpath: opts[:guestpath], + hostpath: opts[:hostpath] + )) + end # Mount them! if machine.guest.capability?(:nfs_pre) From 282b46904b322fb0468f4ed844267b54915561de Mon Sep 17 00:00:00 2001 From: Allison Larson Date: Wed, 21 Aug 2024 10:49:47 -0700 Subject: [PATCH 2/3] bsd/nfs: Use nfs_update_command instead of restart --- plugins/hosts/bsd/cap/nfs.rb | 11 +++++++---- plugins/hosts/bsd/plugin.rb | 5 +++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/hosts/bsd/cap/nfs.rb b/plugins/hosts/bsd/cap/nfs.rb index c6c003db006..053b37c054a 100644 --- a/plugins/hosts/bsd/cap/nfs.rb +++ b/plugins/hosts/bsd/cap/nfs.rb @@ -13,7 +13,7 @@ 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_update_command = environment.host.capability(:nfs_update_command) logger = Log4r::Logger.new("vagrant::hosts::bsd") nfs_checkexports! if File.file?("/etc/exports") @@ -117,9 +117,8 @@ def self.nfs_export(environment, ui, id, ips, folders) "#{sudo_command}/usr/bin/tee -a /etc/exports >/dev/null") end - # We run restart here instead of "update" just in case nfsd - # is not starting - system(*nfs_restart_command) + # Attempt to update nfsd + system(*nfs_update_command) end def self.nfs_exports_template(environment) @@ -163,6 +162,10 @@ def self.nfs_restart_command(environment) ["sudo", "nfsd", "restart"] end + def self.nfs_update_command(environment) + ["sudo", "nfsd", "update"] + end + protected def self.nfs_cleanup(id) diff --git a/plugins/hosts/bsd/plugin.rb b/plugins/hosts/bsd/plugin.rb index 6501767a786..1e95198549e 100644 --- a/plugins/hosts/bsd/plugin.rb +++ b/plugins/hosts/bsd/plugin.rb @@ -39,6 +39,11 @@ class Plugin < Vagrant.plugin("2") Cap::NFS end + host_capability("bsd", "nfs_update_command") do + require_relative "cap/nfs" + Cap::NFS + end + host_capability("bsd", "resolve_host_path") do require_relative "cap/path" Cap::Path From 380eccc3b98a8290992d47baedb8b21596c50d22 Mon Sep 17 00:00:00 2001 From: Allison Larson Date: Wed, 21 Aug 2024 13:37:26 -0700 Subject: [PATCH 3/3] 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