module DockerToolkit::Runner

Public Instance Methods

die(message) click to toggle source
# File lib/docker_toolkit/runner.rb, line 24
def die(message)
  error(message)
  exit 1
end
ensure_env_defined!(key, env = ENV) click to toggle source
# File lib/docker_toolkit/runner.rb, line 55
def ensure_env_defined!(key, env = ENV)
  env.key?(key) || die("Environment variable #{key} must present!")
  env[key]
end
envsubst(*paths) click to toggle source
# File lib/docker_toolkit/runner.rb, line 60
def envsubst(*paths)
  paths = paths.flatten.map{|c| c.to_s.strip }.reject(&:empty?)
  paths.each do |path|
    Dir.glob("#{path}/**/*.in") do |templ|
      output = templ.sub(/\.in$/, '')
      cmd = "cat '#{templ}' | envsubst > '#{output}'"
      system(cmd) || die("envsubst failed: #{cmd}")
    end
  end
end
envsubst_file(templ, output = nil) click to toggle source
# File lib/docker_toolkit/runner.rb, line 71
def envsubst_file(templ, output = nil)
  output ||= templ.sub(/\.in$/, '')
  die('filename must ends with .in or output must be provided') if output.strip == templ.strip
  cmd = "cat '#{templ}' | envsubst > '#{output}'"
  system(cmd) || die("envsubst failed: #{cmd}")
end
error(message) click to toggle source
# File lib/docker_toolkit/runner.rb, line 20
def error(message)
  STDERR.puts "Error: #{message}"
end
execute(cmd) click to toggle source
# File lib/docker_toolkit/runner.rb, line 42
def execute(cmd)
  puts "Executing: #{cmd}"
  output = `#{cmd}`
  @last_result = $?
  output
end
execute!(cmd, error = nil) click to toggle source
# File lib/docker_toolkit/runner.rb, line 49
def execute!(cmd, error = nil)
  output = execute(cmd)
  ($? || @last_result).success? || die(error || "Can't execute: #{cmd}")
  output
end
init_service(service) click to toggle source
# File lib/docker_toolkit/runner.rb, line 9
def init_service(service)
  STDOUT.sync = true
  STDERR.sync = true

  puts "Starting #{service}..."

  trap('EXIT') do
    puts "Stopping #{service}..."
  end
end
load_envs(string, env = ENV) click to toggle source
# File lib/docker_toolkit/runner.rb, line 29
def load_envs(string, env = ENV)
  envs = Dotenv::Parser.new(string).call
  envs.each_pair do |k, v|
    env[k] = v
  end
  envs
end
load_envs_from_consul(consul, service) click to toggle source
# File lib/docker_toolkit/runner.rb, line 37
def load_envs_from_consul(consul, service)
  envs = execute!("consul.rb --consul=http://#{consul}:8500 --env services/env/#{service} --pristine -d", "Can't load envs")
  load_envs(envs)
end