mirror of
https://github.com/fluencelabs/redis
synced 2025-03-30 22:31:03 +00:00
a first refactoring of redis-trib.rb
This commit is contained in:
parent
407798c1e1
commit
b800a3ab20
@ -3,12 +3,52 @@
|
|||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'redis'
|
require 'redis'
|
||||||
|
|
||||||
class RedisTrib
|
def xputs(s)
|
||||||
def xputs(s)
|
printf s
|
||||||
printf s
|
STDOUT.flush
|
||||||
STDOUT.flush
|
end
|
||||||
|
|
||||||
|
class ClusterNode
|
||||||
|
def initialize(addr)
|
||||||
|
s = addr.split(":")
|
||||||
|
if s.length != 2
|
||||||
|
puts "Invalid node name #{node}"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
@host = s[0]
|
||||||
|
@port = s[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"#{@host}:#{@port}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def connect
|
||||||
|
xputs "Connecting to node #{self}: "
|
||||||
|
begin
|
||||||
|
@r = Redis.new(:host => @ost, :port => @port)
|
||||||
|
@r.ping
|
||||||
|
rescue
|
||||||
|
puts "ERROR"
|
||||||
|
puts "Sorry, can't connect to node #{self}"
|
||||||
|
end
|
||||||
|
puts "OK"
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_cluster
|
||||||
|
info = @r.info
|
||||||
|
if !info["cluster_enabled"] || info["cluster_enabled"].to_i == 0
|
||||||
|
puts "Error: Node #{self} is not configured as a cluster node."
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def r
|
||||||
|
@r
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class RedisTrib
|
||||||
def check_arity(req_args, num_args)
|
def check_arity(req_args, num_args)
|
||||||
if ((req_args > 0 and num_args != req_args) ||
|
if ((req_args > 0 and num_args != req_args) ||
|
||||||
(req_args < 0 and num_args < req_args.abs))
|
(req_args < 0 and num_args < req_args.abs))
|
||||||
@ -17,33 +57,13 @@ class RedisTrib
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_node(node)
|
|
||||||
s = node.split(":")
|
|
||||||
if s.length != 2
|
|
||||||
puts "Invalid node name #{node}"
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
return {:host => s[0], :port => s[1].to_i}
|
|
||||||
end
|
|
||||||
|
|
||||||
def connect_to_node(naddr)
|
|
||||||
xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: "
|
|
||||||
begin
|
|
||||||
r = Redis.new(:host => naddr[:host], :port => naddr[:port])
|
|
||||||
r.ping
|
|
||||||
rescue
|
|
||||||
puts "ERROR"
|
|
||||||
puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}"
|
|
||||||
exit 1
|
|
||||||
end
|
|
||||||
puts "OK"
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_cluster
|
def create_cluster
|
||||||
puts "Creating cluster"
|
puts "Creating cluster"
|
||||||
ARGV[1..-1].each{|node|
|
ARGV[1..-1].each{|n|
|
||||||
naddr = parse_node(node)
|
node = ClusterNode.new(n)
|
||||||
r = connect_to_node(naddr)
|
node.connect
|
||||||
|
node.assert_cluster
|
||||||
|
# node.assert_empty
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user