From a7d59ce11508d35d6d680531fc0add5b3d462aa8 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Tue, 20 Dec 2011 15:02:48 -0800 Subject: [PATCH] add EnviromentConfiguration to keep track of heroku specfic enviroment hacks --- .slugignore | 3 ++ Gemfile | 1 + Gemfile.lock | 7 ++++ app/models/app_config.rb | 4 +-- config/initializers/check_session_secret.rb | 5 +-- config/initializers/faraday.rb | 4 +-- config/initializers/fetch_featured_users.rb | 2 +- config/initializers/version_header.rb | 14 ++++---- lib/enviroment_configuration.rb | 38 +++++++++++++++++++++ lib/tasks/heroku.rake | 21 ++++++++---- lib/tasks/rspec.rake | 7 +++- 11 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 lib/enviroment_configuration.rb diff --git a/.slugignore b/.slugignore index 6d68cf7fe..ed3028f34 100644 --- a/.slugignore +++ b/.slugignore @@ -1,2 +1,5 @@ features spec +tmp +log +public/uploads \ No newline at end of file diff --git a/Gemfile b/Gemfile index cd7717ffc..4c495684d 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ end # configuration gem 'settingslogic', '2.0.6' +gem 'heroku' # database diff --git a/Gemfile.lock b/Gemfile.lock index 4281ce9f4..8982a7103 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -208,6 +208,11 @@ GEM json (>= 1.4.6) haml (3.1.4) hashie (1.2.0) + heroku (2.16.2) + launchy (>= 0.3.2) + rest-client (~> 1.6.1) + rubyzip + term-ansicolor (~> 1.0.5) highline (1.6.8) hoptoad_notifier (2.4.11) activesupport @@ -232,6 +237,7 @@ GEM multi_json jwt (0.1.3) json (>= 1.2.4) + launchy (2.0.3) linecache (0.46) rbx-require-relative (> 0.0.4) linecache19 (0.5.12) @@ -471,6 +477,7 @@ DEPENDENCIES foreman fuubar (= 0.0.6) haml (= 3.1.4) + heroku hoptoad_notifier http_accept_language! i18n-inflector-rails (~> 1.0) diff --git a/app/models/app_config.rb b/app/models/app_config.rb index 60a8469be..409a58c64 100644 --- a/app/models/app_config.rb +++ b/app/models/app_config.rb @@ -16,7 +16,7 @@ class AppConfig < Settingslogic namespace Rails.env def self.load! - unless ENV["HEROKU"] + unless EnviromentConfiguration.heroku? if no_config_file? && !have_old_config_file? $stderr.puts <<-HELP ******** You haven't set up your Diaspora settings file. ********** @@ -52,7 +52,7 @@ Please do the following: Process.exit(1) end - if !ENV["HEROKU"] && no_cert_file_in_prod? + if !EnviromentConfiguration.heroku? && no_cert_file_in_prod? $stderr.puts <<-HELP ******** Diaspora does not know where your SSL-CA-Certificates file is. ********** Please add the root certificate bundle (this is operating system specific) to application.yml. Defaults: diff --git a/config/initializers/check_session_secret.rb b/config/initializers/check_session_secret.rb index 32a1907d7..e943fdd8f 100644 --- a/config/initializers/check_session_secret.rb +++ b/config/initializers/check_session_secret.rb @@ -1,4 +1 @@ -unless File.exists?( File.join(Rails.root, 'config', 'initializers', 'secret_token.rb')) - `rake generate:secret_token` - require File.join(Rails.root, 'config', 'initializers', 'secret_token.rb') -end +EnviromentConfiguration.ensure_secret_token! \ No newline at end of file diff --git a/config/initializers/faraday.rb b/config/initializers/faraday.rb index 1d4a5454e..aac11e4cb 100644 --- a/config/initializers/faraday.rb +++ b/config/initializers/faraday.rb @@ -3,8 +3,8 @@ # the COPYRIGHT file. options = {:timeout => 5} -options[:ssl] = {:ca_file => AppConfig[:ca_file]} unless ENV['HEROKU'] -Faraday.default_connection = Faraday::Connection.new(options ) do |b| +options[:ssl] = {:ca_file => EnviromentConfiguration.ca_cert_file_location} +Faraday.default_connection = Faraday::Connection.new(options) do |b| b.use FaradayStack::FollowRedirects b.adapter Faraday.default_adapter end diff --git a/config/initializers/fetch_featured_users.rb b/config/initializers/fetch_featured_users.rb index f9626ddba..8cc185846 100644 --- a/config/initializers/fetch_featured_users.rb +++ b/config/initializers/fetch_featured_users.rb @@ -5,7 +5,7 @@ if AppConfig[:featured_users].present? && AppConfig[:community_spotlight].blank? puts "DEPRICATION WARNING (10/21/11): Please change `featured_users` in your application.yml to `community_spotlight`. Thanks!" end -unless !ActiveRecord::Base.connection.table_exists?('people') || Rails.env == 'test' || AppConfig[:community_spotlight].nil? || AppConfig[:community_spotlight].count == Person.community_spotlight.count +unless EnviromentConfiguration.prevent_fetching_community_spotlight? print "Fetching community spotlight users from remote servers" AppConfig[:community_spotlight].each do |x| Webfinger.new(x).fetch diff --git a/config/initializers/version_header.rb b/config/initializers/version_header.rb index b16245693..b8fd33266 100644 --- a/config/initializers/version_header.rb +++ b/config/initializers/version_header.rb @@ -3,9 +3,11 @@ # the COPYRIGHT file. -git_cmd = `git log -1 --pretty="format:%H %ci"` -if git_cmd =~ /^([\d\w]+?)\s(.+)$/ - AppConfig[:git_revision] = $1 - AppConfig[:git_update] = $2.strip - ENV["RAILS_ASSET_ID"] = AppConfig[:git_revision][0..8] if Rails.env.production? -end +if EnviromentConfiguration.cache_git_version? + git_cmd = `git log -1 --pretty="format:%H %ci"` + if git_cmd =~ /^([\d\w]+?)\s(.+)$/ + AppConfig[:git_revision] = $1 + AppConfig[:git_update] = $2.strip + ENV["RAILS_ASSET_ID"] = AppConfig[:git_revision][0..8] if Rails.env.production? + end +end \ No newline at end of file diff --git a/lib/enviroment_configuration.rb b/lib/enviroment_configuration.rb new file mode 100644 index 000000000..7c0160277 --- /dev/null +++ b/lib/enviroment_configuration.rb @@ -0,0 +1,38 @@ +module EnviromentConfiguration + def self.heroku? + ENV['HEROKU'] + end + + def self.secret_token_initializer_is_not_present? + !File.exists?( File.join(Rails.root, 'config', 'initializers', 'secret_token.rb')) + end + + def self.prevent_fetching_community_spotlight? + return true if heroku? + !ActiveRecord::Base.connection.table_exists?('people') || Rails.env == 'test' || AppConfig[:community_spotlight].nil? || AppConfig[:community_spotlight].count + end + + def self.cache_git_version? + !self.heroku? + end + + def self.ensure_secret_token! + if heroku? + puts 'heroku app detected; using session secret from config vars...' + Rails.application.config.secret_token = ENV['SECRET_TOKEN'] + elsif secret_token_initializer_is_not_present? + `rake generate:secret_token` + require File.join(Rails.root, 'config', 'initializers', 'secret_token.rb') + else + #do nothing + end + end + + def self.ca_cert_file_location + if self.heroku? + "/usr/lib/ssl/certs/ca-certificates.crt" + else + AppConfig[:ca_file] + end + end +end \ No newline at end of file diff --git a/lib/tasks/heroku.rake b/lib/tasks/heroku.rake index 27c9ee0ab..d7d58e482 100644 --- a/lib/tasks/heroku.rake +++ b/lib/tasks/heroku.rake @@ -1,11 +1,20 @@ namespace :heroku do + HEROKU_CONFIG_ADD_COMMAND = "heroku config:add HEROKU=true" + task :config do puts "Reading config/application.yml and sending config vars to Heroku..." - CONFIG = YAML.load_file('config/application.yml')['production'] rescue {} - command = "heroku config:add" - CONFIG.each {|key, val| command << " #{key}=#{val} " if val } - command << " HEROKU=true " - command << " DB=postgres " - system command + application_config = YAML.load_file('config/application.yml')['production'] rescue {} + application_config.delete_if { |k, v| v.blank? } + + heroku_env = application_config.map{|k, v| "#{k}=#{v}"}.join(' ') + + puts "Generating and setting a new secret token" + token = ActiveSupport::SecureRandom.hex(40)#reloads secret token every time you reload vars.... this expires cookies, and kinda sucks + system "#{HEROKU_CONFIG_ADD_COMMAND} #{heroku_env} SECRET_TOKEN=#{token}" + end + + task :install_requirements do + system 'heroku addons:add lgging:expanded' + system 'heroku addons:add redistogo:nano' end end diff --git a/lib/tasks/rspec.rake b/lib/tasks/rspec.rake index ec34b4dc5..01276849c 100644 --- a/lib/tasks/rspec.rake +++ b/lib/tasks/rspec.rake @@ -12,7 +12,12 @@ begin task :stats => "spec:statsetup" - Rake::Task[:spec].clear + #heroku barfs here :/ + begin + Rake::Task[:spec].clear + rescue + nil + end desc "Run all specs in spec directory" RSpec::Core::RakeTask.new(:spec => spec_prereq)