Module: Aggkit::Runner
- Defined in:
- lib/aggkit/runner.rb
Instance Method Summary collapse
- #die(message) ⇒ Object
- #ensure_env_defined!(key, env = ENV) ⇒ Object
- #envsubst(*paths) ⇒ Object
- #envsubst_file(templ, output = nil) ⇒ Object
- #error(message) ⇒ Object
- #execute(cmd) ⇒ Object
- #execute!(cmd, error = nil) ⇒ Object
- #init_service(service, options = nil) ⇒ Object
- #load_envs(string, env = ENV) ⇒ Object
- #load_envs_from_consul(consul, service) ⇒ Object
Instance Method Details
#die(message) ⇒ Object
25 26 27 28 |
# File 'lib/aggkit/runner.rb', line 25 def die() error() exit 1 end |
#ensure_env_defined!(key, env = ENV) ⇒ Object
72 73 74 75 |
# File 'lib/aggkit/runner.rb', line 72 def ensure_env_defined!(key, env = ENV) env.key?(key) || die("Environment variable #{key} must present!") env[key] end |
#envsubst(*paths) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/aggkit/runner.rb', line 77 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
88 89 90 91 92 93 |
# File 'lib/aggkit/runner.rb', line 88 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
21 22 23 |
# File 'lib/aggkit/runner.rb', line 21 def error() STDERR.puts "Error: #{}" end |
#execute(cmd) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aggkit/runner.rb', line 48 def execute(cmd) puts "Executing: #{cmd}" io = IO.popen(cmd) output = io.read begin io.close rescue StandardError nil end @last_result = $? output end |
#execute!(cmd, error = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aggkit/runner.rb', line 61 def execute!(cmd, error = nil) output = execute(cmd) if @last_result @last_result.success? || die(error || "Can't execute: #{cmd}") else die("Already collected: #{cmd}") end output end |
#init_service(service, options = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/aggkit/runner.rb', line 9 def init_service(service, = nil) STDOUT.sync = true STDERR.sync = true puts "Starting #{service}..." puts "Options: #{.inspect}" if trap('EXIT') do puts "Stopping #{service}..." end end |
#load_envs(string, env = ENV) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/aggkit/runner.rb', line 30 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
38 39 40 41 42 43 44 45 46 |
# File 'lib/aggkit/runner.rb', line 38 def load_envs_from_consul(consul, service) consul_http = if consul['http'] consul else "http://#{consul}:8500" end envs = execute!("aggconsul --consul=#{consul_http} --env services/env/#{service} --pristine -d", "Can't load envs") load_envs(envs) end |