]> git.saurik.com Git - redis.git/commitdiff
Redis-trib initial implementation (currently can not do any actual work)
authorSalvatore Sanfilippo <antirez@gmail.com>
Mon, 11 Apr 2011 14:41:06 +0000 (16:41 +0200)
committerSalvatore Sanfilippo <antirez@gmail.com>
Mon, 11 Apr 2011 14:41:06 +0000 (16:41 +0200)
src/redis-trib.rb [new file with mode: 0755]

diff --git a/src/redis-trib.rb b/src/redis-trib.rb
new file mode 100755 (executable)
index 0000000..d83c9f8
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+
+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 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
+        puts "Creating cluster"
+        ARGV[1..-1].each{|node|
+            naddr = parse_node(node)
+            r = connect_to_node(naddr)
+        }
+    end
+end
+
+COMMANDS={
+    "create-cluster" => ["create_cluster", -2]
+}
+
+# Sanity check
+if ARGV.length == 0
+    puts "Usage: redis-trib <command> <arguments ...>"
+    exit 1
+end
+
+rt = RedisTrib.new
+cmd_spec = COMMANDS[ARGV[0].downcase]
+if !cmd_spec
+    puts "Unknown redis-trib subcommand '#{ARGV[0]}'"
+    exit 1
+end
+rt.check_arity(cmd_spec[1],ARGV.length)
+
+# Dispatch
+rt.send(cmd_spec[0])