avdi (owner)

Revisions

gist: 220299 Download_button fork
public
Public Clone URL: git://gist.github.com/220299.git
Embed All Files: show embed
simpledb.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'right_aws'
require 'progressbar'
 
module Simpledb
 
  # Count the objects in a domain
  def sdb_count(domain, options={})
    results = session.select("select count(*) from #{domain}")
    results[:items].first["Domain"]["Count"].first.to_i
  end
 
  def sdb_rm(domain)
    puts "Are you sure?"
    return unless gets =~ /^y/i
    session.delete_domain(domain)
  end
 
  def sdb_copy(source_domain, destination_domain, options={})
    sdb = session
    sdb.list_domains do |results|
      domains = results[:domains]
      if domains.include?(destination_domain)
        raise "Destination #{destination_domain} already exists"
      end
    end
    sdb.create_domain(destination_domain)
    count = sdb_count(source_domain)
    progress = ProgressBar.new('copy', count)
    sdb.select("select * from #{source_domain}") do |results|
      results[:items].each do |record|
        record.each_pair do |key, data|
          sdb.put_attributes(destination_domain, key, data)
          progress.inc
        end
      end
    end
    progress.finish
  end
 
  def sdb_dump(domain, options={})
    dump = []
    sdb = session
    count = sdb_count(domain)
    progress = ProgressBar.new('copy', count)
    sdb.select("select * from #{domain}") do |results|
      progress.inc(results.size)
      results[:items].each do |record|
        record.each_pair do |key, data|
          dump << data
        end
      end
    end
    progress.finish
    dump
  end
 
  private
 
  def session
    %w[AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY].each do |var|
      ENV.key?(var) or raise "#{var} not set"
    end
    sdb = RightAws::SdbInterface.new
  end
end