sensu-extension 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dfdd3db0fa88d97218101b74858cefdbcbb8c909
4
+ data.tar.gz: 7c3e63a492dd1ad24947ceb5f2c0536797f9b600
5
+ SHA512:
6
+ metadata.gz: 711b0389397edb3a4829547288d3b679e2a8a0ccc884b46b18dda7097af2d9fbc9262f3b57226387c50b6c4351ccdb22e52c2c2a0b4cd6b107e10ed84b3c7c9b
7
+ data.tar.gz: 95986606220f2a1d18a4a6aa8714bb24dafa53a610485ad44e7ad3112544cd579d6409468b1ed709a4a5f96aae0f02a93deae7e3443a083ad7ab59c5b04bea1e
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+ - jruby
9
+ notifications:
10
+ irc:
11
+ - "irc.freenode.net#sensu"
12
+ addons:
13
+ code_climate:
14
+ repo_token: 767b23081fd38d849be07cd85ade8b06120135f8f983b1c7ead44f2ef0272f6d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sensu-extension.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Heavy Water Operations, LLC.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Sensu::Extension
2
+
3
+ [![Build Status](https://travis-ci.org/sensu/sensu-extension.svg?branch=master)](https://travis-ci.org/sensu/sensu-extension)
4
+
5
+ [![Code Climate](https://codeclimate.com/github/sensu/sensu-extension.png)](https://codeclimate.com/github/sensu/sensu-extension)
6
+ [![Code Climate Coverage](https://codeclimate.com/github/sensu/sensu-extension/coverage.png)](https://codeclimate.com/github/sensu/sensu-extension)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'sensu-extension'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ Documentation can be found [here](http://rubydoc.info/github/sensu/sensu-extension/Sensu/Extension).
21
+
22
+ ## Contributing
23
+
24
+ 1. [Fork it](https://github.com/sensu/sensu-extension/fork)
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,123 @@
1
+ gem "sensu-em"
2
+
3
+ require "eventmachine"
4
+ require "sensu/extension/constants"
5
+
6
+ module Sensu
7
+ module Extension
8
+ class Base
9
+ # @!attribute [rw] logger
10
+ # @return [Array] logger provided by Sensu.
11
+ attr_accessor :logger
12
+
13
+ # @!attribute [rw] settings
14
+ # @return [Array] settings hash provided by Sensu.
15
+ attr_accessor :settings
16
+
17
+ # Initialize the extension, call post_init() when the
18
+ # eventmachine reactor starts up.
19
+ def initialize
20
+ EM.next_tick do
21
+ post_init
22
+ end
23
+ end
24
+
25
+ # Override this method to set the extension's name.
26
+ def name
27
+ "base"
28
+ end
29
+
30
+ # Override this method to set the extension's description.
31
+ def description
32
+ "extension description (change me)"
33
+ end
34
+
35
+ # Override this method to change the extension's definition, a
36
+ # hash. You probably don't need to touch this. The hash must
37
+ # contain :type ("extension") and :name.
38
+ def definition
39
+ {
40
+ :type => "extension",
41
+ :name => name
42
+ }
43
+ end
44
+
45
+ # Override this method to do something immediately after the
46
+ # eventmachine reactor is started. This method is great for
47
+ # setting up connections etc.
48
+ def post_init
49
+ true
50
+ end
51
+
52
+ # Override this method to do something when the extension is
53
+ # run, you must yield or call the callback with two parameters,
54
+ # an output string and exit code.
55
+ #
56
+ # @param data [Object, nil] provided by Sensu.
57
+ # @param callback [Proc] provided by Sensu, expecting to be
58
+ # called with two parameters, an output string and exit code.
59
+ def run(data=nil, &callback)
60
+ callback.call("noop", 0)
61
+ end
62
+
63
+ # Override this method to do something before Sensu stops, such
64
+ # as connection or file cleanup. You must yield or call the
65
+ # callback.
66
+ #
67
+ # @param callback [Proc] provided by Sensu, expecting to be
68
+ # called.
69
+ def stop(&callback)
70
+ callback.call
71
+ end
72
+
73
+ # Retrieve the definition object corresponding to a key, acting
74
+ # like a Hash object. Do not override this method!
75
+ #
76
+ # @param key [String, Symbol]
77
+ # @return [Object] value for key.
78
+ def [](key)
79
+ definition[key.to_sym]
80
+ end
81
+
82
+ # Check to see if the definition has a key. Do not override this
83
+ # method!
84
+ #
85
+ # @param key [String, Symbol]
86
+ # @return [TrueClass, FalseClass]
87
+ def has_key?(key)
88
+ definition.has_key?(key.to_sym)
89
+ end
90
+
91
+ # Run the extension with a few safeties. This method wraps
92
+ # run() with a begin;rescue, and duplicates data before passing
93
+ # it to ensure the extension doesn't mutate the original. Do
94
+ # not override this method!
95
+ #
96
+ # @param data [Object, nil) to dup() and pass to run().
97
+ # @param callback [Proc] to pass to run().
98
+ def safe_run(data=nil, &callback)
99
+ begin
100
+ data ? run(data.dup, &callback) : run(&callback)
101
+ rescue => error
102
+ callback.call(error.to_s, 2)
103
+ end
104
+ end
105
+
106
+ # Determine classes that have inherited this class, used by the
107
+ # extension loader. Do not override this method!
108
+ #
109
+ # @return [Array<object>]
110
+ def self.descendants
111
+ ObjectSpace.each_object(Class).select do |klass|
112
+ klass < self
113
+ end
114
+ end
115
+ end
116
+
117
+ # Create an extension class for each category from Base.
118
+ CATEGORIES.each do |category|
119
+ extension_type = category.to_s.chop
120
+ Sensu::Extension.const_set(extension_type.capitalize, Class.new(Base))
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,5 @@
1
+ module Sensu
2
+ module Extension
3
+ CATEGORIES = [:generics, :bridges, :checks, :mutators, :handlers]
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "sensu-extension"
5
+ spec.version = "0.0.1"
6
+ spec.authors = ["Sean Porter"]
7
+ spec.email = ["portertech@gmail.com"]
8
+ spec.summary = "The Sensu extension library"
9
+ spec.description = "The Sensu extension library"
10
+ spec.homepage = "https://github.com/sensu/sensu-extension"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "sensu-em"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "codeclimate-test-reporter"
24
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,90 @@
1
+ require File.join(File.dirname(__FILE__), "helpers")
2
+ require "sensu/extension"
3
+ require "logger"
4
+
5
+ describe "Sensu::Extension::Base" do
6
+ include Helpers
7
+
8
+ before do
9
+ @extension = Sensu::Extension::Base.new
10
+ end
11
+
12
+ it "can provide the extension API" do
13
+ @extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
14
+ end
15
+
16
+ it "can provide default method return values" do
17
+ @extension.post_init.should be_true
18
+ @extension.name.should eq("base")
19
+ @extension.description.should eq("extension description (change me)")
20
+ @extension.definition.should eq({:type => "extension", :name => "base"})
21
+ end
22
+
23
+ it "can have a logger" do
24
+ @extension.logger = Logger.new("/dev/null")
25
+ @extension.logger.formatter = Proc.new do |severity, datetime, progname, message|
26
+ severity.should eq("INFO")
27
+ message.should eq("test")
28
+ end
29
+ @extension.logger.info("test")
30
+ end
31
+
32
+ it "can have settings" do
33
+ settings = {:foo => 1}
34
+ @extension.settings = settings
35
+ @extension.settings.should eq(settings)
36
+ end
37
+
38
+ it "can handle provided callbacks" do
39
+ async_wrapper do
40
+ callback = Proc.new do |output, status|
41
+ output.should eq("noop")
42
+ status.should eq(0)
43
+ @extension.stop do
44
+ async_done
45
+ end
46
+ end
47
+ @extension.run(&callback)
48
+ end
49
+ end
50
+
51
+ it "can pass event data to run" do
52
+ async_wrapper do
53
+ event = {:foo => 1}
54
+ @extension.safe_run(event) do |output, status|
55
+ output.should eq("noop")
56
+ status.should eq(0)
57
+ async_done
58
+ end
59
+ end
60
+ end
61
+
62
+ it "can catch some run errors" do
63
+ async_wrapper do
64
+ @extension.safe_run do |output, status|
65
+ raise "boom" if status == 0
66
+ output.should eq("boom")
67
+ status.should eq(2)
68
+ async_done
69
+ end
70
+ end
71
+ end
72
+
73
+ it "can provide hash like access to definition()" do
74
+ @extension.has_key?(:type).should be_true
75
+ @extension.has_key?(:name).should be_true
76
+ @extension[:type].should eq("extension")
77
+ @extension[:name].should eq("base")
78
+ end
79
+
80
+ it "can provide a list of decendant classes" do
81
+ expected = [
82
+ Sensu::Extension::Generic,
83
+ Sensu::Extension::Bridge,
84
+ Sensu::Extension::Check,
85
+ Sensu::Extension::Mutator,
86
+ Sensu::Extension::Handler
87
+ ]
88
+ Sensu::Extension::Base.descendants.should include(*expected)
89
+ end
90
+ end
@@ -0,0 +1,52 @@
1
+ require File.join(File.dirname(__FILE__), "helpers")
2
+ require "sensu/extension"
3
+
4
+ describe "Sensu::Extension::Generic" do
5
+ include Helpers
6
+
7
+ it "inherits Base" do
8
+ Sensu::Extension::Generic.superclass.should eq(Sensu::Extension::Base)
9
+ extension = Sensu::Extension::Generic.new
10
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
11
+ end
12
+ end
13
+
14
+ describe "Sensu::Extension::Bridge" do
15
+ include Helpers
16
+
17
+ it "inherits Base" do
18
+ Sensu::Extension::Bridge.superclass.should eq(Sensu::Extension::Base)
19
+ extension = Sensu::Extension::Bridge.new
20
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
21
+ end
22
+ end
23
+
24
+ describe "Sensu::Extension::Check" do
25
+ include Helpers
26
+
27
+ it "inherits Base" do
28
+ Sensu::Extension::Check.superclass.should eq(Sensu::Extension::Base)
29
+ extension = Sensu::Extension::Check.new
30
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
31
+ end
32
+ end
33
+
34
+ describe "Sensu::Extension::Mutator" do
35
+ include Helpers
36
+
37
+ it "inherits Base" do
38
+ Sensu::Extension::Mutator.superclass.should eq(Sensu::Extension::Base)
39
+ extension = Sensu::Extension::Mutator.new
40
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
41
+ end
42
+ end
43
+
44
+ describe "Sensu::Extension::Handler" do
45
+ include Helpers
46
+
47
+ it "inherits Base" do
48
+ Sensu::Extension::Handler.superclass.should eq(Sensu::Extension::Base)
49
+ extension = Sensu::Extension::Handler.new
50
+ extension.should respond_to(:name, :description, :definition, :safe_run, :stop, :has_key?, :[])
51
+ end
52
+ end
data/spec/helpers.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "rspec"
2
+ require "eventmachine"
3
+
4
+ unless RUBY_VERSION < "1.9" || RUBY_PLATFORM =~ /java/
5
+ require "codeclimate-test-reporter"
6
+ CodeClimate::TestReporter.start
7
+ end
8
+
9
+ module Helpers
10
+ def timer(delay, &callback)
11
+ periodic_timer = EM::PeriodicTimer.new(delay) do
12
+ callback.call
13
+ periodic_timer.cancel
14
+ end
15
+ end
16
+
17
+ def async_wrapper(&callback)
18
+ EM.run do
19
+ timer(10) do
20
+ raise "test timed out"
21
+ end
22
+ callback.call
23
+ end
24
+ end
25
+
26
+ def async_done
27
+ EM.stop_event_loop
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-extension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Porter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-em
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: The Sensu extension library
84
+ email:
85
+ - portertech@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/sensu/extension.rb
97
+ - lib/sensu/extension/constants.rb
98
+ - sensu-extension.gemspec
99
+ - spec/base_spec.rb
100
+ - spec/categories_spec.rb
101
+ - spec/helpers.rb
102
+ homepage: https://github.com/sensu/sensu-extension
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.0
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: The Sensu extension library
126
+ test_files:
127
+ - spec/base_spec.rb
128
+ - spec/categories_spec.rb
129
+ - spec/helpers.rb