Class: HerokuRails::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku-rails/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
# File 'lib/heroku-rails/runner.rb', line 5

def initialize(config)
  @config = config
  @environments = []
end

Instance Method Details

#add_environment(env) ⇒ Object

add a specific environment to the run list



15
16
17
# File 'lib/heroku-rails/runner.rb', line 15

def add_environment(env)
  @environments << env
end

#all_environmentsObject

use all environments



20
21
22
# File 'lib/heroku-rails/runner.rb', line 20

def all_environments
  @environments = @config.app_environments
end

#authorizeObject



10
11
12
# File 'lib/heroku-rails/runner.rb', line 10

def authorize
  @heroku ||= Heroku::Auth.client
end

#command(*args) ⇒ Object



268
269
270
# File 'lib/heroku-rails/runner.rb', line 268

def command(*args)
  system(*args)
end

#creation_command(*args) ⇒ Object



247
248
249
# File 'lib/heroku-rails/runner.rb', line 247

def creation_command(*args)
  system_with_echo(*args)
end

#destroy_command(*args) ⇒ Object



251
252
253
254
255
# File 'lib/heroku-rails/runner.rb', line 251

def destroy_command(*args)
  # puts args.join(' ')
  @destroy_commands ||= []
  @destroy_commands << args.join(' ')
end

#each_heroku_appObject

cycles through each configured heroku app yields the environment name, the app name, and the repo url



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/heroku-rails/runner.rb', line 209

def each_heroku_app

  if @config.apps.size == 0
    puts "\nNo heroku apps are configured. Run:
      rails generate heroku:config\n\n"
    puts "this will generate a default config/heroku.yml that you should edit"
    puts "and then try running this command again"

    exit(1)
  end

  if @environments.blank? && @config.apps.size == 1
    @environments = [@config.app_environments.first]
  end

  if @environments.present?
    @environments.each do |heroku_env|
      app_name = @config.apps[heroku_env]
      yield(heroku_env, app_name, "[email protected]:#{app_name}.git")
    end
  else
    puts "\nYou must first specify at least one Heroku app:
      rake <app> [<app>] <command>
      rake production restart
      rake demo staging deploy"

    puts "\n\nYou can use also command all Heroku apps for this project:
      rake all heroku:setup\n"

    exit(1)
  end
end

#output_destroy_commands(app) ⇒ Object



257
258
259
260
261
262
263
264
265
266
# File 'lib/heroku-rails/runner.rb', line 257

def output_destroy_commands(app)
  puts "The #{app} had a few things removed from the heroku.yml."
  puts "If they are no longer neccessary, then run the following commands:\n\n"
  (@destroy_commands || []).each do |destroy_command|
    puts destroy_command
  end
  puts "\n\nthese commands may cause data loss so make sure you know that these are necessary"
  # clear destroy commands
  @destroy_commands = []
end

#setup_addonsObject

setup the addons for heroku



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
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/heroku-rails/runner.rb', line 133

def setup_addons
  authorize
  each_heroku_app do |heroku_env, app_name, repo|
    # get the addons that we are aiming towards
    addons = @config.addons(heroku_env)

    # get the addons that are already on the servers
    existing_addons = (@heroku.installed_addons(app_name) || []).map{|a| a["name"]}

    # all apps need the shared database
    addons << "shared-database:5mb" unless addons.index("shared-database:5mb") || addons.index("shared-database:20gb")

    # add "custom_domains" if that addon doesnt already exist
    # and we have domains configured for this app
    addons << "custom_domains:basic" unless @config.domains(heroku_env).empty? or
                                            addons.any?{|a| a =~ /custom_domains/} or
                                            existing_addons.any?{|a| a =~ /custom_domains/}

    # remove the addons that need to be removed
    existing_addons.each do |existing_addon|
      # check to see if we need to delete this addon
      unless addons.include?(existing_addon)
        # delete this addon if they arent on the approved list
        destroy_command "heroku addons:remove #{existing_addon} --app #{app_name}"
      end
    end

    # add the addons that dont exist already
    addons.each do |addon|
      # check to see if we need to add this addon
      unless existing_addons.include?(addon)
        # add this addon if they are not already added
        creation_command "heroku addons:add #{addon} --app #{app_name}"
      end
    end

    # display the destructive commands
    output_destroy_commands(app_name)
  end
end

#setup_appsObject

setup apps (create if necessary)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heroku-rails/runner.rb', line 25

