]> git.saurik.com Git - redis.git/blame - src/redis-trib.rb
Redis-trib initial implementation (currently can not do any actual work)
[redis.git] / src / redis-trib.rb
CommitLineData
407798c1
SS
1#!/usr/bin/env ruby
2
3require 'rubygems'
4require 'redis'
5
6class RedisTrib
7 def xputs(s)
8 printf s
9 STDOUT.flush
10 end
11
12 def check_arity(req_args, num_args)
13 if ((req_args > 0 and num_args != req_args) ||
14 (req_args < 0 and num_args < req_args.abs))
15 puts "Wrong number of arguments for specified sub command"
16 exit 1
17 end
18 end
19
20 def parse_node(node)
21 s = node.split(":")
22 if s.length != 2
23 puts "Invalid node name #{node}"
24 exit 1
25 end
26 return {:host => s[0], :port => s[1].to_i}
27 end
28
29 def connect_to_node(naddr)
30 xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: "
31 begin
32 r = Redis.new(:host => naddr[:host], :port => naddr[:port])
33 r.ping
34 rescue
35 puts "ERROR"
36 puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}"
37 exit 1
38 end
39 puts "OK"
40 end
41
42 def create_cluster
43 puts "Creating cluster"
44 ARGV[1..-1].each{|node|
45 naddr = parse_node(node)
46 r = connect_to_node(naddr)
47 }
48 end
49end
50
51COMMANDS={
52 "create-cluster" => ["create_cluster", -2]
53}
54
55# Sanity check
56if ARGV.length == 0
57 puts "Usage: redis-trib <command> <arguments ...>"
58 exit 1
59end
60
61rt = RedisTrib.new
62cmd_spec = COMMANDS[ARGV[0].downcase]
63if !cmd_spec
64 puts "Unknown redis-trib subcommand '#{ARGV[0]}'"
65 exit 1
66end
67rt.check_arity(cmd_spec[1],ARGV.length)
68
69# Dispatch
70rt.send(cmd_spec[0])