Class: HerokuRails::Runner
- Inherits:
-
Object
- Object
- HerokuRails::Runner
- Defined in:
- lib/heroku-rails/runner.rb
Instance Method Summary collapse
-
#add_environment(env) ⇒ Object
add a specific environment to the run list.
-
#all_environments ⇒ Object
use all environments.
- #authorize ⇒ Object
- #command(*args) ⇒ Object
- #creation_command(*args) ⇒ Object
- #destroy_command(*args) ⇒ Object
-
#each_heroku_app ⇒ Object
cycles through each configured heroku app yields the environment name, the app name, and the repo url.
-
#initialize(config) ⇒ Runner
constructor
A new instance of Runner.
- #output_destroy_commands(app) ⇒ Object
-
#setup_addons ⇒ Object
setup the addons for heroku.
-
#setup_apps ⇒ Object
setup apps (create if necessary).
-
#setup_collaborators ⇒ Object
setup the list of collaborators.
-
#setup_config ⇒ Object
setup configuration.
-
#setup_domains ⇒ Object
setup the domains for heroku.
-
#setup_stacks ⇒ Object
setup the stacks for each app (migrating if necessary).
- #system_with_echo(*args) ⇒ Object
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
28 29 30 |
# File 'lib/heroku-rails/runner.rb', line 28 def add_environment(env) @environments << env end |
#all_environments ⇒ Object
use all environments
33 34 35 |
# File 'lib/heroku-rails/runner.rb', line 33 def all_environments @environments = @config.app_environments end |
#authorize ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/heroku-rails/runner.rb', line 10 def return if @heroku # setup heroku username and password so we can start up a heroku client credentials_path = File.("~/.heroku/credentials") # read in the username,password so we can build the client if File.exists?(credentials_path) auth = File.read(credentials_path) username, password = auth.split("\n") @heroku = Heroku::Client.new(username, password) else puts "Heroku not set up. Run `heroku list` in order to input your credentials and try again" exit(1) end end |
#command(*args) ⇒ Object
283 284 285 |
# File 'lib/heroku-rails/runner.rb', line 283 def command(*args) system(*args) end |
#creation_command(*args) ⇒ Object
260 261 262 |
# File 'lib/heroku-rails/runner.rb', line 260 def creation_command(*args) system_with_echo(*args) end |
#destroy_command(*args) ⇒ Object
264 265 266 267 268 |
# File 'lib/heroku-rails/runner.rb', line 264 def destroy_command(*args) # puts args.join(' ') @destroy_commands ||= [] @destroy_commands << args.join(' ') end |
#each_heroku_app ⇒ Object
cycles through each configured heroku app yields the environment name, the app name, and the repo url
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/heroku-rails/runner.rb', line 222 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
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/heroku-rails/runner.rb', line 270 def output_destroy_commands(app) if @destroy_commands.present? 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_cmd| puts destroy_cmd end puts "\n\nthese commands may cause data loss so make sure you know that these are necessary" # clear destroy commands end @destroy_commands = [] end |
#setup_addons ⇒ Object
setup the addons for heroku
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 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/heroku-rails/runner.rb', line 146 def setup_addons unless @heroku 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_apps ⇒ Object
setup apps (create if necessary)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/heroku-rails/runner.rb', line 38 def setup_apps unless @heroku # 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_collaborators ⇒ Object
setup the list of collaborators
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 |
# File 'lib/heroku-rails/runner.rb', line 72 def setup_collaborators unless @heroku 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_config ⇒ Object
setup configuration
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 |
# File 'lib/heroku-rails/runner.rb', line 112 def setup_config unless @heroku 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_domains ⇒ Object
setup the domains for heroku
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/heroku-rails/runner.rb', line 188 def setup_domains unless @heroku 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_stacks ⇒ Object
setup the stacks for each app (migrating if necessary)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/heroku-rails/runner.rb', line 54 def setup_stacks unless @heroku 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
255 256 257 258 |
# File 'lib/heroku-rails/runner.rb', line 255 def system_with_echo(*args) puts args.join(' ') command(*args) end |