2011-03-27 23:49:12 +00:00
|
|
|
require 'heroku'
|
|
|
|
require 'heroku/client'
|
2011-04-01 00:34:19 +00:00
|
|
|
require 'locomotive/hosting/heroku/custom_domain'
|
2011-04-05 00:18:17 +00:00
|
|
|
require 'locomotive/hosting/heroku/first_installation'
|
2011-03-27 23:49:12 +00:00
|
|
|
|
|
|
|
module Locomotive
|
2011-04-01 00:34:19 +00:00
|
|
|
module Hosting
|
2011-03-27 23:49:12 +00:00
|
|
|
module Heroku
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
class << self
|
|
|
|
attr_accessor :heroku_connection
|
|
|
|
attr_accessor :heroku_domains
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
def heroku?
|
2011-04-01 00:34:19 +00:00
|
|
|
self.config.hosting == :heroku ||
|
|
|
|
(self.config.hosting == :auto && ENV['HEROKU_SLUG'].present?)
|
2011-03-27 23:49:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def enable_heroku
|
2011-04-05 00:18:17 +00:00
|
|
|
self.config.domain = 'heroku.com' unless self.config.multi_sites?
|
2011-04-01 00:34:19 +00:00
|
|
|
|
|
|
|
self.config.heroku ||= {}
|
|
|
|
self.config.heroku[:name] = ENV['APP_NAME']
|
2011-03-27 23:49:12 +00:00
|
|
|
|
2011-04-03 23:59:41 +00:00
|
|
|
raise 'Heroku application name is mandatory' if self.config.heroku[:name].blank?
|
|
|
|
|
2011-03-27 23:49:12 +00:00
|
|
|
self.open_heroku_connection
|
2011-04-03 23:59:41 +00:00
|
|
|
|
|
|
|
self.enhance_site_model_with_heroku
|
2011-03-27 23:49:12 +00:00
|
|
|
|
|
|
|
# "cache" domains for better performance
|
|
|
|
self.heroku_domains = self.heroku_connection.list_domains(self.config.heroku[:name]).collect { |h| h[:domain] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_heroku_connection
|
|
|
|
login = self.config.heroku[:login] || ENV['HEROKU_LOGIN']
|
2011-04-03 23:59:41 +00:00
|
|
|
password = self.config.heroku[:password] || ENV['HEROKU_PASSWORD']
|
2011-03-27 23:49:12 +00:00
|
|
|
|
|
|
|
self.heroku_connection = ::Heroku::Client.new(login, password)
|
|
|
|
end
|
|
|
|
|
2011-04-03 23:59:41 +00:00
|
|
|
def enhance_site_model_with_heroku
|
2011-04-01 00:34:19 +00:00
|
|
|
Site.send :include, Locomotive::Hosting::Heroku::CustomDomain
|
2011-04-05 00:18:17 +00:00
|
|
|
Site.send :include, Locomotive::Hosting::Heroku::FirstInstallation
|
2011-03-27 23:49:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# manage domains
|
|
|
|
|
|
|
|
def add_heroku_domain(name)
|
|
|
|
Locomotive.logger "[add heroku domain] #{name}"
|
|
|
|
self.heroku_connection.add_domain(self.config.heroku[:name], name)
|
|
|
|
self.heroku_domains << name
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_heroku_domain(name)
|
|
|
|
Locomotive.logger "[remove heroku domain] #{name}"
|
|
|
|
self.heroku_connection.remove_domain(self.config.heroku[:name], name)
|
|
|
|
self.heroku_domains.delete(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|