rangeclient 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a72cd3b4ccd4b6b3f32a68d5f406bab49ff6b2f8
4
+ data.tar.gz: c3371d09c8bcc60604898b99c0679d1a33e30ff8
5
+ SHA512:
6
+ metadata.gz: 0afc535ec3ea7a21241c9ba47fc8db00d0e175cccf899634808e2320b7817d7afa2f03b3a5156b9afd981c8f58a61b0340ce2abf03317442be6d7eb2f633ef5b
7
+ data.tar.gz: c60c6f5e0871dbfaa04b24282b33d97028c57d61ccb98b36792bd50c9868843c13d73177a697d5a2c9b085c3fe75a7ef0094cc6e6fd757d1332df02833d1baf2
data/README.md CHANGED
@@ -1,17 +1,16 @@
1
- rangeclient synopsis
2
- ============================
1
+ # rangeclient synopsis
3
2
 
4
3
 
5
- rangehost = ARGV.shift
6
- rangearg = ARGV.shift
4
+ rangehost = ARGV.shift
5
+ rangearg = ARGV.shift
7
6
 
8
- # Create range object for communication with ranged
9
- r = Range::Client.new({:host => rangehost})
7
+ # Create range object for communication with ranged
8
+ r = Range::Client.new({:host => rangehost})
10
9
 
11
- # use ranged to expand the range expression into an Array
12
- # "foo10..12" => [ foo10, foo11, foo12 ] OR %foo => [ foo10, foo11, foo12 ]
13
- hosts = r.expand(rangearg)
10
+ # use ranged to expand the range expression into an Array
11
+ # "foo10..12" => [ foo10, foo11, foo12 ] OR %foo => [ foo10, foo11, foo12 ]
12
+ hosts = r.expand(rangearg)
14
13
 
15
- # use ranged to compress the array of hostnames into a range expression
16
- # [ foo10, foo11, foo12 ] => "foo10..12"
17
- range_exp = r.compress(hosts)
14
+ # use ranged to compress the array of hostnames into a range expression
15
+ # [ foo10, foo11, foo12 ] => "foo10..12"
16
+ range_exp = r.compress(hosts)
data/bin/er CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
  require 'rangeclient'
5
5
 
6
6
  require 'optparse'
7
- require 'ostruct'
7
+
8
8
  options = {
9
9
  :timeout => 60,
10
10
  }
@@ -15,23 +15,36 @@ optparse = OptionParser.new do |opts|
15
15
  opts.on('-c', '--count', 'Print the count of range elements, not the range itself') do |arg|
16
16
  options[:count] = arg
17
17
  end
18
+ opts.on('-s', '--sort', 'Sort expanded output') do |arg|
19
+ options[:sorted] = arg
20
+ end
18
21
  opts.on('-v', '--vip range:80', 'Which host or host:port to query') do |arg|
19
- options[:vip] = arg
22
+ host, port = arg.split(':', 2)
23
+ options[:host] = host
24
+ options[:port] = port unless port.nil?
20
25
  end
21
26
  opts.on('-p', '--port 80', 'Port to use when connecting') do |arg|
22
27
  options[:port] = arg
23
28
  end
24
- opts.on('-t', '--timeout', "Wait this long for a response ") do |arg|
25
- options[:timeout] = arg
29
+ opts.on('-t', '--timeout TIMEOUT', "Wait this long for a response") do |arg|
30
+ options[:timeout] = arg.to_i
31
+ end
32
+ opts.on_tail('-h', '--help', "This help message") do
33
+ puts opts
34
+ exit
26
35
  end
27
36
  end
28
37
  optparse.parse!
29
38
 
30
- range = Range::Client.new
39
+ range = Range::Client.new(options)
31
40
 
32
41
  in_range = ARGV.join ','
33
42
 
34
- raise "No range given" if in_range.empty?
43
+ if in_range.empty?
44
+ puts optparse
45
+ puts "[ERROR] No range specified"
46
+ exit 1
47
+ end
35
48
 
