bootstrap-influxdb 1.2 KB
Newer Older
1 2 3
#!/usr/bin/env ruby
require 'socket'

4 5
HOST = '127.0.0.1'.freeze
DATABASE = 'gitlab'.freeze
6

7
def wait_for_socket(host, port)
8 9 10
  30.times do
    begin
      TCPSocket.new(host, port)
11
      return true
12 13 14 15
    rescue Errno::ECONNREFUSED
      sleep 1
    end
  end
16 17
  false
end
18

19 20
def main(host, port)
  foreman_pid = spawn('./run', 'db')
21

22
  abort "#{$PROGRAM_NAME}: could not connect to InfluxDB at #{host}:#{port}" unless wait_for_socket(host, port)
23

24
  IO.popen(%W[influxdb/bin/influx -host #{host} -port #{port}], 'w') do |influx|
25 26
    influx.puts "CREATE DATABASE #{DATABASE} WITH duration 1d REPLICATION 1;"
  end
27

28
  abort "#{$PROGRAM_NAME}: failed to create InfluxDB database #{DATABASE}" unless $?.success?
29 30

  env = {
31 32
    'INFLUX_HOST' => host.to_s,
    'INFLUX_PORT' => port.to_s,
33 34
    'INFLUX_USER' => "",
    'INFLUX_PASSWORD' => "",
35
    'INFLUX_DATABASE' => DATABASE.to_s
36
  }
37
  unless system(env, *%w[bundle exec rake], chdir: 'influxdb/influxdb-management')
38
    abort "#{$PROGRAM_NAME}: failed to run influxdb-managemant Rake script"
39
  end
40 41
ensure
  Process.kill('TERM', foreman_pid)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
42
  Process.waitpid(foreman_pid)
43 44
end

45
abort "Usage: #{$PROGRAM_NAME} INFLUXDB_HTTP_PORT" if ARGV.count != 1
46 47 48 49

port = ARGV.shift

main(HOST, port)