diff --git a/Gemfile b/Gemfile index 5fb8fc9f..d86a6c00 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,7 @@ gem 'fog', '0.3.7' gem 'mimetype-fu' gem 'actionmailer-with-request' gem 'heroku', '1.18.2' +gem 'bushido' gem 'httparty', '>= 0.6.1' gem 'RedCloth', '4.2.7' gem 'delayed_job', '2.1.4' diff --git a/bushido.json b/bushido.json new file mode 100644 index 00000000..79139bf8 --- /dev/null +++ b/bushido.json @@ -0,0 +1,7 @@ +{ + "platform": "rails", + "platform_version": 3, + "ruby_version": "1.9.2", + "sql": false, + "mongodb": true +} diff --git a/config/initializers/carrierwave.rb b/config/initializers/carrierwave.rb index 708c02e7..613e557f 100644 --- a/config/initializers/carrierwave.rb +++ b/config/initializers/carrierwave.rb @@ -1,3 +1,10 @@ +# TODO: Make this store to RAILS_ROOT/permanent + +# On bushido, the app directory is destroyed on every update, so everything is lost. +# The only place this doesn't happen is the RAILS_ROOT/permanent folder. +# Also, RAILS_ROOT/permanent/store is symlinked to RAILS_ROOT/public/store on every update, +# so store your publicly-accessible files here (e.g. templates, etc.) + CarrierWave.configure do |config| case Rails.env.to_sym diff --git a/config/initializers/locomotive.rb b/config/initializers/locomotive.rb index df684c9a..916616de 100644 --- a/config/initializers/locomotive.rb +++ b/config/initializers/locomotive.rb @@ -10,7 +10,7 @@ Locomotive.configure do |config| # during the installation wizzard. # Ex: # config.default_domain = Rails.env.production? ? 'heroku.com' : 'example.com' - config.default_domain = 'example.com' + config.default_domain = ENV["APP_TLD"] || 'example.com' # configure how many items we display in sub menu in the "Contents" section. config.lastest_items_nb = 5 @@ -32,6 +32,14 @@ Locomotive.configure do |config| # config.heroku = { :name => '', :login => 'john@doe.net', :password => 'easy' } config.heroku = false + # tell if the application is hosted on Bushido. + # If enabled, there's no further configuration needed. + # Bushido will take care of eveything + # + # Ex: + # config.bushido = true + config.bushido = ENV['HOSTING_PLATFORM'] == 'bushido' + # Locomotive uses the DelayedJob gem for the theme import module. # In case you want to deploy to Heroku, you will have to pay for an extra dyno. # If you do not mind about importing theme without DelayedJob, disable it. @@ -41,5 +49,5 @@ Locomotive.configure do |config| config.default_locale = :en # Configure the e-mail address which will be shown in the DeviseMailer, NotificationMailer, ...etc - config.mailer_sender = 'support@example.com' + config.mailer_sender = ENV['BUSHIDO_DOMAIN'] ? "support@#{ENV['BUSHIDO_DOMAIN']}" : 'support@example.com' end diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index e131726c..9e4a5521 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -1,7 +1,9 @@ # Be sure to restart your server when you modify this file. +require 'digest/sha1' # Your secret key for verifying the integrity of signed cookies. # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. -Rails.application.config.secret_token = 'aa84844b97e90edda8e005a686d82c3bef1f8e20a1255301f1d0886fc592a45ef2393d64b0c3d3ea858b1f6406ad6f15305666264716a79fcfa17de93ad0d69d' +token = ENV['BUSHIDO_SALT'] || 'aa84844b97e90edda8e005a686d82c3bef1f8e20a1255301f1d0886fc592a45ef2393d64b0c3d3ea858b1f6406ad6f15305666264716a79fcfa17de93ad0d69d' +Rails.application.config.secret_token = Digest::SHA1.hexdigest(token) diff --git a/config/mongoid.yml b/config/mongoid.yml index c71777ba..1ff4cd9b 100644 --- a/config/mongoid.yml +++ b/config/mongoid.yml @@ -31,3 +31,5 @@ production: # heroku # uri: <%= ENV['MONGOHQ_URL'] %> + # bushido (mongohq_url will also work) + uri: <%= ENV['MONGODB_URL'] %> diff --git a/lib/locomotive.rb b/lib/locomotive.rb index b92dc714..7cfca9f0 100644 --- a/lib/locomotive.rb +++ b/lib/locomotive.rb @@ -9,6 +9,7 @@ require 'locomotive/liquid' require 'locomotive/mongoid' require 'locomotive/carrierwave' require 'locomotive/heroku' +require 'locomotive/bushido' require 'locomotive/custom_fields' require 'locomotive/httparty' require 'locomotive/inherited_resources' @@ -24,6 +25,7 @@ require 'locomotive/session_store' module Locomotive include Locomotive::Heroku + include Locomotive::BushidoSupport class << self attr_accessor :config @@ -51,9 +53,12 @@ module Locomotive :key => Locomotive.config.cookie_key } - # Heroku support + # Hosting-platform support self.enable_heroku if self.heroku? + # Bushido support + self.enable_bushido if self.bushido? + # Devise Devise.mailer_sender = self.config.mailer_sender diff --git a/lib/locomotive/bushido.rb b/lib/locomotive/bushido.rb new file mode 100644 index 00000000..85fa69e3 --- /dev/null +++ b/lib/locomotive/bushido.rb @@ -0,0 +1,69 @@ +require 'bushido' +require 'locomotive/bushido/custom_domain' + +module Locomotive + module BushidoSupport + + extend ActiveSupport::Concern + + included do + class << self + attr_accessor :bushido_domains + attr_accessor :bushido_subdomain + + puts @bushido_domains + end + end + + module ClassMethods + + def bushido? + ENV["HOSTING_PLATFORM"] == "bushido" + end + + + def enable_bushido + self.enhance_site_model + + self.bushido_domains = Bushido::App.domains + self.bushido_subdomain = Bushido::App.subdomain + end + + + def enhance_site_model + Site.send :include, Locomotive::BushidoSupport::CustomDomain + end + + # manage domains + + def add_bushido_domain(name) + Locomotive.logger "[add bushido domain] #{name}" + Bushido::App.add_domain(name) + + if Bushido::Command.last_command_successful? + self.bushido_domains << name + end + end + + + def remove_bushido_domain(name) + Locomotive.logger "[remove bushido domain] #{name}" + Bushido::App.remove_domain(name) + + if Bushido::Command.last_command_successful? + self.bushido_domains.delete(name) + end + end + + + def set_bushido_subdomain(name) + Locomotive.logger "[set bushido subdomain] #{name}.bushi.do" + Bushido::App.set_subdomain(name) + + if Bushido::Command.last_command_successful? + self.bushido_subdomain = name + end + end + end + end +end diff --git a/lib/locomotive/bushido/custom_domain.rb b/lib/locomotive/bushido/custom_domain.rb new file mode 100644 index 00000000..d93f6808 --- /dev/null +++ b/lib/locomotive/bushido/custom_domain.rb @@ -0,0 +1,52 @@ +module Locomotive + module BushidoSupport + module CustomDomain + + extend ActiveSupport::Concern + + included do + + after_save :add_bushido_domains + after_destroy :remove_bushido_domains + + alias_method_chain :add_subdomain_to_domains, :bushido + end + + module InstanceMethods + + protected + + def add_subdomain_to_domains_with_bushido + unless self.domains_change.nil? + full_subdomain = "#{self.subdomain}.#{Locomotive.config.default_domain}" + @bushido_domains_change = { + :added => self.domains_change.last - self.domains_change.first - [full_subdomain], + :removed => self.domains_change.first - self.domains_change.last - [full_subdomain] + } + end + + add_subdomain_to_domains_without_bushido + end + + def add_bushido_domains + return if @bushido_domains_change.nil? + + @bushido_domains_change[:added].each do |name| + Locomotive.add_bushido_domain(name) + end + @bushido_domains_change[:removed].each do |name| + Locomotive.remove_bushido_domain(name) + end + end + + def remove_bushido_domains + self.domains_without_subdomain.each do |name| + Locomotive.remove_bushido_domain(name) + end + end + + end + + end + end +end diff --git a/lib/locomotive/configuration.rb b/lib/locomotive/configuration.rb index 649030d6..c0ad0f95 100644 --- a/lib/locomotive/configuration.rb +++ b/lib/locomotive/configuration.rb @@ -11,6 +11,7 @@ module Locomotive :cookie_key => '_locomotive_session', :enable_logs => false, :heroku => false, + :bushido => false, :delayed_job => true, :default_locale => :en, :mailer_sender => 'support@example.com' diff --git a/lib/locomotive/engine.rb b/lib/locomotive/engine.rb index 2d3a7c6b..b1f233cc 100644 --- a/lib/locomotive/engine.rb +++ b/lib/locomotive/engine.rb @@ -15,6 +15,7 @@ require 'custom_fields' require 'mimetype_fu' require 'actionmailer_with_request' require 'heroku' +require 'bushido' require 'httparty' require 'redcloth' require 'delayed_job_mongoid' diff --git a/lib/tasks/bushido.rake b/lib/tasks/bushido.rake new file mode 100644 index 00000000..159d3c10 --- /dev/null +++ b/lib/tasks/bushido.rake @@ -0,0 +1,15 @@ +require 'bushido' +require 'jammit' + +namespace :bushido do + desc "Prepare an app to run on the Bushido hosting platform, only called during the initial installation. Called just before booting the app." + task :install do + Jammit.package! + end + + desc "Prepare an app to run on the Bushido hosting platform, called on every update. Called just before booting the app." + task :update do + Jammit.package! + end +end +