36
49
  if in_range == '-'
37
50
  in_range = STDIN.readlines.map { |l| l.chomp }.join ','
@@ -45,6 +58,7 @@ if options[:count]
45
58
  elsif not nodes.size.zero?
46
59
  # if we got a result, display it accordingly
47
60
  if options[:expand]
61
+ nodes.sort! if options[:sorted]
48
62
  nodes.each do |node|
49
63
  puts node
50
64
  end
@@ -52,6 +66,6 @@ elsif not nodes.size.zero?
52
66
  puts range.compress(nodes)
53
67
  end
54
68
  else
55
- # no nodes returned.
56
- raise "No nodes specified"
69
+ puts "[WARNING] No nodes returned"
70
+ exit 1
57
71
  end
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'rangeclient'
5
+ require 'pp'
6
+
7
+ require 'optparse'
8
+
9
+ optparse = OptionParser.new do |opts|
10
+ opts.banner = "Usage: #{__FILE__} <range to compress>"
11
+ opts.on_tail('-h', '--help', "This help message") do
12
+ puts opts
13
+ exit
14
+ end
15
+ end
16
+ optparse.parse!
17
+
18
+ range = Range::Client.new()
19
+
20
+ in_range = ARGV.join(',')
21
+
22
+ if in_range.empty?
23
+ puts optparse
24
+ puts "[ERROR] No range specified"
25
+ exit 1
26
+ end
27
+
28
+ if in_range == '-'
29
+ in_range = STDIN.readlines.map { |l| l.chomp }.join(',')
30
+ end
31
+
32
+ nodes = range.compress(in_range.split(','))
33
+
34
+ if not nodes.size.zero?
35
+ puts nodes
36
+ else
37
+ puts "[WARNING] No nodes returned"
38
+ exit 1
39
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/ruby
2
+ # given a range and N buckets, slice the range
3
+ # into N buckets, attempting even bucket distribution
4
+ # based on hostname
5
+
6
+ require 'rubygems'
7
+ require 'rangeclient'
8
+
9
+ range_arg = ARGV.shift;
10
+ num_buckets = ARGV.shift;
11
+ num_buckets = num_buckets.to_i
12
+
13
+ r = Range::Client.new
14
+ hosts = r.expand(range_arg)
15
+ hosts.sort!
16
+
17
+ buckets = []
18
+
19
+ # if we asked for more buckets than exist for hosts
20
+ # reduce the number of buckets to be 1:1 with number of hosts
21
+ num_buckets = hosts.length if num_buckets > hosts.length
22
+
23
+ num_buckets.times do |ii|
24
+ buckets << []
25
+ end
26
+
27
+ current_bucket = 0
28
+ hosts.each do |host|
29
+ buckets[current_bucket] << host
30
+ current_bucket += 1
31
+ current_bucket = current_bucket % num_buckets
32
+ end
33
+
34
+ buckets.each do |hosts|
35
+ puts r.compress(hosts)
36
+ end
@@ -0,0 +1,9 @@
1
+ class Range
2
+ # Provide a fake client for use in testing. Ideally a set of tests should be
3
+ # run against both this and the real client to ensure they are sync.
4
+ FakeClient = Struct.new(:responses) do
5
+ def expand(query)
6
+ responses.fetch(query)
7
+ end
8
+ end
9
+ end
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'rest_client'
4
+ require 'net/http'
5
5
  require 'cgi'
6
6
 
7
7
  class Range::Client
8
+ attr_accessor :host, :port, :timeout
8
9
 
9
10
  # used to split hostnames into component parts for compression
10
11
  @@NodeRegx = /
@@ -14,20 +15,25 @@ class Range::Client
14
15
  /x;
15
16
 
16
17
  def initialize(options = {})
