Class: RedisFailover::CLI
- Inherits:
-
Object
- Object
- RedisFailover::CLI
- Defined in:
- lib/redis_failover/cli.rb
Overview
Parses server command-line arguments.
Class Method Summary collapse
-
.from_file(file, env = nil) ⇒ Hash
Parses options from a YAML file.
-
.invalid_options?(options) ⇒ Boolean
True if required options missing, false otherwise.
-
.parse(source) ⇒ Hash
Parses the source of options.
-
.prepare(options) ⇒ Hash
Prepares the options for the rest of the system.
Class Method Details
.from_file(file, env = nil) ⇒ Hash
Parses options from a YAML file.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/redis_failover/cli.rb', line 97 def self.from_file(file, env = nil) unless File.exists?(file) raise ArgumentError, "File #{file} can't be found" end = YAML.load_file(file) if env = .fetch(env.to_sym) do raise ArgumentError, "Environment #{env} can't be found in config" end end [:nodes] = [:nodes].join(',') [:zkservers] = [:zkservers].join(',') end |
.invalid_options?(options) ⇒ Boolean
Returns true if required options missing, false otherwise.
86 87 88 89 90 |
# File 'lib/redis_failover/cli.rb', line 86 def self.() return true if .empty? return true unless .values_at(:nodes, :zkservers).all? false end |
.parse(source) ⇒ Hash
Parses the source of options.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/redis_failover/cli.rb', line 8 def self.parse(source) = {} parser = OptionParser.new do |opts| opts. = "Usage: redis_node_manager [OPTIONS]" opts.separator "" opts.separator "Specific options:" opts.on('-n', '--nodes NODES', 'Comma-separated redis host:port pairs') do |nodes| [:nodes] = nodes end opts.on('-z', '--zkservers SERVERS', 'Comma-separated ZooKeeper host:port pairs') do |servers| [:zkservers] = servers end opts.on('-p', '--password PASSWORD', 'Redis password') do |password| [:password] = password end opts.on('--znode-path PATH', 'Znode path override for storing redis server list') do |path| [:znode_path] = path end opts.on('--max-failures COUNT', 'Max failures before manager marks node unavailable') do |max| [:max_failures] = Integer(max) end opts.on('-C', '--config PATH', 'Path to YAML config file') do |file| [:config_file] = file end opts.on('--with-chroot ROOT', 'Path to ZooKeepers chroot') do |chroot| [:chroot] = chroot end opts.on('-E', '--environment ENV', 'Config environment to use') do |config_env| [:config_environment] = config_env end opts.on('--node-strategy STRATEGY', 'Strategy used when determining availability of nodes (default: majority)') do |strategy| [:node_strategy] = strategy end opts.on('--failover-strategy STRATEGY', 'Strategy used when failing over to a new node (default: latency)') do |strategy| [:failover_strategy] = strategy end opts.on('--required-node-managers COUNT', 'Required Node Managers that must be reachable to determine node state (default: 1)') do |count| [:required_node_managers] = Integer(count) end opts.on('-h', '--help', 'Display all options') do puts opts exit end end parser.parse(source) if config_file = [:config_file] = from_file(config_file, [:config_environment]) end if () puts parser exit end prepare() end |
.prepare(options) ⇒ Hash
Prepares the options for the rest of the system.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/redis_failover/cli.rb', line 119 def self.prepare() .each_value { |v| v.strip! if v.respond_to?(:strip!) } # turns 'host1:port,host2:port' => [{:host => host, :port => port}, ...] [:nodes] = [:nodes].split(',').map do |node| Hash[[:host, :port].zip(node.split(':'))] end # assume password is same for all redis nodes if password = [:password] [:nodes].each { |opts| opts.update(:password => password) } end if node_strategy = [:node_strategy] [:node_strategy] = node_strategy.to_sym end if failover_strategy = [:failover_strategy] [:failover_strategy] = failover_strategy.to_sym end end |