forked from jqr/heroku_san
/
heroku_san.rb
160 lines (132 loc) · 3.68 KB
/
heroku_san.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
52
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'heroku_san/railtie.rb' if defined?(Rails) && Rails::VERSION::MAJOR == 3
require 'heroku_san/git'
class HerokuSan
attr_reader :app_settings, :config_file
class NoApps < StandardError; end
class Deprecated < StandardError; end
include Git
def initialize(config_file)
@apps = []
@config_file = config_file
@app_settings = parse_yaml(config_file)
# support heroku_san format
if @app_settings.has_key? 'apps'
@app_settings = @app_settings['apps']
@app_settings.each_pair do |stage, app_name|
@app_settings[stage] = {'app' => app_name}
end
end
# load external config
if (config_repo = @app_settings.delete('config_repo'))
require 'tmpdir'
tmp_config_dir = Dir.mktmpdir
tmp_config_file = File.join tmp_config_dir, 'config.yml'
git_clone(config_repo, tmp_config_dir)
extra_config = parse_yaml(tmp_config_file)
else
extra_config = {}
end
# make sure each app has a 'config' section & merge w/extra
@app_settings.keys.each do |name|
@app_settings[name]['config'] ||= {}
@app_settings[name]['config'].merge!(extra_config[name]) if extra_config[name]
end
end
def create_config
template = File.join(File.dirname(__FILE__), 'templates', 'heroku.example.yml')
if File.exists?(@config_file)
false
else
FileUtils.cp(template, @config_file)
true
end
end
def [](stage)
@app_settings[stage]
end
def all
@app_settings.keys
end
def <<(*app)
app.flatten.each do |a|
@apps << a if all.include?(a)
end
self
end
def apps
if !@apps.empty?
@apps
else
case all.size
when 1
$stdout.puts "Defaulting to #{all.first.inspect} since only one app is defined"
all
else
active_branch = self.git_active_branch
all.select do |app|
app == active_branch and ($stdout.puts("Defaulting to '#{app}' as it matches the current branch") || true)
end
end
end
end
def each_app
raise NoApps if apps.empty?
apps.each do |stage|
yield(stage, "git@heroku.com:#{self[stage]['app']}.git", self[stage]['config'])
end
end
def stack(stage)
self[stage]['stack'] ||= %x"heroku stack --app #{self[stage]['app']}".split("\n").select { |b| b =~ /^\* / }.first.gsub(/^\* /, '')
end
def run(stage, command, args = nil)
if stack(stage) =~ /cedar/
sh_heroku stage, "run #{command} #{args}"
else
sh_heroku stage, "run:#{command} #{args}"
end
end
def create(stage)
sh "heroku apps:create #{self[stage]['app']}"
end
def migrate(stage)
run(stage, 'rake', 'db:migrate')
sh_heroku stage, "restart"
end
def maintenance(stage, action)
raise ArgumentError, "Action #{action.inspect} must be one of (:on, :off)", caller if ![:on, :off].include?(action)
sh_heroku stage, "maintenance:#{action}"
end
def sharing_add(stage, email)
sh_heroku stage, "sharing:add #{email}"
end
def sharing_remove(stage, email)
sh_heroku stage, "sharing:remove #{email}"
end
def long_config(stage)
sh_heroku stage, 'config --long'
end
def capture(stage)
sh_heroku stage, 'bundles:capture'
end
def restart(stage)
sh_heroku stage, 'restart'
end
def logs(stage)
sh_heroku stage, 'logs'
end
private
def sh_heroku stage, command
sh "heroku #{command} --app #{self[stage]['app']}"
end
def parse_yaml(config_file)
if File.exists?(config_file)
if defined?(ERB)
YAML.load(ERB.new(File.read(config_file)).result)
else
YAML.load_file(config_file)
end
else
{}
end
end
end