offline_consul-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c743ffb8b2e4a7cdf3c505fb33ddf10f6663bc9e
4
+ data.tar.gz: f81f4f8267a540598515ff80f8a14db60130d69e
5
+ SHA512:
6
+ metadata.gz: de1ac02621828642b1fa5ad9bc61ac1b4a4b4534f2a5438e5ecb1adec36cbf9ee3689eb948ba9c9a15c8e67253d8783131c5c4c3605b4e4e91129288bd0fe21a
7
+ data.tar.gz: fd517ffa9afdc27b7d3e1a9c59bd3e3537b38f01a498343e1e3febbc2dd9ea588a4d814ca7ed3645905e50779731be6d6d18ea64ae64528638c2ab9e9790e6ca
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /pkg/
6
+ /coverage/
7
+ /doc/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consul-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Goldstar Events
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Consul::Client
2
+
3
+ A consul client that gracefully supports offline reads/writes for dev
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'consul-client', :git => 'ssh://git@github.com/goldstar/consul-client.git'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ###Basic Usage
20
+
21
+ The main methods to use are `Consul::Client.read` and `Consul::Client.write`, these will perform all kv queries within the namespace defined by `Consul::Client.namespace`
22
+
23
+ ```ruby
24
+ >> Consul::Client.namespace = "/bags"
25
+ >> mydata = {foo:1, bar:[1,2,:three], baz:false, bat:{one:1, two:2, three:3}}
26
+ => {:foo=>1, :bar=>[1, 2, :three], :baz=>false, :bat=>{:one=>1, :two=>2, :three=>3}}
27
+ >> consul::Client.write('/some/key', mydata)
28
+ => #<Consul::Response:0x007fec1c893180
29
+ >> Consul::Client.read('/some/key')
30
+ => {:foo=>1, :bar=>[1, 2, :three], :baz=>false, :bat=>{:one=>1, :two=>2, :three=>3}}
31
+
32
+ ```
33
+
34
+ ```bash
35
+ curl "http://consul:8500/v1/kv/bags/some/key?raw"
36
+ ---
37
+ :foo: 1
38
+ :bar:
39
+ - 1
40
+ - 2
41
+ - :three
42
+ :baz: false
43
+ :bat:
44
+ :one: 1
45
+ :two: 2
46
+ :three: 3
47
+ ```
48
+
49
+ ###Tweakables
50
+
51
+ ```ruby
52
+ >> Consul::Client.host = "consul_server" # default: "consul"
53
+ >> Consul::Client.port = 12345 # default: 8500
54
+ >> Consul::Client.namespace = "/foo" # default: "/bags"
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/goldstar/consul-client.
66
+
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
71
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "consul/client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consul/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "offline_consul-client"
8
+ spec.version = Consul::Client::VERSION
9
+ spec.authors = ["Jason Scholl"]
10
+ spec.email = ["jscholl@goldstar.com"]
11
+
12
+ spec.summary = %q{Consul client with dev mode}
13
+ spec.description = %q{Consul client with offline mode for dev, and recursive queries, presenting data as a hash}
14
+ spec.homepage = "https://github.com/goldstar/consul-client"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "vcr"
27
+ spec.add_development_dependency "webmock"
28
+ end
@@ -0,0 +1,25 @@
1
+ module Consul::Client
2
+ class Config
3
+ attr_reader :host, :port
4
+ attr_accessor :namespace
5
+
6
+ def initialize
7
+ @port = 8500
8
+ @host = "consul"
9
+ @namespace = "/bags"
10
+ end
11
+
12
+ def host=(newhost)
13
+ @host = newhost
14
+ Consul::Client.http.load_config
15
+ @host
16
+ end
17
+
18
+ def port=(newport)
19
+ @port = newport
20
+ Consul::Client.http.load_config
21
+ @port
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require 'net/http'
2
+
3
+ module Consul
4
+ module Client
5
+ class FakeNetHttpResponse
6
+ attr_reader :body
7
+ def initialize(body="Good news everyone!")
8
+ @body = body
9
+ end
10
+
11
+ def code
12
+ "200"
13
+ end
14
+
15
+ def code_type
16
+ Net::HTTPOK
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Consul
2
+ module Client
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,139 @@
1
+ require "consul/client/version"
2
+ require "consul/client/config"
3
+ require "consul/client/fake_http_response"
4
+ require "consul/http"
5
+ require "consul/response"
6
+ require "consul/data"
7
+
8
+ require "psych"
9
+ require "fileutils"
10
+
11
+ module Consul
12
+ module Client
13
+ attr_writer :namespace
14
+
15
+ def self.config
16
+ @@config ||= Config.new
17
+ end
18
+
19
+ def self.online_read(keypath, recurse=false)
20
+ response = http.get("/kv" + path(keypath) + (recurse ? '?recurse' : ''))
21
+ keyname = File.basename(keypath)
22
+ if response.length == 1 && response.keys.first == keyname
23
+ response = response.data[keyname]
24
+ end
25
+ decode_r(response)
26
+ end
27
+
28
+ def self.offline_read(keypath, recurse=false)
29
+ files = if recurse
30
+ Dir.glob(File.join(local_path, path(keypath) + '*'))
31
+ else
32
+ [File.join(local_path, path(keypath) + ".yaml")]
33
+ end
34
+
35
+ response = Hash[files.map do |filename|
36
+ [File.basename(filename).sub(/\.yaml$/, ''), File.open(filename){ |f| Consul::Data.new('Value' => Base64.encode64(f.read) )}]
37
+ end]
38
+ keyname = File.basename(keypath)
39
+ if response.length == 1 && response.keys.first == keyname
40
+ response = response[keyname]
41
+ end
42
+ decode_r(response)
43
+ end
44
+
45
+ def self.read(keypath, recurse=false)
46
+ if is_online?
47
+ online_read(keypath, recurse)
48
+ else
49
+ offline_read(keypath, recurse)
50
+ end
51
+ end
52
+
53
+ def self.online_write(keypath,data=nil,path_mimic=false)
54
+ data = encode(data)
55
+ http.put("/kv" + path(keypath), data)
56
+ end
57
+
58
+ def self.offline_write(keypath,data=nil)
59
+ data = encode(data)
60
+ filename = File.join(local_path, path(keypath) + ".yaml")
61
+ dirname = File.dirname(filename)
62
+
63
+ unless File.directory?(dirname)
64
+ FileUtils.mkdir_p(dirname)
65
+ end
66
+ File.open(filename, 'w') { |f| f.write(data) }
67
+ Response.new(FakeNetHttpResponse.new)
68
+ end
69
+
70
+ def self.write(keypath,data=nil)
71
+ if is_online?
72
+ online_write(keypath,data)
73
+ else
74
+ offline_write(keypath,data)
75
+ end
76
+ end
77
+
78
+
79
+ #def self.lock(keypath, data=nil)
80
+ # write(keypath + "?acquire=#{session_id}", data)
81
+ #end
82
+
83
+ #def self.unlock(keypath, data=nil)
84
+ # write(keypath + "?release=#{session_id}", data)
85
+ #end
86
+
87
+ #def self.session_id
88
+ # @@session_id ||= http.put("/session/create")
89
+ #end
90
+
91
+ #def self.has_lock?(keypath)
92
+ # read("/kv/" + keypath).session == session_id
93
+ #end
94
+
95
+ def self.http
96
+ @http ||= Http.new
97
+ end
98
+
99
+ def self.local_path
100
+ ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
101
+ end
102
+
103
+ def self.path(keypath)
104
+ File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
105
+ end
106
+
107
+ def self.is_online?
108
+ !::Chef::Config[:solo]
109
+ end
110
+
111
+ def self.decode_r(data)
112
+ if data.respond_to?(:keys)
113
+ Hash[ data.map{ |k,v| [k,decode_r(v)] } ]
114
+ else
115
+ decode(data.value)
116
+ end
117
+ end
118
+
119
+ def self.decode(value)
120
+ Psych.safe_load(value, [Symbol])
121
+ end
122
+
123
+ def self.encode(value)
124
+ encoded = Psych.dump(value)
125
+ if decode(encoded) != value
126
+ raise TypeError, "Unable to relaibly decode #{value.class}"
127
+ end
128
+ encoded
129
+ end
130
+
131
+ def self.encode_r(data)
132
+ if data.respond_to?(:keys)
133
+ Hash[ data.map{ |k,v| [k, encode_r(v)] } ]
134
+ else
135
+ encode(data)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,25 @@
1
+ require 'base64'
2
+
3
+ module Consul
4
+ class Data
5
+
6
+ def initialize(response_hash)
7
+ @response = response_hash
8
+ end
9
+
10
+ def value
11
+ Base64.decode64(hash_map('Value'))
12
+ end
13
+
14
+ def flags
15
+ hash_map(__callee__)
16
+ end
17
+ [:lock_index, :create_index, :modify_index, :session].each{ |a| alias_method a, :flags }
18
+
19
+ private
20
+ def hash_map(key)
21
+ @response[key.to_s.split('_').map{ |w| w.capitalize }.join]
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'net/http'
2
+
3
+ module Consul
4
+ class Http
5
+ attr_accessor :http_client
6
+
7
+ def initialize
8
+ load_config
9
+ end
10
+
11
+ def request(method, uri, data=nil)
12
+ response = http_client.send_request(method, ("/v1" + uri), data)
13
+ Response.new(response, uri.gsub(%r{^/kv/}, ''))
14
+ end
15
+
16
+ def put(path, data=nil)
17
+ request('PUT', path, data)
18
+ end
19
+
20
+ def get(path)
21
+ request('GET', path)
22
+ end
23
+
24
+ def load_config
25
+ self.http_client = Net::HTTP.new(Consul::Client.config.host, Consul::Client.config.port, nil, nil)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ require 'psych'
2
+ require 'net/http'
3
+ require 'forwardable'
4
+
5
+ module Consul
6
+ class Response
7
+ extend Forwardable
8
+ include Enumerable
9
+
10
+ attr_reader :response, :data
11
+ def_delegators :response, :body, :code, :code_type
12
+ def_delegators :data, :keys, :values, :each, :to_a, :length
13
+
14
+ def initialize(net_http_response, request_path='/')
15
+ @response = net_http_response
16
+ @request_path = File.dirname(request_path)
17
+ end
18
+
19
+ def data
20
+ @data ||= init_data
21
+ end
22
+
23
+ #def locked?
24
+ # to_h.has_key?("Session")
25
+ #end
26
+
27
+ private
28
+
29
+ def init_data
30
+ {}.tap do |retval|
31
+ to_ruby.each do |metadata|
32
+ hash_path = metadata['Key'].gsub(%r{^/?#{@request_path}/?}, '')
33
+ retval.merge!(build_data_hash(hash_path,metadata), &merger)
34
+ end
35
+ end
36
+ end
37
+
38
+ def build_data_hash(hash_path, consul_metadata)
39
+ hash_path.split('/').reverse.inject(Data.new(consul_metadata)) { |memo,key| { key => memo } }
40
+ end
41
+
42
+ def to_ruby
43
+ if response.code_type == Net::HTTPOK
44
+ Psych.safe_load(response.body)
45
+ else
46
+ []
47
+ end
48
+ end
49
+
50
+ def merger
51
+ proc { |key,new,old| new.respond_to?(:merge) && new.class == old.class ? new.merge(old, &merger) : old }
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: offline_consul-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Scholl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Consul client with offline mode for dev, and recursive queries, presenting
98
+ data as a hash
99
+ email:
100
+ - jscholl@goldstar.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - consul-client.gemspec
115
+ - lib/consul/client.rb
116
+ - lib/consul/client/config.rb
117
+ - lib/consul/client/fake_http_response.rb
118
+ - lib/consul/client/version.rb
119
+ - lib/consul/data.rb
120
+ - lib/consul/http.rb
121
+ - lib/consul/response.rb
122
+ homepage: https://github.com/goldstar/consul-client
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.5.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Consul client with dev mode
146
+ test_files: []
147
+ has_rdoc: