Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST Request Notifier #406

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -118,6 +118,7 @@ Below you find a list of components that Backup currently supports. If you'd lik
- Prowl - Prowl
- Hipchat - Hipchat
- Pushover - Pushover
- POST Request


[Notifiers Wiki Page](https://github.com/meskyanichi/backup/wiki/Notifiers) [Notifiers Wiki Page](https://github.com/meskyanichi/backup/wiki/Notifiers)


Expand Down Expand Up @@ -220,6 +221,15 @@ Backup::Model.new(:sample_backup, 'A sample backup configuration') do
tweet.on_failure = true tweet.on_failure = true
end 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 end
``` ```


Expand Down Expand Up @@ -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><a href="https://github.com/SteveNewson" target="_blank">Steve Newson ( SteveNewson )</a></td>
<td>Pushover Notifier</td> <td>Pushover Notifier</td>
</tr> </tr>
<tr>
<td><a href="https://github.com/DeanPerry" target="_blank">Dean Perry ( DeanPerry )</a></td>
<td>POST Request Notifier</td>
</tr>
</table> </table>




Expand Down
17 changes: 9 additions & 8 deletions lib/backup.rb
Expand Up @@ -99,14 +99,15 @@ module Encryptor
## ##
# Autoload notification files # Autoload notification files
module Notifier module Notifier
autoload :Base, File.join(NOTIFIER_PATH, 'base') autoload :Base, File.join(NOTIFIER_PATH, 'base')
autoload :Binder, File.join(NOTIFIER_PATH, 'binder') autoload :Binder, File.join(NOTIFIER_PATH, 'binder')
autoload :Mail, File.join(NOTIFIER_PATH, 'mail') autoload :Mail, File.join(NOTIFIER_PATH, 'mail')
autoload :Twitter, File.join(NOTIFIER_PATH, 'twitter') autoload :Twitter, File.join(NOTIFIER_PATH, 'twitter')
autoload :Campfire, File.join(NOTIFIER_PATH, 'campfire') autoload :Campfire, File.join(NOTIFIER_PATH, 'campfire')
autoload :Prowl, File.join(NOTIFIER_PATH, 'prowl') autoload :Prowl, File.join(NOTIFIER_PATH, 'prowl')
autoload :Hipchat, File.join(NOTIFIER_PATH, 'hipchat') autoload :Hipchat, File.join(NOTIFIER_PATH, 'hipchat')
autoload :Pushover, File.join(NOTIFIER_PATH, 'pushover') autoload :Pushover, File.join(NOTIFIER_PATH, 'pushover')
autoload :PostRequest, File.join(NOTIFIER_PATH, 'post_request')
end end


## ##
Expand Down
2 changes: 1 addition & 1 deletion lib/backup/config.rb
Expand Up @@ -116,7 +116,7 @@ def add_dsl_constants!
{ 'RSync' => ['Push', 'Pull', 'Local'] } { 'RSync' => ['Push', 'Pull', 'Local'] }
], ],
# Notifiers # Notifiers
['Mail', 'Twitter', 'Campfire', 'Prowl', 'Hipchat', 'Pushover'] ['Mail', 'Twitter', 'Campfire', 'Prowl', 'Hipchat', 'Pushover', 'PostRequest']
] ]
) )
end end
Expand Down
84 changes: 84 additions & 0 deletions lib/backup/notifier/post_request.rb
@@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
2 changes: 1 addition & 1 deletion spec/cli/utility_spec.rb
Expand Up @@ -222,7 +222,7 @@
[--syncers=SYNCERS] # (cloud_files, rsync_local, rsync_pull, rsync_push, s3) [--syncers=SYNCERS] # (cloud_files, rsync_local, rsync_pull, rsync_push, s3)
[--encryptors=ENCRYPTORS] # (gpg, openssl) [--encryptors=ENCRYPTORS] # (gpg, openssl)
[--compressors=COMPRESSORS] # (bzip2, custom, gzip, lzma, pbzip2) [--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] [--archives]
[--splitter] # use `--no-splitter` to disable [--splitter] # use `--no-splitter` to disable
# Default: true # Default: true
Expand Down
120 changes: 120 additions & 0 deletions spec/notifier/post_request_spec.rb
@@ -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
11 changes: 11 additions & 0 deletions templates/cli/utility/notifier/post_request
@@ -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