Skip to content

Commit

Permalink
Separating config objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenn committed Nov 7, 2017
1 parent a025c0a commit 1ed274e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 40 deletions.
2 changes: 2 additions & 0 deletions lib/dumper/config/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Config
class Base
include POSIX::Spawn
include Dumper::Utility::ObjectFinder

attr_reader :host, :port, :database, :username, :password
end
end
end
48 changes: 9 additions & 39 deletions lib/dumper/config/mongodb.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,25 @@
module Dumper
module Config
class MongoDB < Base
def initialize
def initialize(additional_env: nil)
# @instance = instance || v5_or_later? ? first_instance_of('Mongo::Client') : first_instance_of('Moped::Session')
return unless exist?

@host = default['hosts'].first.split(/:/)[0] # @instance.cluster.servers.first.address.host
@port = default['hosts'].first.split(/:/)[1] || 27017 # @instance.cluster.servers.first.address.port
@database = default['database'] # @instance.database.name
@username = default['user']
@password = default['password']
end

def exist?
default.present?
end

def host
default['hosts'].first.split(/:/)[0]
# @instance.cluster.servers.first.address.host
end

def port
default['hosts'].first.split(/:/)[1] || 27017
# @instance.cluster.servers.first.address.port
end

def database
# @instance.database.name
default['database']
end

def username
default['user']
end

def password
default['password']
end

def dump_tool
tool = 'mongodump'
path = `which #{tool}`.chomp
if path.empty?
# /usr/local/mysql/bin = OSX binary, /usr/local/bin = homebrew, /usr/bin = linux
dir = [ '/usr/local/mysql/bin', '/usr/local/bin', '/usr/bin' ].find do |i|
File.exist?("#{i}/#{tool}")
end
path = "#{dir}/#{tool}" if dir
end
path

end

private

def default
if v5_or_later?
@default ||= if v5_or_later?
Mongoid.clients['default']
else
Mongoid.sessions['default']
Expand Down
24 changes: 24 additions & 0 deletions lib/dumper/config/mysql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Dumper
module Config
class MySQL < Base
def initialize(additional_env: nil)
@rails_env = additional_env || Rails.env

return unless exist?

@host = @config['host']
@port = @config['port']
@database = @config['database']
@username = @config['username']
@password = @config['password']
end

def exist?
defined?(ActiveRecord::Base) and
ActiveRecord::Base.configurations and
@config = ActiveRecord::Base.configurations[@rails_env] and
%w(mysql mysql2 mysql2spatial).include?(@config['adapter'])
end
end
end
end
24 changes: 24 additions & 0 deletions lib/dumper/config/postgresql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Dumper
module Config
class PostgreSQL < Base
def initialize(additional_env: nil)
@rails_env = additional_env || Rails.env

return unless exist?

@host = @config['host']
@port = @config['port']
@database = @config['database']
@username = @config['username']
@password = @config['password']
end

def exist?
defined?(ActiveRecord::Base) and
ActiveRecord::Base.configurations and
@config = ActiveRecord::Base.configurations[@rails_env] and
@config['adapter'] == 'postgresql'
end
end
end
end
31 changes: 31 additions & 0 deletions lib/dumper/config/redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Dumper
module Config
class Redis < Base
def initialize(additional_env: nil)
return unless exist?

@host = @client.host
@port = @client.port
@database = @client.db
@password = @client.password
@dbpath = dbpath
end

def exist?
return unless main_thread_redis = first_instance_of('::Redis')

# redis-rb v4 added CLIENT command support
m = main_thread_redis.respond_to?(:_client) ? :_client : :client
@client = main_thread_redis.send(m)

# New connection for the agent thread
redis = ::Redis.new(host: @client.host, port: @client.port, password: @client.password, db: @client.db)
dir = redis.config(:get, :dir)['dir']
dbfilename = redis.config(:get, :dbfilename)['dbfilename']
dbpath = "#{dir}/#{dbfilename}"

return unless File.exist?(dbpath) # Redis must run on the back up node
end
end
end
end
2 changes: 1 addition & 1 deletion lib/dumper/database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def set_config
:host => config.host,
:port => config.port,
:database => config.database,
:dump_tool => config.dump_tool
:dump_tool => dump_tool_path
}.tap do |h|
h[:username] = config.username if config.username
h[:password] = config.password if config.password
Expand Down

0 comments on commit 1ed274e

Please sign in to comment.