Class: CouchTomato::Replicator

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tomato/replicator.rb

Constant Summary collapse

READ_TIMEOUT =

Timeout for a single database replication.

24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(src_server, dst_server = nil) ⇒ Replicator

24 hours in seconds



6
7
8
9
10
11
# File 'lib/couch_tomato/replicator.rb', line 6

def initialize(src_server, dst_server = nil)
  @src_server = CouchRest::Server.new(src_server)
  @dst_server = dst_server ? CouchRest::Server.new(dst_server) : @src_server

  @db_names = @src_server.databases
end

Instance Method Details

#replicate(src_db_name, dst_db_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/couch_tomato/replicator.rb', line 13

def replicate(src_db_name, dst_db_name)
  raise "Source database '#{src_db_name}' does not exist" unless @db_names.include?(src_db_name)

  @dst_server.database!(dst_db_name)

  src_uri = URI.parse(@src_server.uri)
  dst_uri = URI.parse(@dst_server.uri)

  http = Net::HTTP.new(dst_uri.host, dst_uri.port)
  http.read_timeout = READ_TIMEOUT
  http.post('/_replicate', "{\"source\": \"#{src_uri}/#{src_db_name}\", \"target\": \"#{dst_uri}/#{dst_db_name}\"}")
end

#replicate_all(suffix = nil) ⇒ Object



26
27
28
29
30
# File 'lib/couch_tomato/replicator.rb', line 26

def replicate_all(suffix = nil)
  @db_names.each do |db_name|
    replicate(db_name, "#{db_name}#{suffix}")
  end
end

#replicate_env!(from_env, to_env, prefix = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/couch_tomato/replicator.rb', line 32

def replicate_env!(from_env, to_env, prefix=nil)
  source_dbs = @db_names.select do |e|
    if prefix
      e =~ /^#{prefix}_/ && e =~ /_#{from_env}$/
    else
      e =~ /_#{from_env}$/
    end
  end

  source_dbs.each do |source_db|
    target_db = source_db.gsub(/_#{from_env}$/, "_#{to_env}")
    
    puts "Recreating #{target_db}"
    CouchRest::Database.new(@dst_server, target_db).recreate!
    replicate(source_db, target_db)
  end
end