diplomat 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29561e5c607d45e0a6339e2cead4c2721dc5d90e
4
- data.tar.gz: b82255ac4419a411300b22b0e76cc37546065da9
3
+ metadata.gz: 2244d84cc7a0fe952223ad6001d8f473be47a024
4
+ data.tar.gz: b86af2a9b85baadf3d45382a55ad25a12c1e70d5
5
5
  SHA512:
6
- metadata.gz: e9e71c7eb3886233575aa00184f13179c190fa86d6ae34b1896e453ab354783132bdbbb6940f32aa3c388e53f123ada0aa258e2a5b97e7f76220ad4be413d7ba
7
- data.tar.gz: b12d5ee9ac1ded146904fea9594e7736dc4ba3233dd521c9067c20cd15d4aebf8436814c156ef616aac4f7b014bca1aef048d884f12230ac567c611a9c90eb18
6
+ metadata.gz: e176ec9e8b1e1eebb570c8c0698d38eadab706654ceaba1d8d0f981cd8c046aad6f70cbb9772a8562e2ece337ff02c11417829425af2b142d16d4445d6147ec1
7
+ data.tar.gz: 3faa78e2307bb3c0bb00e4722adfc8eb3bedd1e2afd5f5f98d0ae4efd6f919ec03f3e5d59087e17ade656b762c06b273416f2a44fc096343802754545a263f14
data/README.md CHANGED
@@ -82,6 +82,42 @@ foo_service = Diplomat::Service.get('foo', :all)
82
82
  # => [#<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>,#<OpenStruct Node="indigo", Address="1.2.3.5", ServiceID="indigo_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>]