def setup_apps
  authorize

  # get a list of all my current apps on Heroku (so we don't create dupes)
  @my_apps = @heroku.list.map{|a| a.first}

  each_heroku_app do |heroku_env, app_name, repo|
    next if @my_apps.include?(app_name)

    stack = @config.stack(heroku_env)
    stack_option = " --stack #{stack}" if stack.to_s.size > 0
    creation_command "heroku create #{app_name}#{stack_option} --remote #{app_name}"
  end
end

#setup_collaboratorsObject

setup the list of collaborators



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
# File 'lib/heroku-rails/runner.rb', line 59

def setup_collaborators
  authorize
  each_heroku_app do |heroku_env, app_name, repo|
    # get the remote info about the app from heroku
    heroku_app_info = @heroku.info(app_name) || {}

    # get the intended list of collaborators to add
    collaborator_emails = @config.collaborators(heroku_env)

    # add current user to collaborator list (always)
    collaborator_emails << @heroku.user unless collaborator_emails.include?(@heroku.user)
    collaborator_emails << heroku_app_info[:owner] unless collaborator_emails.include?(heroku_app_info[:owner])

    # get existing collaborators
    existing_emails = heroku_app_info[:collaborators].to_a.map{|c| c[:email]}

    # get the list of collaborators to delete
    existing_emails.each do |existing_email|
      # check to see if we need to delete this person
      unless collaborator_emails.include?(existing_email)
        # delete that collaborator if they arent on the approved list
        destroy_command "heroku sharing:remove #{existing_email} --app #{app_name}"
      end
    end

    # get the list of collaborators to add
    collaborator_emails.each do |collaborator_email|
      # check to see if we need to add this person
      unless existing_emails.include?(collaborator_email)
        # add the collaborator if they are not already on the server
        creation_command "heroku sharing:add #{collaborator_email} --app #{app_name}"
      end
    end

    # display the destructive commands
    output_destroy_commands(app_name)
  end
end

#setup_configObject

setup configuration



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
# File 'lib/heroku-rails/runner.rb', line 99

def setup_config
  authorize
  each_heroku_app do |heroku_env, app_name, repo|
    # get the configuration that we are aiming towards
    new_config = @config.config(heroku_env)

    # default RACK_ENV to the heroku_env (unless its manually set to something else)
    unless new_config["RACK_ENV"].to_s.length > 0
      new_config["RACK_ENV"] = heroku_env
    end

    # get the existing config from heroku's servers
    existing_config = @heroku.config_vars(app_name) || {}

    # find the config variables to add
    add_config = {}
    new_config.each do |new_key, new_val|
      add_config[new_key] = new_val unless existing_config[new_key] == new_val
    end

    # persist the changes onto heroku
    unless add_config.empty?
      # add the config
      set_config = ""
      add_config.each do |key, val|
        set_config << "#{key}='#{val}' "
      end

      creation_command "heroku config:add #{set_config} --app #{app_name}"
    end
  end
end

#setup_domainsObject

setup the domains for heroku



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/heroku-rails/runner.rb', line 175

def setup_domains
  authorize
  each_heroku_app do |heroku_env, app_name, repo|
    # get the domains that we are aiming towards
    domains = @config.domains(heroku_env)

    # get the domains that are already on the servers
    existing_domains = (@heroku.list_domains(app_name) || []).map{|a| a[:domain]}

    # remove the domains that need to be removed
    existing_domains.each do |existing_domain|
      # check to see if we need to delete this domain
      unless domains.include?(existing_domain)
        # delete this domain if they arent on the approved list
        destroy_command "heroku domains:remove #{existing_domain} --app #{app_name}"
      end
    end

    # add the domains that dont exist already
    domains.each do |domain|
      # check to see if we need to add this domain
      unless existing_domains.include?(domain)
        # add this domain if they are not already added
        creation_command "heroku domains:add #{domain} --app #{app_name}"
      end
    end

    # display the destructive commands
    output_destroy_commands(app_name)
  end
end

#setup_stacksObject

setup the stacks for each app (migrating if necessary)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/heroku-rails/runner.rb', line 41

def setup_stacks
  authorize
  each_heroku_app do |heroku_env, app_name, repo|
    # get the intended stack setting
    stack = @config.stack(heroku_env)

    # get the remote info about the app from heroku
    heroku_app_info = @heroku.info(app_name) || {}

    # if the stacks don't match, then perform a migration
    if stack != heroku_app_info[:stack]
      puts "Migrating the app: #{app_name} to the stack: #{stack}"
      creation_command "heroku stack:migrate #{stack} --app #{app_name}"
    end
  end
end

#system_with_echo(*args) ⇒ Object



242
243
244
245
# File 'lib/heroku-rails/runner.rb', line 242

def system_with_echo(*args)
  puts args.join(' ')
  command(*args)
end