Class: Chorizo

Inherits:
Object
  • Object
show all
Defined in:
lib/chorizo.rb

Instance Method Summary collapse

Constructor Details

#initializeChorizo

Returns a new instance of Chorizo.



6
7
8
# File 'lib/chorizo.rb', line 6

def initialize
  @host_names = %w(cloud66 heroku)
end

Instance Method Details

#build_output(env, host) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chorizo.rb', line 22

def build_output(env, host)
  configs = load_config
  output = configs[:base]

  # load environment config
  if configs[:envs][env]
    output.merge! configs[:envs][env]
  else
    STDERR.puts "WARNING: #{env} specific configuration not found".red
  end

  # load host config
  if configs[:hosts][host]
    hc = configs[:hosts][host]
    # load embedded env config in host config, if present
    hc_envs, hc_base = hc.partition { |k,v| v.is_a?(Hash) }
    hc_env = hc_envs.to_h[env]
    hc_output = hc_base.to_h
    hc_output.merge! hc_env if hc_env

    output.merge! hc_output
  else
    STDERR.puts "WARNING: #{host} specific configuration not found".red
  end

  output
end

#cloud66(env) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/chorizo.rb', line 50

def cloud66(env)
  output = build_output(env, 'cloud66')
  output.each do |k,v|
    value = decrypt_value(v)
    puts "#{k.upcase}=#{value}"
  end
end

#decrypt_value(value) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/chorizo.rb', line 68

def decrypt_value(value)
  if value =~ /^ENC\[/
    Open3.popen2("eyaml", "decrypt", "-s", value) do |i, o, t|
      o.read.chomp
    end
  else
    value
  end
end

#heroku(env, app) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/chorizo.rb', line 58

def heroku(env, app)
  output = build_output(env, 'heroku')
  cmd_output = output.map do |k,v|
    value = decrypt_value(v)
    escaped_value = "#{v}".shellescape
    "#{k}=#{escaped_value}"
  end.join(' ')
  system "heroku config:set #{cmd_output} -a #{app}"
end

#load_configObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chorizo.rb', line 10

def load_config
  yml = YAML.load_file('./config/application.yml')
  hashes, base = yml.partition { |k,v| v.is_a?(Hash) }
  hashes = hashes.to_h
  base = base.to_h
  hosts = {}
  @host_names.each do |host|
    hosts[host] = hashes.delete(host) if hashes[host]
  end
  { base: base, hosts: hosts, envs: hashes } 
end