83
83
  ```
84
84
 
85
+ ### Sessions
86
+
87
+ Creating a session:
88
+
89
+ ```ruby
90
+ sessionid = Diplomat::Session.create({:hostname => "server1", :ipaddress => "4.4.4.4"})
91
+ # => "fc5ca01a-c317-39ea-05e8-221da00d3a12"
92
+ ```
93
+ Or destroying a session:
94
+
95
+ ```ruby
96
+ Diplomat::Session.destroy("fc5ca01a-c317-39ea-05e8-221da00d3a12")
97
+ ```
98
+
99
+ ### Locks
100
+
101
+ Acquire a lock:
102
+
103
+ ```ruby
104
+ sessionid = Diplomat::Session.create({:hostname => "server1", :ipaddress => "4.4.4.4"})
105
+ lock_acquired = Diplomat::Lock.acquire("/key/to/lock", sessionid)
106
+ # => true
107
+ ```
108
+ Or wait for a lock to be acquired:
109
+
110
+ ```ruby
111
+ sessionid = Diplomat::Session.create({:hostname => "server1", :ipaddress => "4.4.4.4"})
112
+ lock_acquired = Diplomat::Lock.wait_to_acquire("/key/to/lock", sessionid)
113
+ ```
114
+
115
+ Release a lock:
116
+
117
+ ```ruby
118
+ Diplomat::Lock.release("/key/to/lock", sessionid )
119
+ ```
120
+
85
121
  ### Custom configuration
86
122
 
87
123
  You can create a custom configuration using the following syntax:
@@ -99,14 +135,15 @@ This is traditionally kept inside the `config/initializers` directory if you're
99
135
 
100
136
  ### Todo
101
137
 
102
- - Updating docs with latest changes
103
- - PUTting and DELETEing services
104
- - Allowing the custom configuration of the consul url to connect to
105
- - - ~~Deleting Keys~~ **Needs a test**
106
- - ~~Listing available Services~~ **Done**
107
- - ~~Health~~ **Done**
108
- - ~~Members~~ **Done**
109
- - ~~Status~~ **Done**
138
+ - [ ] Updating Docs with latest changes
139
+ - [ ] PUTing and DELETEing services
140
+ - [x] Allowing the custom configuration of the consul url to connect to
141
+ - [x] Deleting Keys
142
+ - [x] Listing available services
143
+ - [x] Health
144
+ - [x] Members
145
+ - [x] Status
146
+
110
147
 
111
148
  ## Enjoy!
112
149
 
data/lib/diplomat.rb CHANGED
@@ -20,7 +20,7 @@ module Diplomat
20
20
  self.root_path = File.expand_path "..", __FILE__
21
21
  self.lib_path = File.expand_path "../diplomat", __FILE__
22
22
 
23
- require_libs "configuration", "rest_client", "kv", "service", "members", "check", "health"
23
+ require_libs "configuration", "rest_client", "kv", "service", "members", "check", "health", "session", "lock"
24
24
  self.configuration ||= Diplomat::Configuration.new
25
25
 
26
26
  class << self
@@ -0,0 +1,60 @@
1
+ require 'faraday'
2
+
3
+ module Diplomat
4
+ class Lock < Diplomat::RestClient
5
+
6
+ # Acquire a lock
7
+ # @param key [String] the key
8
+ # @param session [String] the session, generated from Diplomat::Session.create
9
+ # @return [Boolean] If the lock was acquired
10
+ def acquire key, session
11
+ raw = @conn.put do |req|
12
+ req.url "/v1/kv/#{key}?acquire=#{session}"
13
+ end
14
+ return true if raw.body == 'true'
15
+ return false
16
+
17
+ end
18
+
19
+ # wait to aquire a lock
20
+ # @param key [String] the key
21
+ # @param session [String] the session, generated from Diplomat::Session.create
22
+ # @param check_interval [Integer] number of seconds to wait between retries
23
+ # @return [Boolean] If the lock was acquired
24
+ def wait_to_acquire key, session, check_interval=10
25
+ acquired = false
26
+ while !acquired
27
+ acquired = self.acquire key, session
28
+ sleep(check_interval) if !acquired
29
+ return true if acquired
30
+ end
31
+ end
32
+
33
+
34
+ # Release a lock
35
+ # @param key [String] the key
36
+ # @param session [String] the session, generated from Diplomat::Session.create
37
+ # @return [nil]
38
+ def release key, session
39
+ raw = @conn.put do |req|
40
+ req.url "/v1/kv/#{key}?release=#{session}"
41
+ end
42
+ return raw.body
43
+ end
44
+
45
+ # @note This is sugar, see (#acquire)
46
+ def self.acquire *args
47
+ Diplomat::Lock.new.acquire *args
48
+ end
49
+
50
+ # @note This is sugar, see (#wait_to_acquire)
51
+ def self.wait_to_acquire *args
52
+ Diplomat::Lock.new.wait_to_acquire *args
53
+ end
54
+
55
+ # @note This is sugar, see (#release)
56
+ def self.release *args
57
+ Diplomat::Lock.new.release *args
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,38 @@
1
+ require 'faraday'
2
+
3
+ module Diplomat
4
+ class Session < Diplomat::RestClient
5
+
6
+ # Create a new session
7
+ # @param value [String] json representation of the local node
8
+ # @return [String] The sesssion id
9
+ def create value
10
+ raw = @conn.put do |req|
11
+ req.url "/v1/session/create"
12
+ req.body = value
13
+ end
14
+ body = JSON.parse(raw.body)
15
+ return body["ID"]
16
+ end
17
+
18
+ # Destroy a session
19
+ # @param id [String] session id
20
+ # @return [nil]
21
+ def destroy id
22
+ raw = @conn.put do |req|
23
+ req.url "/v1/session/destroy/#{id}"
24
+ end
25
+ return raw.body
26
+ end
27
+
28
+ # @note This is sugar, see (#create)
29
+ def self.create *args
30
+ Diplomat::Session.new.create *args
31
+ end
32
+
33
+ # @note This is sugar, see (#destroy)
34
+ def self.destroy *args
35
+ Diplomat::Session.new.destroy *args
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Diplomat
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diplomat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hamelink
@@ -181,9 +181,11 @@ files:
181
181
  - lib/diplomat/configuration.rb
182
182
  - lib/diplomat/health.rb
183
183
  - lib/diplomat/kv.rb
184
+ - lib/diplomat/lock.rb
184
185
  - lib/diplomat/members.rb
185
186
  - lib/diplomat/rest_client.rb
186
187
  - lib/diplomat/service.rb
188
+ - lib/diplomat/session.rb
187
189
  - lib/diplomat/version.rb
188
190
  homepage: https://github.com/johnhamelink/diplomat
189
191
  licenses: