Skip to content

Commit

Permalink
Add a new command that generate a plugin skeleton that can be used to…
Browse files Browse the repository at this point in the history
… create a new plugin

Fixes #4190
  • Loading branch information
Pere Urbon-Bayes committed May 24, 2016
1 parent 27882b1 commit 1d5fc57
Show file tree
Hide file tree
Showing 36 changed files with 750 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/static/plugin-manager.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ Once set, plugin commands install, update can be used through this proxy.

include::offline-plugins.asciidoc[]

include::private-gem-repo.asciidoc[]
include::private-gem-repo.asciidoc[]
90 changes: 90 additions & 0 deletions lib/pluginmanager/generate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# encoding: utf-8
require "pluginmanager/command"
require "pluginmanager/templates/render_context"
require "erb"
require "ostruct"
require "fileutils"

class LogStash::PluginManager::Generate < LogStash::PluginManager::Command

TYPES = [ "input", "filter", "output" ]

option "--type", "TYPE", "Type of the plugin {input, filter, output}s" do |arg|
raise(ArgumentError, "should be one of: input, output or filter") unless TYPES.include?(arg)
arg
end

option "--name", "PLUGIN", "Name of the new plugin"
option "--path", "PATH", "Location where the plugin skeleton will be created", :default => Dir.pwd

def execute
source = File.join(File.dirname(__FILE__), "templates", "#{type}-plugin")
target_path = File.join(path, full_plugin_name)
FileUtils.mkdir(target_path)
puts " Creating #{target_path}"

begin
create_scaffold(source, target_path)
rescue Errno::EACCES => exception
report_exception("Permission denied when executing the plugin manager", exception)
rescue => exception
report_exception("Plugin creation Aborted", exception)
end
end

private

def create_scaffold(source, target)
transform_r(source, target)
end

def transform_r(source, target)
Dir.entries(source).each do |entry|
next if [ ".", ".." ].include?(entry)
source_entry = File.join(source, entry)
target_entry = File.join(target, entry)

if File.directory?(source_entry)
FileUtils.mkdir(target_entry) unless File.exists?(target_entry)
transform_r(source_entry, target_entry)
else
# copy the new file, in case of being an .erb file should render first
if source_entry.end_with?("erb")
target_entry = target_entry.gsub(/.erb$/,"").gsub("example", name)
File.open(target_entry, "w") { |f| f.write(render(source_entry)) }
else
FileUtils.cp(source_entry, target_entry)
end
puts "\t create #{File.join(full_plugin_name, File.basename(target_entry))}"
end
end
end

def render(source)
template = File.read(source)
renderer = ERB.new(template)
context = LogStash::PluginManager::RenderContext.new(options)
renderer.result(context.get_binding)
end

def options
git_data = get_git_info
@options ||= {
:plugin_name => name,
:author => git_data.author,
:email => git_data.email,
:min_version => "2.0",
}
end

def get_git_info
git = OpenStruct.new
git.author = %x{ git config --get user.name }.strip rescue "your_username"
git.email = %x{ git config --get user.email }.strip rescue "[email protected]"
git
end

def full_plugin_name
@full_plugin_name ||= "logstash-#{type}-#{name.downcase}"
end
end
2 changes: 2 additions & 0 deletions lib/pluginmanager/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module PluginManager
require "pluginmanager/update"
require "pluginmanager/pack"
require "pluginmanager/unpack"
require "pluginmanager/generate"

module LogStash
module PluginManager
Expand All @@ -32,6 +33,7 @@ class Main < Clamp::Command
subcommand "pack", "Package currently installed plugins", LogStash::PluginManager::Pack
subcommand "unpack", "Unpack packaged plugins", LogStash::PluginManager::Unpack
subcommand "list", "List all installed plugins", LogStash::PluginManager::List
subcommand "generate", "Create the foundation for a new plugin", LogStash::PluginManager::Generate
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 0.1.0
- Plugin created with the logstash plugin generator
10 changes: 10 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/CONTRIBUTORS.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The following is a list of people who have contributed ideas, code, bug
reports, or in general have helped logstash along its way.

Contributors:
* <%= author %> - <%= email %>

Note: If you've sent us patches, bug reports, or otherwise contributed to
Logstash, and you aren't on the list above and want to be, please let us know
and we'll make sure you're here. Contributions from folks like you are what make
open source awesome.
2 changes: 2 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/DEVELOPER.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# logstash-filter-<%= plugin_name %>
Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
3 changes: 3 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gemspec

11 changes: 11 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
86 changes: 86 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Logstash Plugin

