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
class ChangeHostName
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
if !machine.communicate.test("hostname | grep '#{name}'")
machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"")
machine.communicate.execute("#{su_cmd} hostname #{name}")
machine.communicate.tap do |comm|
comm.execute <<-EOH.sub(/^ */, '')
if hostname | grep '#{name}' ; then
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

View File

@ -7,8 +7,17 @@ module VagrantPlugins
folders.each do |name, opts|
machine.communicate.tap do |comm|
comm.execute("#{sudo} mkdir -p #{opts[:guestpath]}", {shell: "sh"})
comm.execute("#{sudo} /usr/sbin/mount -F nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'", {shell: "sh"})
nfsDescription = "#{ip}:#{opts[:hostpath]}:#{opts[:guestpath]}"
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

View File

@ -1,33 +1,39 @@
require_relative "../../../../base"
require_relative "../../../../../../plugins/guests/smartos/config"
describe "VagrantPlugins::VagrantPlugins::Cap::ChangeHostName" do
let(:plugin) { VagrantPlugins::GuestSmartos::Plugin.components.guest_capabilities[:smartos].get(:change_host_name) }
describe "VagrantPlugins::GuestSmartos::Cap::ChangeHostName" do
let(:caps) do
VagrantPlugins::GuestSmartos::Plugin
.components
.guest_capabilities[:smartos]
end
let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
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
machine.stub(:communicate).and_return(communicator)
machine.stub(:communicate).and_return(comm)
machine.stub(:config).and_return(config)
communicator.stub_command("hostname | grep '#{old_hostname}'", stdout: old_hostname)
end
after do
communicator.verify_expectations!
comm.verify_expectations!
end
describe ".change_host_name" do
it "refreshes the hostname service with the hostname command" do
communicator.expect_command(%Q(pfexec hostname #{new_hostname}))
plugin.change_host_name(machine, new_hostname)
end
let(:cap) { caps.get(:change_host_name) }
it "writes the hostname into /etc/nodename" do
communicator.expect_command(%Q(pfexec sh -c "echo '#{new_hostname}' > /etc/nodename"))
plugin.change_host_name(machine, new_hostname)
it "changes the hostname if appropriate" do
cap.change_host_name(machine, "testhost")
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

View File

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

View File

@ -1,30 +1,38 @@
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(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:config) { double("config", smartos: VagrantPlugins::GuestSmartos::Config.new) }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
machine.stub(:communicate).and_return(communicator)
machine.stub(:communicate).and_return(comm)
machine.stub(:config).and_return(config)
end
after do
communicator.verify_expectations!
comm.verify_expectations!
end
describe ".mount_nfs_folder" do
it "creates the directory mount point" do
communicator.expect_command(%Q(pfexec mkdir -p /mountpoint))
plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint'}})
end
let(:cap) { caps.get(:mount_nfs_folder) }
it "mounts the NFS share" do
communicator.expect_command(%Q(pfexec /usr/sbin/mount -F nfs '1.1.1.1:/some/share' '/mountpoint'))
plugin.mount_nfs_folder(machine, '1.1.1.1', {'nfs' => {guestpath: '/mountpoint', hostpath: '/some/share'}})
it "mounts the folder" do
cap.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