提交 b800a3ab 编写于 作者: S Salvatore Sanfilippo

a first refactoring of redis-trib.rb

上级 407798c1
...@@ -3,47 +3,67 @@ ...@@ -3,47 +3,67 @@
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
end
def check_arity(req_args, num_args)
if ((req_args > 0 and num_args != req_args) ||
(req_args < 0 and num_args < req_args.abs))
puts "Wrong number of arguments for specified sub command"
exit 1
end
end
def parse_node(node) class ClusterNode
s = node.split(":") def initialize(addr)
s = addr.split(":")
if s.length != 2 if s.length != 2
puts "Invalid node name #{node}" puts "Invalid node name #{node}"
exit 1 exit 1
end end
return {:host => s[0], :port => s[1].to_i} @host = s[0]
@port = s[1]
end end
def connect_to_node(naddr) def to_s
xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: " "#{@host}:#{@port}"
end
def connect
xputs "Connecting to node #{self}: "
begin begin
r = Redis.new(:host => naddr[:host], :port => naddr[:port]) @r = Redis.new(:host => @ost, :port => @port)
r.ping @r.ping
rescue rescue
puts "ERROR" puts "ERROR"
puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}" puts "Sorry, can't connect to node #{self}"
exit 1
end end
puts "OK" puts "OK"
end 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)
if ((req_args > 0 and num_args != req_args) ||
(req_args < 0 and num_args < req_args.abs))
puts "Wrong number of arguments for specified sub command"
exit 1
end
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册