This is a plugin for [Logstash](https://github.com/elastic/logstash).

It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.

## Documentation

Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).

- For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
- For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide

## Need Help?

Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.

## Developing

### 1. Plugin Developement and Testing

#### Code
- To get started, you'll need JRuby with the Bundler gem installed.

- Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).

- Install dependencies
```sh
bundle install
```

#### Test

- Update your dependencies

```sh
bundle install
```

- Run tests

```sh
bundle exec rspec
```

### 2. Running your unpublished Plugin in Logstash

#### 2.1 Run in a local Logstash clone

- Edit Logstash `Gemfile` and add the local plugin path, for example:
```ruby
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
```
- Install plugin
```sh
bin/logstash-plugin install --no-verify
```
- Run Logstash with your plugin
```sh
bin/logstash -e 'filter {awesome {}}'
```
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.

#### 2.2 Run in an installed Logstash

You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:

- Build your plugin gem
```sh
gem build logstash-filter-awesome.gemspec
```
- Install the plugin from the Logstash home
```sh
bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
```
- Start Logstash and proceed to test the plugin

## Contributing

All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.

Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.

It is more important to the community that you are able to contribute.

For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
1 change: 1 addition & 0 deletions lib/pluginmanager/templates/filter-plugin/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "logstash/devutils/rake"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"

# This <%= @plugin_name %> filter will replace the contents of the default
# message field with whatever you specify in the configuration.
#
# It is only intended to be used as an <%= @plugin_name %>.
class LogStash::Filters::<%= classify(plugin_name) %> < LogStash::Filters::Base

# Setting the config_name here is required. This is how you
# configure this filter from your Logstash config.
#
# filter {
# <%= @plugin_name %> {
# message => "My message..."
# }
# }
#
config_name "<%= plugin_name %>"

# Replace the message with this value.
config :message, :validate => :string, :default => "Hello World!"


public
def register
# Add instance variables
end # def register

public
def filter(event)

if @message
# Replace the event message with our message as configured in the
# config file.
event["message"] = @message
end

# filter_matched should go in the last line of our successful code
filter_matched(event)
end # def filter
end # class LogStash::Filters::<%= classify(plugin_name) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Gem::Specification.new do |s|
s.name = 'logstash-filter-<%= plugin_name %>'
s.version = '0.1.0'
s.licenses = ['Apache License (2.0)']
s.summary = 'TODO: Write a short summary, because Rubygems requires one.'
s.description = 'TODO: Write a longer description or delete this line.'
s.homepage = 'TODO: Put your plugin's website or public repo URL here.'
s.authors = ['<%= author %>']
s.email = '<%= email %>'
s.require_paths = ['lib']

# Files
s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
# Tests
s.test_files = s.files.grep(%r{^(test|spec|features)/})

# Special flag to let us know this is actually a logstash plugin
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", "~> <%= min_version %>"
s.add_development_dependency 'logstash-devutils'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# encoding: utf-8
require_relative '../spec_helper'
require "logstash/filters/<%= plugin_name %>"

describe LogStash::Filters::<%= classify(plugin_name) %> do
describe "Set to Hello World" do
let(:config) do <<-CONFIG
filter {
<%= plugin_name %> {
message => "Hello World"
}
}
CONFIG
end

sample("message" => "some text") do
expect(subject).to include("message")
expect(subject['message']).to eq('Hello World')
end
end
end
2 changes: 2 additions & 0 deletions lib/pluginmanager/templates/filter-plugin/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
2 changes: 2 additions & 0 deletions lib/pluginmanager/templates/input-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 0.1.0
- Plugin created with the logstash plugin generator
10 changes: 10 additions & 0 deletions lib/pluginmanager/templates/input-plugin/CONTRIBUTORS.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The following is a list of people who have contributed ideas, code, bug
reports, or in general have helped logstash along its way.

Contributors:
* <%= author %> - <%= email %>

Note: If you've sent us patches, bug reports, or otherwise contributed to
Logstash, and you aren't on the list above and want to be, please let us know
and we'll make sure you're here. Contributions from folks like you are what make
open source awesome.
2 changes: 2 additions & 0 deletions lib/pluginmanager/templates/input-plugin/DEVELOPER.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# logstash-input-<%= plugin_name %>
Example input plugin. This should help bootstrap your effort to write your own input plugin!
3 changes: 3 additions & 0 deletions lib/pluginmanager/templates/input-plugin/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gemspec

11 changes: 11 additions & 0 deletions lib/pluginmanager/templates/input-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Loading

0 comments on commit 1d5fc57

Please sign in to comment.