Permalink
Browse files

create connection helper method

access class variables via accessors, easier testing
  • Loading branch information...
1 parent a6256c1 commit fc4c73ec1fe02b246d18438f6ffcbdda16d372a9 @mike-park mike-park committed Jun 14, 2011
Showing with 91 additions and 40 deletions.
  1. +1 −0 heroku_cloud_backup.gemspec
  2. +59 −40 lib/heroku_cloud_backup.rb
  3. +31 −0 spec/lib/heroku_cloud_backup_spec.rb
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_runtime_dependency('fog', '>= 0.7.2')
s.add_runtime_dependency('heroku', '>= 2.1.4')
+ s.add_development_dependency('rspec')
end
View
@@ -38,58 +38,25 @@ def backup_name(to_url)
def execute
log "heroku:backup started"
- @bucket_name = ENV['HCB_BUCKET'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_BUCKET' config variable."))
- @backup_path = ENV['HCB_PREFIX'] || "db"
- @provider = ENV['HCB_PROVIDER'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_PROVIDER' config variable."))
- @key1 = ENV['HCB_KEY1'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY1' config variable."))
- @key2 = ENV['HCB_KEY2'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY2' config variable."))
-
b = client.get_latest_backup
raise HerokuCloudBackup::Errors::NoBackups.new("You don't have any pgbackups. Please run heroku pgbackups:capture first.") if b.empty?
begin
- case @provider
- when 'aws'
- @connection = Fog::Storage.new(
- :provider => 'AWS',
- :aws_access_key_id => @key1,
- :aws_secret_access_key => @key2
- )
- when 'rackspace'
- connection = Fog::Storage.new(
- :provider => 'Rackspace',
- :rackspace_username => @key1,
- :rackspace_api_key => @key2
- )
- when 'google'
- connection = Fog::Storage.new(
- :provider => 'Google',
- :google_storage_secret_access_key => @key1,
- :google_storage_access_key_id => @key2
- )
- else
- raise HerokuCloudBackup::Errors::InvalidProvider.new("Your provider was invalid. Valid values are 'aws', 'rackspace', or 'google'")
- end
- rescue
- raise HerokuCloudBackup::Errors::ConnectionError.new("There was an error connecting to your provider.")
- end
-
- begin
- directory = @connection.directories.get(@bucket_name)
+ directory = connection.directories.get(bucket_name)
rescue Excon::Errors::Forbidden
raise HerokuCloudBackup::Errors::Forbidden.new("You do not have access to this bucket name. It's possible this bucket name is already owned by another user. Please check your credentials (access keys) or select a different bucket name.")
end
if !directory
- directory = @connection.directories.create(:key => @bucket_name)
+ directory = connection.directories.create(:key => bucket_name)
end
public_url = b["public_url"]
created_at = DateTime.parse b["created_at"]
db_name = b["from_name"]
name = "#{created_at.strftime('%Y-%m-%d-%H%M%S')}.dump"
begin
- directory.files.create(:key => "#{@backup_path}/#{b["from_name"]}/#{name}", :body => open(public_url))
+ directory.files.create(:key => "#{backup_path}/#{b["from_name"]}/#{name}", :body => open(public_url))
rescue Exception => e
raise HerokuCloudBackup::Errors::UploadError.new(e.message)
end
@@ -99,16 +66,68 @@ def execute
log "heroku:backup complete"
end
+ def connection=(connection)
+ @connection = connection
+ end
+
+ def connection
+ return @connection if @connection
+ self.connection =
+ begin
+ case provider
+ when 'aws'
+ Fog::Storage.new(:provider => 'AWS',
+ :aws_access_key_id => key1,
+ :aws_secret_access_key => key2
+ )
+ when 'rackspace'
+ Fog::Storage.new(:provider => 'Rackspace',
+ :rackspace_username => key1,
+ :rackspace_api_key => key2
+ )
+ when 'google'
+ Fog::Storage.new(:provider => 'Google',
+ :google_storage_secret_access_key => key1,
+ :google_storage_access_key_id => key2
+ )
+ else
+ raise HerokuCloudBackup::Errors::InvalidProvider.new("Your provider was invalid. Valid values are 'aws', 'rackspace', or 'google'")
+ end
+ rescue => error
+ raise HerokuCloudBackup::Errors::ConnectionError.new("There was an error connecting to your provider. #{error}")
+ end
+ end
+
private
+ def bucket_name
+ ENV['HCB_BUCKET'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_BUCKET' config variable."))
+ end
+
+ def backup_path
+ ENV['HCB_PREFIX'] || "db"
+ end
+
+ def provider
+ ENV['HCB_PROVIDER'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_PROVIDER' config variable."))
+ end
+
+ def key1
+ ENV['HCB_KEY1'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY1' config variable."))
+ end
+
+ def key2
+ ENV['HCB_KEY2'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY2' config variable."))
+ end
+
def prune
number_of_files = ENV['HCB_MAX']
if number_of_files && number_of_files.to_i > 0
- directory = @connection.directories.get(@bucket_name)
- files = directory.files.all(:prefix => @backup_path)
+ directory = connection.directories.get(bucket_name)
+ files = directory.files.all(:prefix => backup_path)
file_count = 0
files.reverse.each do |file|
- if file.key =~ Regexp.new("/#{@backup_path}\/\d{4}-\d{2}-\d{2}-\d{6}\.sql\.gz$/i")
+ if file.key =~ Regexp.new("/#{backup_path}\/\d{4}-\d{2}-\d{2}-\d{6}\.sql\.gz$/i")
file_count += 1
else
next
@@ -120,4 +139,4 @@ def prune
end
end
end
-end
+end
@@ -0,0 +1,31 @@
+require "heroku_cloud_backup"
+
+describe HerokuCloudBackup do
+ before(:each) { Fog.mock! }
+
+ context ".connection" do
+ before(:each) do
+ ENV['HCB_KEY1'] = ENV['HCB_KEY2'] = 'testcredentials'
+ end
+ after(:each) do
+ HerokuCloudBackup.connection = nil
+ end
+
+ %w(aws rackspace google).each do |provider|
+ context "with HCB_PROVIDER=#{provider}" do
+ let(:connection) do
+ ENV['HCB_PROVIDER'] = provider
+ HerokuCloudBackup.connection
+ end
+
+ it "should return a valid storage connection object" do
+ connection.should be
+ end
+
+ it "should return a connection object matching the provider" do
+ connection.class.to_s.should match(/::#{provider}::/i)
+ end
+ end
+ end
+ end
+end

0 comments on commit fc4c73e

Please sign in to comment.