From a849cd2f7d491c43acc68ba2927a81837d8a650d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 5 Oct 2017 11:30:21 -0700 Subject: [PATCH] (#8963) Unify Solaris Guests This commit removes some duplicated code within the Solaris11 guest implemnentation and instead relies on the Solaris guest for the majority of its guest capabilities. --- .../guests/solaris11/cap/change_host_name.rb | 2 - .../solaris11/cap/configure_networks.rb | 8 +-- plugins/guests/solaris11/cap/halt.rb | 26 --------- .../guests/solaris11/cap/insert_public_key.rb | 21 ------- .../cap/mount_virtualbox_shared_folder.rb | 45 --------------- .../guests/solaris11/cap/remove_public_key.rb | 21 ------- plugins/guests/solaris11/cap/rsync.rb | 29 ---------- plugins/guests/solaris11/plugin.rb | 40 ++----------- .../solaris11/cap/change_host_name_test.rb | 36 ++++++++++++ .../solaris11/cap/configure_networks_test.rb | 56 +++++++++++++++++++ .../plugins/guests/solaris11/cap/halt_test.rb | 44 --------------- 11 files changed, 98 insertions(+), 230 deletions(-) delete mode 100644 plugins/guests/solaris11/cap/halt.rb delete mode 100644 plugins/guests/solaris11/cap/insert_public_key.rb delete mode 100644 plugins/guests/solaris11/cap/mount_virtualbox_shared_folder.rb delete mode 100644 plugins/guests/solaris11/cap/remove_public_key.rb delete mode 100644 plugins/guests/solaris11/cap/rsync.rb create mode 100644 test/unit/plugins/guests/solaris11/cap/change_host_name_test.rb create mode 100644 test/unit/plugins/guests/solaris11/cap/configure_networks_test.rb delete mode 100644 test/unit/plugins/guests/solaris11/cap/halt_test.rb diff --git a/plugins/guests/solaris11/cap/change_host_name.rb b/plugins/guests/solaris11/cap/change_host_name.rb index d49eef251..cf7336e90 100644 --- a/plugins/guests/solaris11/cap/change_host_name.rb +++ b/plugins/guests/solaris11/cap/change_host_name.rb @@ -11,8 +11,6 @@ module VagrantPlugins # Only do this if the hostname is not already set if !machine.communicate.test("/usr/sbin/svccfg -s system/identity:node listprop config/nodename | /usr/bin/grep '#{name}'") - #machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"") - #machine.communicate.execute("#{su_cmd} uname -S #{name}") machine.communicate.execute("#{su_cmd} /usr/sbin/svccfg -s system/identity:node setprop config/nodename=\"#{name}\"") machine.communicate.execute("#{su_cmd} /usr/sbin/svccfg -s system/identity:node setprop config/loopback=\"#{name}\"") machine.communicate.execute("#{su_cmd} /usr/sbin/svccfg -s system/identity:node refresh ") diff --git a/plugins/guests/solaris11/cap/configure_networks.rb b/plugins/guests/solaris11/cap/configure_networks.rb index 15aa62679..ca3311177 100644 --- a/plugins/guests/solaris11/cap/configure_networks.rb +++ b/plugins/guests/solaris11/cap/configure_networks.rb @@ -12,19 +12,13 @@ module VagrantPlugins su_cmd = machine.config.solaris11.suexec_cmd mask = "#{network[:netmask]}" cidr = mask.split(".").map { |e| e.to_i.to_s(2).rjust(8, "0") }.join.count("1").to_s - #ifconfig_cmd = "#{su_cmd} /sbin/ifconfig #{device}" - #machine.communicate.execute("#{ifconfig_cmd} plumb") + if network[:type].to_sym == :static - #machine.communicate.execute("#{ifconfig_cmd} inet #{network[:ip]} netmask #{network[:netmask]}") - #machine.communicate.execute("#{ifconfig_cmd} up") - #machine.communicate.execute("#{su_cmd} sh -c \"echo '#{network[:ip]}' > /etc/hostname.#{device}\"") - # ipadm create-addr -T static -a local=172.16.10.15/24 net2/v4 if machine.communicate.test("ipadm | grep #{device}/v4") machine.communicate.execute("#{su_cmd} ipadm delete-addr #{device}/v4") end machine.communicate.execute("#{su_cmd} ipadm create-addr -T static -a #{network[:ip]}/#{cidr} #{device}/v4") elsif network[:type].to_sym == :dhcp - #machine.communicate.execute("#{ifconfig_cmd} dhcp start") if machine.communicate.test("ipadm show-if -o all | grep #{device} | tr -s ' ' | cut -d ' ' -f 6 | grep '4\|6'") machine.communicate.execute("#{su_cmd} ipadm create-addr -T addrconf #{device}/v4") end diff --git a/plugins/guests/solaris11/cap/halt.rb b/plugins/guests/solaris11/cap/halt.rb deleted file mode 100644 index 67e0b4219..000000000 --- a/plugins/guests/solaris11/cap/halt.rb +++ /dev/null @@ -1,26 +0,0 @@ -# A general Vagrant system implementation for "solaris 11". -# -# Contributed by Jan Thomas Moldung - -module VagrantPlugins - module GuestSolaris11 - module Cap - class Halt - def self.halt(machine) - # There should be an exception raised if the line - # - # vagrant::::profiles=Primary Administrator - # - # does not exist in /etc/user_attr. TODO - begin - machine.communicate.execute( - "#{machine.config.solaris11.suexec_cmd} /usr/sbin/shutdown -y -i5 -g0") - rescue IOError, Vagrant::Errors::SSHDisconnected - # Ignore, this probably means connection closed because it - # shut down. - end - end - end - end - end -end diff --git a/plugins/guests/solaris11/cap/insert_public_key.rb b/plugins/guests/solaris11/cap/insert_public_key.rb deleted file mode 100644 index 55087c366..000000000 --- a/plugins/guests/solaris11/cap/insert_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestSolaris11 - module Cap - class InsertPublicKey - def self.insert_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - comm.execute("mkdir -p ~/.ssh") - comm.execute("chmod 0700 ~/.ssh") - comm.execute("printf '#{contents}\\n' >> ~/.ssh/authorized_keys") - comm.execute("chmod 0600 ~/.ssh/authorized_keys") - end - end - end - end - end -end diff --git a/plugins/guests/solaris11/cap/mount_virtualbox_shared_folder.rb b/plugins/guests/solaris11/cap/mount_virtualbox_shared_folder.rb deleted file mode 100644 index 86012cc90..000000000 --- a/plugins/guests/solaris11/cap/mount_virtualbox_shared_folder.rb +++ /dev/null @@ -1,45 +0,0 @@ -# A general Vagrant system implementation for "solaris 11". -# -# Contributed by Jan Thomas Moldung - -module VagrantPlugins - module GuestSolaris11 - module Cap - class MountVirtualBoxSharedFolder - def self.mount_virtualbox_shared_folder(machine, name, guestpath, options) - # These are just far easier to use than the full options syntax - owner = options[:owner] - group = options[:group] - - # Create the shared folder - machine.communicate.execute("#{machine.config.solaris11.suexec_cmd} mkdir -p #{guestpath}") - - if owner.is_a? Integer - mount_uid = owner - else - # We have to use this `id` command instead of `/usr/bin/id` since this - # one accepts the "-u" and "-g" flags. - mount_uid = "`/usr/xpg4/bin/id -u #{owner}`" - end - - if group.is_a? Integer - mount_gid = group - else - mount_gid = "`/usr/xpg4/bin/id -g #{group}`" - end - - # Mount the folder with the proper owner/group - mount_options = "-o uid=#{mount_uid},gid=#{mount_gid}" - if options[:mount_options] - mount_options += ",#{options[:mount_options].join(",")}" - end - - machine.communicate.execute("#{machine.config.solaris11.suexec_cmd} /sbin/mount -F vboxfs #{mount_options} #{name} #{guestpath}") - - # chown the folder to the proper owner/group - machine.communicate.execute("#{machine.config.solaris11.suexec_cmd} chown #{mount_uid}:#{mount_gid} #{guestpath}") - end - end - end - end -end diff --git a/plugins/guests/solaris11/cap/remove_public_key.rb b/plugins/guests/solaris11/cap/remove_public_key.rb deleted file mode 100644 index 91924d3c5..000000000 --- a/plugins/guests/solaris11/cap/remove_public_key.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "vagrant/util/shell_quote" - -module VagrantPlugins - module GuestSolaris11 - module Cap - class RemovePublicKey - def self.remove_public_key(machine, contents) - contents = contents.chomp - contents = Vagrant::Util::ShellQuote.escape(contents, "'") - - machine.communicate.tap do |comm| - if comm.test("test -f ~/.ssh/authorized_keys") - comm.execute( - "sed -i '/^.*#{contents}.*$/d' ~/.ssh/authorized_keys") - end - end - end - end - end - end -end diff --git a/plugins/guests/solaris11/cap/rsync.rb b/plugins/guests/solaris11/cap/rsync.rb deleted file mode 100644 index f86f39a10..000000000 --- a/plugins/guests/solaris11/cap/rsync.rb +++ /dev/null @@ -1,29 +0,0 @@ -require_relative "../../../synced_folders/rsync/default_unix_cap" - -module VagrantPlugins - module GuestSolaris11 - module Cap - class RSync - extend VagrantPlugins::SyncedFolderRSync::DefaultUnixCap - - def self.rsync_command(machine) - "#{machine.config.solaris11.suexec_cmd} rsync" - end - - def self.rsync_pre(machine, opts) - machine.communicate.tap do |comm| - comm.sudo("mkdir -p '#{opts[:guestpath]}'") - end - end - - def self.rsync_post(machine, opts) - if opts.key?(:chown) && !opts[:chown] - return - end - suexec_cmd = machine.config.solaris11.suexec_cmd - machine.communicate.execute("#{suexec_cmd} #{build_rsync_chown(opts)}") - end - end - end - end -end diff --git a/plugins/guests/solaris11/plugin.rb b/plugins/guests/solaris11/plugin.rb index fa94ef598..768f70f4e 100644 --- a/plugins/guests/solaris11/plugin.rb +++ b/plugins/guests/solaris11/plugin.rb @@ -10,16 +10,16 @@ module VagrantPlugins name "Solaris 11 guest." description "Solaris 11 guest support." + guest(:solaris11, :solaris) do + require_relative "guest" + Guest + end + config(:solaris11) do require_relative "config" Config end - guest(:solaris11) do - require_relative "guest" - Guest - end - guest_capability(:solaris11, :change_host_name) do require_relative "cap/change_host_name" Cap::ChangeHostName @@ -29,36 +29,6 @@ module VagrantPlugins require_relative "cap/configure_networks" Cap::ConfigureNetworks end - - guest_capability(:solaris11, :halt) do - require_relative "cap/halt" - Cap::Halt - end - - guest_capability(:solaris11, :mount_virtualbox_shared_folder) do - require_relative "cap/mount_virtualbox_shared_folder" - Cap::MountVirtualBoxSharedFolder - end - - guest_capability(:solaris11, :rsync_installed) do - require_relative "cap/rsync" - Cap::RSync - end - - guest_capability(:solaris11, :rsync_pre) do - require_relative "cap/rsync" - Cap::RSync - end - - guest_capability(:solaris11, :insert_public_key) do - require_relative "cap/insert_public_key" - Cap::InsertPublicKey - end - - guest_capability(:solaris11, :remove_public_key) do - require_relative "cap/remove_public_key" - Cap::RemovePublicKey - end end end end diff --git a/test/unit/plugins/guests/solaris11/cap/change_host_name_test.rb b/test/unit/plugins/guests/solaris11/cap/change_host_name_test.rb new file mode 100644 index 000000000..df48486c2 --- /dev/null +++ b/test/unit/plugins/guests/solaris11/cap/change_host_name_test.rb @@ -0,0 +1,36 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSolaris11::Cap::ChangeHostName" do + let(:caps) do + VagrantPlugins::GuestSolaris11::Plugin + .components + .guest_capabilities[:solaris11] + end + + let(:machine) { double("machine", config: double("config", solaris11: double("solaris11", suexec_cmd: 'sudo'))) } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:cap) { caps.get(:change_host_name) } + let(:name) { "solaris11.domain.com" } + + it "changes the hostname" do + allow(machine.communicate).to receive(:test).and_return(false) + allow(machine.communicate).to receive(:execute) + + expect(machine.communicate).to receive(:execute).with("sudo /usr/sbin/svccfg -s system/identity:node setprop config/nodename=\"#{name}\"") + expect(machine.communicate).to receive(:execute).with("sudo /usr/sbin/svccfg -s system/identity:node setprop config/loopback=\"#{name}\"") + expect(machine.communicate).to receive(:execute).with("sudo /usr/sbin/svccfg -s system/identity:node refresh ") + expect(machine.communicate).to receive(:execute).with("sudo /usr/sbin/svcadm restart system/identity:node ") + cap.change_host_name(machine, name) + end + end +end diff --git a/test/unit/plugins/guests/solaris11/cap/configure_networks_test.rb b/test/unit/plugins/guests/solaris11/cap/configure_networks_test.rb new file mode 100644 index 000000000..87520dd65 --- /dev/null +++ b/test/unit/plugins/guests/solaris11/cap/configure_networks_test.rb @@ -0,0 +1,56 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestSolaris11::Cap::ConfigureNetworks" do + let(:caps) do + VagrantPlugins::GuestSolaris11::Plugin + .components + .guest_capabilities[:solaris11] + end + + let(:machine) { double("machine", config: double("config", solaris11: double("solaris11", suexec_cmd: 'sudo', device: 'net'))) } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".configufre_networks" do + let(:cap) { caps.get(:configure_networks) } + let(:network_1) do + { + interface: 0, + type: "dhcp", + } + end + + let(:network_2) do + { + interface: 1, + type: "static", + ip: "33.33.33.10", + netmask: "255.255.0.0", + gateway: "33.33.0.1", + } + end + + let(:networks) { [network_1, network_2] } + + it "configures the guests network if static" do + allow(machine.communicate).to receive(:test).and_return(true) + + cap.configure_networks(machine, networks) + expect(comm.received_commands[1]).to eq("sudo ipadm delete-addr net1/v4") + expect(comm.received_commands[2]).to eq("sudo ipadm create-addr -T static -a 33.33.33.10/16 net1/v4") + end + + it "configures the guests network if dhcp" do + allow(machine.communicate).to receive(:test).and_return(true) + cap.configure_networks(machine, networks) + expect(comm.received_commands[0]).to eq("sudo ipadm create-addr -T addrconf net0/v4") + end + end +end diff --git a/test/unit/plugins/guests/solaris11/cap/halt_test.rb b/test/unit/plugins/guests/solaris11/cap/halt_test.rb deleted file mode 100644 index 3c4d64eb0..000000000 --- a/test/unit/plugins/guests/solaris11/cap/halt_test.rb +++ /dev/null @@ -1,44 +0,0 @@ -require_relative "../../../../base" - -describe "VagrantPlugins::GuestSolaris11::Cap::Halt" do - let(:caps) do - VagrantPlugins::GuestSolaris11::Plugin - .components - .guest_capabilities[:solaris11] - end - - let(:shutdown_command){ "sudo /usr/sbin/shutdown -y -i5 -g0" } - let(:machine) { double("machine", config: double("config", solaris11: double("solaris11", suexec_cmd: 'sudo'))) } - let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } - - before do - allow(machine).to receive(:communicate).and_return(comm) - end - - after do - comm.verify_expectations! - end - - describe ".halt" do - let(:cap) { caps.get(:halt) } - - it "runs the shutdown command" do - comm.expect_command(shutdown_command) - cap.halt(machine) - end - - it "ignores an IOError" do - comm.stub_command(shutdown_command, raise: IOError) - expect { - cap.halt(machine) - }.to_not raise_error - end - - it "ignores a Vagrant::Errors::SSHDisconnected" do - comm.stub_command(shutdown_command, raise: Vagrant::Errors::SSHDisconnected) - expect { - cap.halt(machine) - }.to_not raise_error - end - end -end