]> git.saurik.com Git - redis.git/commitdiff
a first refactoring of redis-trib.rb
authorSalvatore Sanfilippo <antirez@gmail.com>
Mon, 11 Apr 2011 14:58:47 +0000 (16:58 +0200)
committerSalvatore Sanfilippo <antirez@gmail.com>
Mon, 11 Apr 2011 14:58:47 +0000 (16:58 +0200)
src/redis-trib.rb

index d83c9f8eb97916f3d0ac7604887dbb536c98ba08..8456ed29f884e988ada61982a7f723799b83ef67 100755 (executable)
@@ -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