Created
September 20, 2018 15:59
-
-
Save arifcse019/c8a7096275e16d344f6c53ad884716ea to your computer and use it in GitHub Desktop.
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
#!/usr/bin/ruby | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
require 'optparse' | |
require 'io/console' | |
$stdout = File.new( '/var/log/move_shards_out.log', 'w' ) | |
options = {:node_out => nil, :admin_password => nil } | |
parser = OptionParser.new do|opts| | |
opts.banner = "Usage: move_shards_out.rb [options]" | |
opts.on('-o', '--node_out name1@fqdn/ip', 'Outgoing Node Name') do |node_out| | |
options[:node_out] = node_out; | |
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[: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" | |
print admin_username | |
print admin_password | |
print node_out | |
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"].delete(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 | |
nodes.delete(node_out) | |
end | |
end | |
new_changelog = ["delete", node_out] | |
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