From 7766eb6098e148d23352b2311165889f9bbdba9c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 18 Apr 2012 21:03:03 -0700 Subject: [PATCH] Major guests have been moved to plugins --- lib/vagrant/guest/linux/error.rb | 9 --- lib/vagrant/plugin/v1.rb | 14 ++++ lib/vagrant/registry.rb | 7 ++ lib/vagrant/vm.rb | 10 ++- .../guest_freebsd/guest.rb | 8 +-- plugins/guest_freebsd/plugin.rb | 3 + .../linux.rb => plugins/guest_linux/guest.rb | 10 ++- plugins/guest_linux/plugin.rb | 3 + .../guest_solaris/guest.rb | 8 +-- plugins/guest_solaris/plugin.rb | 3 + test/unit/vagrant/plugin/v1_test.rb | 64 ++++++++++++++----- test/unit/vagrant/registry_test.rb | 6 ++ 12 files changed, 107 insertions(+), 38 deletions(-) delete mode 100644 lib/vagrant/guest/linux/error.rb rename lib/vagrant/guest/freebsd.rb => plugins/guest_freebsd/guest.rb (95%) rename lib/vagrant/guest/linux.rb => plugins/guest_linux/guest.rb (95%) rename lib/vagrant/guest/solaris.rb => plugins/guest_solaris/guest.rb (96%) diff --git a/lib/vagrant/guest/linux/error.rb b/lib/vagrant/guest/linux/error.rb deleted file mode 100644 index b6de25061..000000000 --- a/lib/vagrant/guest/linux/error.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Vagrant - module Guest - class Linux < Vagrant::Guest::Base - class LinuxError < Errors::VagrantError - error_namespace("vagrant.guest.linux") - end - end - end -end diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index 7e16a8667..8cc374d24 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -50,6 +50,20 @@ module Vagrant data[:config] end + # Defines an additionally available guest implementation with + # the given key. + # + # @param [String] name Name of the guest. + def self.guest(name=UNSET_VALUE, &block) + data[:guests] ||= Registry.new + + # Register a new guest class only if a name was given + data[:guests].register(name.to_sym, &block) if name != UNSET_VALUE + + # Return the registry + data[:guests] + end + # Registers the plugin. This makes the plugin actually work with # Vagrant. Prior to registering, the plugin is merely a skeleton. def self.register!(plugin=nil) diff --git a/lib/vagrant/registry.rb b/lib/vagrant/registry.rb index 417b4435f..ccd6770ae 100644 --- a/lib/vagrant/registry.rb +++ b/lib/vagrant/registry.rb @@ -32,6 +32,13 @@ module Vagrant end alias :[] :get + # Checks if the given key is registered with the registry. + # + # @return [Boolean] + def has_key?(key) + @actions.has_key?(key) + end + # Iterate over the keyspace. def each(&block) @actions.each do |key, _| diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 558bbdc9a..505fb32bb 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -52,7 +52,15 @@ module Vagrant raise Errors::VMGuestError, :_key => :invalid_class, :guest => guest.to_s if !(guest <= Guest::Base) @guest = guest.new(self) elsif guest.is_a?(Symbol) - guest_klass = Vagrant.guests.get(guest) + # Look for the guest as a registered plugin + guest_klass = nil + Vagrant.plugin("1").registered.each do |plugin| + if plugin.guest.has_key?(guest) + guest_klass = plugin.guest[guest] + break + end + end + raise Errors::VMGuestError, :_key => :unknown_type, :guest => guest.to_s if !guest_klass @guest = guest_klass.new(self) else diff --git a/lib/vagrant/guest/freebsd.rb b/plugins/guest_freebsd/guest.rb similarity index 95% rename from lib/vagrant/guest/freebsd.rb rename to plugins/guest_freebsd/guest.rb index adf22b6a1..8d671d350 100644 --- a/lib/vagrant/guest/freebsd.rb +++ b/plugins/guest_freebsd/guest.rb @@ -1,13 +1,13 @@ require 'vagrant/util/template_renderer' -module Vagrant - module Guest +module VagrantPlugins + module GuestFreeBSD # A general Vagrant system implementation for "freebsd". # # Contributed by Kenneth Vestergaard - class FreeBSD < Base + class Guest < Vagrant::Guest::Base # Here for whenever it may be used. - class FreeBSDError < Errors::VagrantError + class FreeBSDError < Vagrant::Errors::VagrantError error_namespace("vagrant.guest.freebsd") end diff --git a/plugins/guest_freebsd/plugin.rb b/plugins/guest_freebsd/plugin.rb index 838028e2e..00f9f4555 100644 --- a/plugins/guest_freebsd/plugin.rb +++ b/plugins/guest_freebsd/plugin.rb @@ -3,11 +3,14 @@ require "vagrant" module VagrantPlugins module GuestFreeBSD autoload :Config, File.expand_path("../config", __FILE__) + autoload :Guest, File.expand_path("../guest", __FILE__) class Plugin < Vagrant.plugin("1") name "FreeBSD guest" description "FreeBSD guest support." + config("freebsd") { Config } + guest("freebsd") { Guest } end end end diff --git a/lib/vagrant/guest/linux.rb b/plugins/guest_linux/guest.rb similarity index 95% rename from lib/vagrant/guest/linux.rb rename to plugins/guest_linux/guest.rb index 4730e4088..53d5251ad 100644 --- a/lib/vagrant/guest/linux.rb +++ b/plugins/guest_linux/guest.rb @@ -2,9 +2,13 @@ require 'log4r' require 'vagrant/guest/linux/error' -module Vagrant - module Guest - class Linux < Base +module VagrantPlugins + module GuestLinux + class Guest < Vagrant::Guest::Base + class LinuxError < Vagrant::Errors::VagrantError + error_namespace("vagrant.guest.linux") + end + def initialize(*args) super diff --git a/plugins/guest_linux/plugin.rb b/plugins/guest_linux/plugin.rb index 46d7380ab..ed5149797 100644 --- a/plugins/guest_linux/plugin.rb +++ b/plugins/guest_linux/plugin.rb @@ -3,11 +3,14 @@ require "vagrant" module VagrantPlugins module GuestLinux autoload :Config, File.expand_path("../config", __FILE__) + autoload :Guest, File.expand_path("../guest", __FILE__) class Plugin < Vagrant.plugin("1") name "Linux guest." description "Linux guest support." + config("linux") { Config } + guest("linux") { Guest } end end end diff --git a/lib/vagrant/guest/solaris.rb b/plugins/guest_solaris/guest.rb similarity index 96% rename from lib/vagrant/guest/solaris.rb rename to plugins/guest_solaris/guest.rb index 626ac6910..9baa11bbb 100644 --- a/lib/vagrant/guest/solaris.rb +++ b/plugins/guest_solaris/guest.rb @@ -1,11 +1,11 @@ -module Vagrant - module Guest +module VagrantPlugins + module GuestSolaris # A general Vagrant system implementation for "solaris". # # Contributed by Blake Irvin - class Solaris < Base + class Guest < Vagrant::Guest::Base # Here for whenever it may be used. - class SolarisError < Errors::VagrantError + class SolarisError < Vagrant::Errors::VagrantError error_namespace("vagrant.guest.solaris") end diff --git a/plugins/guest_solaris/plugin.rb b/plugins/guest_solaris/plugin.rb index bf32eb762..03eb181ac 100644 --- a/plugins/guest_solaris/plugin.rb +++ b/plugins/guest_solaris/plugin.rb @@ -3,11 +3,14 @@ require "vagrant" module VagrantPlugins module GuestSolaris autoload :Config, File.expand_path("../config", __FILE__) + autoload :Guest, File.expand_path("../guest", __FILE__) class Plugin < Vagrant.plugin("1") name "Solaris guest." description "Solaris guest support." + config("solaris") { Config } + guest("solaris") { Guest } end end end diff --git a/test/unit/vagrant/plugin/v1_test.rb b/test/unit/vagrant/plugin/v1_test.rb index a241586a7..33b0d7c42 100644 --- a/test/unit/vagrant/plugin/v1_test.rb +++ b/test/unit/vagrant/plugin/v1_test.rb @@ -23,30 +23,60 @@ describe Vagrant::Plugin::V1 do plugin.description.should == "bar" end - it "should register configuration classes" do - plugin = Class.new(described_class) do - config("foo") { "bar" } + describe "configuration" do + it "should register configuration classes" do + plugin = Class.new(described_class) do + config("foo") { "bar" } + end + + plugin.config[:foo].should == "bar" end - plugin.config[:foo].should == "bar" + it "should lazily register configuration classes" do + # Below would raise an error if the value of the config class was + # evaluated immediately. By asserting that this does not raise an + # error, we verify that the value is actually lazily loaded + plugin = nil + expect { + plugin = Class.new(described_class) do + config("foo") { raise StandardError, "FAIL!" } + end + }.to_not raise_error + + # Now verify when we actually get the configuration key that + # a proper error is raised. + expect { + plugin.config[:foo] + }.to raise_error(StandardError) + end end - it "should lazily register configuration classes" do - # Below would raise an error if the value of the config class was - # evaluated immediately. By asserting that this does not raise an - # error, we verify that the value is actually lazily loaded - plugin = nil - expect { + describe "guests" do + it "should register guest classes" do plugin = Class.new(described_class) do - config("foo") { raise StandardError, "FAIL!" } + guest("foo") { "bar" } end - }.to_not raise_error - # Now verify when we actually get the configuration key that - # a proper error is raised. - expect { - plugin.config[:foo] - }.to raise_error(StandardError) + plugin.guest[:foo].should == "bar" + end + + it "should lazily register configuration classes" do + # Below would raise an error if the value of the config class was + # evaluated immediately. By asserting that this does not raise an + # error, we verify that the value is actually lazily loaded + plugin = nil + expect { + plugin = Class.new(described_class) do + guest("foo") { raise StandardError, "FAIL!" } + end + }.to_not raise_error + + # Now verify when we actually get the configuration key that + # a proper error is raised. + expect { + plugin.guest[:foo] + }.to raise_error(StandardError) + end end describe "plugin registration" do diff --git a/test/unit/vagrant/registry_test.rb b/test/unit/vagrant/registry_test.rb index a450b0895..ec7adbcd4 100644 --- a/test/unit/vagrant/registry_test.rb +++ b/test/unit/vagrant/registry_test.rb @@ -47,6 +47,12 @@ describe Vagrant::Registry do instance.get("foo").should == ["value"] end + it "should be able to check if a key exists" do + instance.register("foo", "bar") + instance.should have_key("foo") + instance.should_not have_key("bar") + end + it "should be enumerable" do instance.register("foo", "foovalue") instance.register("bar", "barvalue")