class Astrails::Safe::Cloudfiles

Constants

MAX_CLOUDFILES_FILE_SIZE

Protected Instance Methods

active?() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 8
def active?
  container && user && api_key
end
api_key() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 68
def api_key
  @config[:cloudfiles, :api_key]
end
cleanup() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 44
def cleanup
  return if $LOCAL

  return unless keep = @config[:keep, :cloudfiles]

  puts "listing files: #{container}:#{base}*" if $_VERBOSE
  cf = CloudFiles::Connection.new(user, api_key, true, service_net) unless $LOCAL
  cf_container = cf.container(container)
  files = cf_container.objects(:prefix => base).sort

  cleanup_with_limit(files, keep) do |f|
    puts "removing Cloud File #{container}:#{f}" if $DRY_RUN || $_VERBOSE
    cf_container.delete_object(f) unless $DRY_RUN || $LOCAL
  end
end
container() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 60
def container
  @config[:cloudfiles, :container]
end
get_file_size(path) click to toggle source

UGLY: we need this function for the reason that we can’t double mock on ruby 1.9.2, duh! so we created this func to mock it all together

# File lib/astrails/safe/cloudfiles.rb, line 19
def get_file_size(path)
  File.stat(path).size
end
path() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 12
def path
  @path ||= expand(config[:cloudfiles, :path] || config[:local, :path] || ":kind/:id")
end
save() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 23
def save
  raise RuntimeError, "pipe-streaming not supported for S3." unless @backup.path

  # needed in cleanup even on dry run
  cf = CloudFiles::Connection.new(user, api_key, true, service_net) unless $LOCAL
  puts "Uploading #{container}:#{full_path} from #{@backup.path}" if $_VERBOSE || $DRY_RUN
  unless $DRY_RUN || $LOCAL
    if get_file_size(@backup.path) > MAX_CLOUDFILES_FILE_SIZE
      STDERR.puts "ERROR: File size exceeds maximum allowed for upload to Cloud Files (#{MAX_CLOUDFILES_FILE_SIZE}): #{@backup.path}"
      return
    end
    benchmark = Benchmark.realtime do
      cf_container = cf.create_container(container)
      o = cf_container.create_object(full_path,true)
      o.write(File.open(@backup.path))
    end
    puts "...done" if $_VERBOSE
    puts("Upload took " + sprintf("%.2f", benchmark) + " second(s).") if $_VERBOSE
  end
end
service_net() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 72
def service_net
  @config[:cloudfiles, :service_net] || false
end
user() click to toggle source
# File lib/astrails/safe/cloudfiles.rb, line 64
def user
  @config[:cloudfiles, :user]
end