class Slipcover::DevTools::Replicator

Attributes

database_name[R]
http[R]
logger[R]
source_environment[R]
source_server[R]
target_environment[R]
target_server[R]

Public Class Methods

new(opts) click to toggle source
# File lib/slipcover/dev_tools.rb, line 14
def initialize(opts)
  @source_environment = opts[:source_environment] || "staging"
  @target_environment = opts[:target_environment] || "development"
  @database_name = opts[:database_name]
  @logger = opts[:logger] || Logger.new("/dev/null")

  @source_server = Slipcover::Server.new(Slipcover::Config.yaml_path, source_environment)
  @target_server = Slipcover::Server.new(Slipcover::Config.yaml_path, target_environment)

  @http = HttpAdapter.new
end

Public Instance Methods

perform() click to toggle source
# File lib/slipcover/dev_tools.rb, line 26
def perform
  logger.info "Fetching list of #{source_environment} databases#{" containing string \"" + database_name + "\"" if database_name}..."
  logger.info "Replicating these dbs: #{dbs_to_replicate.join(",")}"

  dbs_to_replicate.each_with_index.map do |db, i|
    Thread.new(db, i, &method(:replicate))
  end.map(&:join)
end

Private Instance Methods

db_matches?(db) click to toggle source
# File lib/slipcover/dev_tools.rb, line 46
def db_matches?(db)
  matches = db.end_with?(source_environment)
  matches &&= db.include?(database_name) if database_name
  matches
end
db_url(server, db_name) click to toggle source
# File lib/slipcover/dev_tools.rb, line 72
def db_url(server, db_name)
  "http://" + server.url + "/" + db_name
end
dbs_to_replicate() click to toggle source
# File lib/slipcover/dev_tools.rb, line 38
def dbs_to_replicate
  @dbs_to_replicate ||= begin
    require 'slipcover/http_adapter'
    dbs = http.get(source_server.url + "/_all_dbs")
    dbs.select { |db| db_matches?(db) }
  end
end
infer_target_db_name(source_db_name) click to toggle source
# File lib/slipcover/dev_tools.rb, line 68
def infer_target_db_name(source_db_name)
  source_db_name.sub(/#{source_environment}$/, target_environment)
end
replicate(source_db_name, th_id) click to toggle source
# File lib/slipcover/dev_tools.rb, line 52
def replicate(source_db_name, th_id)
  target_db_name = infer_target_db_name(source_db_name)
  source_url = db_url(source_server, source_db_name)
  target_url = db_url(target_server, target_db_name)

  logger.info "#{th_id}: Replicating #{source_url} into #{target_url}"

  response = http.post(target_server.url + "/_replicate", { source: source_url, target: target_url, create_target: true })

  logger.debug("#{th_id}: " + response.to_json)

  raise "#{th_id}: Replication of #{source_url} into #{target_url} failed" unless response[:ok]

  logger.info "#{th_id}: Done replicating #{source_url} into #{target_url}"
end