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
27 28 29 30 |
# File 'lib/aggkit/runner.rb', line 27 def die() error() exit 1 end |
#ensure_env_defined!(key, env = ENV) ⇒ Object
58 59 60 61 |
# File 'lib/aggkit/runner.rb', line 58 def ensure_env_defined!(key, env = ENV) env.key?(key) || die("Environment variable #{key} must present!") env[key] end |
#envsubst(*paths) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/aggkit/runner.rb', line 63 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
74 75 76 77 78 79 |
# File 'lib/aggkit/runner.rb', line 74 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
23 24 25 |
# File 'lib/aggkit/runner.rb', line 23 def error() STDERR.puts "Error: #{}" end |
#execute(cmd) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/aggkit/runner.rb', line 45 def execute(cmd) puts "Executing: #{cmd}" output = `#{cmd}` @last_result = $? output end |
#execute!(cmd, error = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/aggkit/runner.rb', line 52 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 20 21 |
# File 'lib/aggkit/runner.rb', line 9 def init_service(service, = nil) STDOUT.sync = true STDERR.sync = true puts "Starting #{service}..." if puts "Options: #{.inspect}" end trap('EXIT') do puts "Stopping #{service}..." end end |
#load_envs(string, env = ENV) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/aggkit/runner.rb', line 32 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
40 41 42 43 |
# File 'lib/aggkit/runner.rb', line 40 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 |