Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-c
-fd
--fail-fast
8 changes: 4 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
AllCops:
Includes:
Include:
- Gemfile
- lib/**
Excludes:
- spec/**
- lib/**/*
Exclude:
- spec/**/*

Encoding:
Enabled: false
Expand Down
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
before_install:
- wget -c https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz
- tar -zxf etcd-v0.4.6-linux-amd64.tar.gz
- bundle install --path .bundle
- wget -c https://github.com/coreos/etcd/releases/download/v2.0.0-rc.1/etcd-v2.0.0-rc.1-linux-amd64.tar.gz
- tar -zxf etcd-v2.0.0-rc.1-linux-amd64.tar.gz
- bundle install

rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
branches:
only:
- master
script: "ETCD_BIN=./etcd-v0.4.6-linux-amd64/etcd bundle exec rake spec"
script: "ETCD_BIN=./etcd-v2.0.0-rc.1-linux-amd64/etcd bundle exec rake spec"
3 changes: 2 additions & 1 deletion etcd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "mixlib-log"
spec.required_ruby_version = '>= 1.9'

spec.add_dependency "mixlib-log"
spec.add_development_dependency "uuid"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
Expand Down
56 changes: 22 additions & 34 deletions lib/etcd/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Etcd
# etcd api, like Etcd::Client#lock and Etcd::Client#eternal_watch, they
# are defined in separate modules and included in this class
class Client

extend Forwardable

HTTP_REDIRECT = ->(r) { r.is_a? Net::HTTPRedirection }
Expand All @@ -26,12 +25,19 @@ class Client
include Stats
include Keys

Config = Struct.new(:use_ssl, :verify_mode, :read_timeout, :ssl_key, :ca_file,
:user_name, :password, :allow_redirect, :ssl_cert)
Config = Struct.new(
:use_ssl,
:verify_mode,
:read_timeout,
:ssl_key,
:ca_file,
:user_name,
:password,
:ssl_cert
)

def_delegators :@config, :use_ssl, :verify_mode, :read_timeout
def_delegators :@config, :user_name, :password, :allow_redirect

def_delegators :@config, :user_name, :password

attr_reader :host, :port, :http, :config

Expand All @@ -48,16 +54,14 @@ def initialize(opts = {})
@port = opts[:port] || 4001
@config = Config.new
@config.read_timeout = opts[:read_timeout] || 60
@config.allow_redirect = opts.key?(:allow_redirect) ? opts[:allow_redirect] : true
@config.use_ssl = opts[:use_ssl] || false
@config.verify_mode = opts.key?(:verify_mode) ? opts[:verify_mode] : OpenSSL::SSL::VERIFY_PEER
@config.user_name = opts[:user_name] || nil
@config.password = opts[:password] || nil
@config.allow_redirect = opts.key?(:allow_redirect) ? opts[:allow_redirect] : true
@config.ca_file = opts.key?(:ca_file) ? opts[:ca_file] : nil
#Provide a OpenSSL X509 cert here and not the path. See README
# Provide a OpenSSL X509 cert here and not the path. See README
@config.ssl_cert = opts.key?(:ssl_cert) ? opts[:ssl_cert] : nil
#Provide the key (content) and not just the filename here.
# Provide the key (content) and not just the filename here.
@config.ssl_key = opts.key?(:ssl_key) ? opts[:ssl_key] : nil
yield @config if block_given?
end
Expand All @@ -73,14 +77,9 @@ def version
api_execute('/version', :get).body
end

# Returns array of all machines in the cluster
def machines
api_execute(version_prefix + '/machines', :get).body.split(',').map(&:strip)
end

# Get the current leader
def leader
api_execute(version_prefix + '/leader', :get).body.strip
api_execute(version_prefix + '/stats/leader', :get).body.strip
end

# This method sends api request to etcd server.
Expand All @@ -104,51 +103,40 @@ def api_execute(path, method, options = {})
else
fail "Unknown http action: #{method}"
end
timeout = options[:timeout] || @read_timeout
http = Net::HTTP.new(host, port)
http.read_timeout = timeout
http.read_timeout = options[:timeout] || read_timeout
setup_https(http)
req.basic_auth(user_name, password) if [user_name, password].all?
Log.debug("Invoking: '#{req.class}' against '#{path}")
res = http.request(req)
Log.debug("Response code: #{res.code}")
process_http_request(res, req, params)
Log.debug("Response body: #{res.body}")
process_http_request(res)
end

