This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Util | |
def mongo_cluster_command_line_options | |
# | |
Mongoid.default_client # Force the setting to be parsed | |
settings = Map.for(Mongoid.clients[:default]) | |
address = settings[:uri] | |
# | |
unless address && address =~ %r`://` | |
raise("no uri in #{ settings.inspect }") | |
end | |
# | |
rest = address.dup | |
scheme, rest = rest.split('://', 2) | |
if rest =~ %r`@` | |
auth, rest = rest.split('@', 2) | |
end | |
hosts_and_ports, rest = rest.split('/', 2) | |
path_info = rest | |
# | |
list = [] | |
list_of = proc{|arg| Coerce.list_of_strings(arg)} | |
esc = proc{|arg| Shellwords.escape(arg.to_s)} | |
list_of[hosts_and_ports].each do |host_and_port| | |
string = "#{ scheme }://" if scheme | |
string << "#{ auth }@" if auth | |
string << "#{ host_and_port }" | |
string << "/#{ path_info }" if path_info | |
uri = URI.parse(string) | |
db = (uri.path.split('/').reject{|x| x.blank?}.first || settings[:database]) | |
host = (uri.host || list_of[settings[:hosts]].first) | |
port = (uri.port || settings[:port]) | |
username = (uri.user || settings[:username]) | |
password = (uri.password || settings[:password]) | |
ssl = !!(settings.get(:options, :ssl)) | |
options = { | |
:db => db, | |
:host => host, | |
:port => port, | |
:username => username, | |
:password => password, | |
:ssl => ssl | |
}.select{|k,v| v.present?} | |
list.push(options) | |
end | |
# | |
list | |
end | |
def mongo_master_command_line_options | |
mongo_cluster_command_line_options.each do |options| | |
command = ['mongo'] | |
db = options[:db] | |
host = options[:host] | |
port = options[:port] | |
username = options[:username] | |
password = options[:password] | |
ssl = options[:ssl] | |
command << "#{ db.inspect }" if db.present? | |
command << "--host #{ host.inspect }" if host.present? | |
command << "--port #{ port.inspect }" if port.present? | |
command << "--username #{ username.inspect }" if username.present? | |
command << "--password #{ password.inspect }" if password.present? | |
command << "--ssl" if ssl.present? | |
mongo = command.join(' ') | |
stdout = `echo 'print("ismaster=" + db.isMaster().ismaster);' | #{ mongo } 2>&1` | |
if $?.success? && stdout =~ /ismaster=true/ | |
return options | |
end | |
end | |
raise "no master connection options found in #{ mongo_cluster_command_line_options.inspect }" | |
end | |
extend Util | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment