Merge pull request #8695 from jen20/smartos-nfs

guests/smartos: Add/fix various guest capabilities
This commit is contained in:
Chris Roberts 2017-07-05 15:28:42 -07:00 committed by GitHub
commit f700b111ce
5 changed files with 69 additions and 37 deletions

View File

@ -3,12 +3,21 @@ module VagrantPlugins
module Cap module Cap
class ChangeHostName class ChangeHostName
def self.change_host_name(machine, name) def self.change_host_name(machine, name)
su_cmd = machine.config.smartos.suexec_cmd sudo = machine.config.smartos.suexec_cmd
# Only do this if the hostname is not already set machine.communicate.tap do |comm|
if !machine.communicate.test("hostname | grep '#{name}'") comm.execute <<-EOH.sub(/^ */, '')
machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"") if hostname | grep '#{name}' ; then
machine.communicate.execute("#{su_cmd} hostname #{name}") exit 0
fi
if [ -d /usbkey ] && [ "$(zonename)" == "global" ] ; then
#{sudo} sed -i '' 's/hostname=.*/hostname=#{name}/' /usbkey/config
fi
#{sudo} echo '#{name}' > /etc/nodename
#{sudo} hostname #{name}
EOH
end end
end end
end end

View File

@ -7,8 +7,17 @@ module VagrantPlugins
folders.each do |name, opts| folders.each do |name, opts|
machine.communicate.tap do |comm| machine.communicate.tap do |comm|
comm.execute("#{sudo} mkdir -p #{opts[:guestpath]}", {shell: "sh"}) nfsDescription = "#{ip}:#{opts[:hostpath]}:#{opts[:guestpath]}"
comm.execute("#{sudo} /usr/sbin/mount -F nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {shell: "sh"})
comm.execute <<-EOH.sub(/^ */, '')
if [ -d /usbkey ] && [ "$(zonename)" == "global" ] ; then
#{sudo} mkdir -p /usbkey/config.inc
printf '#{nfsDescription}\\n' | #{sudo} tee -a /usbkey/config.inc/nfs_mounts
fi
#{sudo} mkdir -p #{opts[:guestpath]}
#{sudo} /usr/sbin/mount -F nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'
EOH
end end
end end
end end

View File

@ -1,33 +1,39 @@
require_relative "../../../../base" require_relative "../../../../base"
require_relative "../../../../../../plugins/guests/smartos/config"
describe "VagrantPlugins::VagrantPlugins::Cap::ChangeHostName" do describe "VagrantPlugins::GuestSmartos::Cap::ChangeHostName" do
let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:change_host_name) } let(:caps) do
VagrantPlugins::GuestSmartos::Plugin
.components
.guest_capabilities[:smartos]
end
let(:machine) { double("machine") } let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) } let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:old_hostname) { 'oldhostname.olddomain.tld' }
let(:new_hostname) { 'newhostname.olddomain.tld' }
before do before do
machine.stub(:communicate).and_return(communicator) machine.stub(:communicate).and_return(comm)
machine.stub(:config).and_return(config) machine.stub(:config).and_return(config)
communicator.stub_command("hostname | grep '#{old_hostname}'", stdout: old_hostname)
end end
after do after do
communicator.verify_expectations! comm.verify_expectations!
end end
describe ".change_host_name" do describe ".change_host_name" do
it "refreshes the hostname service with the hostname command" do let(:cap) { caps.get(:change_host_name) }
communicator.expect_command(%Q(pfexec hostname #{new_hostname}))
plugin.change_host_name(machine, new_hostname)
end
it "writes the hostname into /etc/nodename" do it "changes the hostname if appropriate" do
communicator.expect_command(%Q(pfexec sh -c "echo '#{new_hostname}' > /etc/nodename")) cap.change_host_name(machine, "testhost")
plugin.change_host_name(machine, new_hostname)
expect(comm.received_commands[0]).to match(/if hostname | grep 'testhost' ; then/)
expect(comm.received_commands[0]).to match(/exit 0/)
expect(comm.received_commands[0]).to match(/fi/)
expect(comm.received_commands[0]).to match(/if \[ -d \/usbkey \] && \[ "\$\(zonename\)" == "global" \] ; then/)
expect(comm.received_commands[0]).to match(/pfexec sed -i '' 's\/hostname=\.\*\/hostname=testhost\/' \/usbkey\/config/)
expect(comm.received_commands[0]).to match(/fi/)
expect(comm.received_commands[0]).to match(/pfexec echo 'testhost' > \/etc\/nodename/)
expect(comm.received_commands[0]).to match(/pfexec hostname testhost/)
end end
end end
end end

View File

@ -11,7 +11,7 @@ describe "VagrantPlugins::GuestSmartos::Cap::InsertPublicKey" do
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do before do
allow(machine).to receive(:communicate).and_return(comm) machine.stub(:communicate).and_return(comm)
end end
after do after do

View File

@ -1,30 +1,38 @@
require_relative "../../../../base" require_relative "../../../../base"
require_relative "../../../../../../plugins/guests/smartos/config"
describe "VagrantPlugins::GuestSmartos::Cap::MountNFS" do
let(:caps) do
VagrantPlugins::GuestSmartos::Plugin
.components
.guest_capabilities[:smartos]
end
describe "VagrantPlugins::VagrantPlugins::Cap::MountNFS" do
let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:mount_nfs_folder) }
let(:machine) { double("machine") } let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) } let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do before do
machine.stub(:communicate).and_return(communicator) machine.stub(:communicate).and_return(comm)
machine.stub(:config).and_return(config) machine.stub(:config).and_return(config)
end end
after do after do
communicator.verify_expectations! comm.verify_expectations!
end end
describe ".mount_nfs_folder" do describe ".mount_nfs_folder" do
it "creates the directory mount point" do let(:cap) { caps.get(:mount_nfs_folder) }
communicator.expect_command(%Q(pfexec mkdir -p /mountpoint))
plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint'}})
end
it "mounts the NFS share" do it "mounts the folder" do
communicator.expect_command(%Q(pfexec /usr/sbin/mount -F nfs '1.1.1.1:/some/share' '/mountpoint')) cap.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint', hostpath: '/some/share'}})
plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint', hostpath: '/some/share'}})
expect(comm.received_commands[0]).to match(/if \[ -d \/usbkey \] && \[ "\$\(zonename\)" == "global" \] ; then/)
expect(comm.received_commands[0]).to match(/pfexec mkdir -p \/usbkey\/config.inc/)
expect(comm.received_commands[0]).to match(/printf '1\.1\.1\.1:\/some\/share:\/mountpoint' | pfexec tee -a \/usbkey\/config.inc\/nfs_mounts/)
expect(comm.received_commands[0]).to match(/fi/)
expect(comm.received_commands[0]).to match(/pfexec mkdir -p \/mountpoint/)
expect(comm.received_commands[0]).to match(/pfexec \/usr\/sbin\/mount -F nfs '1\.1\.1\.1:\/some\/share' '\/mountpoint'/)
end end
end end
end end