Merge branch 'clean_heroku'

This commit is contained in:
Maxwell Salzberg 2012-01-03 16:19:03 -08:00
commit 30b180949d
12 changed files with 129 additions and 24 deletions

View file

@ -1,2 +1,5 @@
features
spec
tmp
log
public/uploads

View file

@ -46,6 +46,7 @@ end
# configuration
gem 'settingslogic', '2.0.6'
gem 'heroku'
# database

View file

@ -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)

View file

@ -2,12 +2,14 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'uri'
require File.join(Rails.root, 'lib', 'enviroment_configuration')
class AppConfig < Settingslogic
ARRAY_VARS = [:community_spotlight, :admins]
def self.source_file_name
config_file = File.join(Rails.root, "config", "application.yml")
if !File.exists?(config_file) && (Rails.env == 'test' || Rails.env.include?("integration") || ENV["HEROKU"])
if !File.exists?(config_file) && (Rails.env == 'test' || Rails.env.include?("integration") || EnviromentConfiguration.heroku?)
config_file = File.join(Rails.root, "config", "application.yml.example")
end
config_file
@ -16,7 +18,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 +54,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:
@ -117,10 +119,18 @@ HELP
def self.[] (key)
return self.pod_uri if key == :pod_uri
return ENV[key.to_s] if ENV[key.to_s] && ENV["HEROKU"]
return fetch_from_env(key.to_s) if EnviromentConfiguration.heroku?
super
end
def fetch_from_env(key)
if ARRAY_VARS.include?(key.to_sym)
ENV[key].split(EnviromentConfiguration::ARRAY_SEPERATOR)
else
ENV[key] if ENV[key]
end
end
def self.[]= (key, value)
super
if key.to_sym == :pod_url

View file

@ -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!

View file

@ -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

View file

@ -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

View file

@ -3,9 +3,11 @@
# the COPYRIGHT file.
git_cmd = `git log -1 --pretty="format:%H %ci"`
if git_cmd =~ /^([\d\w]+?)\s(.+)$/
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

View file

@ -0,0 +1,39 @@
module EnviromentConfiguration
ARRAY_SEPERATOR = '%|%'
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

View file

@ -1,11 +1,30 @@
# Copyright (c) 2012, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#
require File.join(Rails.root, 'lib', 'enviroment_configuration')
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 do|key, value|
value =value.join(EnviromentConfiguration::ARRAY_SEPERATOR) if value.respond_to?(:join)
"#{key}=#{value}"
end.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

View file

@ -12,7 +12,12 @@ begin
task :stats => "spec:statsetup"
#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)

View file

@ -141,6 +141,28 @@ describe AppConfig do
end
end
context 'configurations which are arrays' do
it 'should be set to be admins or community_spotlight' do
AppConfig::ARRAY_VARS.should =~ [:community_spotlight, :admins]
end
context 'on heroku' do
before do
ENV['admins'] = "maxwell#{EnviromentConfiguration::ARRAY_SEPERATOR}daniel"
EnviromentConfiguration.stub(:heroku?).and_return(true)
end
after do
EnviromentConfiguration.stub(:heroku?).and_return(false)
end
it 'converts a string with ARRAY_SEPERATOR to an array' do
AppConfig[:admins].should be_a Array
end
end
end
describe ".pod_uri" do
it "properly parses the pod_url" do
AppConfig.pod_uri = nil