offline_consul-client 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e581d353ceef2e170fae3f52957cc6a04a017ae4
4
- data.tar.gz: e3091c3965da1981d52b672555814ca53b5cdcf0
3
+ metadata.gz: e15f6c39aa174b3f7db3b0d0f55b3ba60b4c6559
4
+ data.tar.gz: e768c0465b381487196b44a33245b21bdbe406e5
5
5
  SHA512:
6
- metadata.gz: 55e1354375b9dd7c8bee244d97d2b4345180261c13cba7d769f2a51dca98b2341a2cb59f4834317a967a7a880e24a9ed947ed60e2a9f2c39cb44e1ee08c01d1e
7
- data.tar.gz: b70b527b7248849c3212e8b3ce86d97ef47fe9e9c4ed6c559100dcd7f09cc0062311609f770073fad3ae7f43188ec5bee0d63683212e634a8f10786f587def51
6
+ metadata.gz: 40a2277ad6f515faf6cf6929986c99ca697ae384295534e1ba00a6ce078e766f6bef78b7656815b1b0f4f63b490e11c93cf7adc59316ecf43d5a5be7df16adb2
7
+ data.tar.gz: 27979ace78e9485b53379d1a02c845c78f5475b20e0238e773611e5adf63117449d5ec8dd455026f3479eef6fd74ccd0b7898472f262975fc5a9258eea38215b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- offline_consul-client (0.2.0)
4
+ offline_consul-client (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -7,4 +7,15 @@ require "consul/client"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  require "pry"
10
+
11
+ SOLO = (ARGV.first == "solo")
12
+
13
+ module Chef
14
+ module Config
15
+ def self.[](f)
16
+ SOLO
17
+ end
18
+ end
19
+ end
20
+
10
21
  Pry.start
@@ -1,6 +1,7 @@
1
1
  require "consul/client/version"
2
2
  require "consul/client/config"
3
3
  require "consul/client/connection"
4
+ require "consul/client/offline_connection"
4
5
  require "consul/client/fake_http_response"
5
6
  require "consul/http"
6
7
  require "consul/response"
@@ -17,12 +18,29 @@ module Consul
17
18
  def_delegators :connection, *(Connection.instance_methods - Object.instance_methods)
18
19
 
19
20
  def new
20
- Connection.new
21
+ if is_online?
22
+ Connection.new
23
+ else
24
+ OfflineConnection.new
25
+ end
21
26
  end
22
27
 
23
28
  def connection
24
- @connection ||= Connection.new
29
+ @connection ||= new
25
30
  end
31
+
32
+ def is_online?
33
+ if defined?(Chef)
34
+ !::Chef::Config[:solo]
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ def reconnect
41
+ @connection = new
42
+ end
43
+
26
44
  end
27
45
 
28
46
  end
@@ -8,98 +8,38 @@ module Consul::Client
8
8
  @config ||= Config.new
9
9
  end
10
10
 
11
- def online_read(keypath, opts={})
12
- response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : ''))
13
- end
14
-
15
- def offline_read(keypath, opts={})
16
- files = if opts[:recursive]
17
- Dir.glob(File.join(local_path, path(keypath) + '*'))
18
- else
19
- [File.join(local_path, path(keypath) + ".yaml")]
20
- end
21
-
22
- key_data = files.map do |filename|
23
- {}.tap do |data|
24
- data['LockIndex'] = 0
25
- data['Key'] = File.basename(filename).sub(/\.yaml$/, '')
26
- data['Flags'] = 0
27
- data['Value'] = Base64.encode64(File.read(filename))
28
- data['CreateIndex'] = 0
29
- data['ModifyIndex'] = 0
30
- end
31
- end
32
-
33
- response = Consul::Response.new(Consul::Client::FakeNetHttpResponse.new(Psych.dump(key_data)))
34
- end
35
-
36
11
  def read_obj(keypath, opts={})
37
12
  keyname = File.basename(keypath)
38
- response = if is_online?
39
- online_read(keypath, opts)
40
- else
41
- offline_read(keypath, opts)
42
- end
43
-
13
+ response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : ''))
44
14
  if response.length == 1 && response.keys.first == keyname
45
15
  response[keyname]
46
16
  else
47
- Hash[response.map{ |k,v| [k, v.value] }]
17
+ response
48
18
  end
49
19
  end
50
20
 
51
21
  def read(keypath, opts={})
