POST Request Notifier #406

Closed
wants to merge 3 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+241 −10
Split
View
1 .rspec
@@ -0,0 +1 @@
+--color
View
@@ -118,6 +118,7 @@ Below you find a list of components that Backup currently supports. If you'd lik
- Prowl
- Hipchat
- Pushover
+- POST Request
[Notifiers Wiki Page](https://github.com/meskyanichi/backup/wiki/Notifiers)
@@ -220,6 +221,15 @@ Backup::Model.new(:sample_backup, 'A sample backup configuration') do
tweet.on_failure = true
end
+ notify_by PostRequest do |post|
+ post.on_success = true
+ post.on_warning = true
+ post.on_failure = true
+
+ post.host = "http://my-backup-notifier.com"
+ post.token = "123-abc-123-abc"
+ end
+
end
```
@@ -432,6 +442,10 @@ View the [issue log](https://github.com/meskyanichi/backup/issues) and post them
<td><a href="https://github.com/SteveNewson" target="_blank">Steve Newson ( SteveNewson )</a></td>
<td>Pushover Notifier</td>
</tr>
+ <tr>
+ <td><a href="https://github.com/DeanPerry" target="_blank">Dean Perry ( DeanPerry )</a></td>
+ <td>POST Request Notifier</td>
+ </tr>
</table>
View
@@ -99,14 +99,15 @@ module Encryptor
##
# Autoload notification files
module Notifier
- autoload :Base, File.join(NOTIFIER_PATH, 'base')
- autoload :Binder, File.join(NOTIFIER_PATH, 'binder')
- autoload :Mail, File.join(NOTIFIER_PATH, 'mail')
- autoload :Twitter, File.join(NOTIFIER_PATH, 'twitter')
- autoload :Campfire, File.join(NOTIFIER_PATH, 'campfire')
- autoload :Prowl, File.join(NOTIFIER_PATH, 'prowl')
- autoload :Hipchat, File.join(NOTIFIER_PATH, 'hipchat')
- autoload :Pushover, File.join(NOTIFIER_PATH, 'pushover')
+ autoload :Base, File.join(NOTIFIER_PATH, 'base')
+ autoload :Binder, File.join(NOTIFIER_PATH, 'binder')
+ autoload :Mail, File.join(NOTIFIER_PATH, 'mail')
+ autoload :Twitter, File.join(NOTIFIER_PATH, 'twitter')
+ autoload :Campfire, File.join(NOTIFIER_PATH, 'campfire')
+ autoload :Prowl, File.join(NOTIFIER_PATH, 'prowl')
+ autoload :Hipchat, File.join(NOTIFIER_PATH, 'hipchat')
+ autoload :Pushover, File.join(NOTIFIER_PATH, 'pushover')
+ autoload :PostRequest, File.join(NOTIFIER_PATH, 'post_request')
end
##
View
@@ -116,7 +116,7 @@ def add_dsl_constants!
{ 'RSync' => ['Push', 'Pull', 'Local'] }
],
# Notifiers
- ['Mail', 'Twitter', 'Campfire', 'Prowl', 'Hipchat', 'Pushover']
+ ['Mail', 'Twitter', 'Campfire', 'Prowl', 'Hipchat', 'Pushover', 'PostRequest']
]
)
end
@@ -0,0 +1,84 @@
+# encoding: utf-8
+require 'net/https'
+
+module Backup
+ module Notifier
+ class PostRequest < Base
+
+ ##
+ # The host to send the request to
+ attr_accessor :host
+
+ ##
+ # The Application Token
+ # Defined by the user to possibly separate backup logs
+ attr_accessor :token
+
+
+ def initialize(model, &block)
+ super(model)
+
+ instance_eval(&block) if block_given?
+ end
+
+ private
+
+ ##
+ # Notify the user of the backup operation results.
+ # `status` indicates one of the following:
+ #
+ # `:success`
+ # : The backup completed successfully.
+ # : Notification will be sent if `on_success` was set to `true`
+ #
+ # `:warning`
+ # : The backup completed successfully, but warnings were logged
+ # : Notification will be sent, including a copy of the current
+ # : backup log, if `on_warning` was set to `true`
+ #
+ # `:failure`
+ # : The backup operation failed.
+ # : Notification will be sent, including the Exception which caused
+ # : the failure, the Exception's backtrace, a copy of the current
+ # : backup log and other information if `on_failure` was set to `true`
+ #
+ def notify!(status)
+ name = case status
+ when :success then 'Success'
+ when :failure then 'Failure'
+ when :warning then 'Warning'
+ end
+ message = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
+
+ post(message, status)
+ end
+
+ # Send a POST request to the host with a message & status
+ def post(message, status)
+
+ # Add the token to the URL if present
+ if token
+ uri = URI.parse([host, token].join('/'))
+ else
+ uri = URI.parse(host)
+ end
+
+ http_request = Net::HTTP::Post.new(uri.request_uri)
+ http_request.initialize_http_header({"User-Agent" => "BackupClient/#{Backup::Version.current}"})
+
+ http_request.set_form_data({:message => message, :status => status, :backup_version => Backup::Version.current})
+
+ http = Net::HTTP.new(uri.host, uri.port)
+
+ if uri.scheme == 'https'
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@bumi

bumi May 24, 2013

what is the reason behind using OpenSSL::SSL::VERIFY_NONE ?
I think it should not disable certificate verification by default.

@deanpcmad

deanpcmad May 24, 2013

That code was copied from another gem so I just left it as it is

On 24 May 2013, at 19:26, Michael Bumann notifications@github.com
wrote:

In lib/backup/notifier/post_request.rb:

  •    if token
    
  •      uri = URI.parse([host, token].join('/'))
    
  •    else
    
  •      uri = URI.parse(host)
    
  •    end
    
  •    http_request = Net::HTTP::Post.new(uri.request_uri)
    
  •    http_request.initialize_http_header({"User-Agent" => "BackupClient/#{Backup::Version.current}"})
    
  •    http_request.set_form_data({:message => message, :status => status, :backup_version => Backup::Version.current})
    
  •    http = Net::HTTP.new(uri.host, uri.port)
    
  •    if uri.scheme == 'https'
    
  •      http.use_ssl = true
    
  •      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    what is the reason behind using OpenSSL::SSL::VERIFY_NONE ?
    I think it should not disable certificate verification by default.


Reply to this email directly or view it on GitHub.

+ end
+
+ http_result = http.request(http_request)
+
+ end
+
+ end
+ end
+end
View
@@ -222,7 +222,7 @@
[--syncers=SYNCERS] # (cloud_files, rsync_local, rsync_pull, rsync_push, s3)
[--encryptors=ENCRYPTORS] # (gpg, openssl)
[--compressors=COMPRESSORS] # (bzip2, custom, gzip, lzma, pbzip2)
- [--notifiers=NOTIFIERS] # (campfire, hipchat, mail, prowl, pushover, twitter)
+ [--notifiers=NOTIFIERS] # (campfire, hipchat, mail, post_request, prowl, pushover, twitter)
[--archives]
[--splitter] # use `--no-splitter` to disable
# Default: true
@@ -0,0 +1,120 @@
+# encoding: utf-8
+
+require File.expand_path('../../spec_helper.rb', __FILE__)
+
+describe Backup::Notifier::PostRequest do
+ let(:model) { Backup::Model.new(:test_trigger, 'test label') }
+ let(:notifier) do
+ Backup::Notifier::PostRequest.new(model) do |notifier|
+ notifier.host = 'http://host.com'
+ notifier.token = 'token'
+ end
+ end
+
+ it 'should be a subclass of Notifier::Base' do
+ Backup::Notifier::PostRequest.
+ superclass.should == Backup::Notifier::Base
+ end
+
+ describe '#initialize' do
+ after { Backup::Notifier::PostRequest.clear_defaults! }
+
+ it 'should load pre-configured defaults through Base' do
+ Backup::Notifier::PostRequest.any_instance.expects(:load_defaults!)
+ notifier
+ end
+
+ it 'should pass the model reference to Base' do
+ notifier.instance_variable_get(:@model).should == model
+ end
+
+ context 'when no pre-configured defaults have been set' do
+ it 'should use the values given' do
+ notifier.host.should == 'http://host.com'
+ notifier.token.should == 'token'
+
+ notifier.on_success.should == true
+ notifier.on_warning.should == true
+ notifier.on_failure.should == true
+ end
+
+ it 'should use default values if none are given' do
+ notifier = Backup::Notifier::PostRequest.new(model)
+ notifier.token.should be_nil
+ notifier.host.should be_nil
+
+ notifier.on_success.should == true
+ notifier.on_warning.should == true
+ notifier.on_failure.should == true
+ end
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Notifier::PostRequest.defaults do |n|
+ n.token = 'the_token'
+ n.host = 'http://anotherhost.com'
+ n.on_failure = false
+ end
+ end
+
+ it 'should use pre-configured defaults' do
+ notifier = Backup::Notifier::PostRequest.new(model)
+
+ notifier.token.should == 'the_token'
+ notifier.host.should == 'http://anotherhost.com'
+
+ notifier.on_success.should == true
+ notifier.on_warning.should == true
+ notifier.on_failure.should == false
+ end
+
+ it 'should override pre-configured defaults' do
+ notifier = Backup::Notifier::PostRequest.new(model) do |n|
+ n.token = 'new_token'
+ n.host = 'http://testhost.com'
+ n.on_success = false
+ n.on_failure = true
+ end
+
+ notifier.token.should == 'new_token'
+ notifier.host.should == 'http://testhost.com'
+
+ notifier.on_success.should == false
+ notifier.on_warning.should == true
+ notifier.on_failure.should == true
+ end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
+
+ # Can't really test this area without a working host
+ #
+ # describe '#notify!' do
+ # context 'when status is :success' do
+ # it 'should send Success message' do
+ # notifier.expects(:send_message).with(
+ # '[Backup::Success] test label (test_trigger)'
+ # )
+ # notifier.send(:notify!, :success)
+ # end
+ # end
+
+ # context 'when status is :warning' do
+ # it 'should send Warning message' do
+ # notifier.expects(:send_message).with(
+ # '[Backup::Warning] test label (test_trigger)'
+ # )
+ # notifier.send(:notify!, :warning)
+ # end
+ # end
+
+ # context 'when status is :failure' do
+ # it 'should send Failure message' do
+ # notifier.expects(:send_message).with(
+ # '[Backup::Failure] test label (test_trigger)'
+ # )
+ # notifier.send(:notify!, :failure)
+ # end
+ # end
+ # end # describe '#notify!'
+end
@@ -0,0 +1,11 @@
+ ##
+ # Post Request [Notifier]
+ #
+ notify_by PostRequest do |post|
+ post.on_success = true
+ post.on_warning = true
+ post.on_failure = true
+
+ post.host = "http://host.com"
+ post.token = "TOKEN" # This is optional
+ end