17
- default_host = 'range'
18
- default_host = ENV['RANGE_HOST'] if ENV.has_key?('RANGE_HOST')
19
- default_port = '80'
20
- default_port = ENV['RANGE_PORT'] if ENV.has_key?('RANGE_PORT')
21
- @options = {
22
- :host => default_host,
23
- :port => default_port,
24
- }.merge(options)
18
+ @host = 'range'
19
+ @host = ENV['RANGE_HOST'] if ENV.has_key?('RANGE_HOST')
20
+ @host = options[:host] if options.member?(:host)
21
+
22
+ @port = '80'
23
+ @port = ENV['RANGE_PORT'] if ENV.has_key?('RANGE_PORT')
24
+ @port = options[:port] if options.member?(:port)
25
+
26
+ @timeout = 60
27
+ @timeout = options[:timeout] if options.member?(:timeout)
25
28
  end
26
29
 
27
30
  def expand(arg)
28
31
  escaped_arg = CGI.escape arg
29
- res = RestClient.get "http://#{@options[:host]}:#{@options[:port]}/range/list?#{escaped_arg}"
30
- return res.split "\n"
32
+ http = Net::HTTP.new(@host, @port)
33
+ http.read_timeout = @timeout
34
+ req = Net::HTTP::Get.new('/range/list?' + escaped_arg)
35
+ resp = http.request(req)
36
+ return resp.body.split "\n"
31
37
  end
32
38
 
33
39
 
@@ -82,7 +88,7 @@ class Range::Client
82
88
  node.gsub!(/^([a-z]+)(\d+)([a-z]\w+)\./) { "#{$1}#{$2}.UNDOXXX#{$3}." }
83
89
  end
84
90
  result = _simple_compress(nodes)
85
- result.each do |r|
91
+ result.each_char do |r|
86
92
  r.gsub!(/(\d+\.\.\d+)\.UNDOXXX/) {"{#{$1}}"}
87
93
  r.gsub!(/(\d+)\.UNDOXXX/) {"#{$1}"}
88
94
  end
metadata CHANGED
@@ -1,84 +1,55 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rangeclient
3
- version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 7
10
- version: 0.0.7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Evan Miller
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2013-04-01 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rest-client
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
32
- type: :runtime
33
- version_requirements: *id001
34
- description: Use with ranged from https://github.com/ytoolshed/range
35
- email:
11
+ date: 2013-11-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use with range from https://github.com/square/libcrange
14
+ email:
36
15
  - evan@squareup.com
37
- executables:
16
+ executables:
38
17
  - er
18
+ - range-compress
19
+ - range_split
39
20
  extensions: []
40
-
41
- extra_rdoc_files:
21
+ extra_rdoc_files:
42
22
  - LICENSE.md
43
- files:
23
+ files:
24
+ - lib/range/fake_client.rb
44
25
  - lib/rangeclient.rb
45
26
  - bin/er
27
+ - bin/range-compress
28
+ - bin/range_split
46
29
  - README.md
47
30
  - LICENSE.md
48
31
  homepage: https://github.com/square/prodeng/tree/master/ruby_rangeclient
49
32
  licenses: []
50
-
33
+ metadata: {}
51
34
  post_install_message:
52
- rdoc_options:
35
+ rdoc_options:
53
36
  - --charset=UTF-8
54
- require_paths:
37
+ require_paths:
55
38
  - lib
56
- required_ruby_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
65
- required_rubygems_version: !ruby/object:Gem::Requirement
66
- none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 23
71
- segments:
72
- - 1
73
- - 3
74
- - 6
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
75
48
  version: 1.3.6
76
49
  requirements: []
77
-
78
50
  rubyforge_project:
79
- rubygems_version: 1.8.24
51
+ rubygems_version: 2.1.9
80
52
  signing_key:
81
- specification_version: 3
82
- summary: Simple ranged client for ruby.
53
+ specification_version: 4
54
+ summary: Simple range client for ruby.
83
55
  test_files: []
84
-