diff --git a/lib/logstash/config/mixin.rb b/lib/logstash/config/mixin.rb index f4b0314db..33d2e9e41 100644 --- a/lib/logstash/config/mixin.rb +++ b/lib/logstash/config/mixin.rb @@ -39,6 +39,8 @@ module LogStash::Config::Mixin Regexp => 100, } + GEM_NAME_PREFIX = 'logstash' + # This method is called when someone does 'include LogStash::Config' def self.included(base) # Add the DSL methods to the 'base' given. @@ -121,13 +123,16 @@ module LogStash::Config::Mixin return @config_name end + # Deprecated: Declare the version of the plugin + # inside the gemspec. def plugin_status(status=nil) milestone(status) end + # Deprecated: Declare the version of the plugin + # inside the gemspec. def milestone(m=nil) - @milestone = m if !m.nil? - return @milestone + @logger.error(I18n.t('logstash.plugin.deprecated')) end # Define a new configuration setting @@ -184,7 +189,7 @@ module LogStash::Config::Mixin end end subclass.instance_variable_set("@config", subconfig) - @@milestone_notice_given = false + @@version_notice_given = false end # def inherited def validate(params) @@ -193,7 +198,7 @@ module LogStash::Config::Mixin @logger = Cabin::Channel.get(LogStash) is_valid = true - is_valid &&= validate_milestone + is_valid &&= validate_plugin_version is_valid &&= validate_check_invalid_parameter_names(params) is_valid &&= validate_check_required_parameter_names(params) is_valid &&= validate_check_parameter_values(params) @@ -201,23 +206,36 @@ module LogStash::Config::Mixin return is_valid end # def validate - def validate_milestone - return true if @@milestone_notice_given - docmsg = "For more information about plugin milestones, see http://logstash.net/docs/#{LOGSTASH_VERSION}/plugin-milestones " - plugin_type = ancestors.find { |a| a.name =~ /::Base$/ }.config_name - case @milestone - when 0,1,2 - @logger.warn(I18n.t("logstash.plugin.milestone.#{@milestone}", - :type => plugin_type, :name => @config_name, + def plugin_version + specification = Gem::Specification.find_by_name(plugin_gem_name) + major, minor, patch = specification.version.segments + + Struct.new(:major, :minor, :patch) + .new(major, minor, patch) + end + + def plugin_gem_name + [GEM_NAME_PREFIX, @plugin_type, @plugin_name].join('-') + end + + def validate_plugin_version + return true if @@version_notice_given + + if plugin_version.major < 1 + if plugin_version.minor == 9 + @logger.warn(I18n.t("logstash.plugin.version.0-9-0", + :type => @plugin_type, + :name => @config_name, :LOGSTASH_VERSION => LOGSTASH_VERSION)) - when 3 - # No message to log for milestone 3 plugins. - when nil - raise "#{@config_name} must set a milestone. #{docmsg}" else - raise "#{@config_name} set an invalid plugin status #{@milestone}. Valid values are 0, 1, 2, or 3. #{docmsg}" + @logger.warn(I18n.t("logstash.plugin.version.0-1-0", + :type => @plugin_type, + :name => @config_name, + :LOGSTASH_VERSION => LOGSTASH_VERSION)) + end end - @@milestone_notice_given = true + + @@version_notice_given = true return true end diff --git a/locales/en.yml b/locales/en.yml index a5e902811..d4c8e0509 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -34,23 +34,22 @@ en: supported by this plugin. I will continue working as if you had not set this setting. plugin: - milestone: - "0": >- - Using milestone 0 %{type} plugin '%{name}'. This plugin isn't well - supported by the community and likely has no maintainer. For more - information on plugin milestones, see - http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones - "1": >- - Using milestone 1 %{type} plugin '%{name}'. This plugin should work, - but would benefit from use by folks like you. Please let us know if you - find bugs or have suggestions on how to improve this plugin. For more - information on plugin milestones, see - http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones - "2": >- - Using milestone 2 %{type} plugin '%{name}'. This plugin should be - stable, but if you see strange behavior, please let us know! - For more information on plugin milestones, see - http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-milestones + deprecated_milestone: >- + %{plugin} plugin is using the 'milestone' method to declare the version + of the plugin this method is deprecated in favor of declaring the + version inside the gemspec. + version: + 0-9-0: + Using version 0.9.x %{type} plugin '%{name}'. This plugin should but + would benefit from use by folks like you. Please let us know if you + find bugs or have suggestions on how to improve this plugin. For more + information on plugin milestones, see + http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-version + 0-1-0: >- + Using version 0.1.x %{type} plugin '%{name}'. This plugin isn't well + supported by the community and likely has no maintainer. For more + information on plugin milestones, see + http://logstash.net/docs/%{LOGSTASH_VERSION}/plugin-version agent: sighup: >- SIGHUP received. diff --git a/spec/core/plugin_spec.rb b/spec/core/plugin_spec.rb index 764981295..7dfeba020 100644 --- a/spec/core/plugin_spec.rb +++ b/spec/core/plugin_spec.rb @@ -29,4 +29,60 @@ describe LogStash::Plugin do end expect(LogStash::Plugin.lookup("filter", "lady_gaga")).to eq(LogStash::Filters::LadyGaga) end + + context "when validating the plugin version" do + let(:plugin_name) { 'logstash-filter-stromae' } + subject do + Class.new(LogStash::Filters::Base) do + config_name 'stromae' + end + end + + it "doesn't warn the user if the version is superior or equal to 1.0.0" do + allow(Gem::Specification).to receive(:find_by_name) + .with(plugin_name) + .and_return(double(:version => Gem::Version.new('1.0.0'))) + + expect_any_instance_of(Cabin::Channel).not_to receive(:warn) + subject.validate({}) + end + + it 'warns the user if the plugin version is between 0.9.x and 1.0.0' do + allow(Gem::Specification).to receive(:find_by_name) + .with(plugin_name) + .and_return(double(:version => Gem::Version.new('0.9.1'))) + + expect_any_instance_of(Cabin::Channel).to receive(:warn) + .with(/Using version 0.9.x/) + + subject.validate({}) + end + + it 'warns the user if the plugin version is inferior to 0.9.x' do + allow(Gem::Specification).to receive(:find_by_name) + .with(plugin_name) + .and_return(double(:version => Gem::Version.new('0.1.1'))) + + expect_any_instance_of(Cabin::Channel).to receive(:warn) + .with(/Using version 0.1.x/) + subject.validate({}) + end + + it "doesnt show the version notice more than once" do + class LogStash::Filters::Stromae < LogStash::Filters::Base + config_name "stromae" + end + + allow(Gem::Specification).to receive(:find_by_name) + .with(plugin_name) + .and_return(double(:version => Gem::Version.new('0.1.1'))) + + expect_any_instance_of(Cabin::Channel).to receive(:warn) + .once + .with(/Using version 0.1.x/) + + LogStash::Filters::Stromae.validate({}) + LogStash::Filters::Stromae.validate({}) + end + end end