etcd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in etcd.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ranjib Dey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # Etcd
2
+
3
+ A ruby client for [etcd](https://github.com/coreos/etcd)
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'etcd'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install etcd
17
+
18
+ ## Usage
19
+
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'etcd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "etcd"
8
+ spec.version = Etcd::VERSION
9
+ spec.authors = ["Ranjib Dey"]
10
+ spec.email = ["ranjib@pagerduty.com"]
11
+ spec.description = %q{Ruby client library for etcd}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/ranjib/etcd-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mixlib-cli"
22
+ spec.add_dependency "mixlib-log"
23
+ spec.add_dependency "hashie"
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,6 @@
1
+ require 'etcd/client'
2
+ module Etcd
3
+ def self.client(opts={})
4
+ Etcd::Client.new(opts)
5
+ end
6
+ end
@@ -0,0 +1,128 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'hashie'
4
+ require 'etcd/log'
5
+
6
+ module Etcd
7
+ class Client
8
+
9
+ attr_reader :host, :port, :http, :allow_redirect
10
+
11
+ def initialize(opts={})
12
+ @host = opts[:host] || '127.0.0.1'
13
+ @port = opts[:port] || 4001
14
+ @allow_redirect = opts[:allow_redirect] || true
15
+ end
16
+
17
+ def version_prefix
18
+ '/v1'
19
+ end
20
+
21
+ def machines
22
+ api_execute('/machines', :get).split(",")
23
+ end
24
+
25
+ def leader
26
+ api_execute('/leader', :get)
27
+ end
28
+
29
+ def key_endpoint
30
+ version_prefix + '/keys'
31
+ end
32
+
33
+ def watch_endpoint
34
+ version_prefix + '/watch'
35
+ end
36
+
37
+ def test_and_set(key, value, prevValue, ttl = nil)
38
+ path = key_endpoint + key
39
+ payload = {'value' => value, 'prevValue' => prevValue }
40
+ payload['ttl'] = ttl unless ttl.nil?
41
+ response = api_execute(path, :post, payload)
42
+ Hashie::Mash.new(JSON.parse(response))
43
+ end
44
+
45
+ def set(key, value, ttl=nil)
46
+ path = key_endpoint + key
47
+ payload = {'value' => value}
48
+ payload['ttl'] = ttl unless ttl.nil?
49
+ response = api_execute(path, :post, payload)
50
+ Hashie::Mash.new(JSON.parse(response))
51
+ end
52
+
53
+ def delete(key)
54
+ response = api_execute(key_endpoint + key, :delete)
55
+ Hashie::Mash.new(JSON.parse(response))
56
+ end
57
+
58
+ def get(key)
59
+ response = api_execute(key_endpoint + key, :get)
60
+ obj = JSON.parse(response)
61
+ if obj.is_a?(Array)
62
+ obj.map{|e| Hashie::Mash.new(e)}
63
+ else
64
+ Hashie::Mash.new(obj)
65
+ end
66
+ end
67
+
68
+ def watch(key, index=nil)
69
+ response = if index.nil?
70
+ api_execute(watch_endpoint + key, :get)
71
+ else
72
+ api_execute(watch_endpoint + key, :post, {'index' => index})
73
+ end
74
+ Hashie::Mash.new(JSON.parse(response))
75
+ end
76
+
77
+ def api_execute(path, method, params=nil)
78
+
79
+ http = if path=~/^http/
80
+ uri = URI.parse(path)
81
+ path = uri.path
82
+ Net::HTTP.new(uri.host, uri.port)
83
+ else
84
+ Net::HTTP.new(host, port)
85
+ end
86
+
87
+ case method
88
+ when :get
89
+ unless params.nil?
90
+ encoded_params = URI.encode_www_form(params)
91
+ path+= "?" + encoded_params
92
+ end
93
+ req = Net::HTTP::Get.new(path)
94
+ when :post
95
+ encoded_params = URI.encode_www_form(params)
96
+ req = Net::HTTP::Post.new(path)
97
+ req.body= encoded_params
98
+ Log.debug("Setting body for post '#{encoded_params}'")
99
+ when :delete
100
+ unless params.nil?
101
+ encoded_params = URI.encode_www_form(params)
102
+ path+= "?" + encoded_params
103
+ end
104
+ req = Net::HTTP::Delete.new(path)
105
+ end
106
+
107
+ Log.debug("Invoking: '#{req.class}' against '#{path}")
108
+ res = http.request(req)
109
+ Log.debug("Response code: #{res.code}")
110
+ if res.is_a?(Net::HTTPSuccess)
111
+ Log.debug("Http success")
112
+ res.body
113
+ elsif redirect?(res.code.to_i) and allow_redirect
114
+ Log.debug("Http redirect, following")
115
+ api_execute(res['location'], method, params)
116
+ else
117
+ Log.debug("Http error")
118
+ Log.debug(res.body)
119
+ #res.error!
120
+ res
121
+ end
122
+ end
123
+
124
+ def redirect?(code)
125
+ (code >= 300) and (code < 400)
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,6 @@
1
+ require 'mixlib/log'
2
+ module Etcd
3
+ class Log
4
+ extend Mixlib::Log
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Etcd
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etcd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ranjib Dey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mixlib-cli
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mixlib-log
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Ruby client library for etcd
127
+ email:
128
+ - ranjib@pagerduty.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - etcd.gemspec
139
+ - lib/etcd.rb
140
+ - lib/etcd/client.rb
141
+ - lib/etcd/log.rb
142
+ - lib/etcd/version.rb
143
+ homepage: https://github.com/ranjib/etcd-ruby
144
+ licenses:
145
+ - MIT
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ segments:
157
+ - 0
158
+ hash: 3406514257723307020
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ segments:
166
+ - 0
167
+ hash: 3406514257723307020
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.23
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Ruby client library for etcd
174
+ test_files: []