def setup_https(http)
http.use_ssl = use_ssl
http.verify_mode = verify_mode
unless config.ssl_cert.nil?
if config.ssl_cert
Log.debug('Setting up ssl cert')
http.cert = config.ssl_cert
end
unless config.ssl_key.nil?
if config.ssl_key
Log.debug('Setting up ssl key')
http.key = config.ssl_key
end
unless config.ca_file.nil?
if config.ca_file
Log.debug('Setting up ssl ca file to :' + config.ca_file)
http.ca_file = config.ca_file
end
end

# need to ahve original request to process the response when it redirects
def process_http_request(res, req = nil, params = nil)
# need to have original request to process the response when it redirects
def process_http_request(res)
case res
when HTTP_SUCCESS
Log.debug('Http success')
res
when HTTP_REDIRECT
if allow_redirect
uri = URI(res['location'])
@host = uri.host
@port = uri.port
Log.debug("Http redirect, setting new host to: #{@host}:#{@port}, and retrying")
api_execute(uri.path, req.method.downcase.to_sym, params: params)
else
Log.debug('Http redirect not allowed')
res.error!
end
when HTTP_CLIENT_ERROR
fail Error.from_http_response(res)
else
Expand Down
8 changes: 6 additions & 2 deletions lib/etcd/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ def watch(key, opts = {})
params[:consistent] = opts[:consistent] if opts.key?(:consistent)
params[:recursive] = opts[:recursive] if opts.key?(:recursive)

response = api_execute(key_endpoint + key, :get,
timeout: timeout, params: params)
response = api_execute(
key_endpoint + key,
:get,
timeout: timeout,
params: params
)
Response.from_http_response(response)
end

Expand Down
28 changes: 0 additions & 28 deletions lib/etcd/mod/leader.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/etcd/mod/lock.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/etcd/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(opts = {})
@expiration = opts['expiration']
@dir = opts['dir']

if opts['dir'] && (!!opts['nodes'])
if opts['dir'] && opts['nodes']
opts['nodes'].each do |data|
children << Node.new(data)
end
Expand All @@ -39,7 +39,7 @@ def children
end

def directory?
!! @dir
! @dir.nil?
end
end
end
17 changes: 0 additions & 17 deletions spec/etcd/basic_auth_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
require 'spec_helper'

describe 'Etcd basic auth client' do

before(:all) do
start_daemon(2)
end
after(:all) do
stop_daemon
end

let(:client) do
Etcd.client(host: 'localhost') do |config|
config.user_name = 'test'
Expand All @@ -25,13 +17,4 @@
it '#password' do
expect(client.password).to eq('pwd')
end

it 'should set basic auth' do
Net::HTTPRequest.any_instance.should_receive(:basic_auth).with('test', 'pwd')
key = random_key
value = uuid.generate
client.set(key, value: value)
sleep 1
expect(read_only_client.get(key).value).to eq(value)
end
end
30 changes: 2 additions & 28 deletions spec/etcd/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
require 'spec_helper'

describe Etcd::Client do

before(:all) do
start_daemon(3)
end

after(:all) do
stop_daemon
end

let(:client) do
etcd_client
end

it 'should return the leader address' do
expect(client.leader).to_not be_nil
end

it '#machines' do
expect(client.machines).to include('http://127.0.0.1:4001')
end

it '#version' do
expect(client.version).to match(/^etcd v?0\.\d+\.\d+(\+git)?/)
it '#version' do #etcd 2.0.0-rc.1
expect(client.version).to match(/^etcd v?\d+\.\d+\.\d+.*$/)
end

it '#version_prefix' do
Expand All @@ -36,15 +19,6 @@
client.api_execute('/v2/keys/x', :do)
end.to raise_error
end

it 'should redirect api request when allow_redirect is set' do
key = random_key
value = uuid.generate
resp = client.set(key, value: value)
resp.node.key.should eql key
resp.node.value.should eql value
client.get(key).value.should eql resp.value
end
end

context '#http header based metadata' do
Expand Down
Loading