capistrano-consul_kv_lock 0.1.1 → 0.2.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1faefc322b8e5b7f0b50d750d6b8e891e23fd776
|
4
|
+
data.tar.gz: b2702796331c0bafe1fb3703dee0479abeb2ff48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ddd65c39dec7d0b59eb9b6385348fde4a6bd044e4009cc7dae7955e628d5b3d9c76e8bcffb0f6a5c7d274480d38d4f42c645ce8316794da493af529a595ba62
|
7
|
+
data.tar.gz: 27744fca1979c3c2a393f0e3f6b30e899bd6b99622784ae99a8a6dda3702008c38dd3543fcd6effe134be0849a3cfaeecf73b396283da0975cdfd6217b314a0f
|
@@ -1,10 +1,5 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
module Capistrano
|
5
|
-
module ConsulKvLock
|
6
|
-
# Your code goes here...
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
1
|
+
require "capistrano/framework"
|
2
|
+
require "capistrano/consul_kv_lock/latch"
|
10
3
|
require "capistrano/consul_kv_lock/version"
|
4
|
+
|
5
|
+
load File.expand_path("../tasks/consul_kv_lock.rake", __FILE__)
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Capistrano
|
2
|
+
module ConsulKvLock
|
3
|
+
class SSHKittyLogger
|
4
|
+
def initialize
|
5
|
+
@coordinator = SSHKit::Coordinator.new('localhost')
|
6
|
+
end
|
7
|
+
|
8
|
+
%w(debug info warn error fatal).each do |ll|
|
9
|
+
define_method(ll) do |str|
|
10
|
+
@coordinator.each do
|
11
|
+
run_locally { self.send(ll, "[consul-client] #{str}") }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Latch
|
18
|
+
def self.set_instance(consul_url, options={})
|
19
|
+
@_instance ||= p(new(consul_url, options))
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.instance
|
23
|
+
@_instance
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(consul_url, options={})
|
27
|
+
@logger = SSHKittyLogger.new
|
28
|
+
@client = begin
|
29
|
+
consul = URI.parse(consul_url)
|
30
|
+
Consul::Client.v1.http(host: consul.host, port: consul.port, logger: @logger)
|
31
|
+
end
|
32
|
+
@lock_key = options[:consul_lock_key] || 'deployment/locked'
|
33
|
+
@session_id = nil
|
34
|
+
end
|
35
|
+
attr_reader :client, :lock_key
|
36
|
+
|
37
|
+
def session_request
|
38
|
+
{
|
39
|
+
"LockDelay" => 15,
|
40
|
+
"Name" => "lock-for-#{lock_key.tr("/", "-")}",
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def locked?
|
45
|
+
r = client.get("/kv/#{lock_key}")
|
46
|
+
!!(Base64.decode64(r[0]['Value']) =~ /\A["'](t(rue)?|1|y(es)?)["']\z/)
|
47
|
+
rescue Consul::Client::ResponseException => e
|
48
|
+
# in case of 404
|
49
|
+
if e.message.include?('404')
|
50
|
+
return false
|
51
|
+
else
|
52
|
+
raise e
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_session
|
57
|
+
@logger.debug "Session request: #{session_request.inspect}"
|
58
|
+
r = client.put("/session/create", session_request)
|
59
|
+
@session_id = r['ID']
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete_session
|
63
|
+
with_session {
|
64
|
+
client.put("/session/destroy/#{@session_id}", "")
|
65
|
+
}
|
66
|
+
@session_id = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def lock
|
70
|
+
with_session { client.put("/kv/#{lock_key}?acquire=#{@session_id}", "true") }
|
71
|
+
end
|
72
|
+
|
73
|
+
def unlock
|
74
|
+
with_session { client.put("/kv/#{lock_key}?release=#{@session_id}", "false") }
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
def with_session &blk
|
79
|
+
return false unless @session_id
|
80
|
+
blk.call
|
81
|
+
return true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -4,68 +4,51 @@ require 'base64'
|
|
4
4
|
require 'consul/client'
|
5
5
|
|
6
6
|
namespace :consul_kv_lock do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
%w(debug info warn error fatal).each do |ll|
|
13
|
-
define_method(ll) do |str|
|
14
|
-
@coordinator.each do
|
15
|
-
run_locally { self.send(ll, "[consul-client] #{str}") }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
7
|
+
def latch
|
8
|
+
Capistrano::ConsulKvLock::Latch.instance || \
|
9
|
+
Capistrano::ConsulKvLock::Latch.set_instance(fetch(:consul_url), consul_lock_key: fetch(:consul_lock_key))
|
19
10
|
end
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def lock_key
|
29
|
-
fetch(:consul_lock_key)
|
12
|
+
task :check_lock do
|
13
|
+
if latch.locked?
|
14
|
+
fail("Deployment is locked!")
|
15
|
+
end
|
30
16
|
end
|
31
17
|
|
32
|
-
|
33
|
-
|
34
|
-
!!(Base64.decode64(r[0]['Value']) =~ /\A["'](t(rue)?|1|y(es)?)["']\z/)
|
35
|
-
rescue Consul::Client::ResponseException => e
|
36
|
-
# in case of 404
|
37
|
-
if e.message.include?('404')
|
38
|
-
return false
|
39
|
-
else
|
40
|
-
raise e
|
41
|
-
end
|
18
|
+
task :start_session do
|
19
|
+
latch.create_session
|
42
20
|
end
|
43
21
|
|
44
|
-
task :
|
45
|
-
|
46
|
-
fail("Deployment is locked!")
|
47
|
-
end
|
22
|
+
task :destroy_session do
|
23
|
+
latch.delete_session
|
48
24
|
end
|
49
25
|
|
50
26
|
task :lock do
|
51
27
|
run_locally do
|
52
28
|
info("Setting lock to #{fetch(:consul_url)}")
|
53
|
-
|
29
|
+
unless latch.lock
|
30
|
+
warn("Setting lock to #{fetch(:consul_url)} failed! Skipping.")
|
31
|
+
end
|
54
32
|
end
|
55
33
|
end
|
56
34
|
|
57
35
|
task :unlock do
|
58
36
|
run_locally do
|
59
37
|
info("Deleting lock from #{fetch(:consul_url)}")
|
60
|
-
|
38
|
+
unless latch.unlock
|
39
|
+
warn("Deleting lock from #{fetch(:consul_url)} failed! Skipping.")
|
40
|
+
end
|
61
41
|
end
|
62
42
|
end
|
63
43
|
end
|
64
44
|
|
65
45
|
before 'deploy:starting', 'consul_kv_lock:lock'
|
66
|
-
before 'consul_kv_lock:lock', 'consul_kv_lock:
|
46
|
+
before 'consul_kv_lock:lock', 'consul_kv_lock:start_session'
|
47
|
+
before 'consul_kv_lock:start_session', 'consul_kv_lock:check_lock'
|
48
|
+
|
67
49
|
after 'deploy:finished', 'consul_kv_lock:unlock'
|
68
50
|
after 'deploy:failed', 'consul_kv_lock:unlock'
|
51
|
+
after 'consul_kv_lock:unlock', 'consul_kv_lock:destroy_session'
|
69
52
|
|
70
53
|
namespace :load do
|
71
54
|
task :defaults do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capistrano-consul_kv_lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Uchio KONDO
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- capistrano-consul_kv_lock.gemspec
|
83
83
|
- lib/capistrano-consul_kv_lock.rb
|
84
84
|
- lib/capistrano/consul_kv_lock.rb
|
85
|
+
- lib/capistrano/consul_kv_lock/latch.rb
|
85
86
|
- lib/capistrano/consul_kv_lock/version.rb
|
86
87
|
- lib/capistrano/tasks/consul_kv_lock.rake
|
87
88
|
homepage: https://github.com/udzura/capistrano-consul_kv_lock
|