engine/lib/locomotive/hosting/heroku.rb

69 lines
1.9 KiB
Ruby
Raw Normal View History

2011-03-27 23:49:12 +00:00
require 'heroku'
require 'heroku/client'
require 'locomotive/hosting/heroku/custom_domain'
2011-03-27 23:49:12 +00:00
module Locomotive
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?
self.config.hosting == :heroku ||
(self.config.hosting == :auto && ENV['HEROKU_SLUG'].present?)
2011-03-27 23:49:12 +00:00
end
def enable_heroku
# raise 'Heroku application name is mandatory' if self.config.heroku[:name].blank?
self.config.domain = 'heroku.com'
self.config.heroku ||= {}
self.config.heroku[:name] = ENV['APP_NAME']
2011-03-27 23:49:12 +00:00
self.open_heroku_connection
self.enhance_site_model
# "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']
password = self.config.heroku[:password] rescue ENV['HEROKU_PASSWORD']
2011-03-27 23:49:12 +00:00
self.heroku_connection = ::Heroku::Client.new(login, password)
end
def enhance_site_model
Site.send :include, Extensions::Site::SubdomainDomains
Site.send :include, Locomotive::Hosting::Heroku::CustomDomain
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