From b800a3ab20440fdd55947f52f3efcc689ef858d3 Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 16:58:47 +0200 Subject: [PATCH] a first refactoring of redis-trib.rb --- src/redis-trib.rb | 70 ++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index d83c9f8e..8456ed29 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -3,47 +3,67 @@ require 'rubygems' require 'redis' -class RedisTrib - def xputs(s) - printf s - STDOUT.flush - 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 xputs(s) + printf s + STDOUT.flush +end - def parse_node(node) - s = node.split(":") +class ClusterNode + def initialize(addr) + s = addr.split(":") if s.length != 2 puts "Invalid node name #{node}" exit 1 end - return {:host => s[0], :port => s[1].to_i} + @host = s[0] + @port = s[1] end - def connect_to_node(naddr) - xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: " + def to_s + "#{@host}:#{@port}" + end + + def connect + xputs "Connecting to node #{self}: " begin - r = Redis.new(:host => naddr[:host], :port => naddr[:port]) - r.ping + @r = Redis.new(:host => @ost, :port => @port) + @r.ping rescue puts "ERROR" - puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}" - exit 1 + 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) + 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 puts "Creating cluster" - ARGV[1..-1].each{|node| - naddr = parse_node(node) - r = connect_to_node(naddr) + ARGV[1..-1].each{|n| + node = ClusterNode.new(n) + node.connect + node.assert_cluster + # node.assert_empty } end end -- 2.45.2