ddslbg 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --fail-fast
@@ -0,0 +1,10 @@
1
+ guard 'rspec', all_after_pass: false do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch('spec/spec_helper.rb') { 'spec' }
4
+
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/ddslbg/(.+)\.rb$}) { |m| "spec/ddslbg/#{m[1]}_spec.rb" }
7
+ watch('lib/ddslbg.rb') { 'spec' }
8
+ watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
9
+ watch('spec/spec_helper.rb') { 'spec' }
10
+ end
data/README.md CHANGED
@@ -11,21 +11,68 @@ Just a proof of concept.
11
11
  gem install ddslbg
12
12
  ```
13
13
 
14
+ Note that this gem spawns a Java process running a standalone jar, so a working
15
+ JRE is required.
16
+
14
17
  ## Usage
15
18
 
19
+ Please see the [main DDSL documentation](https://github.com/mbknor/ddsl) for
20
+ concepts and details.
21
+
22
+ ### Initialize the client
23
+
16
24
  ```ruby
17
25
  require 'ddslbg'
18
-
19
26
  $ddsl = Ddslbg::Client.new
27
+ ```
28
+
29
+ ### Set a list of zookeepers to use
30
+
31
+ ```ruby
32
+ $ddsl.zookeeper_hosts = ['localhost:2181', 'some.other.server.com:2181']
33
+ ```
34
+
35
+ ### List available services
36
+
37
+ ```ruby
38
+ $ddsl.available_services
39
+ ```
40
+
41
+ ### Toggle service state
20
42
 
43
+ ```ruby
21
44
  service = {
22
45
  id: {environment: 'test', serviceType: 'http', name: 'cmd-tool', version: '0.1'},
23
46
  sl: {url: 'http://localhost:4321/hi', quality: 1.0, lastUpdated: 1347398923243, ip: '127.0.0.1'}
24
47
  }
25
48
 
