leo_manager_client 0.4.7

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/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in leo_manager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,178 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # leo_manager_client
2
+
3
+ The library of LeoFS-Manager's client for Ruby
4
+
5
+ ## Installation
6
+
7
+ $ gem install leo_manager_client
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ require "leo_manager_client"
13
+
14
+ manager = LeoManager::Client.new("localhost:10020")
15
+ manager.status #=> #<LeoManager::Status:0x00000001578310 @system_info=#<LeoManager::Status::SystemInfo:0x000000015782e8 @version="0.10.1", @n="1", @r="1", @w="1", @d="1", @ring_size="128", @ring_cur=nil, @ring_prev=nil>, @node_list=[#<LeoManager::Status::NodeInfo:0x00000001578298 @type="S", @node="storage_0@127.0.0.1", @state="running", @ring_cur="a039acc0", @ring_prev="a039acc0", @joined_at="2012-09-21 15:08:22 +0900">, #<LeoManager::Status::NodeInfo:0x00000001578248 @type="G", @node="gateway_0@127.0.0.1", @state="running", @ring_cur="a039acc0", @ring_prev="a039acc0", @joined_at="2012-09-21 15:08:25 +0900">]>
16
+ ```
17
+
18
+ ## Contributing
19
+
20
+ 1. Fork it
21
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
22
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
23
+ 4. Push to the branch (`git push origin my-new-feature`)
24
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # ======================================================================
2
+ #
3
+ # LeoFS Manager Client
4
+ #
5
+ # Copyright (c) 2012-2013 Rakuten, Inc.
6
+ #
7
+ # This file is provided to you under the Apache License,
8
+ # Version 2.0 (the "License"); you may not use this file
9
+ # except in compliance with the License. You may obtain
10
+ # a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing,
15
+ # software distributed under the License is distributed on an
16
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ # KIND, either express or implied. See the License for the
18
+ # specific language governing permissions and limitations
19
+ # under the License.
20
+ #
21
+ # ======================================================================
22
+ require 'rubygems'
23
+ require 'rake'
24
+ require "bundler/gem_tasks"
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+
28
+ task :default => :spec
29
+
30
+ desc "Run all specs in spec directory"
31
+ RSpec::Core::RakeTask.new(:spec) do |t|
32
+ t.rspec_opts = "-c -f d --tty"
33
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "leo_manager_client"
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "leo_manager_client"
8
+ gem.version = LeoManager::VERSION
9
+ gem.authors = ["Yosuke Hara"]
10
+ gem.email = ["leofaststorage@gmail.com"]
11
+ gem.description = %q{Client for LeoFS-Manager}
12
+ gem.summary = %q{Client for LeoFS-Manager}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ end
@@ -0,0 +1,418 @@
1
+ # ======================================================================
2
+ #
3
+ # LeoFS Manager Client
4
+ #
5
+ # Copyright (c) 2012 Rakuten, Inc.
6
+ #
7
+ # This file is provided to you under the Apache License,
8
+ # Version 2.0 (the "License"); you may not use this file
9
+ # except in compliance with the License. You may obtain
10
+ # a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing,
15
+ # software distributed under the License is distributed on an
16
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ # KIND, either express or implied. See the License for the
18
+ # specific language governing permissions and limitations
19
+ # under the License.
20
+ #
21
+ # ======================================================================
22
+ require "socket"
23
+ require "json"
24
+ require "time"
25
+
26
+ require_relative "leo_manager_models"
27
+
28
+ module LeoManager
29
+ VERSION = "0.4.7"
30
+
31
+ class Client
32
+ CMD_VERSION = "version"
33
+ CMD_LOGIN = "login %s %s"
34
+ CMD_STATUS = "status %s"
35
+ CMD_START = "start"
36
+ CMD_DETACH = "detach %s"
37
+ CMD_SUSPEND = "suspend %s"
38
+ CMD_RESUME = "resume %s"
39
+ CMD_REBALANCE = "rebalance"
40
+ CMD_WHEREIS = "whereis %s"
41
+ CMD_DU = "du %s"
42
+ CMD_COMPACT_START = "compact start %s %s %s"
43
+ CMD_COMPACT_START_ALL = "compact start %s all"
44
+ CMD_COMPACT_SUSPEND = "compact suspend %s"
45
+ CMD_COMPACT_RESUME = "compact resume %s"
46
+ CMD_COMPACT_STATUS = "compact status %s"
47
+ CMD_PURGE = "purge %s"
48
+ CMD_CRE_USER = "create-user %s %s"
49
+ CMD_UPD_USER_ROLE = "update-user-role %s %s"
50
+ CMD_UPD_USER_PASS = "update-user-password %s %s"
51
+ CMD_DEL_USER = "delete-user %s"
52
+ CMD_GET_USERS = "get-users"
53
+ CMD_SET_ENDPOINT = "set-endpoint %s"
54
+ CMD_DEL_ENDPOINT = "delete-endpoint %s"
55
+ CMD_GET_ENDPOINTS = "get-endpoints"
56
+ CMD_ADD_BUCKET = "add-bucket %s %s"
57
+ CMD_DELETE_BUCKET = "delete-bucket %s %s"
58
+ CMD_GET_BUCKETS = "get-buckets"
59
+ CMD_RECOVER_FILE = "recover file %s"
60
+ CMD_RECOVER_NODE = "recover node %s"
61
+ CMD_RECOVER_RING = "recover ring %s"
62
+
63
+ USER_ROLES = RoleDef.invert
64
+
65
+ # ======================================================================
66
+ # APIs
67
+ # ======================================================================
68
+ # Constructor
69
+ #
70
+ def initialize(*servers)
71
+ @servers = parse_servers(servers)
72
+ set_current_server
73
+ @mutex = Mutex.new
74
+ connect
75
+ end
76
+
77
+ # servers to connect
78
+ attr_reader :servers
79
+ # the server currently connected
80
+ attr_reader :current_server
81
+
82
+ # Retrieve LeoFS's version from LeoFS Manager
83
+ # Return::
84
+ # Version of LeoFS
85
+ def version
86
+ h = sender(CMD_VERSION)
87
+ return h[:result]
88
+ end
89
+
90
+ # Retrieve LeoFS's system status from LeoFS Manager
91
+ # Return::
92
+ # Status
93
+ def status(node=nil)
94
+ Status.new(sender(CMD_STATUS % node))
95
+ end
96
+
97
+ # Login as specifies user
98
+ # Return::
99
+ # LoginInfo
100
+ def login(user_id, password)
101
+ LoginInfo.new(sender(CMD_LOGIN % [user_id, password]))
102
+ end
103
+
104
+ # Launch LeoFS's storage cluster
105
+ # Return::
106
+ # _nil_
107
+ def start
108
+ sender(CMD_START)
109
+ nil
110
+ end
111
+
112
+ # Leave a node from the storage cluster
113
+ # Return::
114
+ # _nil_
115
+ def detach(node)
116
+ sender(CMD_DETACH % node)
117
+ nil
118
+ end
119
+
120
+ # Suspend a node in the storage cluster
121
+ # Return::
122
+ # _nil_
123
+ def suspend(node)
124
+ sender(CMD_SUSPEND % node)
125
+ nil
126
+ end
127
+
128
+ # Resume a node in the storage cluster
129
+ # Return::
130
+ # _nil_
131
+ def resume(node)
132
+ sender(CMD_RESUME % node)
133
+ nil
134
+ end
135
+
136
+ # Execute 'rebalance' in the storage cluster
137
+ # Return::
138
+ # _nil_
139
+ def rebalance
140
+ sender(CMD_REBALANCE)
141
+ nil
142
+ end
143
+
144
+ # Retrieve assigned file information
145
+ # Return::
146
+ # Array of AssignedFile
147
+ def whereis(path)
148
+ assigned_info = sender(CMD_WHEREIS % path)[:assigned_info]
149
+ assigned_info.map {|h| AssignedFile.new(h)}
150
+ end
151
+
152
+ # Retrieve storage status from the storage
153
+ # Return::
154
+ # StorageStat
155
+ def du(node)
156
+ StorageStat.new(sender(CMD_DU % node))
157
+ end
158
+
159
+ # Execute 'compact start'
160
+ # Return::
161
+ # _nil_
162
+ def compact_start(node, num_of_targets_or_all, num_of_concurrents=nil)
163
+ case num_of_targets_or_all.to_s
164
+ when /^all$/i
165
+ sender(CMD_COMPACT_START_ALL % node)
166
+ else
167
+ num_of_concurrents = num_of_concurrents ? Integer(num_of_concurrents) : ""
168
+ sender(CMD_COMPACT_START % [node, Integer(num_of_targets_or_all), num_of_concurrents])
169
+ end
170
+ nil
171
+ end
172
+
173
+ # Execute 'compact suspend'
174
+ # Return::
175
+ # _nil_
176
+ def compact_suspend(node)
177
+ sender(CMD_COMPACT_SUSPEND % node)
178
+ nil
179
+ end
180
+
181
+ # Execute 'compact suspend'
182
+ # Return::
183
+ # _nil_
184
+ def compact_resume(node)
185
+ sender(CMD_COMPACT_RESUME % node)
186
+ nil
187
+ end
188
+
189
+ # Execute 'compact status'
190
+ # Return::
191
+ # _nil_
192
+ def compact_status(node)
193
+ compaction = sender(CMD_COMPACT_STATUS % node)[:compaction_status]
194
+ CompactionStatus.new(compaction)
195
+ end
196
+
197
+ # Purge a cache in gateways
198
+ # Return::
199
+ # _nil_
200
+ def purge(path)
201
+ sender(CMD_PURGE % path)
202
+ nil
203
+ end
204
+
205
+ # Generate credential for LeoFS
206
+ # Return::
207
+ # Credential
208
+ def create_user(user_id, password=nil)
209
+ Credential.new(sender(CMD_CRE_USER % [user_id, password]))
210
+ end
211
+
212
+ # Update user role
213
+ # Return ::
214
+ # _nil_
215
+ def update_user_role(user_id, role)
216
+ role = role.to_sym if role.is_a? String
217
+ role = USER_ROLES[role] if role.is_a? Symbol
218
+ sender(CMD_UPD_USER_ROLE % [user_id, role])
219
+ nil
220
+ end
221
+
222
+ # Update user password
223
+ # Return::
224
+ # _nil_
225
+ def update_user_password(user_id, new_password)
226
+ sender(CMD_UPD_USER_PASS % [user_id, new_password])
227
+ nil
228
+ end
229
+
230
+ # Delete user
231
+ # Return::
232
+ # _nil_
233
+ def delete_user(user_id)
234
+ sender(CMD_DEL_USER % user_id)
235
+ nil
236
+ end
237
+
238
+ def get_users
239
+ users = sender(CMD_GET_USERS)[:users]
240
+ users.map {|account| User.new(account) }
241
+ end
242
+
243
+ # Insert an endpoint in the system
244
+ # Return::
245
+ # _nil_
246
+ def set_endpoint(endpoint)
247
+ sender(CMD_SET_ENDPOINT % endpoint)
248
+ nil
249
+ end
250
+
251
+ # Remove an endpoint from the system
252
+ # Return::
253
+ # _nil_
254
+ def delete_endpoint(endpoint)
255
+ sender(CMD_DEL_ENDPOINT % endpoint)
256
+ nil
257
+ end
258
+ alias :del_endpoint :delete_endpoint
259
+
260
+ # Retrieve an endpoint in the system
261
+ # Return::
262
+ # Array of Endpoint
263
+ def get_endpoints
264
+ endpoints = sender(CMD_GET_ENDPOINTS)[:endpoints]
265
+ endpoints.map {|endpoint| Endpoint.new(endpoint) }
266
+ end
267
+
268
+ # Add an Bucket in the system
269
+ # Return::
270
+ # _nil_
271
+ def add_bucket(bucket_name, access_key_id)
272
+ sender(CMD_ADD_BUCKET % [bucket_name, access_key_id])
273
+ nil
274
+ end
275
+
276
+ # Delete an Bucket in the system
277
+ # Return::
278
+ # _nil_
279
+ def delete_bucket(bucket_name, access_key_id)
280
+ sender(CMD_DELETE_BUCKET % [bucket_name, access_key_id])
281
+ nil
282
+ end
283
+
284
+ # Retrieve all buckets from the system
285
+ # Return::
286
+ # Array of Bucket
287
+ def get_buckets
288
+ buckets = sender(CMD_GET_BUCKETS)[:buckets]
289
+ buckets.map {|bucket| Bucket.new(bucket) }
290
+ end
291
+
292
+ # Recover file
293
+ # Return::
294
+ # nil
295
+ def recover_file(path)
296
+ sender(CMD_RECOVER_FILE % path)
297
+ nil
298
+ end
299
+
300
+ # Recover node
301
+ # Return::
302
+ # nil
303
+ def recover_node(node)
304
+ sender(CMD_RECOVER_NODE % node)
305
+ nil
306
+ end
307
+
308
+ # Recover ring
309
+ # Return::
310
+ # nil
311
+ def recover_ring(node)
312
+ sender(CMD_RECOVER_RING % node)
313
+ nil
314
+ end
315
+
316
+ # Disconnect to LeoFS Manager explicitly
317
+ # Return::
318
+ # _nil_
319
+ def disconnect!
320
+ disconnect
321
+ end
322
+
323
+ ## ======================================================================
324
+ ## PRIVATE
325
+ ## ======================================================================
326
+ private
327
+
328
+ def parse_servers(servers)
329
+ servers.map do |server|
330
+ if server.is_a? String
331
+ m = server.match(/(?<host>.+):(?<port>[0-9]{1,5})/)
332
+ host = m[:host]
333
+ port = Integer(m[:port])
334
+
335
+ raise "Invalid Port Number: #{port}" unless 0 <= port && port <= 65535
336
+ { :host => host, :port => port, :retry_count => 0 }
337
+ else
338
+ server
339
+ end
340
+ end
341
+ end
342
+
343
+ def set_current_server
344
+ @servers.delete(@current_server) if @current_server
345
+ raise "No servers to connect" if @servers.empty?
346
+ @current_server = @servers.first
347
+ end
348
+
349
+ # Connect to LeoFS Manager
350
+ def connect
351
+ retry_count = 0
352
+ begin
353
+ @socket = TCPSocket.new(@current_server[:host], @current_server[:port])
354
+ rescue => ex
355
+ warn "Faild to connect: #{ex.class} (server: #{@current_server})"
356
+ warn ex.message
357
+ retry_count += 1
358
+ if retry_count > 3
359
+ set_current_server
360
+ warn "Connecting another server: #{@current_server}"
361
+ retry_count = 0
362
+ end
363
+ sleep 1
364
+ retry
365
+ end
366
+ @socket.autoclose = true
367
+ nil
368
+ end
369
+
370
+ def disconnect
371
+ @socket.close if @socket && !@socket.closed?
372
+ end
373
+
374
+ def reconnect
375
+ disconnect
376
+ sleep 1
377
+ connect
378
+ end
379
+
380
+ # Send a request to LeoFS Manager
381
+ # Return::
382
+ # Hash
383
+ def sender(command)
384
+ response = nil
385
+ begin
386
+ @mutex.synchronize do
387
+ @socket.print "#{command}\r\n"
388
+ line = @socket.readline
389
+ warn line if $DEBUG
390
+ response = JSON.parse(line, symbolize_names: true)
391
+ end
392
+ rescue EOFError => ex
393
+ warn "EOFError occured (server: #{@current_server})"
394
+ reconnect
395
+ rescue => ex
396
+ raise "An Error occured: #{ex.class} (server: #{@current_server})\n#{ex.message}"
397
+ else
398
+ raise response[:error] if response.has_key?(:error)
399
+ return response
400
+ end
401
+ end
402
+ end
403
+ end
404
+
405
+ # This section runs only when the file executed directly.
406
+ if __FILE__ == $PROGRAM_NAME
407
+ require "pp"
408
+
409
+ $DEBUG = true
410
+ m = LeoManager::Client.new("localhost:10020", "localhost:10021")
411
+ p m.version
412
+ p m.status
413
+ p m.status("storage_0@127.0.0.1")
414
+ p m.get_buckets()
415
+ p m.whereis("photo/hawaii-0.jpg")
416
+ p m.du("storage_0@127.0.0.1")
417
+ p m.compact_status("storage_0@127.0.0.1")
418
+ end