consul_do 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: 2a1c3d214b88585c7c81c78fd7c2259bb3649f90
4
+ data.tar.gz: 90c3338c79704716ff7846f3d82c410645ce4d10
5
+ SHA512:
6
+ metadata.gz: b98f75d271bfb21af5d4bef9eb855c5573c31b41d058e8acd91684adc88e300fb38baf5652764a793d22ab2b4f6588100be56bd98eb7945dfcfebbcae3025413
7
+ data.tar.gz: a7b9091b66e273b8b8fc8d1aa743df4f6538fd53b0e712dff2eda242100eef54a7e23ba9bd1822f1ce7ffa95a2386786974800cea8f938f0c7fca3998d091f15
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
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.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consul_do.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Goldstar Events, Inc.
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,55 @@
1
+ # ConsulDo
2
+
3
+ Consul key-based leader election and task coordinator.
4
+
5
+ consul-do is a simple command-line tool that will return a 0 or 1 exit status
6
+ depending on wether or not the host it's running on holds the lock for the consul
7
+ key in question. See https://www.consul.io/docs/guides/leader-election.html for a
8
+ general overview of the consul leader election process.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'consul_do'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install consul_do
25
+
26
+ ## Usage
27
+
28
+ $ consul-do --help
29
+ Usage: consul-do OPTIONS
30
+ -k, --key KEY Coordination key
31
+ -h, --consul-host HOST Consul hostname
32
+ -p, --consul-port PORT Consul port
33
+ --http_proxy http://HOST:PORT
34
+ Use supplied proxy instead of ENV
35
+
36
+ $ consul-do -k my_key && echo "do stuff"
37
+ do stuff
38
+
39
+ $ consul-do -k not_my_key && echo "don't do stuff"
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ 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).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/consul_do.
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
55
+
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_do"
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
data/consul_do.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'consul_do/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "consul_do"
8
+ spec.version = ConsulDo::VERSION
9
+ spec.authors = ["Jason Scholl"]
10
+ spec.email = ["jscholl@goldstar.com"]
11
+
12
+ spec.summary = %q{Consul key-based leader elections.}
13
+ spec.description = %q{Consul key-based leader elections and task coordination.}
14
+ spec.homepage = "https://github.com/goldstar/consul_do"
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", "~> 3.3"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ spec.add_development_dependency "vcr", "~> 2.9"
27
+ spec.add_development_dependency "webmock", "~> 1.21"
28
+ spec.add_development_dependency "simplecov", "~> 0.10"
29
+ end
data/exe/consul-do ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'consul_do'
4
+
5
+ ConsulDo.elect.get_lock
6
+ exit_code = (ConsulDo.elect.is_leader?) ? 0 : 1
7
+ ConsulDo.elect.cleanup
8
+
9
+ exit exit_code
data/lib/consul_do.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "consul_do/version"
2
+ require "consul_do/elect"
3
+ require "consul_do/config"
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ module ConsulDo
8
+ def self.config
9
+ @config ||= Config.new
10
+ end
11
+
12
+ def self.configure!
13
+ @config = Config.new
14
+ yield config
15
+ end
16
+
17
+ def self.elect
18
+ @elect ||= Elect.new
19
+ end
20
+
21
+ def self.http_put(dest_url, data = nil)
22
+ uri = URI.parse(dest_url)
23
+ request = config.http_client.new(uri.host, uri.port)
24
+ log "http_put", request.send_request('PUT', "#{ [uri.path, uri.query].compact.join('?') }", data.to_json, {'Content-type' => 'application/json'})
25
+ end
26
+
27
+ def self.http_get(dest_url)
28
+ log "http_get", config.http_client.get_response(URI(dest_url))
29
+ end
30
+
31
+ def self.log(msg, retval)
32
+ puts [msg,retval.to_s].join(":\n ") if config.verbose
33
+ retval
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ require 'optparse'
2
+
3
+ module ConsulDo
4
+ class Config
5
+
6
+ attr_accessor :key, :host, :port, :http_proxy, :verbose
7
+
8
+ def initialize
9
+ @host = 'localhost'
10
+ @port = '8500'
11
+ @key = 'consul_do'
12
+ @http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
13
+ parse
14
+ end
15
+
16
+ def parse
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Usage: consul-do OPTIONS"
19
+ opts.on("-k", "--key KEY=consul_do", "Coordination key"){ |v| self.key = v }
20
+ opts.on("-h", "--consul-host HOST=localhost", "Consul hostname"){ |v| self.host = v }
21
+ opts.on("-p", "--consul-port PORT=8500", "Consul port"){ |v| self.port = v }
22
+ opts.on("--http_proxy http://HOST:PORT", "Use supplied proxy instead of ENV "){ |v| self.http_proxy = v }
23
+ opts.on("-v", "--verbose", "Consul port"){ |v| self.verbose = v }
24
+ end.parse!
25
+ end
26
+
27
+ def proxy
28
+ begin
29
+ URI.parse(http_proxy)
30
+ rescue URI::InvalidURIError
31
+ URI::Generic.new(nil,nil,nil,nil,nil,nil,nil,nil,nil)
32
+ end
33
+ end
34
+
35
+ def session_name
36
+ "consul-do_#{key}_session"
37
+ end
38
+
39
+ def http_client
40
+ if ConsulDo.config.proxy.host && ConsulDo.config.proxy.port
41
+ ConsulDo.log "http_client", Net::HTTP.Proxy(ConsulDo.config.proxy.host, ConsulDo.config.proxy.port, ConsulDo.config.proxy.user, ConsulDo.config.proxy.password)
42
+ else
43
+ ConsulDo.log "http_client", Net::HTTP
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,100 @@
1
+ require 'json'
2
+
3
+ module ConsulDo
4
+ class Elect
5
+
6
+ def initialize()
7
+ yield ConsulDo.configure! if block_given?
8
+ end
9
+
10
+ def base_url
11
+ "http://#{ConsulDo.config.host}:#{ConsulDo.config.port}"
12
+ end
13
+
14
+ def get_key
15
+ url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader"
16
+ response = ConsulDo.http_get(url)
17
+ case response
18
+ when Net::HTTPSuccess
19
+ ConsulDo.log "get_key", JSON.parse(response.body).first
20
+ else
21
+ ConsulDo.log "get_key", {}
22
+ end
23
+ end
24
+
25
+ def get_session_info(session_id)
26
+ url = "#{base_url}/v1/session/info/#{session_id}"
27
+ response = ConsulDo.http_get(url)
28
+ if response.body == "null"
29
+ raise "Invalid Session"
30
+ else
31
+ ConsulDo.log "get_session_info", JSON.parse(response.body).first
32
+ end
33
+ end
34
+
35
+ def create_session
36
+ url = "#{base_url}/v1/session/create"
37
+ response = ConsulDo.http_put(url, {"name" => ConsulDo.config.session_name})
38
+ case response
39
+ when Net::HTTPSuccess
40
+ ConsulDo.log "create_session", JSON.parse(response.body)['ID']
41
+ else
42
+ raise "Could not create session: #{response.code} - #{response.message} (#{response.body})"
43
+ end
44
+ end
45
+
46
+ def session
47
+ ConsulDo.log "session", @session ||= create_session
48
+ end
49
+
50
+ def delete_session(session_id = session)
51
+ url = "#{base_url}/v1/session/destroy/#{session_id}"
52
+ response = ConsulDo.http_put(url)
53
+ case response
54
+ when Net::HTTPSuccess
55
+ @session = nil
56
+ else
57
+ raise "Could not delete session: #{response.code} - #{response.message} (#{response.body})"
58
+ end
59
+
60
+ response
61
+ end
62
+
63
+ def delete_sessions
64
+ url = "#{base_url}/v1/session/node/#{get_session_info(session)['Node']}"
65
+ response = ConsulDo.http_get(url)
66
+ JSON.parse(response.body).each do |session_hash|
67
+ if block_given?
68
+ delete_session(session_hash['ID']) if yield session_hash['Name']
69
+ else
70
+ delete_session(session_hash['ID']) if session_hash['Name'] == ConsulDo.config.session_name
71
+ end
72
+ end
73
+ end
74
+
75
+ def get_lock
76
+ url = "#{base_url}/v1/kv/service/#{ConsulDo.config.key}/leader?acquire=#{session}"
77
+ response = ConsulDo.http_put(url, {'updated' => Time.now})
78
+ @session_has_lock = true if response.body == "true"
79
+ end
80
+
81
+ def session_has_lock?
82
+ @session_has_lock
83
+ end
84
+
85
+ def is_leader?
86
+ leader_session = get_key['Session']
87
+ if (leader_session &&
88
+ (session_has_lock? || get_session_info(leader_session)['Node'] == get_session_info(session)['Node']))
89
+ ConsulDo.log "is_leader?", true
90
+ else
91
+ ConsulDo.log "is_leader?", false
92
+ end
93
+ end
94
+
95
+ def cleanup
96
+ delete_session unless session_has_lock?
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module ConsulDo
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consul_do
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: 2015-09-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: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
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.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: vcr
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.21'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.10'
111
+ description: Consul key-based leader elections and task coordination.
112
+ email:
113
+ - jscholl@goldstar.com
114
+ executables:
115
+ - consul-do
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - consul_do.gemspec
129
+ - exe/consul-do
130
+ - lib/consul_do.rb
131
+ - lib/consul_do/config.rb
132
+ - lib/consul_do/elect.rb
133
+ - lib/consul_do/version.rb
134
+ homepage: https://github.com/goldstar/consul_do
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.4.5
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Consul key-based leader elections.
158
+ test_files: []