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
56 57 58 59 |
# File 'lib/aggkit/runner.rb', line 56 def ensure_env_defined!(key, env = ENV) env.key?(key) || die("Environment variable #{key} must present!") env[key] end |
#envsubst(*paths) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aggkit/runner.rb', line 61 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
72 73 74 75 76 77 |
# File 'lib/aggkit/runner.rb', line 72 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
43 44 45 46 47 48 |
# File 'lib/aggkit/runner.rb', line 43 def execute(cmd) puts "Executing: #{cmd}" output = `#{cmd}` @last_result = $? output end |
#execute!(cmd, error = nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/aggkit/runner.rb', line 50 def execute!(cmd, error = nil) output = execute(cmd) ($? || @last_result).success? || die(error || "Can't execute: #{cmd}") 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 |
# File 'lib/aggkit/runner.rb', line 38 def load_envs_from_consul(consul, service) envs = execute!("aggconsul --consul=http://#{consul}:8500 --env services/env/#{service} --pristine -d", "Can't load envs") load_envs(envs) end |