Module: Dev::Consul
- Defined in:
- lib/dev/consul.rb,
lib/dev/consul/build.rb,
lib/dev/consul/version.rb
Overview
Helpers to fetch and run a development-instance of consul
Defined Under Namespace
Modules: Build
Constant Summary collapse
- RELEASE =
'3'.freeze
- VERSION =
'0.6.4'.freeze
Class Attribute Summary collapse
-
.output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
- .architecture ⇒ Object
- .bin ⇒ Object
- .bindir ⇒ Object
- .block ⇒ Object
-
.log(*message) ⇒ Object
Logging helper.
- .platform ⇒ Object
- .run(**options) ⇒ Object
- .stop ⇒ Object
- .wait ⇒ Object
Class Attribute Details
.output ⇒ Object (readonly)
Returns the value of attribute output.
39 40 41 |
# File 'lib/dev/consul.rb', line 39 def output @output end |
Class Method Details
.architecture ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/dev/consul.rb', line 15 def architecture case RUBY_PLATFORM when /x86_64/ then 'amd64' when /amd64/ then 'amd64' when /386/ then '386' when /arm/ then 'arm' else raise NameError, "Unable to detect system architecture for #{RUBY_PLATFORM}" end end |
.bin ⇒ Object
35 36 37 |
# File 'lib/dev/consul.rb', line 35 def bin File.join(bindir, "consul_#{VERSION}_#{platform}_#{architecture}") end |
.bindir ⇒ Object
11 12 13 |
# File 'lib/dev/consul.rb', line 11 def bindir File.('../../bin', __dir__) end |
.block ⇒ Object
101 102 103 |
# File 'lib/dev/consul.rb', line 101 def block @thread.join unless @thread.nil? end |
.log(*message) ⇒ Object
Logging helper
42 43 44 45 46 47 |
# File 'lib/dev/consul.rb', line 42 def log(*) return unless output.is_a?(IO) output.write(.join(' ') + "\n") output.flush end |
.platform ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/dev/consul.rb', line 25 def platform case RUBY_PLATFORM when /darwin/ then 'darwin' when /freebsd/ then 'freebsd' when /linux/ then 'linux' when /solaris/ then 'solaris' else raise NameError, "Unable to detect system platfrom for #{RUBY_PLATFORM}" end end |
.run(**options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dev/consul.rb', line 49 def run(**) @output = .fetch(:output, $stdout) log "Starting #{bin}" ## Fork a child process for Consul from a thread @stopped = false @thread = Thread.new do IO.popen("#{bin} agent -dev -advertise=127.0.0.1", 'r+') do |io| Thread.current[:process] = io.pid log "Started #{bin} (#{io.pid})" ## Stream output loop do break if io.eof? chunk = io.readpartial(1024) next unless output.is_a?(IO) output.write(chunk) output.flush end end end self end |
.stop ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dev/consul.rb', line 105 def stop unless @thread.nil? unless @thread[:process].nil? log "Stop #{bin} (#{@thread[:process]})" Process.kill('TERM', @thread[:process]) end @thread.join end @thread = nil @stopped = true end |
.wait ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dev/consul.rb', line 76 def wait ## Wait for the service to become ready loop do break if @stopped || @thread.nil? || !@thread.alive? leader = begin Net::HTTP.get('localhost', '/v1/status/leader', 8500) rescue Errno::ECONNREFUSED log 'Waiting for Consul HTTP API to be ready' sleep 1 next end if leader == '""' log 'Waiting for RAFT to initialize' sleep 1 next end log 'Consul HTTP API is ready!' break end end |