(#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.
This commit is contained in:
Brian Cain 2017-10-05 11:30:21 -07:00
parent 32a4b73dd4
commit a849cd2f7d
11 changed files with 98 additions and 230 deletions

View File

@ -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 ")

View File

@ -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

View File

@ -1,26 +0,0 @@
# A general Vagrant system implementation for "solaris 11".
#
# Contributed by Jan Thomas Moldung <janth@moldung.no>
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

View File

@ -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

View File

@ -1,45 +0,0 @@
# A general Vagrant system implementation for "solaris 11".
#
# Contributed by Jan Thomas Moldung <janth@moldung.no>
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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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