52
- obj = read_obj(keypath, opts)
53
- if obj.respond_to?(:value)
54
- obj.value
55
- else
56
- obj
57
- end
58
- end
59
-
60
- def online_delete(keypath, opts={})
61
- response = http.delete("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')).value
22
+ response = read_obj(keypath, opts)
23
+ read_value_r(response)
62
24
  end
63
25
 
64
- def offline_delete(keypath, opts={})
65
- #do nothing, lets not delete our local files
66
- true
67
- end
68
-
69
- def delete(keypath, opts={})
70
- if is_online?
71
- online_delete(keypath, opts={})
26
+ def read_value_r(response)
27
+ if response.respond_to?(:keys)
28
+ Hash[ response.map{ |k,v| [k,read_value_r(v)] } ]
72
29
  else
73
- offline_delete(keypath, opts={})
30
+ response.value
74
31
  end
75
32
  end
76
33
 
77
- def online_write(keypath,data=nil,path_mimic=false)
78
- data = encode(data)
79
- http.put("/kv" + path(keypath), data)
34
+ def delete(keypath, opts={})
35
+ http.delete("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')).value
80
36
  end
81
37
 
82
- def offline_write(keypath,data=nil)
38
+ def write(keypath,data=nil,path_mimic=false)
83
39
  data = encode(data)
84
- filename = File.join(local_path, path(keypath) + ".yaml")
85
- dirname = File.dirname(filename)
86
-
87
- unless File.directory?(dirname)
88
- FileUtils.mkdir_p(dirname)
89
- end
90
- File.open(filename, 'w') { |f| f.write(data) }
91
- Consul::Response.new(Consul::Client::FakeNetHttpResponse.new)
92
- end
93
-
94
- def write(keypath,data=nil)
95
- if is_online?
96
- online_write(keypath,data)
97
- else
98
- offline_write(keypath,data)
99
- end
40
+ http.put("/kv" + path(keypath), data)
100
41
  end
101
42
 
102
-
103
43
  #def lock(keypath, data=nil)
104
44
  # write(keypath + "?acquire=#{session_id}", data)
105
45
  #end
@@ -120,22 +60,10 @@ module Consul::Client
120
60
  @http ||= Consul::Http.new
121
61
  end
122
62
 
123
- def local_path
124
- ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
125
- end
126
-
127
63
  def path(keypath)
128
64
  File.join("/" + config.namespace, keypath).gsub(%r{//}, '/')
129
65
  end
130
66
 
131
- def is_online?
132
- if defined?(Chef)
133
- !::Chef::Config[:solo]
134
- else
135
- false
136
- end
137
- end
138
-
139
67
  def decode_r(data)
140
68
  if data.respond_to?(:keys)
141
69
  Hash[ data.map{ |k,v| [k,decode_r(v)] } ]
@@ -0,0 +1,68 @@
1
+ require "psych"
2
+ require "fileutils"
3
+
4
+ module Consul::Client
5
+ class OfflineConnection < Connection
6
+
7
+ def read_obj(keypath, opts={})
8
+ keyname = File.basename(keypath)
9
+
10
+ response = http_get(keypath, opts)
11
+ if response.length == 1 && response.keys.first == keyname
12
+ response[keyname]
13
+ else
14
+ response
15
+ end
16
+ end
17
+
18
+ def delete(keypath, opts={})
19
+ #do nothing, lets not delete our local files
20
+ true
21
+ end
22
+
23
+ def write(keypath,data=nil)
24
+ data = encode(data)
25
+ filename = File.join(local_path, path(keypath) + ".yaml")
26
+ dirname = File.dirname(filename)
27
+
28
+ unless File.directory?(dirname)
29
+ FileUtils.mkdir_p(dirname)
30
+ end
31
+ File.open(filename, 'w') { |f| f.write(data) }
32
+ Consul::Response.new(Consul::Client::FakeNetHttpResponse.new)
33
+ end
34
+
35
+ private
36
+
37
+ def files(keypath, opts)
38
+ if opts[:recursive]
39
+ recursive_files = Dir.glob(File.join(local_path, path(keypath) + '*', '**', '*.yaml'))
40
+ path_files = Dir.glob(File.join(local_path, path(keypath) + '*.yaml'))
41
+
42
+ recursive_files | path_files
43
+ else
44
+ [File.join(local_path, path(keypath) + ".yaml")]
45
+ end
46
+ end
47
+
48
+ def http_get(keypath, opts)
49
+ key_data = files(keypath, opts).map do |filename|
50
+ {}.tap do |data|
51
+ data['LockIndex'] = 0
52
+ data['Key'] = filename.sub(/^#{File.dirname(File.join(local_path, path(keypath)))}/, '').chomp('.yaml')
53
+ data['Flags'] = 0
54
+ data['Value'] = Base64.encode64(File.read(filename))
55
+ data['CreateIndex'] = 0
56
+ data['ModifyIndex'] = 0
57
+ end
58
+ end
59
+
60
+ Consul::Response.new(Consul::Client::FakeNetHttpResponse.new(Psych.dump(key_data)))
61
+ end
62
+
63
+ def local_path
64
+ ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client"
65
+ end
66
+
67
+ end
68
+ end
@@ -1,5 +1,5 @@
1
1
  module Consul
2
2
  module Client
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: offline_consul-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Scholl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-20 00:00:00.000000000 Z
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,7 @@ files:
132
132
  - lib/consul/client/config.rb
133
133
  - lib/consul/client/connection.rb
134
134
  - lib/consul/client/fake_http_response.rb
135
+ - lib/consul/client/offline_connection.rb
135
136
  - lib/consul/client/version.rb
136
137
  - lib/consul/data.rb
137
138
  - lib/consul/http.rb