26
- $ddsl.up(service) # Register the service, uses ddsl serviceUp
49
+ $ddsl.up(service)
50
+ $ddsl.down(service)
51
+ ```
27
52
 
28
- $ddsl.available_services # List all available services, uses ddsl getAllAvailableServices
53
+ ### Get service locations
29
54
 
30
- $ddsl.down(service) # Explicitly deregister the service, uses ddsl serviceDown
55
+ ```ruby
56
+ service_request = {
57
+ 'sid' => {'environment' => 'test', 'serviceType' => 'telnet', 'name' => 'telnetServer', 'version' => '0.1'},
58
+ 'cid' => {'environment' => 'Client env', 'name' => 'client name', 'version' => 'version', 'ip' => 'ip-address'}
59
+ }
60
+
61
+ $ddsl.best_service_location(service_request)
62
+ $ddsl.service_locations(service_request)
31
63
  ```
64
+
65
+ ### Set local fallbacks
66
+
67
+ ```ruby
68
+ fallbacks = {
69
+ 'ServiceId(test,telnet,telnetServer,0.1)' => 'http://example.com/foo',
70
+ 'ServiceId(test,http,BarServer,1.0)' => 'http://example.com/bar'
71
+ }
72
+
73
+ $ddsl.fallback_urls = fallbacks
74
+ ```
75
+
76
+ ### Cleanly disconnect
77
+
78
+ Use `$ddsl.disconnect!` to kill off the spawned process.
@@ -18,4 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ['lib']
19
19
 
20
20
  gem.add_development_dependency 'awesome_print'
21
+ gem.add_development_dependency 'guard-rspec'
22
+ gem.add_development_dependency 'rb-fsevent'
21
23
  end
@@ -1,5 +1,2 @@
1
- require 'open3'
2
- require 'json'
3
-
4
1
  require 'ddslbg/version'
5
2
  require 'ddslbg/client'
@@ -1,3 +1,8 @@
1
+ require 'open3'
2
+ require 'json'
3
+
4
+ require 'ddslbg/error'
5
+
1
6
  module Ddslbg
2
7
  class Client
3
8
  BUNDLED_JAR_PATH = File.expand_path(
@@ -9,11 +14,12 @@ module Ddslbg
9
14
  def initialize(options={})
10
15
  default_opts = {
11
16
  java_cmd: 'java -jar',
12
- jar_path: BUNDLED_JAR_PATH
17
+ jar_path: BUNDLED_JAR_PATH,
18
+ auto_connect: false
13
19
  }
14
20
  @options = default_opts.merge(options)
15
21
 
16
- connect!
22
+ connect! if @options[:auto_connect]
17
23
  end
18
24
 
19
25
  def available_services
@@ -28,29 +34,65 @@ module Ddslbg
28
34
  send('serviceDown', service)
29
35
  end
30
36
 
31
- # Send a raw command to DDSL.
32
- #
37
+ def best_service_location(service_request)
38
+ send('getBestServiceLocation', service_request)
39
+ end
40
+
41
+ def service_locations(service_request)
42
+ send('getServiceLocations', service_request)
43
+ end
44
+
45
+ def fallback_urls=(fallback_urls)
46
+ send('setFallbackUrlsMap', fallback_urls, parse: false)
47
+ end
48
+
49
+ def zookeeper_hosts=(hosts=[])
50
+ send('setZookeeperHosts', hosts, parse: false)
51
+ end
52
+
33
53
  # `data` can be a Ruby array or hash. It is converted to JSON before it is
34
54
  # sent to DDSL.
35
- def send(msg, data=nil)
36
- cmd = msg
37
- cmd += " #{data.to_json}" if data
38
- @stdin.puts(cmd)
39
- parse(process_line(@stdout.gets))
55
+ def send(msg, data={}, options={})
56
+ default_opts = { parse: true }
57
+ options = default_opts.merge(options)
58
+ msg += " #{data.to_json}" unless !data || data.empty?
59
+ res = process_line(do_send(msg))
60
+ res = parse(res) if options[:parse]
61
+ res
40
62
  end
41
63
 
42
- private
64
+ # Sends a raw command to DDSL and returns the response as a unprocessed string.
65
+ def do_send(cmd)
66
+ connect! unless connected?
67
+ @stdin.puts(cmd)
68
+ @stdout.gets
69
+ end
43
70
 
44
71
  def connect!
45
72
  cmd = [options[:java_cmd], options[:jar_path]].join(' ')
46
- @stdin, @stdout, @stderr = Open3.popen3(cmd)
73
+ @stdin, @stdout, @wait_thr = Open3.popen2(cmd)
74
+
75
+ process_line(@stdout.gets).include?('ddsl-cmdline-tool started')
76
+ end
77
+
78
+ def disconnect!
79
+ return false unless connected?
80
+ @stdin.close unless @stdin.closed?
81
+ @stdout.close unless @stdout.closed?
82
+ Process.kill(:INT, @wait_thr.pid)
83
+ true
84
+ end
47
85
 
48
- process_line(@stdout.gets)
86
+ def connected?
87
+ !!@wait_thr && @wait_thr.alive?
49
88
  end
50
89
 
90
+ private
91
+
51
92
  def process_line(line='')
52
93
  status, data = line.scan(/^([a-z]+) (.+)/).flatten
53
- raise StandardError.new("[ddslbg] #{line}") unless status == 'ok'
94
+ data.chomp!
95
+ raise Error.new(data) unless status == 'ok'
54
96
  data
55
97
  end
56
98
 
@@ -0,0 +1,4 @@
1
+ module Ddslbg
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Ddslbg
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ require 'ddslbg/client'
4
+
5
+ module Ddslbg
6
+ describe Client do
7
+ subject { Client.new(auto_connect: false) }
8
+
9
+ it 'should raise if it receives an error' do
10
+ response = "error java.net.UnknownHostException: some.other.server.com\n"
11
+ subject.should_receive(:do_send).and_return(response)
12
+
13
+ begin
14
+ subject.send('any message')
15
+ rescue Ddslbg::Error => e
16
+ e.to_s.should == 'java.net.UnknownHostException: some.other.server.com'
17
+ end
18
+ end
19
+
20
+ describe '#available_services' do
21
+ specify do
22
+ subject.should_receive(:do_send).with('getAllAvailableServices').and_return("ok []\n")
23
+ subject.available_services.should == []
24
+ end
25
+ end
26
+
27
+ describe '#up' do
28
+ specify do
29
+ service = {
30
+ id: {environment: 'test', serviceType: 'http', name: 'cmd-tool', version: '0.1'},
31
+ sl: {url: 'http://localhost:4321/hi', quality: 1.0, lastUpdated: 1347398923243, ip: '127.0.0.1'}
32
+ }
33
+ subject.should_receive(:do_send).with("serviceUp #{service.to_json}").and_return("ok true\n")
34
+ subject.up(service).should be_true
35
+ end
36
+ end
37
+
38
+ describe '#fallback_urls=' do
39
+ specify do
40
+ fallbacks = {
41
+ 'ServiceId(test,telnet,telnetServer,0.1)' => 'http://example.com/foo',
42
+ 'ServiceId(test,http,BarServer,1.0)' => 'http://example.com/bar'
43
+ }
44
+
45
+ response = "ok fallbackUrls-map has been updated: {\"ServiceId(test,telnet,telnetServer,0.1)\":\"http://example.com/foo\",\"ServiceId(test,http,BarServer,1.0)\":\"http://example.com/bar\"}\n"
46
+
47
+ subject.should_receive(:do_send)
48
+ .with("setFallbackUrlsMap #{fallbacks.to_json}")
49
+ .and_return(response)
50
+
51
+ subject.fallback_urls = fallbacks
52
+ end
53
+ end
54
+
55
+ describe '#zookeeper_hosts=' do
56
+ specify do
57
+ hosts = ['localhost:2181', 'some.other.server.com:2181']
58
+ response = "ok ZookeeperHosts-list has been configured\n"
59
+
60
+ subject.should_receive(:do_send)
61
+ .with("setZookeeperHosts #{hosts.to_json}")
62
+ .and_return(response)
63
+
64
+ subject.zookeeper_hosts = hosts
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,13 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ end
6
+ end
7
+
8
+ require 'bundler/setup'
9
+ require 'awesome_print'
10
+
11
+ # Requires supporting ruby files with custom matchers and macros, etc,
12
+ # in spec/support/ and its subdirectories.
13
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ # Annotate specs with focus: true for focused runs
3
+ config.treat_symbols_as_metadata_keys_with_true_values = true
4
+ config.filter_run focus: true
5
+ config.run_all_when_everything_filtered = true
6
+ config.order = 'random'
7
+ end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: ddslbg
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bjørn Arild Mæland
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-16 00:00:00.000000000 Z
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  prerelease: false
@@ -27,6 +27,38 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
29
  none: false
30
+ - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ type: :development
39
+ name: guard-rspec
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ type: :development
55
+ name: rb-fsevent
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
30
62
  description: This library spawns a child process which communicates with the DDSL
31
63
  command line tool
32
64
  email:
@@ -36,14 +68,20 @@ extensions: []
36
68
  extra_rdoc_files: []
37
69
  files:
38
70
  - .gitignore
71
+ - .rspec
39
72
  - Gemfile
73
+ - Guardfile
40
74
  - LICENSE.txt
41
75
  - README.md
42
76
  - Rakefile
43
77
  - ddslbg.gemspec
44
78
  - lib/ddslbg.rb
45
79
  - lib/ddslbg/client.rb
80
+ - lib/ddslbg/error.rb
46
81
  - lib/ddslbg/version.rb
82
+ - spec/ddslbg/client_spec.rb
83
+ - spec/spec_helper.rb
84
+ - spec/support/rspec_config.rb
47
85
  - vendor/bin/ddsl-cmdline-tool_2.10-0.3.5-SNAPSHOT-one-jar.jar
48
86
  homepage: https://github.com/bmaland/ddslbg
49
87
  licenses: []
@@ -69,4 +107,7 @@ rubygems_version: 1.8.25
69
107
  signing_key:
70
108
  specification_version: 3
71
109
  summary: Ruby client which wraps ddsl-cmdline-tool
72
- test_files: []
110
+ test_files:
111
+ - spec/ddslbg/client_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/support/rspec_config.rb