Module: Dev::Vault

Defined in:
lib/dev/vault.rb,
lib/dev/vault/build.rb,
lib/dev/vault/version.rb

Overview

Helpers to fetch and run a development-instance of vault

Defined Under Namespace

Modules: Build

Constant Summary collapse

VERSION =
'0.5.2'.freeze

Class Method Summary collapse

Class Method Details

.architectureObject



17
18
19
20
21
22
23
24
25
# File 'lib/dev/vault.rb', line 17

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



36
37
38
# File 'lib/dev/vault.rb', line 36

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

.bindirObject



13
14
15
# File 'lib/dev/vault.rb', line 13

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

.mount(name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/dev/vault.rb', line 44

def mount(name)
  post = Net::HTTP::Post.new("/v1/sys/mounts/#{name}")
  post.body = JSON.generate(:type => name)
  post['X-Vault-Token'] = token

  Net::HTTP.new('localhost', 8200).request(post)
end

.output(arg = nil) ⇒ Object



52
53
54
55
# File 'lib/dev/vault.rb', line 52

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

.platformObject



27
28
29
30
31
32
33
34
# File 'lib/dev/vault.rb', line 27

def platform
  case RUBY_PLATFORM
  when /darwin/ then 'darwin'
  when /freebsd/ then 'freebsd'
  when /linux/ then 'linux'
  else raise NameError, "Unable to detect system platfrom for #{RUBY_PLATFORM}"
  end
end

.runObject



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dev/vault.rb', line 57

def run
  puts "Starting #{bin}"

  ## Fork a child process for Vault from a thread
  @thread = Thread.new do
    IO.popen(%(#{bin} server -dev -dev-root-token-id="#{token}"), '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
      break if @stopped

      status = Net::HTTP.get('localhost', '/v1/sys/seal-status', 8200)
      status = JSON.parse(status, :symbolize_names => true)

      if status[:sealed]
        puts 'Waiting for Vault HTTP API to be ready'
        sleep 1

        next
      end

      puts 'Vault HTTP API is ready!'
      break

    rescue Errno::ECONNREFUSED, JSON::ParseError
      puts 'Waiting for Vault HTTP API to be ready'
      sleep 1
    end
  end
end

.stopObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dev/vault.rb', line 110

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
  @stopped = true
end

.tokenObject



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

def token
  @token ||= SecureRandom.uuid
end

.waitObject



106
107
108
# File 'lib/dev/vault.rb', line 106

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