Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding airbrake notification support

  • Loading branch information...
commit f569df36ff1f55df4b9a0e86a3b949d6203ed9db 1 parent 7c0d539
@mattberther authored
View
6 Gemfile.lock
@@ -5,6 +5,7 @@ PATH
aws-s3 (~> 0.6.2)
cloudfiles (~> 1.4.7)
net-sftp (~> 2.0.4)
+ toadhopper (~> 2.0)
GEM
remote: http://rubygems.org/
@@ -14,15 +15,16 @@ GEM
mime-types
xml-simple
builder (3.0.0)
- cloudfiles (1.4.7)
+ cloudfiles (1.4.18)
mime-types (>= 1.16)
mime-types (1.17.2)
- net-sftp (2.0.4)
+ net-sftp (2.0.5)
net-ssh (>= 2.0.9)
net-ssh (2.2.1)
rake (0.8.7)
rr (1.0.4)
rspec (1.3.2)
+ toadhopper (2.0)
xml-simple (1.1.1)
PLATFORMS
View
1  astrails-safe.gemspec
@@ -33,6 +33,7 @@ Remote storage is supported on Amazon S3, Rackspace Cloud Files, or just plain S
s.add_runtime_dependency 'aws-s3', '~> 0.6.2'
s.add_runtime_dependency 'cloudfiles', '~> 1.4.7'
s.add_runtime_dependency 'net-sftp', '~> 2.0.4'
+ s.add_runtime_dependency 'toadhopper', '~> 2.0'
end
View
33 lib/astrails/safe.rb
@@ -3,6 +3,7 @@
require 'net/sftp'
require 'fileutils'
require 'benchmark'
+require 'toadhopper'
require 'tempfile'
require 'extensions/mktmpdir'
@@ -40,22 +41,30 @@ module Safe
def safe(&block)
config = Config::Node.new(&block)
- #config.dump
-
- [[Mysqldump, [:mysqldump, :databases]],
- [Pgdump, [:pgdump, :databases]],
- [Archive, [:tar, :archives]],
- [Svndump, [:svndump, :repos]]
- ].each do |klass, path|
- if collection = config[*path]
- collection.each do |name, config|
- klass.new(name, config).backup.run(config, :gpg, :gzip, :local, :s3, :cloudfiles, :sftp)
+ begin
+ [[Mysqldump, [:mysqldump, :databases]],
+ [Pgdump, [:pgdump, :databases]],
+ [Archive, [:tar, :archives]],
+ [Svndump, [:svndump, :repos]]
+ ].each do |klass, path|
+ if collection = config[*path]
+ collection.each do |name, config|
+ klass.new(name, config).backup.run(config, :gpg, :gzip, :local, :s3, :cloudfiles, :sftp)
+ end
+ end
+ end
+ rescue => e
+ begin
+ if config["airbrake"]
+ toad = Toadhopper.new(config["airbrake"]["api_key"])
+ toad.post!(e)
end
+ rescue
end
+ ensure
+ Astrails::Safe::TmpFile.cleanup
end
-
- Astrails::Safe::TmpFile.cleanup
end
module_function :safe
end
View
2  lib/astrails/safe/config/builder.rb
@@ -4,7 +4,7 @@ module Config
class Builder
COLLECTIONS = %w/database archive repo/
ITEMS = %w/s3 cloudfiles key secret bucket api_key container service_net path gpg password keep local mysqldump pgdump command options
- user host port socket skip_tables tar files exclude filename svndump repo_path sftp/
+ user host port socket skip_tables tar files exclude filename svndump repo_path sftp airbrake/
NAMES = COLLECTIONS + ITEMS
def initialize(node)
@node = node
View
76 spec/integration/airbrake_integration_spec.rb
@@ -0,0 +1,76 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require 'toadhopper'
+
+describe "a failing backup" do
+ before :each do
+ @exception = RuntimeError.new("BOOM")
+ stub.instance_of(Astrails::Safe::Source).backup { raise @exception }
+
+ # need both local and instance vars
+ # instance variables are used in tests
+ # local variables are used in the backup definition (instance vars can't be seen)
+ @root = root = "tmp/airbrake_backup_example"
+
+ # clean state
+ rm_rf @root
+ mkdir_p @root
+
+ # create source tree
+ @src = src = "#{@root}/src"
+ @dst = dst = "#{@root}/backup"
+ mkdir_p @dst
+
+ @now = Time.now
+ @timestamp = @now.strftime("%y%m%d-%H%M")
+
+ stub(Time).now {@now} # Freeze
+ end
+
+ describe "when not configured to send airbrake notifications" do
+ it "should not notify the toadhopper" do
+ dont_allow(Toadhopper).post!
+
+ Astrails::Safe.safe do
+ local :path => "#{@dst}/:kind"
+ tar do
+ archive :test1, :files => @src
+ end
+ end
+ end
+ end
+
+ describe "when configured to send airbrake notifications" do
+ def perform_backup
+ Astrails::Safe.safe do
+ airbrake do
+ api_key "test_api_key"
+ end
+
+ local :path => "#{@dst}/:kind"
+ tar do
+ archive :test1, :files => @src
+ end
+ end
+ end
+
+ it "should assign the specified api key" do
+ mock(Toadhopper).new('test_api_key')
+ perform_backup
+ end
+
+ it "should notifiy the toadhopper" do
+ mock.instance_of(Toadhopper).post!(@exception)
+ perform_backup
+ end
+
+ it "should clean up the tmp files" do
+ mock(Astrails::Safe::TmpFile).cleanup
+ perform_backup
+ end
+
+ it "should not raise exceptions from the toadhopper" do
+ mock.instance_of(Toadhopper).post!(@exception) { raise RuntimeError.new("From the toad") }
+ lambda { perform_backup }.should_not raise_exception("From the toad")
+ end
+ end
+end
View
6 spec/unit/config_spec.rb
@@ -3,6 +3,10 @@
describe Astrails::Safe::Config do
it "should parse example config" do
config = Astrails::Safe::Config::Node.new do
+ airbrake do
+ api_key "test_airbrake_api_key"
+ end
+
local do
path "path"
end
@@ -104,6 +108,8 @@
end
expected = {
+ "airbrake" => {"api_key" => "test_airbrake_api_key"},
+
"local" => {"path" => "path"},
"s3" => {
Please sign in to comment.
Something went wrong with that request. Please try again.