Skip to content

Instantly share code, notes, and snippets.

@arifcse019
Created September 20, 2018 15:55
Show Gist options
  • Save arifcse019/43a638e4ce837b029d62d59fd0b9a20f to your computer and use it in GitHub Desktop.
Save arifcse019/43a638e4ce837b029d62d59fd0b9a20f to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'net/http'
require 'uri'
require 'json'
require 'optparse'
require 'io/console'
$stdout = File.new( '/var/log/move_shards_in.log', 'w' )
options = {:node_out => nil, :node_in => nil, :admin_password => nil }
parser = OptionParser.new do|opts|
opts.banner = "Usage: move_shards_in.rb [options]"
opts.on('-o', '--node_out name1@fqdn/ip', 'Outgoing Node Name') do |node_out|
options[:node_out] = node_out;
end
opts.on('-i', '--node_in name2@fqdn/ip', 'Incoming Node Name') do |node_in|
options[:node_in] = node_in;
end
opts.on('-p', '--password password', 'Couch Admin Password') do |admin_password|
options[:admin_password] = admin_password;
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
parser.parse!
if options[:node_out] == nil
print 'Enter Outgoing Node: '
options[:node_out] = gets.chomp
end
if options[:node_in] == nil
print 'Enter Incoming Node: '
options[:node_in] = gets.chomp
end
if options[:admin_password] == nil
print 'Enter Admin Password: '
password = STDIN.noecho(&:gets).chomp
print "\n"
else
password = options[:admin_password]
end
admin_username = 'admin'
admin_password = password
node_out = options[:node_out] #"couchdb@10.0.1.252"
node_in = options[:node_in] #"couchdb@primary.couchcluster.internal"
print admin_username
print admin_password
print node_out
print node_in
def http_call (uri_string, username = '', password = '', data_json = {}, verb = 'Get')
uri = URI.parse(uri_string)
http = Net::HTTP.new(uri.host,uri.port)
if verb == 'Put'
request = Net::HTTP::Put.new(uri.request_uri)
request.body = data_json
else
request = Net::HTTP::Get.new(uri.request_uri)
end
request.basic_auth(username, password)
response = http.request(request)
response_json = JSON.parse response.body
end
all_dbs_response_json = http_call "http://127.0.0.1:5984/_all_dbs", admin_username, admin_password
all_dbs_response_json.each do |db|
print "\n#{db}\n"
info_about_db_response_json = http_call "http://127.0.0.1:5986/_dbs/#{db}", admin_username, admin_password
#swap by_node information
if info_about_db_response_json["by_node"].has_key? node_out
info_about_db_response_json["by_node"][node_in] = info_about_db_response_json["by_node"][node_out]
else
next
end
#replace outgoing nodes in each shard of by_range dictionary
info_about_db_response_json["by_range"].each do |shard, nodes|
if nodes.include? node_out and not nodes.include? node_in
nodes.push(node_in)
end
end
new_changelog = ["add", node_in]
info_about_db_response_json["changelog"].push(new_changelog)
print JSON.pretty_generate(info_about_db_response_json)
info_about_db_response_json_after = http_call "http://127.0.0.1:5986/_dbs/#{db}", admin_username, admin_password, info_about_db_response_json.to_json, 'Put'
print JSON.pretty_generate(info_about_db_response_json_after)
end
$stdout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment