desi 0.2.0 → 0.2.1

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.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Desi
1
+ Desi
2
+ ====
3
+ [![Build Status](https://secure.travis-ci.org/AF83/desi.png)](http://travis-ci.org/AF83/desi)
2
4
 
3
5
  Desi (Developper ElasticSearch Installer) is very simple tool to quickly set up
4
6
  an [Elastic Search](http://www.elasticsearch.org/) local install for
@@ -51,6 +53,24 @@ will be spun up by (`desi start`)
51
53
  Desi::LocalInstall.new.releases.detect(&:current?).version #=> "0.19.9"
52
54
  ```
53
55
 
56
+ ### Start a node instance and get the cluster's status
57
+
58
+ * command-line
59
+
60
+ ```shell
61
+ $ desi start
62
+ * Elastic Search 0.19.9 started
63
+ $ desi status
64
+ OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status yellow
65
+ ```
66
+
67
+
68
+ * library
69
+
70
+ ```ruby
71
+ Desi::ProcessManager.new.start.status #=> "OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status green"
72
+ ```
73
+
54
74
 
55
75
  ### List and delete some indices
56
76
 
@@ -65,7 +85,7 @@ will be spun up by (`desi start`)
65
85
  bar
66
86
  baz
67
87
 
68
- $ # List all indices on remote cluster 129.168.1.42, reachable on port 9800
88
+ $ # List all indices with "foo" in their name on remote cluster 129.168.1.42, port 9800
69
89
  $ desi indices --host 129.168.1.42:9800 foo
70
90
  Indices from host http://192.168.1.42:9800 matching the pattern /foo/
71
91
 
@@ -86,16 +106,16 @@ will be spun up by (`desi start`)
86
106
  # All local indices
87
107
  Desi::IndexManager.new.list #=> ["foo", "bar", "baz"]
88
108
 
89
- # All local indices whose name starts with `b`
109
+ # All local indices whose name starts with "b"
90
110
  Desi::IndexManager.new.list("^b") #=> ["bar", "baz"]
91
111
 
92
112
  # All indices from distant cluster
93
113
  Desi::IndexManager.new(host: "192.168.1.42:9800").list #=> ["remotefoo1", "remotefoo2"]
94
114
 
95
- # Delete all local indices whose name starts with `ba`
115
+ # Delete all local indices whose name starts with "ba"
96
116
  Desi::IndexManager.new.delete!("^ba") #=> nil
97
117
 
98
- # The indices actually disappeared
118
+ # The indices actually disappeared! \o/
99
119
  Desi::IndexManager.new.list #=> ["foo"]
100
120
  ```
101
121
 
data/Rakefile CHANGED
@@ -2,3 +2,6 @@
2
2
  require "bundler/gem_tasks"
3
3
 
4
4
  FileList['tasks/**/*.rake'].each { |task| import task }
5
+
6
+ desc 'Default: run all specs'
7
+ task :default => :spec
@@ -20,7 +20,7 @@ module Desi
20
20
  #
21
21
  # @note The +:http_client_factory+ should return an instance that responds
22
22
  # to #get and #delete
23
- # @return [undefined]
23
+ # @return [void]
24
24
  #
25
25
  # @api public
26
26
  def initialize(opts = {})
@@ -60,7 +60,7 @@ module Desi
60
60
  # Delete all indices matching the specified pattern
61
61
  #
62
62
  # @param [#to_s] pattern Regexp pattern used to restrict the selection
63
- # @return [undefined]
63
+ # @return [void]
64
64
  #
65
65
  # @note No confirmation is needed, so beware!
66
66
  #
@@ -77,7 +77,7 @@ module Desi
77
77
  @outputter.puts "The following indices from host #{@host} are now deleted" if @verbose
78
78
 
79
79
  indices(Regexp.new(pattern)).each do |index|
80
- @client.delete(index)
80
+ @client.delete("/#{index}")
81
81
  @outputter.puts " * #{index}" if @verbose
82
82
  end
83
83
  end
@@ -85,7 +85,7 @@ module Desi
85
85
  # Empty (remove all records) from indices matching the specified pattern
86
86
  #
87
87
  # @param [#to_s] pattern Regexp pattern used to restrict the selection
88
- # @return [undefined]
88
+ # @return [void]
89
89
  #
90
90
  # @note No confirmation is needed, so beware!
91
91
  #
@@ -102,7 +102,7 @@ module Desi
102
102
  @outputter.puts "The following indices from host #{@host} are now emptied" if @verbose
103
103
 
104
104
  indices(Regexp.new(pattern)).each do |index|
105
- @client.delete("#{index}/_query?q=*")
105
+ @client.delete("/#{index}/_query?q=*")
106
106
  @outputter.puts " * #{index}" if @verbose
107
107
  end
108
108
  end
@@ -110,7 +110,7 @@ module Desi
110
110
  private
111
111
 
112
112
  def indices(pattern)
113
- JSON.parse(@client.get('_status').body)["indices"].keys.select {|i|
113
+ JSON.parse(@client.get('/_status').body)["indices"].keys.select {|i|
114
114
  i =~ pattern
115
115
  }
116
116
  end
@@ -7,6 +7,19 @@ require "desi/http_client"
7
7
  require "desi/local_install"
8
8
 
9
9
  module Desi
10
+
11
+ # The ProcessManager will start, stop and restart a local Elastic Search node
12
+ # instance, in addition to reporting its status
13
+ #
14
+ # @example Start up the instance and check its status
15
+ # Desi::ProcessManager.new.start.status #=> "OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status green"
16
+ #
17
+ # @example Retrieve the currently running cluster's version
18
+ # Desi::ProcessManager.new.running_version #=> "0.19.9"
19
+ #
20
+ # @example Retrieve a distant cluster's version
21
+ # Desi::ProcessManager.new(host: "http://somewhere.com:9200").running_version #=> "0.18.5"
22
+ #
10
23
  class ProcessManager
11
24
 
12
25
  def initialize(opts = {})
@@ -16,6 +29,16 @@ module Desi
16
29
  @client = opts.fetch(:http_client_factory, Desi::HttpClient).new(@host)
17
30
  end
18
31
 
32
+ # Start the cluster
33
+ #
34
+ # This will be a no-op if the cluster is already started.
35
+ #
36
+ # @note This method will also output its result on STDOUT if +@verbose+ is
37
+ # true
38
+ #
39
+ # @return [self]
40
+ #
41
+ # @api public
19
42
  def start
20
43
  if cluster_ready?
21
44
  puts "ES cluster is already running" if @verbose
@@ -23,15 +46,39 @@ module Desi
23
46
  start_cluster
24
47
  puts " * Elastic Search #{running_version} started" if @verbose
25
48
  end
26
- end
27
-
49
+ self
50
+ end
51
+
52
+ # Restart the cluster
53
+ #
54
+ # Stop the cluster (if its up) and start it again.
55
+ #
56
+ # @note We use the pidfile to determine if the cluster is up. If a node was
57
+ # started with another tool, you may end up with more than 1 node
58
+ # running.
59
+ #
60
+ # @note This method will also output its result on STDOUT if +@verbose+ is
61
+ # true
62
+ #
63
+ # @return [self]
64
+ #
65
+ # @api public
28
66
  def restart
29
67
  puts " * (Re)starting cluster" if @verbose
30
68
  stop if has_pid?
31
69
  start_cluster
32
70
  puts " * Elastic Search #{running_version} started" if @verbose
71
+ self
33
72
  end
34
73
 
74
+ # Stop the cluster
75
+ #
76
+ # @note This method will also output its result on STDOUT if +@verbose+ is
77
+ # true
78
+ #
79
+ # @return [self]
80
+ #
81
+ # @api public
35
82
  def stop
36
83
  if pid
37
84
  puts " * Will stop instance with pid #{pid}" if @verbose
@@ -39,13 +86,19 @@ module Desi
39
86
  else
40
87
  puts " * No pidfile detected!. Won't stop" if @verbose
41
88
  end
42
- end
43
-
44
- def restart
45
- stop
46
- start
47
- end
48
-
89
+ self
90
+ end
91
+
92
+ # Get information about the cluster's status
93
+ #
94
+ # Desi::ProcessManager.new.status #=> "OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status green"
95
+ #
96
+ # @note This method will also output its result on STDOUT if +@verbose+ is
97
+ # true
98
+ #
99
+ # @return [String]
100
+ #
101
+ # @api public
49
102
  def status
50
103
  if version = running_version
51
104
  msg = "OK. Elastic Search cluster '#{cluster_health.cluster_name}' (v#{version}) is running on #{cluster_health.number_of_nodes} node(s) with status #{cluster_health.status}"
@@ -56,14 +109,27 @@ module Desi
56
109
  msg
57
110
  end
58
111
 
112
+ # Whether the pidfile actually holds a PID
113
+ #
114
+ # @return [Boolean]
59
115
  def has_pid?
60
116
  pid && !pid.empty?
61
117
  end
62
118
 
119
+ # PID as retrieved from the pidfile
120
+ #
121
+ # @return [String]
63
122
  def pid
64
123
  @pid ||= File.read(pidfile) if pidfile.exist?
65
124
  end
66
125
 
126
+ # Release number of the currently running cluster
127
+ #
128
+ # @example
129
+ # Desi::ProcessManager.new.running_version #=> "0.19.9"
130
+ #
131
+ #
132
+ # @return [String,nil]
67
133
  def running_version
68
134
  begin
69
135
  JSON.parse(@client.get('/').body)["version"]["number"]
@@ -72,6 +138,21 @@ module Desi
72
138
  end
73
139
  end
74
140
 
141
+ protected
142
+
143
+ # Return cluster health data straight from the cluster
144
+ #
145
+ # see
146
+ # http://www.elasticsearch.org/guide/reference/api/admin-cluster-health.html
147
+ # for further information on the response's structure
148
+ #
149
+ # @return [Hash]
150
+ def cluster_health
151
+ @cluster_health ||= OpenStruct.new(JSON.parse(@client.get('/_cluster/health').body))
152
+ end
153
+
154
+ private
155
+
75
156
  def wait_until_cluster_becomes_ready(max_wait = 10, step = 0.5)
76
157
  wait_for(max_wait, step) { cluster_ready? }
77
158
  end
@@ -80,8 +161,6 @@ module Desi
80
161
  wait_for(max_wait, step) { !cluster_ready? }
81
162
  end
82
163
 
83
- private
84
-
85
164
  def start_cluster
86
165
  line = Cocaine::CommandLine.new(@local_install.launcher.to_s, "-p :pidfile", pidfile: pidfile.to_s)
87
166
  line.run
@@ -124,9 +203,6 @@ module Desi
124
203
  delay < max_wait
125
204
  end
126
205
 
127
- def cluster_health
128
- @cluster_health ||= OpenStruct.new(JSON.parse(@client.get('/_cluster/health').body))
129
- end
130
206
 
131
207
  end
132
208
  end
data/lib/desi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Desi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -18,7 +18,7 @@ describe Desi::IndexManager do
18
18
  end
19
19
 
20
20
  def stub_indices(*names)
21
- stub_request(:get, '_status', {"indices" => Hash[Array(names).zip]})
21
+ stub_request(:get, '/_status', {"indices" => Hash[Array(names).zip]})
22
22
  end
23
23
 
24
24
  before do
@@ -63,7 +63,7 @@ describe Desi::IndexManager do
63
63
  end
64
64
 
65
65
  it "deletes all matching indices" do
66
- http_client.should_receive(:delete).with("foo")
66
+ http_client.should_receive(:delete).with("/foo")
67
67
  http_client.should_not_receive(:delete).with("bar")
68
68
 
69
69
  subject.delete!('f.*')
@@ -96,7 +96,7 @@ describe Desi::IndexManager do
96
96
  end
97
97
 
98
98
  it "deletes all matching indices" do
99
- http_client.should_receive(:delete).with("foo/_query?q=*")
99
+ http_client.should_receive(:delete).with("/foo/_query?q=*")
100
100
  http_client.should_not_receive(:delete).with("bar")
101
101
 
102
102
  subject.empty!('f.*')
data/tasks/spec.rake ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = %w[--color]
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: desi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: boson
@@ -168,6 +168,7 @@ extra_rdoc_files: []
168
168
  files:
169
169
  - .gitignore
170
170
  - .rspec
171
+ - .travis.yml
171
172
  - .yardopts
172
173
  - Gemfile
173
174
  - Guardfile
@@ -189,6 +190,7 @@ files:
189
190
  - lib/desi/version.rb
190
191
  - spec/desi/index_manager_spec.rb
191
192
  - spec/spec_helper.rb
193
+ - tasks/spec.rake
192
194
  - tasks/yard.rake
193
195
  homepage: https://github.com/AF83/desi/
194
196
  licenses: []
@@ -204,7 +206,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
204
206
  version: '0'
205
207
  segments:
206
208
  - 0
207
- hash: 1725098947325688636
209
+ hash: 3454323007955509059
208
210
  required_rubygems_version: !ruby/object:Gem::Requirement
209
211
  none: false
210
212
  requirements:
@@ -213,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
215
  version: '0'
214
216
  segments:
215
217
  - 0
216
- hash: 1725098947325688636
218
+ hash: 3454323007955509059
217
219
  requirements: []
218
220
  rubyforge_project:
219
221
  rubygems_version: 1.8.24