Module: DockerToolkit::Runner

Defined in:
lib/docker_toolkit/runner.rb

Instance Method Summary collapse

Instance Method Details

#die(message) ⇒ Object



24
25
26
27
# File 'lib/docker_toolkit/runner.rb', line 24

def die(message)
  error(message)
  exit 1
end

#ensure_env_defined!(key, env = ENV) ⇒ Object



55
56
57
58
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
75
76
# 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) ⇒ Object



20
21
22
# File 'lib/docker_toolkit/runner.rb', line 20

def error(message)
  STDERR.puts "Error: #{message}"
end

#execute(cmd) ⇒ Object



42
43
44
45
46
47
# File 'lib/docker_toolkit/runner.rb', line 42

def execute(cmd)
  puts "Executing: #{cmd}"
  output = `#{cmd}`
  @last_result = $?
  output
end

#execute!(cmd, error = nil) ⇒ Object



49
50
51
52
53
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
# 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