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 =
'2'.freeze
VERSION =
'0.6.4'.freeze

Class Method Summary collapse

Class Method Details

.architectureObject



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

.binObject



35
36
37
# File 'lib/dev/consul.rb', line 35

def bin
  File.join(bindir, "consul_#{VERSION}_#{platform}_#{architecture}")
end

.bindirObject



11
12
13
# File 'lib/dev/consul.rb', line 11

def bindir
  File.expand_path('../../bin', __dir__)
end

.output(arg = nil) ⇒ Object



39
40
41
42
# File 'lib/dev/consul.rb', line 39

def output(arg = nil)
  @thread[:output] = arg unless @thread.nil? || arg.nil?
  @thread[:output] unless @thread.nil?
end

.platformObject



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

.runObject



44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dev/consul.rb', line 44

def run
  puts "Starting #{bin}"

  ## Fork a child process for Consul from a thread
  @thread = Thread.new do
    IO.popen("#{bin} agent -dev -advertise=127.0.0.1", 'r+') do |io|
      Thread.current[:process] = io.pid
      puts "Started #{bin} (#{io.pid})"

      ## Stream output
      loop do
        break if io.eof?
        chunk = io.readpartial(1024)

        if Thread.current[:output]
          Thread.current[:output].write(chunk)
          Thread.current[:output].flush
        end
      end
    end
  end

  @thread[:output] = $stdout

  ## Wait for the service to become ready
  loop do
    begin
      leader = Net::HTTP.get('localhost', '/v1/status/leader', 8500)

      if leader == '""'
        puts 'Waiting for Consul HTTP API to be ready'
        sleep 1

        next
      end

      puts 'Consul HTTP API is ready!'
      break

    rescue Errno::ECONNREFUSED
      puts 'Waiting for Consul HTTP API to be ready'
      sleep 1
    end
  end
end

.stopObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dev/consul.rb', line 94

def stop
  unless @thread.nil?
    unless @thread[:process].nil?
      puts "Stop #{bin} (#{@thread[:process]})"
      Process.kill('INT', @thread[:process])
    end

    @thread.join
  end

  @thread = nil
end

.waitObject



90
91
92
# File 'lib/dev/consul.rb', line 90

def wait
  @thread.join unless @thread.nil?
end