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
9
10
11
# File 'lib/chorizo.rb', line 6

def initialize
  @host_names = %w(cloud66 heroku)

  config_file = './config/chorizo.yml'
  @config = YAML.load_file(config_file) if File.exist?(config_file)
end

Instance Method Details

#build_output(env, host) ⇒ Object



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

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



87
88
89
90
91
92
93
# File 'lib/chorizo.rb', line 87

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



105
106
107
108
109
110
111
112
113
# File 'lib/chorizo.rb', line 105

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



95
96
97
98
99
100
101
102
103
# File 'lib/chorizo.rb', line 95

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

#load_configObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chorizo.rb', line 13

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

#run(env, opts, target: nil, app: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chorizo.rb', line 53

def run(env, opts, target: nil, app: nil)
  if @config
    if target && target != @config[env]['target']
      STDERR.puts 'WARNING: target differs from configuration'.red
    end
    target ||= @config[env]['target']

    if app && app != @config[env]['app']
      STDERR.puts 'WARNING: app differs from configuration'.red
    end
    app ||= @config[env]['app']
  end

  unless target && @host_names.include?(target)
    error = "please specify a valid target [#{@host_names.join(', ')}]"
    STDERR.puts(error.red)
    puts opts
    exit 1
  end

  case target
  when 'cloud66'
    cloud66(env)
  when 'heroku'
    unless app
      STDERR.puts 'please specify an app for heroku'.red
      puts opts
      exit 1
    end

    heroku(env, app)
  end
end